forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectSearch.spec.js
More file actions
256 lines (231 loc) · 6.33 KB
/
Copy pathProjectSearch.spec.js
File metadata and controls
256 lines (231 loc) · 6.33 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* 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/>. */
// @flow
import React from "react";
import { mount, shallow } from "enzyme";
import { ProjectSearch } from "../ProjectSearch";
import { statusType } from "../../reducers/project-text-search";
import { mockcx } from "../../utils/test-mockup";
const hooks = { on: [], off: [] };
const shortcuts = {
dispatch(eventName) {
hooks.on.forEach(hook => {
if (hook.event === eventName) {
hook.cb();
}
});
hooks.off.forEach(hook => {
if (hook.event === eventName) {
hook.cb();
}
});
},
on: jest.fn((event, cb) => hooks.on.push({ event, cb })),
off: jest.fn((event, cb) => hooks.off.push({ event, cb }))
};
const context = { shortcuts };
const testResults = [
{
filepath: "testFilePath1",
type: "RESULT",
matches: [
{
match: "match1",
value: "some thing match1",
column: 30,
type: "MATCH"
},
{
match: "match2",
value: "some thing match2",
column: 60,
type: "MATCH"
},
{ match: "match3", value: "some thing match3", column: 90, type: "MATCH" }
]
},
{
filepath: "testFilePath2",
type: "RESULT",
matches: [
{
match: "match4",
value: "some thing match4",
column: 80,
type: "MATCH"
},
{ match: "match5", value: "some thing match5", column: 40, type: "MATCH" }
]
}
];
const testMatch = {
type: "MATCH",
match: "match1",
value: "some thing match1",
sourceId: "some-target/source42",
line: 3,
column: 30
};
function render(overrides = {}, mounted = false) {
const props = {
cx: mockcx,
status: "DONE",
sources: {},
results: [],
query: "foo",
activeSearch: "project",
closeProjectSearch: jest.fn(),
searchSources: jest.fn(),
clearSearch: jest.fn(),
updateSearchStatus: jest.fn(),
selectSpecificLocation: jest.fn(),
doSearchForHighlight: jest.fn(),
setActiveSearch: jest.fn(),
...overrides
};
return mounted
? mount(<ProjectSearch {...props} />, { context })
: shallow(<ProjectSearch {...props} />, { context });
}
describe("ProjectSearch", () => {
beforeEach(() => {
context.shortcuts.on.mockClear();
context.shortcuts.off.mockClear();
});
it("renders nothing when disabled", () => {
const component = render({ activeSearch: "" });
expect(component).toMatchSnapshot();
});
it("where <Enter> has not been pressed", () => {
const component = render({ query: "" });
expect(component).toMatchSnapshot();
});
it("found no search results", () => {
const component = render();
expect(component).toMatchSnapshot();
});
it("should display loading message while search is in progress", () => {
const component = render({
query: "match",
status: statusType.fetching
});
expect(component).toMatchSnapshot();
});
it("found search results", () => {
const component = render(
{
query: "match",
results: testResults
},
true
);
expect(component).toMatchSnapshot();
});
it("turns off shortcuts on unmount", () => {
const component = render({
query: ""
});
expect(component).toMatchSnapshot();
component.unmount();
expect(context.shortcuts.off).toHaveBeenCalled();
});
it("calls inputOnChange", () => {
const component = render(
{
results: testResults
},
true
);
component
.find("SearchInput input")
.simulate("change", { target: { value: "bar" } });
expect(component.state().inputValue).toEqual("bar");
});
it("onKeyDown Escape/Other", () => {
const searchSources = jest.fn();
const component = render(
{
results: testResults,
searchSources
},
true
);
component.find("SearchInput input").simulate("keydown", { key: "Escape" });
expect(searchSources).not.toHaveBeenCalled();
searchSources.mockClear();
component
.find("SearchInput input")
.simulate("keydown", { key: "Other", stopPropagation: jest.fn() });
expect(searchSources).not.toHaveBeenCalled();
});
it("onKeyDown Enter", () => {
const searchSources = jest.fn();
const component = render(
{
results: testResults,
searchSources
},
true
);
component
.find("SearchInput input")
.simulate("keydown", { key: "Enter", stopPropagation: jest.fn() });
expect(searchSources).toHaveBeenCalledWith(mockcx, "foo");
});
it("onEnterPress shortcut no match or setExpanded", () => {
const selectSpecificLocation = jest.fn();
const component = render(
{
results: testResults,
selectSpecificLocation
},
true
);
component.instance().state.focusedItem = null;
shortcuts.dispatch("Enter");
expect(selectSpecificLocation).not.toHaveBeenCalled();
});
it("onEnterPress shortcut match", () => {
const selectSpecificLocation = jest.fn();
const component = render(
{
results: testResults,
selectSpecificLocation
},
true
);
component.instance().state.focusedItem = { ...testMatch };
shortcuts.dispatch("Enter");
expect(selectSpecificLocation).toHaveBeenCalledWith(mockcx, {
sourceId: "some-target/source42",
line: 3,
column: 30
});
});
it("state.inputValue responds to prop.query changes", () => {
const component = render({ query: "foo" });
expect(component.state().inputValue).toEqual("foo");
component.setProps({ query: "" });
expect(component.state().inputValue).toEqual("");
});
describe("showErrorEmoji", () => {
it("false if not done & results", () => {
const component = render({
status: statusType.fetching,
results: testResults
});
expect(component).toMatchSnapshot();
});
it("false if not done & no results", () => {
const component = render({
status: statusType.fetching
});
expect(component).toMatchSnapshot();
});
// "false if done & has results"
// is the same test as "found search results"
// "true if done & has no results"
// is the same test as "found no search results"
});
});