nodeCtx.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { expect } from 'chai';
  2. import { Tree, TreeNode, TreeRaw } from '../src/tree';
  3. function getIDList(nodes: TreeNode[]): string[] {
  4. return nodes.map(node => node.ID);
  5. }
  6. describe('NodeCtx', () => {
  7. const rawData: TreeRaw[] = [
  8. { ID: '1', parentID: '-1', seq: 1 },
  9. { ID: '2', parentID: '-1', seq: 3 },
  10. { ID: '3', parentID: '-1', seq: 2 },
  11. { ID: '4', parentID: '-1', seq: 4 },
  12. { ID: '5', parentID: '-1', seq: 5 },
  13. { ID: '6', parentID: '1', seq: 2 },
  14. { ID: '7', parentID: '1', seq: 1 },
  15. { ID: '8', parentID: '7', seq: 1 },
  16. { ID: '9', parentID: '2', seq: 1 },
  17. ];
  18. const tree = new Tree(rawData);
  19. it('row', () => {
  20. const node = tree.find('4');
  21. if (node) {
  22. const row = node.getCtx().row();
  23. expect(row).to.equal(7);
  24. }
  25. });
  26. it('depth', () => {
  27. const node = tree.find('1');
  28. if (node) {
  29. const depth = node.getCtx().depth();
  30. expect(depth).to.equal(0);
  31. }
  32. const deepNode = tree.find('8');
  33. if (deepNode) {
  34. const depth = deepNode.getCtx().depth();
  35. expect(depth).to.equal(2);
  36. }
  37. });
  38. it('visible', () => {
  39. const node = tree.find('1');
  40. if (node) {
  41. node.getCtx().expanded = true;
  42. const testNode = tree.find('8');
  43. if (testNode) {
  44. const visibleA = testNode.getCtx().visible();
  45. expect(visibleA).to.equal(false);
  46. node.getCtx().expanded = false;
  47. const visibleB = testNode.getCtx().visible();
  48. expect(visibleB).to.equal(false);
  49. }
  50. }
  51. });
  52. it('parent', () => {
  53. const node = tree.find('6');
  54. if (node) {
  55. const parent = node.getCtx().parent();
  56. expect(parent).to.have.property('ID', '1');
  57. }
  58. });
  59. it('parent', () => {
  60. const node = tree.find('6');
  61. if (node) {
  62. const parent = node.getCtx().parent();
  63. expect(parent).to.have.property('ID', '1');
  64. }
  65. });
  66. it('next', () => {
  67. const node = tree.find('1');
  68. if (node) {
  69. const next = node.getCtx().next();
  70. expect(next).to.have.property('ID', '3');
  71. }
  72. });
  73. it('prev', () => {
  74. const node = tree.find('3');
  75. if (node) {
  76. const prev = node.getCtx().prev();
  77. expect(prev).to.have.property('ID', '1');
  78. }
  79. });
  80. it('children', () => {
  81. const node = tree.find('1');
  82. if (node) {
  83. const children = node.getCtx().children();
  84. const IDList = getIDList(children);
  85. expect(IDList).to.have.ordered.members(['7', '6']);
  86. }
  87. });
  88. it('firstChild', () => {
  89. const node = tree.find('1');
  90. if (node) {
  91. const firstChild = node.getCtx().firstChild();
  92. expect(firstChild).to.have.property('ID', '7');
  93. }
  94. });
  95. it('lastChild', () => {
  96. const node = tree.find('1');
  97. if (node) {
  98. const lastChild = node.getCtx().lastChild();
  99. expect(lastChild).to.have.property('ID', '6');
  100. }
  101. });
  102. it('posterity', () => {
  103. const node = tree.find('1');
  104. if (node) {
  105. const posterity = node.getCtx().posterity();
  106. const IDList = getIDList(posterity);
  107. expect(IDList).to.have.ordered.members(['7', '8', '6']);
  108. }
  109. });
  110. it('progenitor', () => {
  111. const node = tree.find('8');
  112. if (node) {
  113. const progenitor = node.getCtx().progenitor();
  114. if (progenitor) {
  115. expect(progenitor).to.have.property('ID', '1');
  116. }
  117. }
  118. });
  119. it('ancestor', () => {
  120. const node = tree.find('8');
  121. if (node) {
  122. const ancestor = node.getCtx().ancestor();
  123. const IDList = getIDList(ancestor);
  124. expect(IDList).to.have.ordered.members(['7', '1']);
  125. }
  126. });
  127. it('brothers', () => {
  128. const node = tree.find('6');
  129. if (node) {
  130. const brothers = node.getCtx().brothers();
  131. const IDList = getIDList(brothers);
  132. expect(IDList).to.have.ordered.members(['7', '6']);
  133. const excludeBrothers = node.getCtx().brothers(false);
  134. const exIDList = getIDList(excludeBrothers);
  135. expect(exIDList).to.have.ordered.members(['7']);
  136. }
  137. });
  138. it('nextBrothers', () => {
  139. const node = tree.find('2');
  140. if (node) {
  141. const nextBrothers = node.getCtx().nextBrothers();
  142. const IDList = getIDList(nextBrothers);
  143. expect(IDList).to.have.ordered.members(['4', '5']);
  144. }
  145. });
  146. it('prevBrothers', () => {
  147. const node = tree.find('2');
  148. if (node) {
  149. const prevBrothers = node.getCtx().prevBrothers();
  150. const IDList = getIDList(prevBrothers);
  151. expect(IDList).to.have.ordered.members(['1', '3']);
  152. }
  153. });
  154. });