jqtree 1.7.4 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/.eslintrc +5 -1
  2. package/bower.json +1 -1
  3. package/config/babel.config.json +1 -1
  4. package/config/production +2 -0
  5. package/devserver/devserver_scroll.js +8 -0
  6. package/devserver/test_scroll.html +28 -0
  7. package/devserver/test_scroll_container.html +39 -0
  8. package/docs/_config.yml +1 -1
  9. package/docs/_entries/general/changelog.md +12 -0
  10. package/docs/package.json +1 -1
  11. package/docs/pnpm-lock.yaml +30 -30
  12. package/package.json +31 -27
  13. package/src/dataLoader.ts +44 -19
  14. package/src/dragAndDropHandler/dragElement.ts +42 -0
  15. package/src/dragAndDropHandler/hitAreasGenerator.ts +175 -0
  16. package/src/dragAndDropHandler/index.ts +470 -0
  17. package/src/dragAndDropHandler/types.ts +12 -0
  18. package/src/dragAndDropHandler/visibleNodeIterator.ts +97 -0
  19. package/src/elementsRenderer.ts +75 -40
  20. package/src/jqtreeMethodTypes.ts +40 -0
  21. package/src/jqtreeOptions.ts +43 -25
  22. package/src/keyHandler.ts +59 -38
  23. package/src/mouse.widget.ts +3 -3
  24. package/src/mouseWidgetTypes.ts +6 -0
  25. package/src/node.ts +32 -48
  26. package/src/nodeElement/borderDropHint.ts +32 -0
  27. package/src/nodeElement/folderElement.ts +133 -0
  28. package/src/nodeElement/ghostDropHint.ts +68 -0
  29. package/src/nodeElement/index.ts +101 -0
  30. package/src/nodeUtils.ts +10 -0
  31. package/src/playwright/coverage.ts +1 -4
  32. package/src/playwright/playwright.test.ts +203 -15
  33. package/src/playwright/testUtils.ts +23 -15
  34. package/src/saveStateHandler.ts +75 -26
  35. package/src/scrollHandler/containerScrollParent.ts +177 -0
  36. package/src/scrollHandler/createScrollParent.ts +50 -0
  37. package/src/scrollHandler/documentScrollParent.ts +182 -0
  38. package/src/scrollHandler/types.ts +7 -0
  39. package/src/scrollHandler.ts +36 -248
  40. package/src/selectNodeHandler.ts +10 -16
  41. package/src/test/jqTree/keyboard.test.ts +18 -23
  42. package/src/test/jqTree/loadOnDemand.test.ts +2 -3
  43. package/src/test/jqTree/methods.test.ts +33 -4
  44. package/src/test/jqTree/options.test.ts +15 -4
  45. package/src/test/node.test.ts +85 -26
  46. package/src/test/nodeUtils.test.ts +21 -0
  47. package/src/tree.jquery.ts +262 -83
  48. package/src/util.ts +3 -0
  49. package/src/version.ts +1 -1
  50. package/tree.jquery.debug.js +1922 -2608
  51. package/tree.jquery.debug.js.map +1 -1
  52. package/tree.jquery.js +2 -2
  53. package/tree.jquery.js.map +1 -1
  54. package/lib/dataLoader.js +0 -124
  55. package/lib/dragAndDropHandler.js +0 -596
  56. package/lib/elementsRenderer.js +0 -268
  57. package/lib/jqtreeOptions.js +0 -1
  58. package/lib/keyHandler.js +0 -115
  59. package/lib/mouse.widget.js +0 -256
  60. package/lib/node.js +0 -717
  61. package/lib/nodeElement.js +0 -277
  62. package/lib/playwright/coverage.js +0 -96
  63. package/lib/playwright/playwright.test.js +0 -228
  64. package/lib/playwright/testUtils.js +0 -184
  65. package/lib/saveStateHandler.js +0 -278
  66. package/lib/scrollHandler.js +0 -250
  67. package/lib/selectNodeHandler.js +0 -129
  68. package/lib/simple.widget.js +0 -159
  69. package/lib/test/global.d.js +0 -3
  70. package/lib/test/jqTree/accessibility.test.js +0 -37
  71. package/lib/test/jqTree/create.test.js +0 -48
  72. package/lib/test/jqTree/events.test.js +0 -210
  73. package/lib/test/jqTree/keyboard.test.js +0 -225
  74. package/lib/test/jqTree/loadOnDemand.test.js +0 -218
  75. package/lib/test/jqTree/methods.test.js +0 -1347
  76. package/lib/test/jqTree/options.test.js +0 -548
  77. package/lib/test/node.test.js +0 -1160
  78. package/lib/test/nodeUtil.test.js +0 -27
  79. package/lib/test/support/exampleData.js +0 -36
  80. package/lib/test/support/jqTreeMatchers.js +0 -70
  81. package/lib/test/support/matchers.d.js +0 -1
  82. package/lib/test/support/setupTests.js +0 -7
  83. package/lib/test/support/testUtil.js +0 -32
  84. package/lib/test/support/treeStructure.js +0 -39
  85. package/lib/test/util.test.js +0 -26
  86. package/lib/tree.jquery.d.js +0 -1
  87. package/lib/tree.jquery.js +0 -1106
  88. package/lib/types.js +0 -1
  89. package/lib/typings.d.js +0 -2
  90. package/lib/util.js +0 -18
  91. package/lib/version.js +0 -9
  92. package/src/dragAndDropHandler.ts +0 -719
  93. package/src/nodeElement.ts +0 -272
  94. package/src/types.ts +0 -19
