ghostTable.spec.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. describe('GhostTable', () => {
  2. var hotSettings = {
  3. data: [['A', '1', 'A\nB\nC'], ['B', '2', 'A-----B-------C'], ['C', '3', 'A---\n--B-------C']]
  4. };
  5. var gt;
  6. beforeEach(function() {
  7. this.$container = $('<div id="testContainer"></div>').appendTo('body');
  8. });
  9. afterEach(function() {
  10. if (this.$container) {
  11. destroy();
  12. this.$container.remove();
  13. }
  14. if (gt) {
  15. gt.clean();
  16. gt = null;
  17. }
  18. });
  19. describe('row', () => {
  20. it('should throw exception if we try to add column after added row', () => {
  21. var hot = handsontable(hotSettings);
  22. var exception = false;
  23. var samples = new Map();
  24. gt = new Handsontable.__GhostTable(hot);
  25. gt.addRow(0, samples);
  26. try {
  27. gt.addColumn(0, samples);
  28. } catch (ex) {
  29. exception = true;
  30. }
  31. expect(exception).toBe(true);
  32. });
  33. it('should create container element only for first row', () => {
  34. var hot = handsontable(hotSettings);
  35. var samples = new Map();
  36. gt = new Handsontable.__GhostTable(hot);
  37. spyOn(gt, 'createContainer').and.callThrough();
  38. gt.addRow(0, samples);
  39. gt.addRow(0, samples);
  40. gt.addRow(0, samples);
  41. gt.addRow(1, samples);
  42. gt.addRow(2, samples);
  43. expect(gt.createContainer.calls.count()).toBe(1);
  44. expect(gt.createContainer.calls.mostRecent().args).toEqual(['handsontable']);
  45. });
  46. it('should add row to rows collection after call `addRow` method', () => {
  47. var hot = handsontable(hotSettings);
  48. var samples = new Map();
  49. gt = new Handsontable.__GhostTable(hot);
  50. expect(gt.rows.length).toBe(0);
  51. samples.clear();
  52. samples.set(0, {strings: [{value: 'Foo', row: 0}, {value: 'Foo Bar', row: 0}]});
  53. gt.addRow(0, samples);
  54. expect(gt.rows.length).toBe(1);
  55. expect(gt.rows[0].row).toBe(0);
  56. expect(gt.rows[0].table.className).toBe('htCore');
  57. expect(gt.rows[0].table.nodeName).toBe('TABLE');
  58. expect(gt.rows[0].table.querySelectorAll('colgroup > col').length).toBe(2);
  59. expect(gt.rows[0].table.querySelector('tbody > tr > td').innerHTML).toBe('Foo');
  60. samples.clear();
  61. samples.set(0, {strings: [{value: 'Bar', row: 1}, {value: 'Baz1234', row: 1}]});
  62. gt.addRow(1, samples);
  63. expect(gt.rows.length).toBe(2);
  64. expect(gt.rows[1].row).toBe(1);
  65. expect(gt.rows[1].table.className).toBe('htCore');
  66. expect(gt.rows[1].table.nodeName).toBe('TABLE');
  67. expect(gt.rows[1].table.querySelectorAll('colgroup > col').length).toBe(2);
  68. expect(gt.rows[1].table.querySelector('tbody > tr > td').innerHTML).toBe('Bar');
  69. });
  70. it('should get valid heights', () => {
  71. var hot = handsontable(hotSettings);
  72. var heightSpy = jasmine.createSpy();
  73. var samples = new Map();
  74. gt = new Handsontable.__GhostTable(hot);
  75. samples.clear();
  76. samples.set(0, {strings: [{value: 'Foo', row: 0}, {value: 'Foo.....Bar', row: 0}]});
  77. gt.addRow(0, samples);
  78. samples.clear();
  79. samples.set(0, {strings: [{value: 'Foo\nBar\nsqw', row: 1}]});
  80. gt.addRow(1, samples);
  81. samples.clear();
  82. samples.set(0, {strings: [{value: 'Foo', row: 0}, {value: 'Foo Bar', row: 0}]});
  83. gt.addRow(2, samples);
  84. gt.getHeights(heightSpy);
  85. expect(heightSpy.calls.count()).toBe(3);
  86. expect(heightSpy.calls.argsFor(0)[0]).toBe(0);
  87. expect(heightSpy.calls.argsFor(0)[1]).toBe(23);
  88. expect(heightSpy.calls.argsFor(1)[0]).toBe(1);
  89. expect(heightSpy.calls.argsFor(1)[1]).toBe(64);
  90. expect(heightSpy.calls.argsFor(2)[0]).toBe(2);
  91. expect(heightSpy.calls.argsFor(2)[1]).toBe(43);
  92. });
  93. });
  94. describe('column', () => {
  95. it('should throw exception if we try to add row after added column', () => {
  96. var hot = handsontable(hotSettings);
  97. var exception = false;
  98. var samples = new Map();
  99. gt = new Handsontable.__GhostTable(hot);
  100. gt.addColumn(0, samples);
  101. try {
  102. gt.addRow(0, samples);
  103. } catch (ex) {
  104. exception = true;
  105. }
  106. expect(exception).toBe(true);
  107. });
  108. it('should create container element only for first column', () => {
  109. var hot = handsontable(hotSettings);
  110. var samples = new Map();
  111. gt = new Handsontable.__GhostTable(hot);
  112. spyOn(gt, 'createContainer').and.callThrough();
  113. gt.addColumn(0, samples);
  114. gt.addColumn(0, samples);
  115. gt.addColumn(0, samples);
  116. gt.addColumn(1, samples);
  117. gt.addColumn(2, samples);
  118. expect(gt.createContainer.calls.count()).toBe(1);
  119. expect(gt.createContainer.calls.mostRecent().args).toEqual(['handsontable']);
  120. });
  121. it('should add column to columns collection after call `addColumn` method', () => {
  122. var hot = handsontable(hotSettings);
  123. var samples = new Map();
  124. gt = new Handsontable.__GhostTable(hot);
  125. expect(gt.columns.length).toBe(0);
  126. samples.clear();
  127. samples.set(0, {strings: [{value: 'Foo', col: 0}, {value: 'Foo Bar', col: 0}]});
  128. gt.addColumn(0, samples);
  129. expect(gt.columns.length).toBe(1);
  130. expect(gt.columns[0].col).toBe(0);
  131. expect(gt.columns[0].table.className).toBe('htCore');
  132. expect(gt.columns[0].table.style.width).toBe('auto');
  133. expect(gt.columns[0].table.style.tableLayout).toBe('auto');
  134. expect(gt.columns[0].table.nodeName).toBe('TABLE');
  135. expect(gt.columns[0].table.querySelectorAll('thead > tr > th').length).toBe(1);
  136. expect(gt.columns[0].table.querySelector('tbody > tr > td').innerHTML).toBe('Foo');
  137. samples.clear();
  138. samples.set(0, {strings: [{value: 'Bar', row: 1}, {value: 'Baz1234', row: 1}]});
  139. gt.addColumn(1, samples);
  140. expect(gt.columns.length).toBe(2);
  141. expect(gt.columns[1].col).toBe(1);
  142. expect(gt.columns[1].table.className).toBe('htCore');
  143. expect(gt.columns[1].table.nodeName).toBe('TABLE');
  144. expect(gt.columns[1].table.querySelectorAll('thead > tr > th').length).toBe(1);
  145. expect(gt.columns[1].table.querySelector('tbody > tr > td').innerHTML).toBe('Bar');
  146. });
  147. it('should get valid widths', () => {
  148. var hot = handsontable(hotSettings);
  149. var widthSpy = jasmine.createSpy();
  150. var samples = new Map();
  151. gt = new Handsontable.__GhostTable(hot);
  152. samples.clear();
  153. samples.set(0, {strings: [{value: 'Foo', col: 0}, {value: 'Foo.....Bar', col: 0}]});
  154. gt.addColumn(0, samples);
  155. samples.clear();
  156. samples.set(0, {strings: [{value: 'Foo\nBar\nsqw', col: 1}]});
  157. gt.addColumn(1, samples);
  158. samples.clear();
  159. samples.set(0, {strings: [{value: 'Foo', col: 0}, {value: 'Foo Bar', col: 0}]});
  160. gt.addColumn(2, samples);
  161. gt.getWidths(widthSpy);
  162. expect(widthSpy.calls.count()).toBe(3);
  163. expect(widthSpy.calls.argsFor(0)[0]).toBe(0);
  164. expect(widthSpy.calls.argsFor(0)[1]).toBeAroundValue(87, 4);
  165. expect(widthSpy.calls.argsFor(1)[0]).toBe(1);
  166. expect(widthSpy.calls.argsFor(1)[1]).toBeAroundValue(41, 4);
  167. expect(widthSpy.calls.argsFor(2)[0]).toBe(2);
  168. expect(widthSpy.calls.argsFor(2)[1]).toBeAroundValue(68, 4);
  169. });
  170. });
  171. it('should reset internal state after call `clean` method', () => {
  172. var hot = handsontable(hotSettings);
  173. var samples = new Map();
  174. gt = new Handsontable.__GhostTable(hot);
  175. gt.addColumn(0, samples);
  176. gt.rows.push({});
  177. gt.getWidths(() => {});
  178. expect(gt.columns.length).toBe(1);
  179. expect(gt.samples).toBeDefined();
  180. expect(gt.injected).toBe(true);
  181. expect(gt.container).toBeDefined();
  182. expect(document.querySelector('.htGhostTable')).toBeDefined();
  183. gt.clean();
  184. expect(gt.columns.length).toBe(0);
  185. expect(gt.samples).toBe(null);
  186. expect(gt.injected).toBe(false);
  187. expect(gt.container).toBe(null);
  188. expect(document.querySelector('.htGhostTable')).toBe(null);
  189. });
  190. it('should be detected as vertical if at least one row is added', () => {
  191. var hot = handsontable(hotSettings);
  192. var samples = new Map();
  193. var gt = new Handsontable.__GhostTable(hot);
  194. gt.addRow(0, samples);
  195. expect(gt.isVertical()).toBe(true);
  196. expect(gt.isHorizontal()).toBe(false);
  197. });
  198. it('should be detected as horizontal if at least one column is added', () => {
  199. var hot = handsontable(hotSettings);
  200. var samples = new Map();
  201. var gt = new Handsontable.__GhostTable(hot);
  202. gt.addColumn(0, samples);
  203. expect(gt.isVertical()).toBe(false);
  204. expect(gt.isHorizontal()).toBe(true);
  205. });
  206. });