summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/src/components/SecondaryPanes/tests/EventListeners.spec.js
blob: 58d7776e5a15792972083e2d220e09eb35fd75e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */

import React from "devtools/client/shared/vendor/react";
import { shallow } from "enzyme";
import EventListeners from "../EventListeners";

function getCategories() {
  return [
    {
      name: "Category 1",
      events: [
        { name: "Subcategory 1", id: "category1.subcategory1" },
        { name: "Subcategory 2", id: "category1.subcategory2" },
      ],
    },
    {
      name: "Category 2",
      events: [
        { name: "Subcategory 3", id: "category2.subcategory1" },
        { name: "Subcategory 4", id: "category2.subcategory2" },
      ],
    },
  ];
}

function generateDefaults(overrides = {}) {
  const defaults = {
    activeEventListeners: [],
    expandedCategories: [],
    categories: [],
  };

  return { ...defaults, ...overrides };
}

function render(overrides = {}) {
  const props = generateDefaults(overrides);
  const component = shallow(
    React.createElement(EventListeners.WrappedComponent, props)
  );
  return { component, props };
}

describe("EventListeners", () => {
  it("should render", async () => {
    const { component } = render();
    expect(component).toMatchSnapshot();
  });

  it("should render categories appropriately", async () => {
    const props = {
      ...generateDefaults(),
      categories: getCategories(),
    };
    const { component } = render(props);
    expect(component).toMatchSnapshot();
  });

  it("should render expanded categories appropriately", async () => {
    const props = {
      ...generateDefaults(),
      categories: getCategories(),
      expandedCategories: ["Category 2"],
    };
    const { component } = render(props);
    expect(component).toMatchSnapshot();
  });

  it("should render checked subcategories appropriately", async () => {
    const props = {
      ...generateDefaults(),
      categories: getCategories(),
      activeEventListeners: ["category1.subcategory2"],
      expandedCategories: ["Category 1"],
    };
    const { component } = render(props);
    expect(component).toMatchSnapshot();
  });

  it("should filter the event listeners based on the event name", async () => {
    const props = {
      ...generateDefaults(),
      categories: getCategories(),
    };
    const { component } = render(props);
    component.find(".event-search-input").simulate("focus");

    const searchInput = component.find(".event-search-input");
    // Simulate a search query of "Subcategory 3" to display just one event which
    // will be the Subcategory 3 event
    searchInput.simulate("change", {
      currentTarget: { value: "Subcategory 3" },
    });

    const displayedEvents = component.find(".event-listener-event");
    expect(displayedEvents).toHaveLength(1);
  });

  it("should filter the event listeners based on the category name", async () => {
    const props = {
      ...generateDefaults(),
      categories: getCategories(),
    };
    const { component } = render(props);
    component.find(".event-search-input").simulate("focus");

    const searchInput = component.find(".event-search-input");
    // Simulate a search query of "Category 1" to display two events which will be
    // the Subcategory 1 event and the Subcategory 2 event
    searchInput.simulate("change", { currentTarget: { value: "Category 1" } });

    const displayedEvents = component.find(".event-listener-event");
    expect(displayedEvents).toHaveLength(2);
  });

  it("should be case insensitive when filtering events and categories", async () => {
    const props = {
      ...generateDefaults(),
      categories: getCategories(),
    };
    const { component } = render(props);
    component.find(".event-search-input").simulate("focus");

    const searchInput = component.find(".event-search-input");
    // Simulate a search query of "Subcategory 3" to display just one event which
    // will be the Subcategory 3 event
    searchInput.simulate("change", {
      currentTarget: { value: "sUbCaTeGoRy 3" },
    });

    const displayedEvents = component.find(".event-listener-event");
    expect(displayedEvents).toHaveLength(1);
  });
});