tests.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. var chai = require('chai');
  2. var sinon = require('sinon');
  3. var sinonChai = require('sinon-chai');
  4. var should = chai.should();
  5. chai.use(sinonChai);
  6. require('jsdom-global')();
  7. var $ = require('jquery');
  8. require('../../src/js/jquery.orgchart');
  9. describe('orgchart -- integration tests', function () {
  10. document.body.innerHTML = '<div id="chart-container"></div>';
  11. var $container = $('#chart-container'),
  12. ds = {
  13. 'id': 'n1',
  14. 'name': 'Lao Lao',
  15. 'children': [
  16. { 'id': 'n2', 'name': 'Bo Miao' },
  17. { 'id': 'n3', 'name': 'Su Miao',
  18. 'children': [
  19. { 'id': 'n5', 'name': 'Tie Hua',
  20. 'children' : [
  21. { 'id': 'n8', 'name': 'Dan Dan' }
  22. ]
  23. },
  24. { 'id': 'n6', 'name': 'Hei Hei',
  25. 'children': [
  26. { 'id': 'n9', 'name': 'Er Dan' }
  27. ]
  28. },
  29. { 'id': 'n7', 'name': 'Pang Pang',
  30. 'children': [
  31. { 'id': 'n10', 'name': 'San Dan' }
  32. ]
  33. }
  34. ]
  35. },
  36. { 'id': 'n4', 'name': 'Hong Miao' },
  37. ]
  38. },
  39. oc = {},
  40. hierarchy = {
  41. id: 'n1',
  42. children: [
  43. { id: 'n2' },
  44. { id: 'n3',
  45. children: [
  46. { id: 'n5',
  47. children: [
  48. { id: 'n8' }
  49. ]
  50. },
  51. { id: 'n6',
  52. children: [
  53. { id: 'n9'}
  54. ]
  55. },
  56. { id: 'n7',
  57. children: [
  58. { id: 'n10' }
  59. ]
  60. }
  61. ]
  62. },
  63. { id: 'n4' }
  64. ]
  65. },
  66. $laolao,
  67. $bomiao,
  68. $sumiao,
  69. $hongmiao,
  70. $chunmiao,
  71. $tiehua,
  72. $heihei,
  73. $pangpang,
  74. $dandan,
  75. $erdan;
  76. beforeEach(function () {
  77. oc = $container.orgchart({
  78. 'data': ds
  79. }),
  80. $laolao = $('#n1'),
  81. $bomiao = $('#n2'),
  82. $sumiao = $('#n3'),
  83. $hongmiao = $('#n4'),
  84. $tiehua = $('#n5'),
  85. $heihei = $('#n6'),
  86. $pangpang = $('#n7'),
  87. $dandan = $('#n8'),
  88. $erdan = $('#n9'),
  89. $sandan = $('#n10');
  90. });
  91. afterEach(function () {
  92. $laolao = $bomiao = $sumiao = $hongmiao = $chunmiao = $tiehua = $heihei = $pangpang = $dandan = $erdan = $sandan = null;
  93. $container.empty();
  94. });
  95. it('addParent()', function () {
  96. oc.addParent($laolao, { 'name': 'Lao Ye', 'id': 'n0' });
  97. $laolao.closest('.nodes').prevAll().should.lengthOf(3);
  98. oc.$chart.find('.node:first').should.deep.equal($('#n0'));
  99. });
  100. describe('addChildren()', function () {
  101. it('Add child nodes to the leaf node', function () {
  102. oc.addChildren($bomiao, [{'name': 'Li Xin', 'id': 'n11' }]);
  103. $bomiao.closest('tr').siblings().should.lengthOf(3);
  104. $bomiao.closest('tr').siblings('.nodes').find('.node').attr('id').should.equal('n11');
  105. });
  106. it('Add child nodes to the un-leaf node', function () {
  107. oc.addChildren($sumiao, [{'name': 'Li Xin', 'id': 'n11' }]);
  108. $sumiao.closest('tr').siblings('.nodes').children().should.lengthOf(4);
  109. $sumiao.closest('tr').siblings('.nodes').children(':last').find('.node').attr('id').should.equal('n11');
  110. });
  111. });
  112. describe('addSiblings()', function () {
  113. it('Just add sibling nodes', function () {
  114. oc.addSiblings($sumiao, [{'name': 'Li Xin', 'id': 'n11' }]);
  115. $laolao.closest('tr').siblings('.nodes').children().should.lengthOf(4);
  116. $laolao.closest('tr').siblings('.nodes').children(':last').find('.node').attr('id').should.equal('n11');
  117. });
  118. it('Add sibling nodes as well as parent node', function () {
  119. oc.addSiblings($laolao, { 'name': 'Lao Ye', 'id': 'n0', 'children': [{'name': 'Li Xin', 'id': 'n11' }] });
  120. $laolao.closest('.nodes').prevAll().should.lengthOf(3);
  121. oc.$chart.find('.node:first').should.deep.equal($('#n0'));
  122. $laolao.closest('table').parent().siblings().should.lengthOf(1);
  123. $laolao.closest('table').parent().siblings().find('.node').attr('id').should.equal('n11');
  124. });
  125. });
  126. describe('removeNodes()', function () {
  127. it('Remove leaf node', function () {
  128. oc.removeNodes($dandan);
  129. $tiehua.closest('tr').siblings().should.lengthOf(0);
  130. });
  131. it('Remove parent node', function () {
  132. oc.removeNodes($tiehua);
  133. $sumiao.closest('tr').siblings('.nodes').children().should.lengthOf(2);
  134. $('#n5').should.lengthOf(0);
  135. $('#n8').should.lengthOf(0);
  136. });
  137. it('Remove parent node', function () {
  138. oc.removeNodes($laolao);
  139. oc.$chartContainer.is(':empty').should.be.true;
  140. });
  141. });
  142. });