|
| 1 | +import 'reflect-metadata'; |
| 2 | +import { TreeViewUtil } from '../../helper'; |
| 3 | +// import { container } from 'tsyringe'; |
| 4 | +// import { IExplorerService, ExplorerService } from '../explorerService'; |
| 5 | +describe('Test panelService', () => { |
| 6 | + // TODO: error: Attempted to construct an undefined constructor. Could mean a circular dependency problem. Try using `delay` function... |
| 7 | + // const explorerService = container.resolve<IExplorerService>(ExplorerService); |
| 8 | + // console.log('explorerService', explorerService) |
| 9 | +}); |
| 10 | + |
| 11 | +describe('Test TreeViewUtil Class', () => { |
| 12 | + const createTree = () => { |
| 13 | + return new TreeViewUtil({ |
| 14 | + id: 1, |
| 15 | + module: 'root', |
| 16 | + children: [ |
| 17 | + { |
| 18 | + id: 2, |
| 19 | + module: 'a', |
| 20 | + children: [{ id: 3, module: 'c' }], |
| 21 | + }, |
| 22 | + { |
| 23 | + id: 4, |
| 24 | + module: 'b', |
| 25 | + }, |
| 26 | + ], |
| 27 | + }); |
| 28 | + }; |
| 29 | + |
| 30 | + test('empty Tree', () => { |
| 31 | + const tree = new TreeViewUtil(); |
| 32 | + expect(tree.obj).toEqual({ children: [] }); |
| 33 | + }); |
| 34 | + |
| 35 | + test('custom TreeViewUtil childNodeName', () => { |
| 36 | + let tree = new TreeViewUtil(null, 'properties'); |
| 37 | + expect(tree.obj).toEqual({ properties: [] }); |
| 38 | + tree = new TreeViewUtil( |
| 39 | + { |
| 40 | + id: 10, |
| 41 | + propName: 'root', |
| 42 | + properties: [ |
| 43 | + { |
| 44 | + id: 11, |
| 45 | + propName: 'a', |
| 46 | + properties: [{ id: 12, propName: 'c' }], |
| 47 | + }, |
| 48 | + { |
| 49 | + id: 13, |
| 50 | + propName: 'b', |
| 51 | + }, |
| 52 | + ], |
| 53 | + }, |
| 54 | + 'properties' |
| 55 | + ); |
| 56 | + |
| 57 | + const { obj, indexes } = tree; |
| 58 | + expect(indexes['10']).toEqual({ |
| 59 | + id: 10, |
| 60 | + node: obj, |
| 61 | + properties: [11, 13], |
| 62 | + }); |
| 63 | + |
| 64 | + expect(indexes['11']).toEqual({ |
| 65 | + id: 11, |
| 66 | + parent: 10, |
| 67 | + properties: [12], |
| 68 | + node: obj.properties[0], |
| 69 | + next: 13, |
| 70 | + }); |
| 71 | + |
| 72 | + expect(indexes['12']).toEqual({ |
| 73 | + id: 12, |
| 74 | + parent: 11, |
| 75 | + node: obj.properties[0].properties[0], |
| 76 | + }); |
| 77 | + |
| 78 | + expect(indexes['13']).toEqual({ |
| 79 | + id: 13, |
| 80 | + parent: 10, |
| 81 | + node: obj.properties[1], |
| 82 | + prev: 11, |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + test('Test TreeViewUtil generate method', () => { |
| 87 | + const tree = createTree(); |
| 88 | + const { obj, indexes } = tree; |
| 89 | + |
| 90 | + expect(indexes['1']).toEqual({ |
| 91 | + id: 1, |
| 92 | + node: obj, |
| 93 | + children: [2, 4], |
| 94 | + }); |
| 95 | + |
| 96 | + expect(indexes['2']).toEqual({ |
| 97 | + id: 2, |
| 98 | + parent: 1, |
| 99 | + children: [3], |
| 100 | + node: obj.children[0], |
| 101 | + next: 4, |
| 102 | + }); |
| 103 | + |
| 104 | + expect(indexes['3']).toEqual({ |
| 105 | + id: 3, |
| 106 | + parent: 2, |
| 107 | + node: obj.children[0].children[0], |
| 108 | + }); |
| 109 | + |
| 110 | + expect(indexes['4']).toEqual({ |
| 111 | + id: 4, |
| 112 | + parent: 1, |
| 113 | + node: obj.children[1], |
| 114 | + prev: 2, |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + test('Test TreeViewUtil get method', () => { |
| 119 | + const tree = createTree(); |
| 120 | + const { obj } = tree; |
| 121 | + |
| 122 | + expect(tree.get(1)).toEqual(obj); |
| 123 | + expect(tree.get(100)).toBeNull(); |
| 124 | + }); |
| 125 | + |
| 126 | + test('Test TreeViewUtil remove method', () => { |
| 127 | + const tree = createTree(); |
| 128 | + const { obj } = tree; |
| 129 | + |
| 130 | + const node = tree.remove(2); |
| 131 | + |
| 132 | + expect(node).toEqual({ |
| 133 | + id: 2, |
| 134 | + module: 'a', |
| 135 | + children: [{ id: 3, module: 'c' }], |
| 136 | + }); |
| 137 | + expect(obj).toEqual({ |
| 138 | + id: 1, |
| 139 | + module: 'root', |
| 140 | + children: [{ id: 4, module: 'b' }], |
| 141 | + }); |
| 142 | + expect(tree.getIndex(2)).toBeUndefined(); |
| 143 | + expect(tree.getIndex(3)).toBeUndefined(); |
| 144 | + }); |
| 145 | + |
| 146 | + test('Test TreeViewUtil insert method', () => { |
| 147 | + const tree = createTree(); |
| 148 | + const { obj } = tree; |
| 149 | + |
| 150 | + tree.insert({ id: 5, module: 'd' }, 3, 0); |
| 151 | + |
| 152 | + expect(obj).toEqual({ |
| 153 | + id: 1, |
| 154 | + module: 'root', |
| 155 | + children: [ |
| 156 | + { |
| 157 | + id: 2, |
| 158 | + module: 'a', |
| 159 | + children: [ |
| 160 | + { |
| 161 | + id: 3, |
| 162 | + module: 'c', |
| 163 | + children: [{ id: 5, module: 'd' }], |
| 164 | + }, |
| 165 | + ], |
| 166 | + }, |
| 167 | + { id: 4, module: 'b' }, |
| 168 | + ], |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + test('Test TreeViewUtil insertBefore method', () => { |
| 173 | + const tree = createTree(); |
| 174 | + const { obj } = tree; |
| 175 | + tree.insertBefore({ id: 5, module: 'd' }, 3); |
| 176 | + |
| 177 | + expect(obj).toEqual({ |
| 178 | + id: 1, |
| 179 | + module: 'root', |
| 180 | + children: [ |
| 181 | + { |
| 182 | + id: 2, |
| 183 | + module: 'a', |
| 184 | + children: [ |
| 185 | + { id: 5, module: 'd' }, |
| 186 | + { id: 3, module: 'c' }, |
| 187 | + ], |
| 188 | + }, |
| 189 | + { id: 4, module: 'b' }, |
| 190 | + ], |
| 191 | + }); |
| 192 | + }); |
| 193 | + |
| 194 | + test('Test TreeViewUtil insertAfter method', () => { |
| 195 | + const tree = createTree(); |
| 196 | + const { obj } = tree; |
| 197 | + tree.insertAfter({ id: 5, module: 'd' }, 3); |
| 198 | + |
| 199 | + expect(obj).toEqual({ |
| 200 | + id: 1, |
| 201 | + module: 'root', |
| 202 | + children: [ |
| 203 | + { |
| 204 | + id: 2, |
| 205 | + module: 'a', |
| 206 | + children: [ |
| 207 | + { id: 3, module: 'c' }, |
| 208 | + { id: 5, module: 'd' }, |
| 209 | + ], |
| 210 | + }, |
| 211 | + { id: 4, module: 'b' }, |
| 212 | + ], |
| 213 | + }); |
| 214 | + }); |
| 215 | + |
| 216 | + test('Test TreeViewUtil prepend method', () => { |
| 217 | + const tree = createTree(); |
| 218 | + const { obj } = tree; |
| 219 | + tree.prepend({ id: 5, module: 'd' }, 1); |
| 220 | + |
| 221 | + expect(obj).toEqual({ |
| 222 | + id: 1, |
| 223 | + module: 'root', |
| 224 | + children: [ |
| 225 | + { |
| 226 | + id: 5, |
| 227 | + module: 'd', |
| 228 | + }, |
| 229 | + { |
| 230 | + id: 2, |
| 231 | + module: 'a', |
| 232 | + children: [{ id: 3, module: 'c' }], |
| 233 | + }, |
| 234 | + { id: 4, module: 'b' }, |
| 235 | + ], |
| 236 | + }); |
| 237 | + }); |
| 238 | + |
| 239 | + test('Test TreeViewUtil append method', () => { |
| 240 | + const tree = createTree(); |
| 241 | + const { obj } = tree; |
| 242 | + tree.append({ id: 5, module: 'd' }, 1); |
| 243 | + |
| 244 | + expect(obj).toEqual({ |
| 245 | + id: 1, |
| 246 | + module: 'root', |
| 247 | + children: [ |
| 248 | + { |
| 249 | + id: 2, |
| 250 | + module: 'a', |
| 251 | + children: [{ id: 3, module: 'c' }], |
| 252 | + }, |
| 253 | + { id: 4, module: 'b' }, |
| 254 | + { |
| 255 | + id: 5, |
| 256 | + module: 'd', |
| 257 | + }, |
| 258 | + ], |
| 259 | + }); |
| 260 | + }); |
| 261 | +}); |
0 commit comments