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
651 lines (566 loc) · 19.5 KB
/
Copy pathSourcesTree.spec.js
File metadata and controls
651 lines (566 loc) · 19.5 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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/* 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 "react";
import { shallow } from "enzyme";
import SourcesTree from "../../components/PrimaryPanes/SourcesTree";
import * as I from "immutable";
import { showMenu } from "devtools-contextmenu";
import { copyToTheClipboard } from "../../utils/clipboard";
jest.mock("devtools-contextmenu", () => ({ showMenu: jest.fn() }));
jest.mock("../../utils/clipboard", () => ({ copyToTheClipboard: jest.fn() }));
describe("SourcesTree", () => {
afterEach(() => {
copyToTheClipboard.mockClear();
showMenu.mockClear();
});
it("Should show the tree with nothing expanded", async () => {
const { component } = render();
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 mockSource = I.Map({
"server1.conn13.child1/41": createMockSource(
"server1.conn13.child1/41",
"http://mdn.com/three.js"
)
});
await component.setProps({
...props,
sources: mockSource
});
expect(component.state("uncollapsedTree")).toEqual(
defaultState.uncollapsedTree
);
});
it("updates tree with a new item", async () => {
const { component, props } = render();
const sources = props.sources.merge({
"server1.conn13.child1/42": createMockSource(
"server1.conn13.child1/42",
"http://mdn.com/four.js"
)
});
await component.setProps({
...props,
sources: sources
});
expect(
component.state("uncollapsedTree").contents[0].contents
).toHaveLength(4);
});
it("updates sources if sources are emptied", async () => {
const { component, props, defaultState } = render();
expect(defaultState.uncollapsedTree.contents).toHaveLength(1);
await component.setProps({
...props,
sources: I.Map({})
});
expect(component.state("uncollapsedTree").contents).toHaveLength(0);
});
it("recreates tree if projectRoot is changed", async () => {
const { component, props, defaultState } = render();
const sources = I.Map({
"server1.conn13.child1/41": createMockSource(
"server1.conn13.child1/41",
"http://mozilla.com/three.js"
)
});
expect(defaultState.uncollapsedTree.contents[0].contents).toHaveLength(
3
);
await component.setProps({
...props,
sources: sources,
projectRoot: "mozilla"
});
expect(
component.state("uncollapsedTree").contents[0].contents
).toHaveLength(1);
});
it("recreates tree if debugeeUrl is changed", async () => {
const { component, props, defaultState } = render();
const mockSource = I.Map({
"server1.conn13.child1/41": createMockSource(
"server1.conn13.child1/41",
"http://mdn.com/three.js"
)
});
expect(defaultState.uncollapsedTree.contents[0].contents).toHaveLength(
3
);
await component.setProps({
...props,
debuggeeUrl: "mozilla",
sources: mockSource
});
expect(
component.state("uncollapsedTree").contents[0].contents
).toHaveLength(1);
});
});
describe("updates list items", () => {
it("updates list items if shownSource changes", async () => {
const { component, props } = render();
await component.setProps({
...props,
shownSource: "http://mdn.com/three.js"
});
expect(component).toMatchSnapshot();
expect(props.selectLocation).toHaveBeenCalledWith({
sourceId: "server1.conn13.child1/41"
});
});
});
describe("updates highlighted items", () => {
it("updates highlightItems if selectedSource changes", async () => {
const { component, props } = render();
const mockSource = I.Map({
"server1.conn13.child1/41": createMockSource(
"server1.conn13.child1/41",
"http://mdn.com/three.js"
)
});
await component.setProps({
...props,
selectedSource: mockSource
});
expect(component).toMatchSnapshot();
});
});
});
describe("focusItem", () => {
it("update the focused item", async () => {
const { component, instance, props } = render();
const item = createMockItem();
await instance.focusItem(item);
await component.update();
await component
.find(".sources-list")
.simulate("keydown", { keyCode: 13 });
expect(props.selectLocation).toHaveBeenCalledWith({
sourceId: item.contents.get("id")
});
});
});
describe("with custom root", () => {
it("renders custom root source list", async () => {
const { component } = render({
projectRoot: "mdn.com"
});
expect(component).toMatchSnapshot();
});
it("calls clearProjectDirectoryRoot on click", async () => {
const { component, props } = render({
projectRoot: "mdn"
});
component.find(".sources-clear-root").simulate("click");
expect(props.clearProjectDirectoryRoot).toHaveBeenCalled();
});
it("renders empty custom root source list", async () => {
const { component } = render({
projectRoot: "custom",
sources: I.Map()
});
expect(component).toMatchSnapshot();
});
});
describe("onContextMenu of the tree", () => {
it("shows context menu on directory to set as root", async () => {
const menuOptions = [
{
accesskey: "r",
click: expect.any(Function),
disabled: false,
id: "node-set-directory-root",
label: "Set directory root"
}
];
const mockEvent = {
preventDefault: jest.fn(),
stopPropagation: jest.fn()
};
const { props, instance } = render({
projectRoot: "root/"
});
await instance.onContextMenu(mockEvent, createMockDirectory());
expect(showMenu).toHaveBeenCalledWith(mockEvent, menuOptions);
expect(mockEvent.preventDefault).toHaveBeenCalled();
expect(mockEvent.stopPropagation).toHaveBeenCalled();
showMenu.mock.calls[0][1][0].click();
expect(props.setProjectDirectoryRoot).toHaveBeenCalled();
expect(props.clearProjectDirectoryRoot).not.toHaveBeenCalled();
expect(copyToTheClipboard).not.toHaveBeenCalled();
});
it("shows context menu on file to copy source uri", async () => {
const menuOptions = [
{
accesskey: "u",
click: expect.any(Function),
disabled: false,
id: "node-menu-copy-source",
label: "Copy source URI"
}
];
const mockEvent = {
preventDefault: jest.fn(),
stopPropagation: jest.fn()
};
const { props, instance } = render({
projectRoot: "root/"
});
await instance.onContextMenu(mockEvent, createMockItem());
expect(showMenu).toHaveBeenCalledWith(mockEvent, menuOptions);
expect(mockEvent.preventDefault).toHaveBeenCalled();
expect(mockEvent.stopPropagation).toHaveBeenCalled();
showMenu.mock.calls[0][1][0].click();
expect(props.setProjectDirectoryRoot).not.toHaveBeenCalled();
expect(props.clearProjectDirectoryRoot).not.toHaveBeenCalled();
expect(copyToTheClipboard).toHaveBeenCalled();
});
it("shows context menu on root to remove directory root", async () => {
const menuOptions = [
{
click: expect.any(Function),
disabled: false,
id: "node-remove-directory-root",
label: "Remove directory root"
}
];
const { props, instance } = render({
projectRoot: "root/"
});
const mockEvent = {
preventDefault: jest.fn(),
stopPropagation: jest.fn()
};
await instance.onContextMenu(
mockEvent,
createMockDirectory("root/", "root")
);
expect(showMenu).toHaveBeenCalledWith(mockEvent, menuOptions);
expect(mockEvent.preventDefault).toHaveBeenCalled();
expect(mockEvent.stopPropagation).toHaveBeenCalled();
showMenu.mock.calls[0][1][0].click();
expect(props.setProjectDirectoryRoot).not.toHaveBeenCalled();
expect(props.clearProjectDirectoryRoot).toHaveBeenCalled();
expect(copyToTheClipboard).not.toHaveBeenCalled();
});
});
describe("renderItem", () => {
it("should show icon for webpack item", async () => {
const { instance } = render();
const item = createMockDirectory("webpack://", "webpack://");
const node = renderItem(instance, item);
expect(node).toMatchSnapshot();
});
it("should show icon for angular item", async () => {
const { instance } = render();
const item = createMockDirectory("ng://", "ng://");
const node = renderItem(instance, item);
expect(node).toMatchSnapshot();
});
it("should show icon for moz-extension item", async () => {
const { instance } = render();
const item = createMockDirectory("moz-extension://", "moz-extension://");
const node = renderItem(instance, item);
expect(node).toMatchSnapshot();
});
it("should show icon for folder with arrow", async () => {
const { instance } = render();
const node = renderItem(instance, createMockDirectory());
expect(node).toMatchSnapshot();
});
it("should show icon for folder with expanded arrow", async () => {
const { instance } = render();
const node = renderItem(instance, createMockDirectory(), 1, false, true);
expect(node).toMatchSnapshot();
});
it("should show focused item for folder with expanded arrow", async () => {
const { instance } = render();
const node = renderItem(instance, createMockDirectory(), 1, true, true);
expect(node).toMatchSnapshot();
});
it("should show source item with source icon", async () => {
const { instance } = render();
const node = renderItem(instance, createMockItem());
expect(node).toMatchSnapshot();
});
it("should show source item with source icon with focus", async () => {
const { instance } = render();
const node = renderItem(instance, createMockItem(), 1, true, false);
expect(node).toMatchSnapshot();
});
it("should show domain item", async () => {
const { instance } = render();
const item = createMockItem("root", "root");
const node = renderItem(instance, item, 0);
expect(node).toMatchSnapshot();
});
it("should show domain item as debuggee", async () => {
const { instance } = render();
const item = createMockItem("root", "http://mdn.com");
const node = renderItem(instance, item, 0);
expect(node).toMatchSnapshot();
});
it("should show domain item as debuggee with focus and arrow", async () => {
const { instance } = render();
const item = createMockDirectory("root", "http://mdn.com");
const node = renderItem(instance, item, 0, true);
expect(node).toMatchSnapshot();
});
it("should not show domain item when the projectRoot exists", async () => {
const { instance } = render({
projectRoot: "root/"
});
const node = renderItem(instance, createMockItem(), 0);
expect(node).toMatchSnapshot();
});
it("should show menu on contextmenu of an item", async () => {
const { instance } = render();
const item = createMockItem();
instance.onContextMenu = jest.fn(() => {});
const event = { event: "contextmenu" };
const node = shallow(renderItem(instance, item, 1, true));
node.simulate("contextmenu", event);
expect(instance.onContextMenu).toHaveBeenCalledWith(event, item);
});
it("should focus on and select item on click", async () => {
const { component, instance, props } = render();
const item = createMockItem();
const event = { event: "click" };
const setExpanded = jest.fn();
const node = shallow(
renderItem(instance, item, 1, true, false, setExpanded)
);
node.simulate("click", event);
await component
.find(".sources-list")
.simulate("keydown", { keyCode: 13 });
expect(props.selectLocation).toHaveBeenCalledWith({
sourceId: item.contents.get("id")
});
expect(setExpanded).not.toHaveBeenCalled();
});
it("should focus on and expand directory on click", async () => {
const { component, instance, props } = render();
const event = { event: "click" };
const setExpanded = jest.fn();
const mockDirectory = createMockDirectory();
const node = shallow(
renderItem(instance, mockDirectory, 1, true, false, setExpanded)
);
node.simulate("click", event);
expect(component.state("focusedItem")).toEqual(mockDirectory);
expect(setExpanded).toHaveBeenCalled();
expect(props.selectLocation).not.toHaveBeenCalledWith();
});
});
describe("selectItem", () => {
it("should select item with no children", async () => {
const { instance, props } = render();
instance.selectItem(createMockItem());
expect(props.selectLocation).toHaveBeenCalledWith({
sourceId: "server1.conn13.child1/39"
});
});
it("should not select item with children", async () => {
const { props, instance } = render();
instance.selectItem(createMockDirectory());
expect(props.selectLocation).not.toHaveBeenCalled();
});
it("should select item on enter onKeyDown event", async () => {
const { component, props, instance } = render();
await instance.focusItem(createMockItem());
await component.update();
await component
.find(".sources-list")
.simulate("keydown", { keyCode: 13 });
expect(props.selectLocation).toHaveBeenCalledWith({
sourceId: "server1.conn13.child1/39"
});
});
it("does not select if no item is focused on", async () => {
const { component, props } = render();
await component
.find(".sources-list")
.simulate("keydown", { keyCode: 13 });
expect(props.selectLocation).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("mdn.com");
expect(parent.contents).toHaveLength(3);
});
});
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/one.js/");
});
it("should return path for blackboxedboxed item", async () => {
const item = createMockItem(
"http://mdn.com/blackboxed.js",
"blackboxed.js",
I.Map({ id: "server1.conn13.child1/59" })
);
const source = I.Map({
"server1.conn13.child1/59": createMockSource(
"server1.conn13.child1/59",
"http://mdn.com/blackboxed.js",
true
)
});
const { instance } = render({
sources: source
});
const path = instance.getPath(item);
expect(path).toEqual("http://mdn.com/blackboxed.js/blackboxed.js/update");
});
});
});
function generateDefaults(overrides) {
const defaultSources = I.Map({
"server1.conn13.child1/39": createMockSource(
"server1.conn13.child1/39",
"http://mdn.com/one.js"
),
"server1.conn13.child1/40": createMockSource(
"server1.conn13.child1/40",
"http://mdn.com/two.js"
),
"server1.conn13.child1/41": createMockSource(
"server1.conn13.child1/41",
"http://mdn.com/three.js"
)
});
return {
autoExpandAll: true,
selectLocation: jest.fn(),
setExpandedState: jest.fn(),
sources: defaultSources,
debuggeeUrl: "http://mdn.com",
clearProjectDirectoryRoot: jest.fn(),
setProjectDirectoryRoot: jest.fn(),
projectRoot: "",
...overrides
};
}
function renderItem(
instance,
item = createMockItem(),
depth = 1,
focused = false,
expanded = false,
setExpanded = jest.fn()
) {
return instance.renderItem(item, depth, focused, null, expanded, {
setExpanded: setExpanded
});
}
function render(overrides = {}) {
const props = generateDefaults(overrides);
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) {
return I.Map({
id: id,
url: url,
isPrettyPrinted: false,
isWasm: false,
sourceMapURL: null,
isBlackBoxed: isBlackBoxed,
loadedState: "unloaded"
});
}
function createMockDirectory(path = "folder/", name = "folder", contents = []) {
return {
name,
path,
contents
};
}
function createMockItem(
path = "http://mdn.com/one.js",
name = "one.js",
contents = I.Map({ id: "server1.conn13.child1/39" })
) {
return {
name,
path,
contents
};
}