c8f6051f31a8c3b04adf775e414cab526e05002333f3a4bad338ac29daf1da7a7cd74d1eb2c4c11d18cd809b4b83c7f7ca4a674b7c2e38c4f99a75ce39aa19 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. describe('stretchH option', () => {
  2. var $table,
  3. $container,
  4. $wrapper,
  5. debug = false;
  6. beforeEach(() => {
  7. $wrapper = $('<div></div>').css({overflow: 'hidden', position: 'relative'});
  8. $wrapper.width(500).height(201);
  9. $container = $('<div></div>');
  10. $table = $('<table></table>'); // create a table that is not attached to document
  11. $wrapper.append($container);
  12. $container.append($table);
  13. $wrapper.appendTo('body');
  14. createDataArray();
  15. });
  16. afterEach(() => {
  17. if (!debug) {
  18. $('.wtHolder').remove();
  19. }
  20. $wrapper.remove();
  21. });
  22. it('should stretch all visible columns when stretchH equals \'all\'', () => {
  23. createDataArray(20, 2);
  24. $wrapper.width(500).height(400);
  25. var wt = new Walkontable.Core({
  26. table: $table[0],
  27. data: getData,
  28. totalRows: getTotalRows,
  29. totalColumns: getTotalColumns,
  30. stretchH: 'all',
  31. rowHeaders: [function(row, TH) {
  32. TH.innerHTML = row + 1;
  33. }]
  34. });
  35. wt.draw();
  36. expect($table.outerWidth()).toBeAroundValue(wt.wtTable.holder.clientWidth);
  37. // fix differences between Mac and Linux PhantomJS
  38. expect($table.find('col:eq(2)').width() - $table.find('col:eq(1)').width()).toBeInArray([-1, 0, 1]);
  39. });
  40. it('should stretch all visible columns when stretchH equals \'all\' and window is resized', (done) => {
  41. createDataArray(20, 2);
  42. $wrapper.width(500).height(400);
  43. var wt = new Walkontable.Core({
  44. table: $table[0],
  45. data: getData,
  46. totalRows: getTotalRows,
  47. totalColumns: getTotalColumns,
  48. stretchH: 'all',
  49. rowHeaders: [function(row, TH) {
  50. TH.innerHTML = row + 1;
  51. }]
  52. });
  53. wt.draw();
  54. var initialTableWidth = $table.outerWidth();
  55. expect(initialTableWidth).toBeAroundValue($table[0].clientWidth);
  56. $wrapper.width(600).height(500);
  57. var evt = document.createEvent('CustomEvent'); // MUST be 'CustomEvent'
  58. evt.initCustomEvent('resize', false, false, null);
  59. window.dispatchEvent(evt);
  60. setTimeout(() => {
  61. var currentTableWidth = $table.outerWidth();
  62. expect(currentTableWidth).toBeAroundValue($table[0].clientWidth);
  63. expect(currentTableWidth).toBeGreaterThan(initialTableWidth);
  64. done();
  65. }, 10);
  66. });
  67. it('should stretch all visible columns when stretchH equals \'all\' (when rows are of variable height)', function() {
  68. createDataArray(20, 2);
  69. for (var i = 0, ilen = this.data.length; i < ilen; i++) {
  70. if (i % 2) {
  71. this.data[i][0] += ' this is a cell that contains a lot of text, which will make it multi-line';
  72. }
  73. }
  74. $wrapper.width(300);
  75. $wrapper.css({
  76. overflow: 'hidden'
  77. });
  78. var wt = new Walkontable.Core({
  79. table: $table[0],
  80. data: getData,
  81. totalRows: getTotalRows,
  82. totalColumns: getTotalColumns,
  83. stretchH: 'all'
  84. });
  85. wt.draw();
  86. var expectedColWidth = ((300 - getScrollbarWidth()) / 2);
  87. expectedColWidth = Math.floor(expectedColWidth);
  88. var wtHider = $table.parents('.wtHider');
  89. expect(wtHider.find('col:eq(0)').width()).toBeAroundValue(expectedColWidth);
  90. expect(wtHider.find('col:eq(1)').width() - expectedColWidth).toBeInArray([0, 1]); // fix differences between Mac and Linux PhantomJS
  91. });
  92. it('should stretch last visible column when stretchH equals \'last\' (vertical scroll)', () => {
  93. createDataArray(20, 2);
  94. $wrapper.width(300).height(201);
  95. var wt = new Walkontable.Core({
  96. table: $table[0],
  97. data: getData,
  98. totalRows: getTotalRows,
  99. totalColumns: getTotalColumns,
  100. stretchH: 'last',
  101. rowHeaders: [function(row, TH) {
  102. TH.innerHTML = row + 1;
  103. }]
  104. });
  105. wt.draw();
  106. var wtHider = $table.parents('.wtHider');
  107. expect(wtHider.outerWidth()).toBe(getTableWidth($table));
  108. expect(wtHider.find('col:eq(1)').width()).toBeLessThan(wtHider.find('col:eq(2)').width());
  109. });
  110. it('should stretch last column when stretchH equals \'last\' (horizontal scroll)', () => {
  111. createDataArray(5, 20);
  112. $wrapper.width(400).height(201);
  113. spec().data[0][19] = 'longer text';
  114. var wt = new Walkontable.Core({
  115. table: $table[0],
  116. data: getData,
  117. totalRows: getTotalRows,
  118. totalColumns: getTotalColumns,
  119. stretchH: 'last',
  120. columnHeaders: [function(index, TH) {
  121. TH.innerHTML = index + 1;
  122. }],
  123. columnWidth(index) {
  124. return index === 19 ? 100 : 50;
  125. }
  126. });
  127. wt.draw();
  128. wt.scrollHorizontal(19);
  129. wt.draw();
  130. var wtHider = $table.parents('.wtHider');
  131. expect(wtHider.find('col:eq(6)').width()).toBe(100);
  132. });
  133. it('should stretch last visible column when stretchH equals \'last\' (no scrolls)', () => {
  134. createDataArray(2, 2);
  135. $wrapper.width(300).height(201);
  136. var wt = new Walkontable.Core({
  137. table: $table[0],
  138. data: getData,
  139. totalRows: getTotalRows,
  140. totalColumns: getTotalColumns,
  141. stretchH: 'last',
  142. rowHeaders: [function(row, TH) {
  143. TH.innerHTML = row + 1;
  144. }]
  145. });
  146. wt.draw();
  147. var wtHider = $table.parents('.wtHider');
  148. expect(wtHider.outerWidth()).toBe(getTableWidth($table));
  149. expect(wtHider.find('col:eq(1)').width()).toBeLessThan(wtHider.find('col:eq(2)').width());
  150. });
  151. it('should not stretch when stretchH equals \'none\'', () => {
  152. createDataArray(20, 2);
  153. $wrapper.width(300).height(201);
  154. var wt = new Walkontable.Core({
  155. table: $table[0],
  156. data: getData,
  157. totalRows: getTotalRows,
  158. totalColumns: getTotalColumns,
  159. stretchH: 'none',
  160. rowHeaders: [function(row, TH) {
  161. TH.innerHTML = row + 1;
  162. }]
  163. });
  164. wt.draw();
  165. var wtHider = $table.parents('.wtHider');
  166. expect($table.width()).toBeLessThan($wrapper.width());
  167. expect($table.find('col:eq(1)').width()).toBe($table.find('col:eq(2)').width());
  168. });
  169. });