319cafdaa038b7e8eb11f1b5ceebcd2c8323f5c319298aacb93fc8b9b91656eaddf722b656d258220bc4e656ab607d2eb959dc1a06d429dde4fe40c3717ee6 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. 'use strict';
  2. describe('Walkontable.ViewportColumnsCalculator', function () {
  3. function allColumns20() {
  4. return 20;
  5. }
  6. it('should render first 5 columns in unscrolled container', function () {
  7. var calc = new Walkontable.ViewportColumnsCalculator(100, 0, 1000, allColumns20);
  8. expect(calc.startColumn).toBe(0);
  9. expect(calc.startPosition).toBe(0);
  10. expect(calc.endColumn).toBe(4);
  11. var visibleCalc = new Walkontable.ViewportColumnsCalculator(100, 0, 1000, allColumns20, null, true);
  12. expect(visibleCalc.startColumn).toBe(0);
  13. expect(visibleCalc.endColumn).toBe(4);
  14. });
  15. it('should render 6 columns, starting from 3 in container scrolled to half of fourth column', function () {
  16. var calc = new Walkontable.ViewportColumnsCalculator(100, 70, 1000, allColumns20);
  17. expect(calc.startColumn).toBe(3);
  18. expect(calc.startPosition).toBe(60);
  19. expect(calc.endColumn).toBe(8);
  20. var visibleCalc = new Walkontable.ViewportColumnsCalculator(100, 70, 1000, allColumns20, null, true);
  21. expect(visibleCalc.startColumn).toBe(4);
  22. expect(visibleCalc.endColumn).toBe(7);
  23. });
  24. it('should render 10 columns, starting from 1 in container scrolled to half of fourth column (with render overrides)', function () {
  25. var overrideFn = function overrideFn(calc) {
  26. calc.startColumn -= 2;
  27. calc.endColumn += 2;
  28. };
  29. var calc = new Walkontable.ViewportColumnsCalculator(100, 70, 1000, allColumns20, overrideFn);
  30. expect(calc.startColumn).toBe(1);
  31. expect(calc.startPosition).toBe(20);
  32. expect(calc.endColumn).toBe(10);
  33. var visibleCalc = new Walkontable.ViewportColumnsCalculator(100, 70, 1000, allColumns20, null, true);
  34. expect(visibleCalc.startColumn).toBe(4);
  35. expect(visibleCalc.endColumn).toBe(7);
  36. });
  37. it('should return number of rendered columns', function () {
  38. var calc = new Walkontable.ViewportColumnsCalculator(100, 50, 1000, allColumns20);
  39. expect(calc.count).toBe(6);
  40. var visibleCalc = new Walkontable.ViewportColumnsCalculator(100, 50, 1000, allColumns20, null, true);
  41. expect(visibleCalc.count).toBe(4);
  42. });
  43. it('should render all columns if their size is smaller than viewport', function () {
  44. var calc = new Walkontable.ViewportColumnsCalculator(200, 0, 8, allColumns20);
  45. expect(calc.startColumn).toBe(0);
  46. expect(calc.endColumn).toBe(7);
  47. expect(calc.count).toBe(8);
  48. var visibleCalc = new Walkontable.ViewportColumnsCalculator(200, 0, 8, allColumns20, null, true);
  49. expect(visibleCalc.startColumn).toBe(0);
  50. expect(visibleCalc.endColumn).toBe(7);
  51. expect(visibleCalc.count).toBe(8);
  52. });
  53. it('should render all columns if their size is exactly the viewport', function () {
  54. var calc = new Walkontable.ViewportColumnsCalculator(200, 0, 10, allColumns20);
  55. expect(calc.startColumn).toBe(0);
  56. expect(calc.endColumn).toBe(9);
  57. expect(calc.count).toBe(10);
  58. var visibleCalc = new Walkontable.ViewportColumnsCalculator(200, 0, 10, allColumns20, null, true);
  59. expect(visibleCalc.startColumn).toBe(0);
  60. expect(visibleCalc.endColumn).toBe(9);
  61. expect(visibleCalc.count).toBe(10);
  62. });
  63. it('should render all columns if their size is slightly larger than viewport', function () {
  64. var calc = new Walkontable.ViewportColumnsCalculator(199, 0, 10, allColumns20);
  65. expect(calc.startColumn).toBe(0);
  66. expect(calc.endColumn).toBe(9);
  67. expect(calc.count).toBe(10);
  68. var visibleCalc = new Walkontable.ViewportColumnsCalculator(199, 0, 10, allColumns20, null, true);
  69. expect(visibleCalc.startColumn).toBe(0);
  70. expect(visibleCalc.endColumn).toBe(8);
  71. expect(visibleCalc.count).toBe(9);
  72. });
  73. it('should set null values if total columns is 0', function () {
  74. var calc = new Walkontable.ViewportColumnsCalculator(200, 0, 0, allColumns20);
  75. expect(calc.startColumn).toBe(null);
  76. expect(calc.startPosition).toBe(null);
  77. expect(calc.endColumn).toBe(null);
  78. expect(calc.count).toBe(0);
  79. var visibleCalc = new Walkontable.ViewportColumnsCalculator(200, 0, 0, allColumns20, null, true);
  80. expect(visibleCalc.startColumn).toBe(null);
  81. expect(visibleCalc.endColumn).toBe(null);
  82. });
  83. it('should set null values if total columns is 0 (with overrideFn provided)', function () {
  84. var overrideFn = function overrideFn(myCalc) {
  85. myCalc.startColumn = 0;
  86. myCalc.endColumn = 0;
  87. };
  88. var calc = new Walkontable.ViewportColumnsCalculator(200, 0, 0, allColumns20, overrideFn);
  89. expect(calc.startColumn).toBe(null);
  90. expect(calc.startPosition).toBe(null);
  91. expect(calc.endColumn).toBe(null);
  92. expect(calc.count).toBe(0);
  93. var visibleCalc = new Walkontable.ViewportColumnsCalculator(200, 0, 0, allColumns20, null, true);
  94. expect(visibleCalc.startColumn).toBe(null);
  95. expect(visibleCalc.endColumn).toBe(null);
  96. });
  97. it('should scroll backwards if total columns is reached', function () {
  98. var calc = new Walkontable.ViewportColumnsCalculator(190, 350, 20, allColumns20);
  99. expect(calc.startColumn).toBe(10);
  100. expect(calc.startPosition).toBe(200);
  101. expect(calc.endColumn).toBe(19);
  102. expect(calc.count).toBe(10);
  103. var visibleCalc = new Walkontable.ViewportColumnsCalculator(190, 350, 20, allColumns20, null, true);
  104. expect(visibleCalc.startColumn).toBe(11);
  105. expect(visibleCalc.endColumn).toBe(19);
  106. });
  107. it('should update stretchAllRatio after refreshStretching call (stretch: all)', function () {
  108. var calc = new Walkontable.ViewportColumnsCalculator(250, 0, 20, allColumns20, null, true, 'all');
  109. expect(calc.stretchAllRatio).toBe(0);
  110. expect(calc.stretchLastWidth).toBe(0);
  111. calc.refreshStretching(414);
  112. expect(calc.stretchAllRatio).toBe(1.035);
  113. expect(calc.stretchLastWidth).toBe(0);
  114. });
  115. it('should update stretchAllRatio after refreshStretching call (stretch: last)', function () {
  116. var calc = new Walkontable.ViewportColumnsCalculator(250, 0, 5, allColumns20, null, true, 'last');
  117. expect(calc.stretchAllRatio).toBe(0);
  118. expect(calc.stretchLastWidth).toBe(0);
  119. calc.refreshStretching(414);
  120. expect(calc.stretchAllRatio).toBe(0);
  121. expect(calc.stretchLastWidth).toBe(334);
  122. });
  123. it('should return valid stretched column width (stretch: all)', function () {
  124. var calc = new Walkontable.ViewportColumnsCalculator(250, 0, 5, allColumns20, null, true, 'all');
  125. expect(calc.getStretchedColumnWidth(0, 50)).toBe(null);
  126. expect(calc.needVerifyLastColumnWidth).toBe(true);
  127. calc.refreshStretching(417);
  128. expect(calc.getStretchedColumnWidth(0, allColumns20())).toBe(83);
  129. expect(calc.getStretchedColumnWidth(1, allColumns20())).toBe(83);
  130. expect(calc.getStretchedColumnWidth(2, allColumns20())).toBe(83);
  131. expect(calc.getStretchedColumnWidth(3, allColumns20())).toBe(83);
  132. expect(calc.needVerifyLastColumnWidth).toBe(true);
  133. expect(calc.getStretchedColumnWidth(4, allColumns20())).toBe(85);
  134. expect(calc.needVerifyLastColumnWidth).toBe(false);
  135. });
  136. it('should return valid stretched column width (stretch: last)', function () {
  137. var calc = new Walkontable.ViewportColumnsCalculator(250, 0, 5, allColumns20, null, true, 'last');
  138. expect(calc.getStretchedColumnWidth(0, 50)).toBe(null);
  139. calc.refreshStretching(417);
  140. expect(calc.getStretchedColumnWidth(0, allColumns20())).toBe(null);
  141. expect(calc.getStretchedColumnWidth(1, allColumns20())).toBe(null);
  142. expect(calc.getStretchedColumnWidth(2, allColumns20())).toBe(null);
  143. expect(calc.getStretchedColumnWidth(3, allColumns20())).toBe(null);
  144. expect(calc.getStretchedColumnWidth(4, allColumns20())).toBe(337);
  145. });
  146. it('call refreshStretching should clear stretchAllColumnsWidth and needVerifyLastColumnWidth property', function () {
  147. var calc = new Walkontable.ViewportColumnsCalculator(250, 0, 5, allColumns20, null, true, 'all');
  148. expect(calc.stretchAllColumnsWidth.length).toBe(0);
  149. expect(calc.needVerifyLastColumnWidth).toBe(true);
  150. calc.refreshStretching(417);
  151. calc.getStretchedColumnWidth(0, allColumns20());
  152. calc.getStretchedColumnWidth(1, allColumns20());
  153. calc.getStretchedColumnWidth(2, allColumns20());
  154. calc.getStretchedColumnWidth(3, allColumns20());
  155. calc.getStretchedColumnWidth(4, allColumns20());
  156. expect(calc.stretchAllColumnsWidth.length).toBe(5);
  157. expect(calc.needVerifyLastColumnWidth).toBe(false);
  158. calc.refreshStretching(201);
  159. expect(calc.stretchAllColumnsWidth.length).toBe(0);
  160. expect(calc.needVerifyLastColumnWidth).toBe(true);
  161. });
  162. });