|
1 | 1 | import { expect } from "chai";
|
2 |
| -import { arrayToTree } from "./arrayToTree"; |
| 2 | +import { arrayToTree, countNodes } from "./arrayToTree"; |
3 | 3 |
|
4 | 4 | describe("arrayToTree", () => {
|
5 | 5 | it("should work with nested objects", () => {
|
@@ -28,6 +28,68 @@ describe("arrayToTree", () => {
|
28 | 28 | ]);
|
29 | 29 | });
|
30 | 30 |
|
| 31 | + it("should work with nested objects if throwIfOrphans is set to true", () => { |
| 32 | + expect( |
| 33 | + arrayToTree([ |
| 34 | + { id: "4", parentId: null, custom: "abc" }, |
| 35 | + { id: "31", parentId: "4", custom: "12" }, |
| 36 | + { id: "1941", parentId: "418", custom: "de" }, |
| 37 | + { id: "1", parentId: "418", custom: "ZZZz" }, |
| 38 | + { id: "418", parentId: null, custom: "ü" }, |
| 39 | + ], { throwIfOrphans: true }) |
| 40 | + ).to.deep.equal([ |
| 41 | + { |
| 42 | + data: { id: "4", parentId: null, custom: "abc" }, |
| 43 | + children: [ |
| 44 | + { data: { id: "31", parentId: "4", custom: "12" }, children: [] }, |
| 45 | + ], |
| 46 | + }, |
| 47 | + { |
| 48 | + data: { id: "418", parentId: null, custom: "ü" }, |
| 49 | + children: [ |
| 50 | + { data: { id: "1941", parentId: "418", custom: "de" }, children: [] }, |
| 51 | + { data: { id: "1", parentId: "418", custom: "ZZZz" }, children: [] }, |
| 52 | + ], |
| 53 | + }, |
| 54 | + ]); |
| 55 | + }); |
| 56 | + |
| 57 | + it("should ignore circular parent child relations", () => { |
| 58 | + expect( |
| 59 | + arrayToTree([ |
| 60 | + { id: "4", parentId: "31", custom: "abc" }, |
| 61 | + { id: "31", parentId: "4", custom: "12" }, |
| 62 | + ]) |
| 63 | + ).to.deep.equal([ |
| 64 | + ]); |
| 65 | + |
| 66 | + expect( |
| 67 | + arrayToTree([ |
| 68 | + { id: "4", parentId: "31", custom: "abc" }, |
| 69 | + { id: "31", parentId: "5", custom: "12" }, |
| 70 | + { id: "5", parentId: "4", custom: "12" }, |
| 71 | + ]) |
| 72 | + ).to.deep.equal([ |
| 73 | + ]); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should throw if throwIfOrphans is enabled and circular parent child relations are encountered, see #37", () => { |
| 77 | + expect( |
| 78 | + () => arrayToTree([ |
| 79 | + { id: "4", parentId: "31", custom: "abc" }, |
| 80 | + { id: "31", parentId: "4", custom: "12" }, |
| 81 | + ], { throwIfOrphans: true }) |
| 82 | + ).to.throw('The items array contains nodes with a circular parent/child relationship.'); |
| 83 | + |
| 84 | + expect( |
| 85 | + () => arrayToTree([ |
| 86 | + { id: "4", parentId: "31", custom: "abc" }, |
| 87 | + { id: "31", parentId: "5", custom: "12" }, |
| 88 | + { id: "5", parentId: "4", custom: "12" }, |
| 89 | + ], { throwIfOrphans: true }) |
| 90 | + ).to.throw('The items array contains nodes with a circular parent/child relationship.'); |
| 91 | + }); |
| 92 | + |
31 | 93 | it("should work with integer keys", () => {
|
32 | 94 | expect(
|
33 | 95 | arrayToTree([
|
@@ -622,3 +684,32 @@ describe("arrayToTree", () => {
|
622 | 684 | ]);
|
623 | 685 | });
|
624 | 686 | });
|
| 687 | + |
| 688 | +describe("countNodes", () => { |
| 689 | + it("should work with nested objects", () => { |
| 690 | + expect( |
| 691 | + countNodes(arrayToTree([ |
| 692 | + { id: "4", parentId: null, custom: "abc" }, |
| 693 | + { id: "31", parentId: "4", custom: "12" }, |
| 694 | + { id: "1941", parentId: "418", custom: "de" }, |
| 695 | + { id: "1", parentId: "418", custom: "ZZZz" }, |
| 696 | + { id: "418", parentId: null, custom: "ü" }, |
| 697 | + ]), 'children') |
| 698 | + ).to.equal(5); |
| 699 | + }); |
| 700 | + |
| 701 | + it("should work for 1 node", () => { |
| 702 | + expect( |
| 703 | + countNodes(arrayToTree([ |
| 704 | + { id: "4", parentId: null, custom: "abc" }, |
| 705 | + ]), 'children') |
| 706 | + ).to.equal(1); |
| 707 | + }); |
| 708 | + |
| 709 | + it("should work for 0 nodes", () => { |
| 710 | + expect( |
| 711 | + countNodes(arrayToTree([ |
| 712 | + ]), 'children') |
| 713 | + ).to.equal(0); |
| 714 | + }); |
| 715 | +}); |
0 commit comments