samplesGenerator.spec.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import SamplesGenerator from 'handsontable/utils/samplesGenerator';
  2. describe('SamplesGenerator', () => {
  3. it('should internally call `generateSamples` when calling `generateRowSamples`', () => {
  4. const sg = new SamplesGenerator();
  5. spyOn(sg, 'generateSamples').and.returnValue('test');
  6. const result = sg.generateRowSamples('first param', 'second param');
  7. expect(result).toBe('test');
  8. expect(sg.generateSamples.calls.count()).toBe(1);
  9. expect(sg.generateSamples.calls.argsFor(0)[0]).toBe('row');
  10. expect(sg.generateSamples.calls.argsFor(0)[1]).toBe('second param');
  11. expect(sg.generateSamples.calls.argsFor(0)[2]).toBe('first param');
  12. });
  13. it('should internally call `generateSamples` when calling `generateColumnSamples`', () => {
  14. const sg = new SamplesGenerator();
  15. spyOn(sg, 'generateSamples').and.returnValue('test');
  16. const result = sg.generateColumnSamples('first param', 'second param');
  17. expect(result).toBe('test');
  18. expect(sg.generateSamples.calls.count()).toBe(1);
  19. expect(sg.generateSamples.calls.argsFor(0)[0]).toBe('col');
  20. expect(sg.generateSamples.calls.argsFor(0)[1]).toBe('second param');
  21. expect(sg.generateSamples.calls.argsFor(0)[2]).toBe('first param');
  22. });
  23. it('should generate collection of Maps when range is passed as Number', () => {
  24. const sg = new SamplesGenerator();
  25. spyOn(sg, 'generateSample').and.callFake((type, range, index) => {
  26. const map = new Map();
  27. map.set(index, { type, range, index });
  28. return map;
  29. });
  30. const result = sg.generateSamples('row', 10, 1);
  31. expect(result instanceof Map).toBe(true);
  32. expect(result.size).toBe(1);
  33. expect(result.get(1).get(1).type).toBe('row');
  34. });
  35. it('should generate collection of Maps when range is passed as Object', () => {
  36. const sg = new SamplesGenerator();
  37. spyOn(sg, 'generateSample').and.callFake((type, range, index) => {
  38. const map = new Map();
  39. map.set(index, { type, range, index });
  40. return map;
  41. });
  42. const result = sg.generateSamples('col', 10, { from: 4, to: 12 });
  43. expect(result instanceof Map).toBe(true);
  44. expect(result.size).toBe(9);
  45. expect(result.get(7).get(7).type).toBe('col');
  46. });
  47. it('should generate row sample', () => {
  48. const sg = new SamplesGenerator((row, col) => {
  49. const data = [
  50. ['AA', { id: 2 }, 'C', [1, 2, 3, 4, 5], 123456789],
  51. ];
  52. return {
  53. value: data[row][col]
  54. };
  55. });
  56. spyOn(sg, 'dataFactory').and.callThrough();
  57. const result = sg.generateSample('row', { from: 0, to: 4 }, 0);
  58. expect(sg.dataFactory.calls.count()).toBe(5);
  59. expect(sg.dataFactory.calls.mostRecent().args[0]).toBe(0);
  60. expect(sg.dataFactory.calls.mostRecent().args[1]).toBe(4);
  61. expect(result instanceof Map).toBe(true);
  62. expect(result.size).toBe(4);
  63. expect(result.get(1).strings).toEqual([{ value: { id: 2 }, col: 1 }, { value: 'C', col: 2 }]);
  64. expect(result.get(2).strings).toEqual([{ value: 'AA', col: 0 }]);
  65. });
  66. it('should generate row sample with limited generated items (when the data source contains the same values)', () => {
  67. const sg = new SamplesGenerator((row, col) => {
  68. const data = [
  69. [true, true, true, true, true, true, true, true, true, true],
  70. ];
  71. return {
  72. value: data[row][col]
  73. };
  74. });
  75. spyOn(sg, 'dataFactory').and.callThrough();
  76. const result = sg.generateSample('row', { from: 0, to: 9 }, 0);
  77. expect(result.size).toBe(1);
  78. expect(result.get(4).strings).toEqual([{ col: 0, value: true }]);
  79. });
  80. it('should generate row sample controlled by `bundleCountSeed` (in case of collecting more samples despite their repeatability)', () => {
  81. let seedIndex = 0;
  82. const sg = new SamplesGenerator((row, col) => {
  83. const data = [
  84. [true, true, true, true, true, true, true, true, true, true],
  85. ];
  86. seedIndex += 1;
  87. return {
  88. value: data[row][col],
  89. bundleCountSeed: seedIndex,
  90. };
  91. });
  92. spyOn(sg, 'dataFactory').and.callThrough();
  93. const result = sg.generateSample('row', { from: 0, to: 9 }, 0);
  94. expect(result.size).toBe(10);
  95. expect(result.get(5).strings).toEqual([{ col: 0, value: true }]);
  96. expect(result.get(6).strings).toEqual([{ col: 1, value: true }]);
  97. expect(result.get(7).strings).toEqual([{ col: 2, value: true }]);
  98. expect(result.get(8).strings).toEqual([{ col: 3, value: true }]);
  99. expect(result.get(9).strings).toEqual([{ col: 4, value: true }]);
  100. expect(result.get(10).strings).toEqual([{ col: 5, value: true }]);
  101. expect(result.get(11).strings).toEqual([{ col: 6, value: true }]);
  102. expect(result.get(12).strings).toEqual([{ col: 7, value: true }]);
  103. expect(result.get(13).strings).toEqual([{ col: 8, value: true }]);
  104. expect(result.get(14).strings).toEqual([{ col: 9, value: true }]);
  105. });
  106. it('should generate column sample', () => {
  107. const sg = new SamplesGenerator((row, col) => {
  108. const data = [
  109. [1, 2, 3, 44],
  110. ['AA', 'BB', 'C', 'D'],
  111. ['zz', 'xxx', 'c-c', 'vvvvv'],
  112. [[1], [1, 2], [1, 2], [4]],
  113. [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }],
  114. ];
  115. return {
  116. value: data[row][col]
  117. };
  118. });
  119. spyOn(sg, 'dataFactory').and.callThrough();
  120. const result = sg.generateSample('col', { from: 0, to: 4 }, 3);
  121. expect(sg.dataFactory.calls.count()).toBe(5);
  122. expect(sg.dataFactory.calls.mostRecent().args[0]).toBe(4);
  123. expect(sg.dataFactory.calls.mostRecent().args[1]).toBe(3);
  124. expect(result instanceof Map).toBe(true);
  125. expect(result.size).toBe(3);
  126. expect(result.get(1).strings).toEqual([{ value: 'D', row: 1 }, { value: [4], row: 3 }, { value: { id: 4 }, row: 4 }]);
  127. expect(result.get(2).strings).toEqual([{ value: 44, row: 0 }]);
  128. });
  129. it('should generate column sample with limited generated items (when the data source contains the same values)', () => {
  130. const sg = new SamplesGenerator((row, col) => {
  131. const data = [
  132. [true],
  133. [true],
  134. [true],
  135. [true],
  136. [true],
  137. [true],
  138. [true],
  139. [true],
  140. [true],
  141. [true],
  142. ];
  143. return {
  144. value: data[row][col]
  145. };
  146. });
  147. spyOn(sg, 'dataFactory').and.callThrough();
  148. const result = sg.generateSample('col', { from: 0, to: 9 }, 0);
  149. expect(result.size).toBe(1);
  150. expect(result.get(4).strings).toEqual([{ row: 0, value: true }]);
  151. });
  152. it('should generate column sample controlled by `bundleCountSeed` (in case of collecting more samples despite their repeatability)', () => {
  153. let seedIndex = 0;
  154. const sg = new SamplesGenerator((row, col) => {
  155. const data = [
  156. [true],
  157. [true],
  158. [true],
  159. [true],
  160. [true],
  161. [true],
  162. [true],
  163. [true],
  164. [true],
  165. [true],
  166. ];
  167. seedIndex += 1;
  168. return {
  169. value: data[row][col],
  170. bundleCountSeed: seedIndex,
  171. };
  172. });
  173. spyOn(sg, 'dataFactory').and.callThrough();
  174. const result = sg.generateSample('col', { from: 0, to: 9 }, 0);
  175. expect(result.size).toBe(10);
  176. expect(result.get(5).strings).toEqual([{ row: 0, value: true }]);
  177. expect(result.get(6).strings).toEqual([{ row: 1, value: true }]);
  178. expect(result.get(7).strings).toEqual([{ row: 2, value: true }]);
  179. expect(result.get(8).strings).toEqual([{ row: 3, value: true }]);
  180. expect(result.get(9).strings).toEqual([{ row: 4, value: true }]);
  181. expect(result.get(10).strings).toEqual([{ row: 5, value: true }]);
  182. expect(result.get(11).strings).toEqual([{ row: 6, value: true }]);
  183. expect(result.get(12).strings).toEqual([{ row: 7, value: true }]);
  184. expect(result.get(13).strings).toEqual([{ row: 8, value: true }]);
  185. expect(result.get(14).strings).toEqual([{ row: 9, value: true }]);
  186. });
  187. });