|
2 | 2 | getVisibleNodeCount, |
3 | 3 | getVisibleNodeInfoAtIndex, |
4 | 4 | changeNodeAtPath, |
| 5 | + getVisibleNodeInfoFlattened, |
5 | 6 | } from './tree-data-utils'; |
6 | 7 |
|
7 | 8 | const keyFromTreeIndex = (node, treeIndex) => treeIndex; |
@@ -213,6 +214,125 @@ describe('getVisibleNodeInfoAtIndex', () => { |
213 | 214 | }); |
214 | 215 | }); |
215 | 216 |
|
| 217 | + |
| 218 | +describe('getVisibleNodeInfoAtIndex', () => { |
| 219 | + it('should handle empty data', () => { |
| 220 | + expect(getVisibleNodeInfoFlattened([], keyFromTreeIndex)).toEqual([]); |
| 221 | + expect(getVisibleNodeInfoFlattened(null, keyFromTreeIndex)).toEqual([]); |
| 222 | + expect(getVisibleNodeInfoFlattened(undefined, keyFromTreeIndex)).toEqual([]); |
| 223 | + }); |
| 224 | + |
| 225 | + it('should handle flat data', () => { |
| 226 | + expect(getVisibleNodeInfoFlattened([ { key: 0 } ], keyFromTreeIndex)).toEqual([ |
| 227 | + { node: { key: 0 }, parentPath: [], lowerSiblingCounts: [ 0 ] }, |
| 228 | + ]); |
| 229 | + expect(getVisibleNodeInfoFlattened([ { key: 0 }, { key: 1 } ], keyFromTreeIndex)).toEqual([ |
| 230 | + { node: { key: 0 }, parentPath: [], lowerSiblingCounts: [ 1 ] }, |
| 231 | + { node: { key: 1 }, parentPath: [], lowerSiblingCounts: [ 0 ] } |
| 232 | + ]); |
| 233 | + }); |
| 234 | + |
| 235 | + it('should handle hidden nested data', () => { |
| 236 | + const treeData = [ |
| 237 | + { |
| 238 | + key: 0, |
| 239 | + children: [ |
| 240 | + { |
| 241 | + key: 1, |
| 242 | + children: [ |
| 243 | + { key: 2 }, |
| 244 | + { key: 3 }, |
| 245 | + ], |
| 246 | + }, |
| 247 | + { |
| 248 | + key: 4, |
| 249 | + children: [ |
| 250 | + { key: 5 }, |
| 251 | + ], |
| 252 | + }, |
| 253 | + ], |
| 254 | + }, |
| 255 | + { key: 6 }, |
| 256 | + ]; |
| 257 | + |
| 258 | + expect(getVisibleNodeInfoFlattened(treeData, keyFromTreeIndex)).toEqual([ |
| 259 | + { node: treeData[0], parentPath: [], lowerSiblingCounts: [ 1 ] }, |
| 260 | + { node: treeData[1], parentPath: [], lowerSiblingCounts: [ 0 ] } |
| 261 | + ]); |
| 262 | + }); |
| 263 | + |
| 264 | + it('should handle partially expanded nested data', () => { |
| 265 | + const treeData = [ |
| 266 | + { |
| 267 | + expanded: true, |
| 268 | + key: 0, |
| 269 | + children: [ |
| 270 | + { |
| 271 | + key: 1, |
| 272 | + children: [ |
| 273 | + { key: 2 }, |
| 274 | + { key: 3 }, |
| 275 | + ], |
| 276 | + }, |
| 277 | + { |
| 278 | + expanded: true, |
| 279 | + key: 4, |
| 280 | + children: [ |
| 281 | + { key: 5 }, |
| 282 | + ], |
| 283 | + }, |
| 284 | + ], |
| 285 | + }, |
| 286 | + { key: 6 }, |
| 287 | + ]; |
| 288 | + |
| 289 | + expect(getVisibleNodeInfoFlattened(treeData, keyFromKey)).toEqual([ |
| 290 | + { node: treeData[0], parentPath: [], lowerSiblingCounts: [ 1 ] }, |
| 291 | + { node: treeData[0].children[0], parentPath: [0], lowerSiblingCounts: [ 1, 1 ] }, |
| 292 | + { node: treeData[0].children[1], parentPath: [0], lowerSiblingCounts: [ 1, 0 ] }, |
| 293 | + { node: treeData[0].children[1].children[0], parentPath: [0, 4], lowerSiblingCounts: [ 1, 0, 0 ] }, |
| 294 | + { node: treeData[1], parentPath: [], lowerSiblingCounts: [ 0 ] }, |
| 295 | + ]); |
| 296 | + }); |
| 297 | + |
| 298 | + it('should handle fully expanded nested data', () => { |
| 299 | + const treeData = [ |
| 300 | + { |
| 301 | + expanded: true, |
| 302 | + key: 0, |
| 303 | + children: [ |
| 304 | + { |
| 305 | + expanded: true, |
| 306 | + key: 1, |
| 307 | + children: [ |
| 308 | + { key: 2 }, |
| 309 | + { key: 3 }, |
| 310 | + ], |
| 311 | + }, |
| 312 | + { |
| 313 | + expanded: true, |
| 314 | + key: 4, |
| 315 | + children: [ |
| 316 | + { key: 5 }, |
| 317 | + ], |
| 318 | + }, |
| 319 | + ], |
| 320 | + }, |
| 321 | + { key: 6 }, |
| 322 | + ]; |
| 323 | + |
| 324 | + expect(getVisibleNodeInfoFlattened(treeData, keyFromTreeIndex)).toEqual([ |
| 325 | + { node: treeData[0], parentPath: [], lowerSiblingCounts: [1] }, |
| 326 | + { node: treeData[0].children[0], parentPath: [0], lowerSiblingCounts: [1, 1] }, |
| 327 | + { node: treeData[0].children[0].children[0], parentPath: [0, 1], lowerSiblingCounts: [1, 1, 1] }, |
| 328 | + { node: treeData[0].children[0].children[1], parentPath: [0, 1], lowerSiblingCounts: [1, 1, 0] }, |
| 329 | + { node: treeData[0].children[1], parentPath: [0], lowerSiblingCounts: [1, 0] }, |
| 330 | + { node: treeData[0].children[1].children[0], parentPath: [0, 4], lowerSiblingCounts: [1, 0, 0] }, |
| 331 | + { node: treeData[1], parentPath: [], lowerSiblingCounts: [0] }, |
| 332 | + ]); |
| 333 | + }); |
| 334 | +}); |
| 335 | + |
216 | 336 | describe('changeNodeAtPath', () => { |
217 | 337 | it('should handle empty data', () => { |
218 | 338 | expect(() => changeNodeAtPath([], [1], {}, keyFromTreeIndex)).toThrow(); |
|
0 commit comments