idTreeTest.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. const fs = require('fs');
  5. var test = require('tape');
  6. var idTreeUtil = null;
  7. var idTreeSetting = {
  8. id: 'ID',
  9. pid: 'ParentID',
  10. nid: 'NextSiblingID',
  11. rootId: -1,
  12. autoUpdate: false
  13. };
  14. var testTreeData = null;
  15. var testTree = null;
  16. var getKeyIDs = function (node) {
  17. var data = [];
  18. if (node) {
  19. data.push(node.getID());
  20. data.push(node.getParentID());
  21. data.push(node.getNextSiblingID());
  22. } else {
  23. for (var i = 1; i <= 3; i++) {
  24. data.push(-1);
  25. }
  26. }
  27. return data;
  28. }
  29. test('loading idTree Module: ', function (t) {
  30. var idTreeCode = fs.readFileSync(__dirname + '../../../../public/web/idTree.js', 'utf8', 'r');
  31. eval(idTreeCode);
  32. idTreeUtil = idTree;
  33. t.pass('pass for loading idTree Module');
  34. t.end();
  35. });
  36. test('loading Testing Data: ', function (t) {
  37. var idTreeDataCode = fs.readFileSync(__dirname + '../../../tmp_data/data_15690.js', 'utf8', 'r');
  38. eval(idTreeDataCode);
  39. testTreeData = datas;
  40. t.pass('pass for loading Testing Data');
  41. t.end();
  42. });
  43. test('idTree loading Testing Data: ', function (t) {
  44. t.plan(1);
  45. testTree = idTreeUtil.createNew(idTreeSetting);
  46. testTree.loadDatas(testTreeData);
  47. t.equal(testTree.count(), testTreeData.length);
  48. t.end();
  49. });
  50. test('upMove Node: ', function (t) {
  51. t.plan(3);
  52. var testNodeID = 13431;
  53. var testResult = [[13429, 13427, 13431], [13430, 13427, 13432], [13431, 13427, 13430]];
  54. var node = testTree.findNode(testNodeID), pre = node.preSibling, prePre = pre ? pre.preSibling : null;
  55. node.upMove();
  56. t.deepEqual(getKeyIDs(prePre), testResult[0]);
  57. t.deepEqual(getKeyIDs(pre), testResult[1]);
  58. t.deepEqual(getKeyIDs(node), testResult[2]);
  59. t.end();
  60. });
  61. test('downMove Node: ', function (t) {
  62. t.plan(3);
  63. var testNodeID = 13431;
  64. var testResult = [[13429, 13427, 13430], [13430, 13427, 13431], [13431, 13427, 13432]];
  65. var node = testTree.findNode(testNodeID), pre = node.preSibling, next = node.nextSibling;
  66. node.downMove();
  67. t.deepEqual(getKeyIDs(pre), testResult[0]);
  68. t.deepEqual(getKeyIDs(next), testResult[1]);
  69. t.deepEqual(getKeyIDs(node), testResult[2]);
  70. t.end();
  71. });
  72. test('upLevel Node: ', function (t) {
  73. t.plan(4);
  74. var testNodeID = 13431;
  75. var node = testTree.findNode(testNodeID), orgPre = node.preSibling, orgNext = node.nextSibling, orgParent = node.parent;
  76. var orgChildrenCount = node.children.length, orgNextSiblingCount = orgParent.children.length - node.siblingIndex() - 1;
  77. node.upLevel();
  78. t.equal(orgNext.parent, node);
  79. t.equal(node.children.length, orgChildrenCount + orgNextSiblingCount);
  80. t.equal(orgPre.nextSibling, null);
  81. t.equal(orgParent.nextSibling, node);
  82. t.end();
  83. });
  84. test('downLevel Node: ', function (t) {
  85. t.plan(4);
  86. var testNodeID = 13431;
  87. var node = testTree.findNode(testNodeID), orgPre = node.preSibling, orgNext = node.nextSibling, orgParent = node.parent;
  88. var newPre = orgPre.lastChild();
  89. node.downLevel();
  90. t.equal(orgPre.nextSibling, orgNext);
  91. t.equal(orgPre.lastChild(), node);
  92. t.equal(newPre.nextSibling, node);
  93. t.equal(node.nextSibling, null);
  94. t.end();
  95. });
  96. test('insert Node: ', function (t) {
  97. t.plan(5);
  98. var parent = testTree.findNode(13574), next = testTree.findNode(13598);
  99. var orgTreeCount = testTree.count(), orgParentChildrenCount = parent.children.length, orgNextPre = next.preSibling;
  100. var newNode = testTree.insert(parent.getID(), next.getID());
  101. t.equal(testTree.count(), orgTreeCount + 1);
  102. t.equal(parent.children.length, orgParentChildrenCount + 1);
  103. t.equal(orgNextPre.nextSibling, newNode);
  104. t.equal(newNode.parent, parent);
  105. t.equal(newNode.nextSibling, next);
  106. t.end();
  107. });
  108. test('delete Node: ', function (t) {
  109. t.plan(3);
  110. var testNodeID = 13598;
  111. var node = testTree.findNode(testNodeID), orgParent = node.parent, orgPre = node.preSibling, orgNext = node.nextSibling;
  112. var orgTreeCount = testTree.count(), orgParentChildrenCount = orgParent.children.length;
  113. testTree.delete(node);
  114. t.equal(testTree.count(), orgTreeCount - node.posterityCount() - 1);
  115. t.equal(orgParent.children.length, orgParentChildrenCount - 1);
  116. t.equal(orgPre.nextSibling, orgNext);
  117. t.end();
  118. });