9f1d26571a50411379c2affc218cef3f590d58ed9cf9da972ab28edb27147ce8c39c8e0b2a1193b3b39348c63cf710892b28691fdda045023519f6033d7d5d 5.9 KB

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