|
| 1 | +import {FlatTreeControl} from './flat-tree-control'; |
| 2 | + |
| 3 | +describe('CdkFlatTreeControl', () => { |
| 4 | + let treeControl: FlatTreeControl<TestData>; |
| 5 | + let getLevel = (node: TestData) => node.level; |
| 6 | + let isExpandable = (node: TestData) => node.children && node.children.length > 0; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + treeControl = new FlatTreeControl<TestData>(getLevel, isExpandable); |
| 10 | + }); |
| 11 | + |
| 12 | + describe('base tree control actions', () => { |
| 13 | + it('should be able to expand and collapse dataNodes', () => { |
| 14 | + const nodes = generateData(10, 4); |
| 15 | + const secondNode = nodes[1]; |
| 16 | + const sixthNode = nodes[5]; |
| 17 | + treeControl.dataNodes = nodes; |
| 18 | + |
| 19 | + treeControl.expand(secondNode); |
| 20 | + |
| 21 | + |
| 22 | + expect(treeControl.isExpanded(secondNode)) |
| 23 | + .toBeTruthy('Expect second node to be expanded'); |
| 24 | + expect(treeControl.expansionModel.selected) |
| 25 | + .toContain(secondNode, 'Expect second node in expansionModel'); |
| 26 | + expect(treeControl.expansionModel.selected.length) |
| 27 | + .toBe(1, 'Expect only second node in expansionModel'); |
| 28 | + |
| 29 | + treeControl.toggle(sixthNode); |
| 30 | + |
| 31 | + expect(treeControl.isExpanded(secondNode)) |
| 32 | + .toBeTruthy('Expect second node to stay expanded'); |
| 33 | + expect(treeControl.isExpanded(sixthNode)) |
| 34 | + .toBeTruthy('Expect sixth node to be expanded'); |
| 35 | + expect(treeControl.expansionModel.selected) |
| 36 | + .toContain(sixthNode, 'Expect sixth node in expansionModel'); |
| 37 | + expect(treeControl.expansionModel.selected) |
| 38 | + .toContain(secondNode, 'Expect second node in expansionModel'); |
| 39 | + expect(treeControl.expansionModel.selected.length) |
| 40 | + .toBe(2, 'Expect two dataNodes in expansionModel'); |
| 41 | + |
| 42 | + treeControl.collapse(seconNode); |
| 43 | + |
| 44 | + expect(treeControl.isExpanded(secondNode)) |
| 45 | + .toBeFalsy('Expect second node to be collapsed'); |
| 46 | + expect(treeControl.expansionModel.selected.length) |
| 47 | + .toBe(1, 'Expect one node in expansionModel'); |
| 48 | + expect(treeControl.isExpanded(sixthNode)).toBeTruthy('Expect sixth node to stay expanded'); |
| 49 | + expect(treeControl.expansionModel.selected) |
| 50 | + .toContain(sixthNode, 'Expect sixth node in expansionModel'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should return correct expandable values', () => { |
| 54 | + const nodes = generateData(10, 4); |
| 55 | + treeControl.dataNodes = nodes; |
| 56 | + |
| 57 | + for (let i = 0; i < 10; i++) { |
| 58 | + expect(treeControl.isExpandable(nodes[i])) |
| 59 | + .toBeTruthy(`Expect node[${i}] to be expandable`); |
| 60 | + |
| 61 | + for (let j = 0; j < 4; j++) { |
| 62 | + expect(treeControl.isExpandable(nodes[i].children[j])) |
| 63 | + .toBeFalsy(`Expect node[${i}]'s child[${j}] to be not expandable`); |
| 64 | + } |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + it('should return correct levels', () => { |
| 69 | + const numNodes = 10; |
| 70 | + const numChildren = 4; |
| 71 | + const numGrandChildren = 2; |
| 72 | + const nodes = generateData(numNodes, numChildren, numGrandChildren); |
| 73 | + treeControl.dataNodes = nodes; |
| 74 | + |
| 75 | + for (let i = 0; i < numNodes; i++) { |
| 76 | + expect(treeControl.getLevel(nodes[i])) |
| 77 | + .toBe(1, `Expec node[${i}]'s level to be 1`); |
| 78 | + |
| 79 | + for (let j = 0; j < numChildren; j++) { |
| 80 | + expect(treeControl.getLevel(nodes[i].children[j])) |
| 81 | + .toBe(2, `Expect node[${i}]'s child[${j}] to be not expandable`); |
| 82 | + |
| 83 | + for (let k = 0; k < numGrandChildren; k++) { |
| 84 | + expect(treeControl.getLevel(nodes[i].children[j].children[k])) |
| 85 | + .toBe(3, `Expect node[${i}]'s child[${j}] to be not expandable`); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + it('should toggle descendants correctly', () => { |
| 92 | + const numNodes = 10; |
| 93 | + const numChildren = 4; |
| 94 | + const numGrandChildren = 2; |
| 95 | + const nodes = generateData(numNodes, numChildren, numGrandChildren); |
| 96 | + |
| 97 | + let data = []; |
| 98 | + flatten(nodes, data); |
| 99 | + treeControl.dataNodes = data; |
| 100 | + |
| 101 | + treeControl.expandDescendants(nodes[1]); |
| 102 | + |
| 103 | + const expandedNodesNum = 1 + numChildren + numChildren * numGrandChildren; |
| 104 | + expect(treeControl.expansionModel.selected.length) |
| 105 | + .toBe(expandedNodesNum, `Expect expanded ${expandedNodesNum} nodes`); |
| 106 | + |
| 107 | + expect(treeControl.isExpanded(nodes[1])).toBeTruthy('Expect second node to be expanded'); |
| 108 | + for (let i = 0; i < numChildren; i++) { |
| 109 | + |
| 110 | + expect(treeControl.isExpanded(nodes[1].children[i])) |
| 111 | + .toBeTruthy(`Expect second node's children to be expanded`); |
| 112 | + for (let j = 0; j < numGrandChildren; j++) { |
| 113 | + expect(treeControl.isExpanded(nodes[1].children[i].children[j])) |
| 114 | + .toBeTruthy(`Expect second node grand children to be not expanded`); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + }); |
| 119 | + |
| 120 | + it('should be able to expand/collapse all the dataNodes', () => { |
| 121 | + const numNodes = 10; |
| 122 | + const numChildren = 4; |
| 123 | + const numGrandChildren = 2; |
| 124 | + const nodes = generateData(numNodes, numChildren, numGrandChildren); |
| 125 | + let data = []; |
| 126 | + flatten(nodes, data); |
| 127 | + treeControl.dataNodes = data; |
| 128 | + |
| 129 | + treeControl.expandDescendants(nodes[1]); |
| 130 | + |
| 131 | + treeControl.collapseAll(); |
| 132 | + |
| 133 | + expect(treeControl.expansionModel.selected.length).toBe(0, `Expect no expanded nodes`); |
| 134 | + |
| 135 | + treeControl.expandAll(); |
| 136 | + |
| 137 | + const totalNumber = numNodes + numNodes * numChildren |
| 138 | + + numNodes * numChildren * numGrandChildren; |
| 139 | + expect(treeControl.expansionModel.selected.length) |
| 140 | + .toBe(totalNumber, `Expect ${totalNumber} expanded nodes`); |
| 141 | + }); |
| 142 | + }); |
| 143 | +}); |
| 144 | + |
| 145 | +export class TestData { |
| 146 | + a: string; |
| 147 | + b: string; |
| 148 | + c: string; |
| 149 | + level: number; |
| 150 | + children: TestData[]; |
| 151 | + |
| 152 | + constructor(a: string, b: string, c: string, level: number = 1, children: TestData[] = []) { |
| 153 | + this.a = a; |
| 154 | + this.b = b; |
| 155 | + this.c = c; |
| 156 | + this.level = level; |
| 157 | + this.children = children; |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +function generateData(dataLength: number, childLength: number, grandChildLength: number = 0) |
| 162 | + : TestData[] { |
| 163 | + let data = <any>[]; |
| 164 | + let nextIndex = 0; |
| 165 | + for (let i = 0; i < dataLength; i++) { |
| 166 | + let children = <any>[]; |
| 167 | + for (let j = 0; j < childLength; j++) { |
| 168 | + let grandChildren = <any>[]; |
| 169 | + for (let k = 0; k < grandChildLength; k++) { |
| 170 | + grandChildren.push(new TestData(`a_${nextIndex}`, `b_${nextIndex}`, `c_${nextIndex++}`, 3)); |
| 171 | + } |
| 172 | + children.push( |
| 173 | + new TestData(`a_${nextIndex}`, `b_${nextIndex}`, `c_${nextIndex++}`, 2, grandChildren)); |
| 174 | + } |
| 175 | + data.push(new TestData(`a_${nextIndex}`, `b_${nextIndex}`, `c_${nextIndex++}`, 1, children)); |
| 176 | + } |
| 177 | + return data; |
| 178 | +} |
| 179 | + |
| 180 | +function flatten(nodes: TestData[], data: TestData[]) { |
| 181 | + for (let node of nodes) { |
| 182 | + data.push(node); |
| 183 | + |
| 184 | + if (node.children && node.children.length > 0) { |
| 185 | + flatten(node.children, data); |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments