forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourcesTree.spec.js
More file actions
461 lines (403 loc) · 12.6 KB
/
Copy pathSourcesTree.spec.js
File metadata and controls
461 lines (403 loc) · 12.6 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/* 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 { shallow } from "enzyme";
import { showMenu } from "devtools-contextmenu";
import SourcesTree from "../SourcesTree";
import { makeMockSource, mockcx } from "../../../utils/test-mockup";
import { copyToTheClipboard } from "../../../utils/clipboard";
jest.mock("devtools-contextmenu", () => ({ showMenu: jest.fn() }));
jest.mock("../../../utils/clipboard", () => ({
copyToTheClipboard: jest.fn()
}));
describe("SourcesTree", () => {
afterEach(() => {
(copyToTheClipboard: any).mockClear();
showMenu.mockClear();
});
it("Should show the tree with nothing expanded", async () => {
const { component } = render();
expect(component).toMatchSnapshot();
});
it("Should show a 'No Sources' message if there are no sources", async () => {
const { component, defaultState } = render();
const sourceTree = defaultState.sourceTree;
sourceTree.contents = [];
component.setState({ sourceTree: sourceTree });
expect(component).toMatchSnapshot();
});
describe("When loading initial source", () => {
it("Shows the tree with one.js, two.js and three.js expanded", async () => {
const { component, props } = render();
await component.setProps({
...props,
expanded: ["one.js", "two.js", "three.js"]
});
expect(component).toMatchSnapshot();
});
});
describe("After changing expanded nodes", () => {
it("Shows the tree with four.js, five.js and six.js expanded", async () => {
const { component, props } = render();
await component.setProps({
...props,
expanded: ["four.js", "five.js", "six.js"]
});
expect(component).toMatchSnapshot();
});
});
describe("on receiving new props", () => {
describe("recreates tree", () => {
it("does not recreate tree if no new source is added", async () => {
const { component, props, defaultState } = render();
const sources = {
"server1.conn13.child1/41": createMockSource(
"server1.conn13.child1/41",
"http://mdn.com/three.js"
)
};
await component.setProps({ ...props, sources });
expect(component.state("uncollapsedTree")).toEqual(
defaultState.uncollapsedTree
);
});
it("updates tree with a new item", async () => {
const { component, props } = render();
const newSource = createMockSource(
"server1.conn13.child1/43",
"http://mdn.com/four.js",
true,
""
);
const newThreadSources = {
...props.sources.FakeThread,
"server1.conn13.child1/43": newSource
};
await component.setProps({
...props,
sources: newThreadSources
});
expect(
component.state("uncollapsedTree").contents[0].contents[0].contents
).toHaveLength(5);
});
it("updates sources if sources are emptied", async () => {
const { component, props, defaultState } = render();
expect(defaultState.uncollapsedTree.contents).toHaveLength(1);
await component.setProps({
...props,
sources: {},
sourceCount: 0
});
expect(component.state("uncollapsedTree").contents).toHaveLength(0);
});
it("recreates tree if projectRoot is changed", async () => {
const { component, props, defaultState } = render();
const sources = {
FakeThread: {
"server1.conn13.child1/41": createMockSource(
"server1.conn13.child1/41",
"http://mozilla.com/three.js"
)
}
};
expect(
defaultState.uncollapsedTree.contents[0].contents[0].contents
).toHaveLength(5);
await component.setProps({
...props,
sources,
projectRoot: "mozilla"
});
expect(
component.state("uncollapsedTree").contents[0].contents
).toHaveLength(1);
});
it("recreates tree if debuggeeUrl is changed", async () => {
const { component, props, defaultState } = render();
const sources = {
FakeThread: {
"server1.conn13.child1/41": createMockSource(
"server1.conn13.child1/41",
"http://mdn.com/three.js",
true,
""
)
}
};
expect(
defaultState.uncollapsedTree.contents[0].contents[0].contents
).toHaveLength(5);
await component.setProps({
...props,
debuggeeUrl: "mozilla",
sources
});
expect(
component.state("uncollapsedTree").contents[0].contents
).toHaveLength(1);
});
});
describe("updates threads", () => {
it("adds sources to the correct thread", async () => {
const { component, props } = render();
const newSource = createMockSource(
"server1.conn13.child1/43",
"http://mdn.com/four.js",
true,
""
);
const newThreadSources = {
FakeThread1: {
"server1.conn13.child1/43": newSource
}
};
expect(component.state("uncollapsedTree").contents[0].name).toEqual(
"FakeThread"
);
expect(
component.state("uncollapsedTree").contents[0].contents[0].contents
).toHaveLength(5);
expect(component.state("uncollapsedTree").contents[1]).toEqual(
undefined
);
await component.setProps({
...props,
sources: {
...props.sources,
...newThreadSources
}
});
expect(component.state("uncollapsedTree").contents[0].name).toEqual(
"FakeThread"
);
expect(
component.state("uncollapsedTree").contents[0].contents[0].contents
).toHaveLength(5);
expect(component.state("uncollapsedTree").contents[1].name).toEqual(
"FakeThread1"
);
expect(
component.state("uncollapsedTree").contents[1].contents[0].contents
).toHaveLength(1);
});
});
describe("updates highlighted items", () => {
it("updates highlightItems if selectedSource changes", async () => {
const { component, props } = render();
const mockSource = createMockSource(
"server1.conn13.child1/41",
"http://mdn.com/three.js",
false,
null
);
await component.setProps({
...props,
selectedSource: mockSource
});
expect(component).toMatchSnapshot();
});
});
});
describe("activateItem", () => {
it("select activated item", async () => {
const { instance, props } = render();
const item = createMockItem();
const spy = jest.spyOn(instance, "selectItem");
instance.onActivate(item);
expect(spy).toHaveBeenCalledWith(item);
expect(props.selectSource).toHaveBeenCalledWith(
mockcx,
"server1.conn13.child1/39"
);
});
});
describe("selectItem", () => {
it("should select item with no children", async () => {
const { instance, props } = render();
instance.selectItem(createMockItem());
expect(props.selectSource).toHaveBeenCalledWith(
mockcx,
"server1.conn13.child1/39"
);
});
it("should not select item with children", async () => {
const { props, instance } = render();
instance.selectItem(createMockDirectory());
expect(props.selectSource).not.toHaveBeenCalled();
});
});
describe("handles items", () => {
it("getChildren from directory", async () => {
const { component } = render();
const item = createMockDirectory("http://mdn.com/views", "views", [
"a",
"b"
]);
const children = component
.find("ManagedTree")
.props()
.getChildren(item);
expect(children).toEqual(["a", "b"]);
});
it("getChildren from non directory", async () => {
const { component } = render();
const children = component
.find("ManagedTree")
.props()
.getChildren(createMockItem());
expect(children).toEqual([]);
});
it("onExpand", async () => {
const { component, props } = render();
const expandedState = ["x", "y"];
await component
.find("ManagedTree")
.props()
.onExpand({}, expandedState);
expect(props.setExpandedState).toHaveBeenCalledWith(expandedState);
});
it("onCollapse", async () => {
const { component, props } = render();
const expandedState = ["y", "z"];
await component
.find("ManagedTree")
.props()
.onCollapse({}, expandedState);
expect(props.setExpandedState).toHaveBeenCalledWith(expandedState);
});
it("getParent", async () => {
const { component } = render();
const item = component.state("sourceTree").contents[0].contents[0];
const parent = component
.find("ManagedTree")
.props()
.getParent(item);
expect(parent.path).toEqual("FakeThread");
expect(parent.contents[0].contents).toHaveLength(5);
});
});
describe("getPath", () => {
it("should return path for item", async () => {
const { instance } = render();
const path = instance.getPath(createMockItem());
expect(path).toEqual("http://mdn.com/one.js/server1.conn13.child1/39/");
});
it("should return path for generated item", async () => {
const { instance } = render();
const pathOriginal = instance.getPath(
createMockItem("http://mdn.com/four.js", "four.js", {
id: "server1.conn13.child1/42/originalSource-sha"
})
);
expect(pathOriginal).toEqual(
"http://mdn.com/four.js/server1.conn13.child1/42/originalSource-sha/"
);
const pathGenerated = instance.getPath(
createMockItem("http://mdn.com/four.js", "four.js", {
id: "server1.conn13.child1/42"
})
);
expect(pathGenerated).toEqual(
"http://mdn.com/four.js/server1.conn13.child1/42/"
);
});
});
});
function generateDefaults(overrides: Object) {
const defaultSources = {
FakeThread: {
"server1.conn13.child1/39": createMockSource(
"server1.conn13.child1/39",
"http://mdn.com/one.js",
false,
null
),
"server1.conn13.child1/40": createMockSource(
"server1.conn13.child1/40",
"http://mdn.com/two.js",
false,
null
),
"server1.conn13.child1/41": createMockSource(
"server1.conn13.child1/41",
"http://mdn.com/three.js",
false,
null
),
"server1.conn13.child1/42/originalSource-sha": createMockSource(
"server1.conn13.child1/42/originalSource-sha",
"http://mdn.com/four.js",
false,
null
),
"server1.conn13.child1/42": createMockSource(
"server1.conn13.child1/42",
"http://mdn.com/four.js",
false,
"data:application/json?charset=utf?dsffewrsf"
)
}
};
return {
cx: mockcx,
autoExpandAll: true,
selectSource: jest.fn(),
setExpandedState: jest.fn(),
sources: defaultSources,
debuggeeUrl: "http://mdn.com",
clearProjectDirectoryRoot: jest.fn(),
setProjectDirectoryRoot: jest.fn(),
focusItem: jest.fn(),
projectRoot: "",
threads: [
{
name: "FakeThread",
actor: "FakeThread"
},
{
name: "FakeThread1",
actor: "FakeThread1"
}
],
...overrides
};
}
function render(overrides = {}) {
const props = generateDefaults(overrides);
// $FlowIgnore
const component = shallow(<SourcesTree.WrappedComponent {...props} />);
const defaultState = component.state();
const instance = component.instance();
instance.shouldComponentUpdate = () => true;
return { component, props, defaultState, instance };
}
function createMockSource(id, url, isBlackBoxed = false, sourceMapURL = null) {
return {
...makeMockSource(url, id),
isBlackBoxed,
sourceMapURL
};
}
function createMockDirectory(path = "folder/", name = "folder", contents = []) {
return {
type: "directory",
name,
path,
contents
};
}
function createMockItem(
path = "http://mdn.com/one.js",
name = "one.js",
contents = { id: "server1.conn13.child1/39" }
) {
return {
type: "source",
name,
path,
contents
};
}