@@ -1,1160 +0,0 @@
1
- "use strict";
2
-
3
- var _givens = _interopRequireDefault(require("givens"));
4
- var _node = require("../node");
5
- var _exampleData = _interopRequireDefault(require("./support/exampleData"));
6
- require("jest-extended");
7
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
8
- var context = describe;
9
- describe("addAfter", function () {
10
- var given = (0, _givens["default"])();
11
- given("node1", function () {
12
- return given.tree.getNodeByNameMustExist("node1");
13
- });
14
- given("tree", function () {
15
- return new _node.Node().loadFromData(_exampleData["default"]);
16
- });
17
- context("when moving after node1", function () {
18
- it("returns the new node", function () {
19
- expect(given.node1.addAfter("new node")).toMatchObject({
20
- name: "new node"
21
- });
22
- });
23
- it("adds after the node", function () {
24
- given.node1.addAfter("new node");
25
- expect(given.tree).toMatchObject({
26
- children: [expect.objectContaining({
27
- name: "node1"
28
- }), expect.objectContaining({
29
- name: "new node"
30
- }), expect.objectContaining({
31
- name: "node2"
32
- })]
33
- });
34
- });
35
- });
36
- context("when moving after the root node", function () {
37
- it("returns null", function () {
38
- expect(given.tree.addAfter("new node")).toBeNull();
39
- });
40
- it("doesn't add anything", function () {
41
- expect(given.tree).toMatchObject({
42
- children: [expect.objectContaining({
43
- name: "node1"
44
- }), expect.objectContaining({
45
- name: "node2"
46
- })]
47
- });
48
- });
49
- });
50
- context("when adding a node with children", function () {
51
- it("adds the children", function () {
52
- given.node1.addAfter({
53
- name: "new node",
54
- children: ["newchild1", "newchild2"]
55
- });
56
- expect(given.tree).toMatchObject({
57
- children: [expect.objectContaining({
58
- name: "node1"
59
- }), expect.objectContaining({
60
- name: "new node",
61
- children: [expect.objectContaining({
62
- name: "newchild1"
63
- }), expect.objectContaining({
64
- name: "newchild2"
65
- })]
66
- }), expect.objectContaining({
67
- name: "node2"
68
- })]
69
- });
70
- });
71
- });
72
- });
73
- describe("addBefore", function () {
74
- var given = (0, _givens["default"])();
75
- given("node2", function () {
76
- return given.tree.getNodeByNameMustExist("node2");
77
- });
78
- given("tree", function () {
79
- return new _node.Node().loadFromData(_exampleData["default"]);
80
- });
81
- it("returns the new node", function () {
82
- expect(given.node2.addBefore("new node")).toMatchObject({
83
- name: "new node"
84
- });
85
- });
86
- it("adds before the node", function () {
87
- given.node2.addBefore("new node");
88
- expect(given.tree).toMatchObject({
89
- children: [expect.objectContaining({
90
- name: "node1"
91
- }), expect.objectContaining({
92
- name: "new node"
93
- }), expect.objectContaining({
94
- name: "node2"
95
- })]
96
- });
97
- });
98
- context("with a root node", function () {
99
- it("returns null", function () {
100
- expect(given.tree.addBefore("new node")).toBeNull();
101
- });
102
- it("does nothing", function () {
103
- given.tree.addBefore("new node");
104
- expect(given.tree).toMatchObject({
105
- children: [expect.objectContaining({
106
- name: "node1"
107
- }), expect.objectContaining({
108
- name: "node2"
109
- })]
110
- });
111
- });
112
- });
113
- context("when adding a node with children", function () {
114
- it("adds the children", function () {
115
- given.node2.addBefore({
116
- name: "new node",
117
- children: ["newchild1", "newchild2"]
118
- });
119
- expect(given.tree).toMatchObject({
120
- children: [expect.objectContaining({
121
- name: "node1"
122
- }), expect.objectContaining({
123
- name: "new node",
124
- children: [expect.objectContaining({
125
- name: "newchild1"
126
- }), expect.objectContaining({
127
- name: "newchild2"
128
- })]
129
- }), expect.objectContaining({
130
- name: "node2"
131
- })]
132
- });
133
- });
134
- });
135
- });
136
- describe("addChild", function () {
137
- var given = (0, _givens["default"])();
138
- given("node", function () {
139
- return new _node.Node();
140
- });
141
- beforeEach(function () {
142
- given.node.addChild(new _node.Node({
143
- id: 100,
144
- name: "child1"
145
- }));
146
- });
147
- it("adds the child", function () {
148
- expect(given.node.children).toEqual(expect.arrayContaining([expect.objectContaining({
149
- id: 100,
150
- name: "child1"
151
- })]));
152
- });
153
- it("sets the parent of the child", function () {
154
- var _given$node$children$;
155
- expect((_given$node$children$ = given.node.children[0]) === null || _given$node$children$ === void 0 ? void 0 : _given$node$children$.parent).toEqual(given.node);
156
- });
157
- });
158
- describe("addChildAtPosition", function () {
159
- var given = (0, _givens["default"])();
160
- given("child", function () {
161
- return new _node.Node("new");
162
- });
163
- given("node", function () {
164
- return new _node.Node("new").loadFromData(["child1", "child2"]);
165
- });
166
- beforeEach(function () {
167
- given.node.addChildAtPosition(given.child, given.index);
168
- });
169
- context("with index 0", function () {
170
- given("index", function () {
171
- return 0;
172
- });
173
- it("adds at the start", function () {
174
- expect(given.node.children).toEqual([expect.objectContaining({
175
- name: "new"
176
- }), expect.objectContaining({
177
- name: "child1"
178
- }), expect.objectContaining({
179
- name: "child2"
180
- })]);
181
- expect(given.node.children).toEqual([expect.objectContaining({
182
- name: "new"
183
- }), expect.objectContaining({
184
- name: "child1"
185
- }), expect.objectContaining({
186
- name: "child2"
187
- })]);
188
- });
189
- });
190
- context("with index 1", function () {
191
- given("index", function () {
192
- return 1;
193
- });
194
- it("inserts at index 1", function () {
195
- expect(given.node.children).toEqual([expect.objectContaining({
196
- name: "child1"
197
- }), expect.objectContaining({
198
- name: "new"
199
- }), expect.objectContaining({
200
- name: "child2"
201
- })]);
202
- });
203
- });
204
- context("with non existing index", function () {
205
- given("index", function () {
206
- return 99;
207
- });
208
- it("adds add the end", function () {
209
- expect(given.node.children).toEqual([expect.objectContaining({
210
- name: "child1"
211
- }), expect.objectContaining({
212
- name: "child2"
213
- }), expect.objectContaining({
214
- name: "new"
215
- })]);
216
- });
217
- });
218
- });
219
- describe("addParent", function () {
220
- var given = (0, _givens["default"])();
221
- given("node1", function () {
222
- return new _node.Node("node1");
223
- });
224
- given("tree", function () {
225
- return new _node.Node({}, true);
226
- });
227
- beforeEach(function () {
228
- given.tree.addChild(given.node1);
229
- given.node1.append("child1");
230
- });
231
- it("adds a parent node", function () {
232
- given.node1.addParent("parent1");
233
- expect(given.tree).toMatchObject({
234
- name: "",
235
- children: [expect.objectContaining({
236
- name: "parent1",
237
- children: [expect.objectContaining({
238
- name: "node1",
239
- children: [expect.objectContaining({
240
- name: "child1"
241
- })]
242
- })]
243
- })]
244
- });
245
- });
246
- it("returns the new node", function () {
247
- expect(given.node1.addParent("parent1")).toMatchObject({
248
- name: "parent1"
249
- });
250
- });
251
- context("with a root node", function () {
252
- it("returns null", function () {
253
- expect(given.tree.addParent("parent1")).toBeNull();
254
- });
255
- });
256
- });
257
- describe("append", function () {
258
- var given = (0, _givens["default"])();
259
- given("node", function () {
260
- return new _node.Node("node1");
261
- });
262
- it("appends a node", function () {
263
- given.node.append("child1");
264
- given.node.append("child2");
265
- expect(given.node).toMatchObject({
266
- name: "node1",
267
- children: [expect.objectContaining({
268
- name: "child1"
269
- }), expect.objectContaining({
270
- name: "child2"
271
- })]
272
- });
273
- });
274
- it("returns the new node", function () {
275
- expect(given.node.append("child1")).toMatchObject({
276
- name: "child1"
277
- });
278
- });
279
- context("when adding a node with children", function () {
280
- it("adds the children", function () {
281
- given.node.append({
282
- name: "new node",
283
- children: ["newchild1", "newchild2"]
284
- });
285
- expect(given.node).toMatchObject({
286
- children: [expect.objectContaining({
287
- name: "new node",
288
- children: [expect.objectContaining({
289
- name: "newchild1"
290
- }), expect.objectContaining({
291
- name: "newchild2"
292
- })]
293
- })]
294
- });
295
- });
296
- });
297
- });
298
- describe("constructor", function () {
299
- var given = (0, _givens["default"])();
300
- given("node", function () {
301
- return given.params === undefined ? new _node.Node() : new _node.Node(given.params);
302
- });
303
- context("without parameters", function () {
304
- it("creates a node", function () {
305
- expect(given.node.name).toBe("");
306
- expect(given.node.id).toBeUndefined();
307
- });
308
- });
309
- context("with a string", function () {
310
- given("params", function () {
311
- return "n1";
312
- });
313
- it("creates a node", function () {
314
- expect(given.node).toMatchObject({
315
- name: "n1",
316
- children: [],
317
- parent: null
318
- });
319
- expect(given.node).not.toHaveProperty("id");
320
- });
321
- });
322
- context("with an object with a name property", function () {
323
- given("params", function () {
324
- return {
325
- id: 123,
326
- name: "n1"
327
- };
328
- });
329
- it("creates a node", function () {
330
- expect(given.node).toMatchObject({
331
- id: 123,
332
- name: "n1"
333
- });
334
- });
335
- });
336
- context("when the name property is null", function () {
337
- given("params", function () {
338
- return {
339
- name: null
340
- };
341
- });
342
- it("sets the name to an empty string", function () {
343
- expect(given.node.name).toBe("");
344
- });
345
- });
346
- context("with an object with more properties", function () {
347
- given("params", function () {
348
- return {
349
- color: "green",
350
- id: 123,
351
- name: "n1",
352
- url: "/abc"
353
- };
354
- });
355
- it("creates a node", function () {
356
- expect(given.node).toMatchObject({
357
- color: "green",
358
- id: 123,
359
- name: "n1",
360
- url: "/abc"
361
- });
362
- });
363
- });
364
- context("with an object with a label property", function () {
365
- given("params", function () {
366
- return {
367
- id: 123,
368
- label: "n1",
369
- url: "/"
370
- };
371
- });
372
- it("creates a node", function () {
373
- expect(given.node).toMatchObject({
374
- id: 123,
375
- name: "n1",
376
- url: "/"
377
- });
378
- // todo: match object?
379
- expect(given.node).not.toHaveProperty("label");
380
- expect(given.node.children).toHaveLength(0);
381
- expect(given.node.parent).toBeNull();
382
- });
383
- });
384
- context("with an object with a parent", function () {
385
- given("params", function () {
386
- return {
387
- name: "n1",
388
- parent: "abc"
389
- };
390
- });
391
- it("doesn't set the parent", function () {
392
- expect(given.node.name).toBe("n1");
393
- expect(given.node.parent).toBeNull();
394
- });
395
- });
396
- context("with an object with children", function () {
397
- given("params", function () {
398
- return {
399
- name: "n1",
400
- children: ["c"]
401
- };
402
- });
403
- it("doesn't set the children", function () {
404
- // todo: match object?
405
- expect(given.node.name).toBe("n1");
406
- expect(given.node.children).toHaveLength(0);
407
- });
408
- });
409
- });
410
- describe("getChildIndex", function () {
411
- var given = (0, _givens["default"])();
412
- given("child2", function () {
413
- return new _node.Node("child2");
414
- });
415
- given("node", function () {
416
- return new _node.Node();
417
- });
418
- beforeEach(function () {
419
- given.node.addChild(new _node.Node("child1"));
420
- given.node.addChild(given.child2);
421
- given.node.addChild(new _node.Node("child3"));
422
- });
423
- context("when a child exists", function () {
424
- it("returns the index", function () {
425
- expect(given.node.getChildIndex(given.child2)).toBe(1);
426
- });
427
- });
428
- context("when a child doesn't exist", function () {
429
- it("returns -1", function () {
430
- var nonExistingChild = new _node.Node("non-existing");
431
- expect(given.node.getChildIndex(nonExistingChild)).toBe(-1);
432
- });
433
- });
434
- });
435
- describe("getData", function () {
436
- var given = (0, _givens["default"])();
437
- given("tree", function () {
438
- return new _node.Node().loadFromData(_exampleData["default"]);
439
- });
440
- it("returns the tree data", function () {
441
- expect(given.tree.getData()).toEqual([expect.objectContaining({
442
- name: "node1",
443
- children: [expect.objectContaining({
444
- name: "child1"
445
- }), expect.objectContaining({
446
- name: "child2"
447
- })]
448
- }), expect.objectContaining({
449
- name: "node2"
450
- })]);
451
- });
452
- it("doesn't include internal attributes", function () {
453
- expect(given.tree.getData()[0]).not.toContainAnyKeys(["element", "isEmptyFolder", "parent"]);
454
- });
455
- context("with includeParent parameter", function () {
456
- it("returns the tree data including the node itself", function () {
457
- expect(given.tree.getData(true)).toEqual([expect.objectContaining({
458
- children: [expect.objectContaining({
459
- name: "node1"
460
- }), expect.objectContaining({
461
- name: "node2"
462
- })]
463
- })]);
464
- });
465
- });
466
- });
467
- describe("getLastChild", function () {
468
- var given = (0, _givens["default"])();
469
- given("node", function () {
470
- return new _node.Node();
471
- });
472
- context("when a node has no children", function () {
473
- it("returns null", function () {
474
- expect(given.node.getLastChild()).toBeNull();
475
- });
476
- });
477
- context("when a node has children", function () {
478
- beforeEach(function () {
479
- given.node.append("child1");
480
- given.node.append("child2");
481
- });
482
- it("returns the last child", function () {
483
- expect(given.node.getLastChild()).toMatchObject({
484
- name: "child2"
485
- });
486
- });
487
- context("when its last child is open and has children", function () {
488
- beforeEach(function () {
489
- var child2 = given.node.children[1];
490
- child2 === null || child2 === void 0 || child2.append("child2a");
491
- child2 === null || child2 === void 0 || child2.append("child2b");
492
- });
493
- context("and the last child is open", function () {
494
- beforeEach(function () {
495
- var child2 = given.node.children[1];
496
- if (child2) {
497
- child2.is_open = true;
498
- }
499
- });
500
- it("returns the last child of that child", function () {
501
- expect(given.node.getLastChild()).toMatchObject({
502
- name: "child2b"
503
- });
504
- });
505
- });
506
- context("and the last child is closed", function () {
507
- it("returns the last child", function () {
508
- expect(given.node.getLastChild()).toMatchObject({
509
- name: "child2"
510
- });
511
- });
512
- });
513
- });
514
- });
515
- });
516
- describe("getNextNode", function () {
517
- var given = (0, _givens["default"])();
518
- given("tree", function () {
519
- return new _node.Node().loadFromData(_exampleData["default"]);
520
- });
521
- context("with a parent node", function () {
522
- given("fromNode", function () {
523
- return given.tree.getNodeByNameMustExist("node1");
524
- });
525
- context("when the parent node is closed", function () {
526
- it("returns the first child", function () {
527
- expect(given.fromNode.getNextNode()).toMatchObject({
528
- name: "child1"
529
- });
530
- });
531
- });
532
- context("when the parent node is open", function () {
533
- beforeEach(function () {
534
- given.fromNode.is_open = true;
535
- });
536
- it("returns the first child", function () {
537
- expect(given.fromNode.getNextNode()).toMatchObject({
538
- name: "child1"
539
- });
540
- });
541
- });
542
- });
543
- context("with the node is the last child", function () {
544
- given("fromNode", function () {
545
- return given.tree.getNodeByNameMustExist("child2");
546
- });
547
- it("returns the next sibling of the parent", function () {
548
- expect(given.fromNode.getNextNode()).toMatchObject({
549
- name: "node2"
550
- });
551
- });
552
- });
553
- context("with includeChildren is false", function () {
554
- context("with an open parent node", function () {
555
- given("fromNode", function () {
556
- return given.tree.getNodeByNameMustExist("node1");
557
- });
558
- beforeEach(function () {
559
- given.fromNode.is_open = true;
560
- });
561
- it("returns the next sibling", function () {
562
- expect(given.fromNode.getNextNode(false)).toMatchObject({
563
- name: "node2"
564
- });
565
- });
566
- });
567
- });
568
- });
569
- describe("getNextVisibleNode", function () {
570
- var given = (0, _givens["default"])();
571
- given("tree", function () {
572
- return new _node.Node().loadFromData(_exampleData["default"]);
573
- });
574
- context("with a parent node", function () {
575
- given("fromNode", function () {
576
- return given.tree.getNodeByNameMustExist("node1");
577
- });
578
- context("when the parent node is closed", function () {
579
- it("returns the next sibling", function () {
580
- expect(given.fromNode.getNextVisibleNode()).toMatchObject({
581
- name: "node2"
582
- });
583
- });
584
- });
585
- context("when the parent node is open", function () {
586
- beforeEach(function () {
587
- given.fromNode.is_open = true;
588
- });
589
- it("returns the first child", function () {
590
- expect(given.fromNode.getNextVisibleNode()).toMatchObject({
591
- name: "child1"
592
- });
593
- });
594
- });
595
- });
596
- context("with the node is the last child", function () {
597
- given("fromNode", function () {
598
- return given.tree.getNodeByNameMustExist("child2");
599
- });
600
- it("returns the next sibling of the parent", function () {
601
- expect(given.fromNode.getNextVisibleNode()).toMatchObject({
602
- name: "node2"
603
- });
604
- });
605
- });
606
- context("with the tree node", function () {
607
- given("fromNode", function () {
608
- return given.tree;
609
- });
610
- it("returns null", function () {
611
- expect(given.fromNode.getNextVisibleNode()).toBeNull();
612
- });
613
- });
614
- });
615
- describe("getNextSibling", function () {
616
- var given = (0, _givens["default"])();
617
- given("node1", function () {
618
- return given.tree.getNodeByNameMustExist("node1");
619
- });
620
- given("node2", function () {
621
- return given.tree.getNodeByNameMustExist("node2");
622
- });
623
- given("tree", function () {
624
- return new _node.Node().loadFromData(_exampleData["default"]);
625
- });
626
- context("with a tree", function () {
627
- it("returns null", function () {
628
- expect(given.tree.getNextSibling()).toBeNull();
629
- });
630
- });
631
- context("when the node is the last child", function () {
632
- it("returns null", function () {
633
- expect(given.node2.getNextSibling()).toBeNull();
634
- });
635
- });
636
- context("when the node is the first child", function () {
637
- it("returns the second child", function () {
638
- expect(given.node1.getNextSibling()).toBe(given.node2);
639
- });
640
- });
641
- });
642
- describe("getNodeByCallback", function () {
643
- var given = (0, _givens["default"])();
644
- given("tree", function () {
645
- return new _node.Node().loadFromData(_exampleData["default"]);
646
- });
647
- context("when a matching node exists", function () {
648
- it("returns the node", function () {
649
- expect(given.tree.getNodeByCallback(function (node) {
650
- return node.name.startsWith("chi");
651
- })).toMatchObject({
652
- name: "child1"
653
- });
654
- });
655
- });
656
- context("when no matching node exists", function () {
657
- it("returns null", function () {
658
- expect(given.tree.getNodeByCallback(function () {
659
- return false;
660
- })).toBeNull();
661
- });
662
- });
663
- });
664
- describe("getNodeByName", function () {
665
- var given = (0, _givens["default"])();
666
- given("tree", function () {
667
- return new _node.Node().loadFromData(_exampleData["default"]);
668
- });
669
- context("when the node exists", function () {
670
- it("returns the node", function () {
671
- expect(given.tree.getNodeByName("child1")).toMatchObject({
672
- id: 125,
673
- name: "child1"
674
- });
675
- });
676
- });
677
- context("when the node doesn't exist", function () {
678
- it("returns null", function () {
679
- expect(given.tree.getNodeByName("non-existing")).toBeNull();
680
- });
681
- });
682
- });
683
- describe("getNodeByNameMustExist", function () {
684
- var given = (0, _givens["default"])();
685
- given("tree", function () {
686
- return new _node.Node().loadFromData(_exampleData["default"]);
687
- });
688
- context("when the node exists", function () {
689
- it("returns the node", function () {
690
- expect(given.tree.getNodeByNameMustExist("child1")).toMatchObject({
691
- id: 125,
692
- name: "child1"
693
- });
694
- });
695
- });
696
- context("when the node doesn't exist", function () {
697
- it("throws an exception", function () {
698
- expect(function () {
699
- return given.tree.getNodeByNameMustExist("non-existing");
700
- }).toThrow("Node with name non-existing not found");
701
- });
702
- });
703
- });
704
- describe("getParent", function () {
705
- var given = (0, _givens["default"])();
706
- given("child1", function () {
707
- return given.tree.getNodeByNameMustExist("child1");
708
- });
709
- given("node1", function () {
710
- return given.tree.getNodeByNameMustExist("node1");
711
- });
712
- given("tree", function () {
713
- return new _node.Node().loadFromData(_exampleData["default"]);
714
- });
715
- context("with a tree", function () {
716
- it("returns null", function () {
717
- expect(given.tree.getParent()).toBeNull();
718
- });
719
- });
720
- context("with a node on the first level", function () {
721
- it("returns null", function () {
722
- expect(given.node1.getParent()).toBeNull();
723
- });
724
- });
725
- context("with a node on the second level", function () {
726
- it("returns the parent", function () {
727
- expect(given.child1.getParent()).toMatchObject({
728
- name: "node1"
729
- });
730
- });
731
- });
732
- });
733
- describe("getPreviousNode", function () {
734
- var given = (0, _givens["default"])();
735
- given("tree", function () {
736
- return new _node.Node().loadFromData(_exampleData["default"]);
737
- });
738
- it("returns null with a tree node", function () {
739
- expect(given.tree.getPreviousNode()).toBeNull();
740
- });
741
- it("returns the last child of the previous sibling when the previous node is closed and has children", function () {
742
- given.tree.getNodeByNameMustExist("node2").is_open = false;
743
- var node2 = given.tree.getNodeByNameMustExist("node2");
744
- expect(node2.getPreviousNode()).toMatchObject({
745
- name: "child2"
746
- });
747
- });
748
- it("returns the last child of the previous sibling when the previous node is open and has children", function () {
749
- given.tree.getNodeByNameMustExist("node2").is_open = true;
750
- var node2 = given.tree.getNodeByNameMustExist("node2");
751
- expect(node2.getPreviousNode()).toMatchObject({
752
- name: "child2"
753
- });
754
- });
755
- it("returns the first child if a node is the second child", function () {
756
- var child2 = given.tree.getNodeByNameMustExist("child2");
757
- expect(child2.getPreviousNode()).toMatchObject({
758
- name: "child1"
759
- });
760
- });
761
- it("returns the parent with a node that is the first child", function () {
762
- var node3 = given.tree.getNodeByNameMustExist("node3");
763
- expect(node3.getPreviousNode()).toMatchObject({
764
- name: "node2"
765
- });
766
- });
767
- });
768
- describe("getPreviousVisibleNode", function () {
769
- var given = (0, _givens["default"])();
770
- given("tree", function () {
771
- return new _node.Node().loadFromData(_exampleData["default"]);
772
- });
773
- it("returns null with a tree node", function () {
774
- expect(given.tree.getPreviousVisibleNode()).toBeNull();
775
- });
776
- it("returns the previous sibling when the previous sibling is closed and has children", function () {
777
- given.tree.getNodeByNameMustExist("node2").is_open = false;
778
- var node2 = given.tree.getNodeByNameMustExist("node2");
779
- expect(node2.getPreviousVisibleNode()).toMatchObject({
780
- name: "node1"
781
- });
782
- });
783
- it("returns the last child of the previous sibling when the previous sibling is open and has children", function () {
784
- given.tree.getNodeByNameMustExist("node1").is_open = true;
785
- var node2 = given.tree.getNodeByNameMustExist("node2");
786
- expect(node2.getPreviousVisibleNode()).toMatchObject({
787
- name: "child2"
788
- });
789
- });
790
- it("returns the first child if a node is the second child", function () {
791
- var child2 = given.tree.getNodeByNameMustExist("child2");
792
- expect(child2.getPreviousVisibleNode()).toMatchObject({
793
- name: "child1"
794
- });
795
- });
796
- it("returns the parent when a node is the first child", function () {
797
- var node3 = given.tree.getNodeByNameMustExist("node3");
798
- expect(node3.getPreviousVisibleNode()).toMatchObject({
799
- name: "node2"
800
- });
801
- });
802
- });
803
- describe("getPreviousSibling", function () {
804
- var given = (0, _givens["default"])();
805
- given("node1", function () {
806
- return given.tree.getNodeByNameMustExist("node1");
807
- });
808
- given("node2", function () {
809
- return given.tree.getNodeByNameMustExist("node2");
810
- });
811
- given("tree", function () {
812
- return new _node.Node().loadFromData(_exampleData["default"]);
813
- });
814
- context("with a tree", function () {
815
- it("returns null", function () {
816
- expect(given.tree.getPreviousSibling()).toBeNull();
817
- });
818
- });
819
- context("when the node is the first child", function () {
820
- it("returns null", function () {
821
- expect(given.node1.getPreviousSibling()).toBeNull();
822
- });
823
- });
824
- context("when the node is the second child", function () {
825
- it("returns the first child", function () {
826
- expect(given.node2.getPreviousSibling()).toBe(given.node1);
827
- });
828
- });
829
- });
830
- describe("hasChildren", function () {
831
- var given = (0, _givens["default"])();
832
- given("node", function () {
833
- return new _node.Node();
834
- });
835
- context("when a node has children", function () {
836
- beforeEach(function () {
837
- given.node.addChild(new _node.Node("child1"));
838
- });
839
- it("returns true", function () {
840
- expect(given.node.hasChildren()).toBe(true);
841
- });
842
- });
843
- context("when a node doesn't have children", function () {
844
- it("returns false", function () {
845
- expect(given.node.hasChildren()).toBe(false);
846
- });
847
- });
848
- });
849
- describe("initFromData", function () {
850
- var given = (0, _givens["default"])();
851
- given("tree", function () {
852
- return new _node.Node();
853
- });
854
- beforeEach(function () {
855
- given.tree.initFromData({
856
- name: "node1",
857
- id: 1,
858
- children: [{
859
- name: "child1",
860
- id: 2
861
- }, {
862
- name: "child2",
863
- id: 3
864
- }]
865
- });
866
- });
867
- it("loads the data", function () {
868
- expect(given.tree).toMatchObject({
869
- id: 1,
870
- name: "node1",
871
- children: [expect.objectContaining({
872
- id: 2,
873
- name: "child1"
874
- }), expect.objectContaining({
875
- id: 3,
876
- name: "child2"
877
- })]
878
- });
879
- });
880
- });
881
- describe("isFolder", function () {
882
- var given = (0, _givens["default"])();
883
- given("node", function () {
884
- return new _node.Node();
885
- });
886
- context("when the node has no children", function () {
887
- it("returns false", function () {
888
- expect(given.node.isFolder()).toBe(false);
889
- });
890
- context("when the node is loaded on demand", function () {
891
- beforeEach(function () {
892
- given.node.load_on_demand = true;
893
- });
894
- it("returns false", function () {
895
- expect(given.node.isFolder()).toBe(true);
896
- });
897
- });
898
- });
899
- context("when the node has a child", function () {
900
- beforeEach(function () {
901
- given.node.append("child1");
902
- });
903
- it("returns false", function () {
904
- expect(given.node.isFolder()).toBe(true);
905
- });
906
- });
907
- });
908
- describe("iterate", function () {
909
- var given = (0, _givens["default"])();
910
- given("tree", function () {
911
- return new _node.Node().loadFromData(_exampleData["default"]);
912
- });
913
- given("visited", function () {
914
- return [];
915
- });
916
- beforeEach(function () {
917
- given.tree.iterate(given.visitor);
918
- });
919
- context("when the visitor function returns true", function () {
920
- var visitAllNodes = function visitAllNodes(node, level) {
921
- given.visited.push([node.name, level]);
922
- return true;
923
- };
924
- given("visitor", function () {
925
- return visitAllNodes;
926
- });
927
- it("visits all nodes", function () {
928
- expect(given.visited).toEqual([["node1", 0], ["child1", 1], ["child2", 1], ["node2", 0], ["node3", 1], ["child3", 2]]);
929
- });
930
- });
931
- context("when the visitor function returns false", function () {
932
- var visitNodes = function visitNodes(node, level) {
933
- given.visited.push([node.name, level]);
934
- return false;
935
- };
936
- given("visitor", function () {
937
- return visitNodes;
938
- });
939
- it("stops the iteration for the current node", function () {
940
- expect(given.visited).toEqual([["node1", 0], ["node2", 0]]);
941
- });
942
- });
943
- });
944
- describe("loadFromData", function () {
945
- var given = (0, _givens["default"])();
946
- given("tree", function () {
947
- return new _node.Node().loadFromData(_exampleData["default"]);
948
- });
949
- it("creates a tree", function () {
950
- expect(given.tree.children).toEqual([expect.objectContaining({
951
- id: 123,
952
- intProperty: 1,
953
- name: "node1",
954
- strProperty: "1",
955
- children: [expect.objectContaining({
956
- id: 125,
957
- name: "child1"
958
- }), expect.objectContaining({
959
- id: 126,
960
- name: "child2"
961
- })]
962
- }), expect.objectContaining({
963
- id: 124,
964
- intProperty: 3,
965
- name: "node2",
966
- strProperty: "3",
967
- children: [expect.objectContaining({
968
- id: 127,
969
- name: "node3"
970
- })]
971
- })]);
972
- });
973
- });
974
- describe("moveNode", function () {
975
- var given = (0, _givens["default"])();
976
- given("child1", function () {
977
- return given.tree.getNodeByNameMustExist("child1");
978
- });
979
- given("child2", function () {
980
- return given.tree.getNodeByNameMustExist("child2");
981
- });
982
- given("node1", function () {
983
- return given.tree.getNodeByNameMustExist("node1");
984
- });
985
- given("node2", function () {
986
- return given.tree.getNodeByNameMustExist("node2");
987
- });
988
- given("tree", function () {
989
- return new _node.Node().loadFromData(_exampleData["default"]);
990
- });
991
- context("when moving after a node", function () {
992
- it("moves the node", function () {
993
- expect(given.tree.moveNode(given.child2, given.node2, _node.Position.After)).toBe(true);
994
- expect(given.tree).toMatchObject({
995
- name: "",
996
- children: [expect.objectContaining({
997
- name: "node1",
998
- children: [expect.objectContaining({
999
- name: "child1"
1000
- })]
1001
- }), expect.objectContaining({
1002
- name: "node2"
1003
- }), expect.objectContaining({
1004
- name: "child2"
1005
- })]
1006
- });
1007
- });
1008
- context("when the target is the root node", function () {
1009
- it("returns false", function () {
1010
- expect(given.tree.moveNode(given.child2, given.tree, _node.Position.After)).toBe(false);
1011
- });
1012
- });
1013
- });
1014
- context("when moving inside a node", function () {
1015
- it("moves the node", function () {
1016
- expect(given.tree.moveNode(given.child1, given.node2, _node.Position.Inside)).toBe(true);
1017
- expect(given.tree).toMatchObject({
1018
- name: "",
1019
- children: [expect.objectContaining({
1020
- name: "node1",
1021
- children: [expect.objectContaining({
1022
- name: "child2"
1023
- })]
1024
- }), expect.objectContaining({
1025
- name: "node2",
1026
- children: [expect.objectContaining({
1027
- name: "child1"
1028
- }), expect.objectContaining({
1029
- name: "node3"
1030
- })]
1031
- })]
1032
- });
1033
- });
1034
- });
1035
- context("when moving before a node", function () {
1036
- it("moves the node", function () {
1037
- expect(given.tree.moveNode(given.child2, given.child1, _node.Position.Before)).toBe(true);
1038
- expect(given.tree).toMatchObject({
1039
- name: "",
1040
- children: [expect.objectContaining({
1041
- name: "node1",
1042
- children: [expect.objectContaining({
1043
- name: "child2"
1044
- }), expect.objectContaining({
1045
- name: "child1"
1046
- })]
1047
- }), expect.objectContaining({
1048
- name: "node2"
1049
- })]
1050
- });
1051
- });
1052
- context("when the target is the root node", function () {
1053
- it("returns false", function () {
1054
- expect(given.tree.moveNode(given.child2, given.tree, _node.Position.Before)).toBe(false);
1055
- });
1056
- });
1057
- });
1058
- context("when the moved node is a parent of the target node", function () {
1059
- it("doesn't move the node", function () {
1060
- expect(given.tree.moveNode(given.node1, given.child1, _node.Position.Before)).toBe(false);
1061
- expect(given.tree).toMatchObject({
1062
- children: [expect.objectContaining({
1063
- name: "node1",
1064
- children: [expect.objectContaining({
1065
- name: "child1"
1066
- }), expect.objectContaining({
1067
- name: "child2"
1068
- })]
1069
- }), expect.objectContaining({
1070
- name: "node2"
1071
- })]
1072
- });
1073
- });
1074
- });
1075
- context("with position None", function () {
1076
- it("returns false", function () {
1077
- expect(given.tree.moveNode(given.child2, given.node2, _node.Position.None)).toBe(false);
1078
- });
1079
- });
1080
- });
1081
- describe("prepend", function () {
1082
- var given = (0, _givens["default"])();
1083
- given("node", function () {
1084
- return new _node.Node("node1");
1085
- });
1086
- it("prepends a node", function () {
1087
- given.node.prepend("child2");
1088
- given.node.prepend("child1");
1089
- expect(given.node).toMatchObject({
1090
- name: "node1",
1091
- children: [expect.objectContaining({
1092
- name: "child1"
1093
- }), expect.objectContaining({
1094
- name: "child2"
1095
- })]
1096
- });
1097
- });
1098
- context("when prepending a node with children", function () {
1099
- it("adds the children", function () {
1100
- given.node.prepend({
1101
- name: "new node",
1102
- children: ["newchild1", "newchild2"]
1103
- });
1104
- expect(given.node).toMatchObject({
1105
- children: [expect.objectContaining({
1106
- name: "new node",
1107
- children: [expect.objectContaining({
1108
- name: "newchild1"
1109
- }), expect.objectContaining({
1110
- name: "newchild2"
1111
- })]
1112
- })]
1113
- });
1114
- });
1115
- });
1116
- });
1117
- describe("remove", function () {
1118
- var given = (0, _givens["default"])();
1119
- given("child1", function () {
1120
- return given.tree.getNodeByNameMustExist("child1");
1121
- });
1122
- given("tree", function () {
1123
- return new _node.Node().loadFromData(_exampleData["default"]);
1124
- });
1125
- beforeEach(function () {
1126
- given.child1.remove();
1127
- });
1128
- it("removes the node", function () {
1129
- expect(given.tree).toMatchObject({
1130
- children: [expect.objectContaining({
1131
- name: "node1",
1132
- children: [expect.objectContaining({
1133
- name: "child2"
1134
- })]
1135
- }), expect.objectContaining({
1136
- name: "node2"
1137
- })]
1138
- });
1139
- });
1140
- });
1141
- describe("removeChild", function () {
1142
- var given = (0, _givens["default"])();
1143
- given("child1", function () {
1144
- return given.tree.getNodeByNameMustExist("child1");
1145
- });
1146
- given("node1", function () {
1147
- return given.tree.getNodeByNameMustExist("node1");
1148
- });
1149
- given("tree", function () {
1150
- return new _node.Node().loadFromData(_exampleData["default"]);
1151
- });
1152
- beforeEach(function () {
1153
- given.node1.removeChild(given.child1);
1154
- });
1155
- it("removes the child", function () {
1156
- expect(given.node1.children).toEqual([expect.objectContaining({
1157
- name: "child2"
1158
- })]);
1159
- });
1160
- });