summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/unit/content-src/components/Search.test.jsx
blob: 54a3b611ccf8a9263422736f81305c2379c39621 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { GlobalOverrider } from "test/unit/utils";
import { mount, shallow } from "enzyme";
import React from "react";
import { _Search as Search } from "content-src/components/Search/Search";

const DEFAULT_PROPS = {
  dispatch() {},
  Prefs: { values: { featureConfig: {} } },
};

describe("<Search>", () => {
  let globals;
  let sandbox;
  beforeEach(() => {
    globals = new GlobalOverrider();
    sandbox = globals.sandbox;

    global.ContentSearchUIController.prototype = { search: sandbox.spy() };
  });
  afterEach(() => {
    globals.restore();
  });

  it("should render a Search element", () => {
    const wrapper = shallow(<Search {...DEFAULT_PROPS} />);
    assert.ok(wrapper.exists());
  });
  it("should not use a <form> element", () => {
    const wrapper = mount(<Search {...DEFAULT_PROPS} />);

    assert.equal(wrapper.find("form").length, 0);
  });
  it("should listen for ContentSearchClient on render", () => {
    const spy = globals.set("addEventListener", sandbox.spy());

    const wrapper = mount(<Search {...DEFAULT_PROPS} />);

    assert.calledOnce(spy.withArgs("ContentSearchClient", wrapper.instance()));
  });
  it("should stop listening for ContentSearchClient on unmount", () => {
    const spy = globals.set("removeEventListener", sandbox.spy());
    const wrapper = mount(<Search {...DEFAULT_PROPS} />);
    // cache the instance as we can't call this method after unmount is called
    const instance = wrapper.instance();

    wrapper.unmount();

    assert.calledOnce(spy.withArgs("ContentSearchClient", instance));
  });
  it("should add gContentSearchController as a global", () => {
    // current about:home tests need gContentSearchController to exist as a global
    // so let's test it here too to ensure we don't break this behaviour
    mount(<Search {...DEFAULT_PROPS} />);
    assert.property(window, "gContentSearchController");
    assert.ok(window.gContentSearchController);
  });
  it("should pass along search when clicking the search button", () => {
    const wrapper = mount(<Search {...DEFAULT_PROPS} />);

    wrapper.find(".search-button").simulate("click");

    const { search } = window.gContentSearchController;
    assert.calledOnce(search);
    assert.propertyVal(search.firstCall.args[0], "type", "click");
  });
  it("should send a UserEvent action", () => {
    global.ContentSearchUIController.prototype.search = () => {
      dispatchEvent(
        new CustomEvent("ContentSearchClient", { detail: { type: "Search" } })
      );
    };
    const dispatch = sinon.spy();
    const wrapper = mount(<Search {...DEFAULT_PROPS} dispatch={dispatch} />);

    wrapper.find(".search-button").simulate("click");

    assert.calledOnce(dispatch);
    const [action] = dispatch.firstCall.args;
    assert.isUserEventAction(action);
    assert.propertyVal(action.data, "event", "SEARCH");
  });
  it("should show our logo when the prop exists.", () => {
    const showLogoProps = Object.assign({}, DEFAULT_PROPS, { showLogo: true });

    const wrapper = shallow(<Search {...showLogoProps} />);
    assert.lengthOf(wrapper.find(".logo-and-wordmark"), 1);
  });
  it("should not show our logo when the prop does not exist.", () => {
    const hideLogoProps = Object.assign({}, DEFAULT_PROPS, { showLogo: false });

    const wrapper = shallow(<Search {...hideLogoProps} />);
    assert.lengthOf(wrapper.find(".logo-and-wordmark"), 0);
  });

  describe("Search Hand-off", () => {
    it("should render a Search element when hand-off is enabled", () => {
      const wrapper = shallow(
        <Search {...DEFAULT_PROPS} handoffEnabled={true} />
      );
      assert.ok(wrapper.exists());
      assert.equal(wrapper.find(".search-handoff-button").length, 1);
    });
    it("should hand-off search when button is clicked", () => {
      const dispatch = sinon.spy();
      const wrapper = shallow(
        <Search {...DEFAULT_PROPS} handoffEnabled={true} dispatch={dispatch} />
      );
      wrapper
        .find(".search-handoff-button")
        .simulate("click", { preventDefault: () => {} });
      assert.calledThrice(dispatch);
      assert.calledWith(dispatch, {
        data: { text: undefined },
        meta: {
          from: "ActivityStream:Content",
          skipLocal: true,
          to: "ActivityStream:Main",
        },
        type: "HANDOFF_SEARCH_TO_AWESOMEBAR",
      });
      assert.calledWith(dispatch, { type: "FAKE_FOCUS_SEARCH" });
      const [action] = dispatch.thirdCall.args;
      assert.isUserEventAction(action);
      assert.propertyVal(action.data, "event", "SEARCH_HANDOFF");
    });
    it("should hand-off search on paste", () => {
      const dispatch = sinon.spy();
      const wrapper = mount(
        <Search {...DEFAULT_PROPS} handoffEnabled={true} dispatch={dispatch} />
      );
      wrapper.instance()._searchHandoffButton = { contains: () => true };
      wrapper.instance().onSearchHandoffPaste({
        clipboardData: {
          getData: () => "some copied text",
        },
        preventDefault: () => {},
      });
      assert.equal(dispatch.callCount, 4);
      assert.calledWith(dispatch, {
        data: { text: "some copied text" },
        meta: {
          from: "ActivityStream:Content",
          skipLocal: true,
          to: "ActivityStream:Main",
        },
        type: "HANDOFF_SEARCH_TO_AWESOMEBAR",
      });
      assert.calledWith(dispatch, { type: "DISABLE_SEARCH" });
      const [action] = dispatch.thirdCall.args;
      assert.isUserEventAction(action);
      assert.propertyVal(action.data, "event", "SEARCH_HANDOFF");
    });
    it("should properly handle drop events", () => {
      const dispatch = sinon.spy();
      const wrapper = mount(
        <Search {...DEFAULT_PROPS} handoffEnabled={true} dispatch={dispatch} />
      );
      const preventDefault = sinon.spy();
      wrapper.find(".fake-editable").simulate("drop", {
        dataTransfer: { getData: () => "dropped text" },
        preventDefault,
      });
      assert.equal(dispatch.callCount, 4);
      assert.calledWith(dispatch, {
        data: { text: "dropped text" },
        meta: {
          from: "ActivityStream:Content",
          skipLocal: true,
          to: "ActivityStream:Main",
        },
        type: "HANDOFF_SEARCH_TO_AWESOMEBAR",
      });
      assert.calledWith(dispatch, { type: "DISABLE_SEARCH" });
      const [action] = dispatch.thirdCall.args;
      assert.isUserEventAction(action);
      assert.propertyVal(action.data, "event", "SEARCH_HANDOFF");
    });
  });
});