VirtualizedTree.spec.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import {VirtualizedTree} from 'sentry/utils/profiling/hooks/useVirtualizedTree/VirtualizedTree';
  2. import {VirtualizedTreeNode} from 'sentry/utils/profiling/hooks/useVirtualizedTree/VirtualizedTreeNode';
  3. const n = d => {
  4. return {...d, children: []};
  5. };
  6. function toFlattenedList(tree: VirtualizedTree<any>): VirtualizedTreeNode<any>[] {
  7. const list: VirtualizedTreeNode<any>[] = [];
  8. function visit(node: VirtualizedTreeNode<any>): void {
  9. list.push(node);
  10. for (let i = 0; i < node.children.length; i++) {
  11. visit(node.children[i]);
  12. }
  13. }
  14. for (let i = 0; i < tree.roots.length; i++) {
  15. visit(tree.roots[i]);
  16. }
  17. return list;
  18. }
  19. describe('VirtualizedTree', () => {
  20. describe('fromRoots', () => {
  21. it('build tree from roots', () => {
  22. const root = n({id: 'root'});
  23. const child1 = n({id: 'child1'});
  24. root.children = [child1];
  25. const tree = VirtualizedTree.fromRoots([root]);
  26. tree.expandNode(tree.roots[0], true, {expandChildren: true});
  27. expect(tree.flattened).toHaveLength(2);
  28. });
  29. it('skips certain nodes roots', () => {
  30. const root = n({id: 'root'});
  31. const child1 = n({id: 'child1'});
  32. const child2 = n({id: 'child2'});
  33. root.children = [child1];
  34. child1.children = [child2];
  35. const tree = VirtualizedTree.fromRoots([root], node => {
  36. return node.node.id === 'child1';
  37. });
  38. tree.expandNode(tree.roots[0], true, {expandChildren: true});
  39. expect(tree.flattened).toHaveLength(2);
  40. expect(tree.flattened[1].depth).toBe(1);
  41. expect(tree.flattened[1].node.id).toBe('child2');
  42. });
  43. });
  44. describe('expandNode', () => {
  45. it('expands a closed node', () => {
  46. const root = n({id: 'root'});
  47. const child1 = n({id: 'child1'});
  48. root.children = [child1];
  49. const tree = VirtualizedTree.fromRoots([root]);
  50. const addedNodes = tree.expandNode(tree.roots[0], true);
  51. expect(addedNodes.length).toBe(1);
  52. expect(tree.flattened[1]).toBe(tree.roots[0].children[0]);
  53. expect(tree.flattened).toHaveLength(2);
  54. });
  55. it('closes a expanded node', () => {
  56. const root = n({id: 'root'});
  57. const child1 = n({id: 'child1'});
  58. root.children = [child1];
  59. const tree = VirtualizedTree.fromRoots([root]);
  60. // Expand all
  61. tree.expandNode(tree.roots[0], true, {expandChildren: true});
  62. // Close root
  63. const removedNodes = tree.expandNode(tree.roots[0], false);
  64. expect(removedNodes.length).toBe(1);
  65. expect(tree.flattened[0]).toBe(tree.roots[0]);
  66. expect(tree.flattened).toHaveLength(1);
  67. });
  68. });
  69. describe('toExpandedList', () => {
  70. it('skips non-expanded nodes', () => {
  71. const root = n({id: 'root'});
  72. const child1 = n({id: 'child1'});
  73. root.children = [child1];
  74. const tree = VirtualizedTree.fromRoots([root]);
  75. tree.roots[0].setExpanded(false);
  76. expect(VirtualizedTree.toExpandedList(tree.roots).map(i => i.node.id)).toEqual([
  77. 'root',
  78. ]);
  79. });
  80. });
  81. describe('sort', () => {
  82. it('sorts the tree at each level', () => {
  83. const root = n({id: 'root', weight: 3});
  84. const child1 = n({id: 'child1', weight: 1});
  85. const child2 = n({id: 'child2', weight: 2});
  86. root.children = [child1, child2];
  87. const tree = VirtualizedTree.fromRoots([root]);
  88. tree.sort((a, b) => {
  89. return b.node.weight - a.node.weight;
  90. });
  91. expect(toFlattenedList(tree).map(i => i.node.id)).toEqual([
  92. 'root',
  93. 'child2',
  94. 'child1',
  95. ]);
  96. });
  97. });
  98. });