Array.spec.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. import {
  2. arrayAvg,
  3. arrayEach,
  4. arrayFilter,
  5. arrayFlatten,
  6. arrayMap,
  7. arrayMax,
  8. arrayMin,
  9. arrayReduce,
  10. arraySum,
  11. } from 'handsontable/helpers/array';
  12. describe('Array helper', () => {
  13. const iterableObject = {
  14. _myArray: [2, 6, 3, 1],
  15. [Symbol.iterator]() {
  16. return this._myArray[Symbol.iterator]();
  17. }
  18. };
  19. //
  20. // Handsontable.helper.arrayAvg
  21. //
  22. describe('arrayAvg', () => {
  23. it('should returns the average value', () => {
  24. expect(arrayAvg([1])).toBe(1);
  25. expect(arrayAvg([1, 1, 2, 3, 4])).toBe(2.2);
  26. });
  27. });
  28. //
  29. // Handsontable.helper.arrayEach
  30. //
  31. describe('arrayEach', () => {
  32. it('should call callback with proper arguments for input data passed as an array', () => {
  33. const cb = jasmine.createSpy('cb');
  34. cb.and.callFake(() => true);
  35. arrayEach([4, 5, 2, 15], cb);
  36. expect(cb.calls.count()).toBe(4);
  37. expect(cb.calls.argsFor(0)).toEqual([4, 0, [4, 5, 2, 15]]);
  38. expect(cb.calls.argsFor(1)).toEqual([5, 1, [4, 5, 2, 15]]);
  39. expect(cb.calls.argsFor(2)).toEqual([2, 2, [4, 5, 2, 15]]);
  40. expect(cb.calls.argsFor(3)).toEqual([15, 3, [4, 5, 2, 15]]);
  41. });
  42. it('should call callback with proper arguments for input data passed as an object with implemented iterator protocol', () => {
  43. const cb = jasmine.createSpy('cb');
  44. cb.and.callFake(() => true);
  45. arrayEach(iterableObject, cb);
  46. expect(cb.calls.count()).toBe(4);
  47. expect(cb.calls.argsFor(0)).toEqual([2, 0, [2, 6, 3, 1]]);
  48. expect(cb.calls.argsFor(1)).toEqual([6, 1, [2, 6, 3, 1]]);
  49. expect(cb.calls.argsFor(2)).toEqual([3, 2, [2, 6, 3, 1]]);
  50. expect(cb.calls.argsFor(3)).toEqual([1, 3, [2, 6, 3, 1]]);
  51. });
  52. it('should break the loop on first invocation when the callback returns `false`', () => {
  53. const cb = jasmine.createSpy('cb');
  54. cb.and.callFake(() => false);
  55. arrayEach([4, 5, 2, 15], cb);
  56. expect(cb.calls.count()).toBe(1);
  57. expect(cb.calls.argsFor(0)).toEqual([4, 0, [4, 5, 2, 15]]);
  58. });
  59. it('should break the loop on when callback returns `false`', () => {
  60. const cb = jasmine.createSpy('cb');
  61. cb.and.callFake(value => value !== 2);
  62. arrayEach([4, 5, 2, 15], cb);
  63. expect(cb.calls.count()).toBe(3);
  64. expect(cb.calls.argsFor(0)).toEqual([4, 0, [4, 5, 2, 15]]);
  65. expect(cb.calls.argsFor(1)).toEqual([5, 1, [4, 5, 2, 15]]);
  66. expect(cb.calls.argsFor(2)).toEqual([2, 2, [4, 5, 2, 15]]);
  67. });
  68. });
  69. //
  70. // Handsontable.helper.arrayFilter
  71. //
  72. describe('arrayFilter', () => {
  73. it('should call callback with proper arguments for input data passed as an array', () => {
  74. const cb = jasmine.createSpy('cb');
  75. cb.and.callFake(() => true);
  76. arrayFilter([4, 5, 2, 15], cb);
  77. expect(cb.calls.count()).toBe(4);
  78. expect(cb.calls.argsFor(0)).toEqual([4, 0, [4, 5, 2, 15]]);
  79. expect(cb.calls.argsFor(1)).toEqual([5, 1, [4, 5, 2, 15]]);
  80. expect(cb.calls.argsFor(2)).toEqual([2, 2, [4, 5, 2, 15]]);
  81. expect(cb.calls.argsFor(3)).toEqual([15, 3, [4, 5, 2, 15]]);
  82. });
  83. it('should call callback with proper arguments for input data passed as an object with implemented iterator protocol', () => {
  84. const cb = jasmine.createSpy('cb');
  85. cb.and.callFake(() => true);
  86. arrayFilter(iterableObject, cb);
  87. expect(cb.calls.count()).toBe(4);
  88. expect(cb.calls.argsFor(0)).toEqual([2, 0, [2, 6, 3, 1]]);
  89. expect(cb.calls.argsFor(1)).toEqual([6, 1, [2, 6, 3, 1]]);
  90. expect(cb.calls.argsFor(2)).toEqual([3, 2, [2, 6, 3, 1]]);
  91. expect(cb.calls.argsFor(3)).toEqual([1, 3, [2, 6, 3, 1]]);
  92. });
  93. it('should return new an array with filtered values', () => {
  94. const cb = jasmine.createSpy('cb');
  95. const data = [4, 5, 2, 15];
  96. cb.and.callFake(value => value % 2);
  97. arrayFilter(data, cb);
  98. expect(arrayFilter(data, cb)).not.toBe(data);
  99. expect(data).toEqual([4, 5, 2, 15]);
  100. expect(arrayFilter(data, cb)).toEqual([5, 15]);
  101. });
  102. });
  103. //
  104. // Handsontable.helper.arrayFlatten
  105. //
  106. describe('arrayFlatten', () => {
  107. it('should returns the flattened array', () => {
  108. expect(arrayFlatten([1])).toEqual([1]);
  109. expect(arrayFlatten([1, 2, 3, [4, 5, 6]])).toEqual([1, 2, 3, 4, 5, 6]);
  110. expect(arrayFlatten([1, [[[2]]], 3, [[4], [5], [6]]])).toEqual([1, 2, 3, 4, 5, 6]);
  111. });
  112. });
  113. //
  114. // Handsontable.helper.arrayMap
  115. //
  116. describe('arrayMap', () => {
  117. it('should returns the mapped array', () => {
  118. expect(arrayMap([1], a => a + 1)).toEqual([2]);
  119. expect(arrayMap([1, 2, 3], () => '')).toEqual(['', '', '']);
  120. });
  121. it('should call callback with proper arguments for input data passed as an array', () => {
  122. const cb = jasmine.createSpy('cb');
  123. cb.and.callFake(value => value);
  124. arrayMap([4, 5, 2, 15], cb);
  125. expect(cb.calls.count()).toBe(4);
  126. expect(cb.calls.argsFor(0)).toEqual([4, 0, [4, 5, 2, 15]]);
  127. expect(cb.calls.argsFor(1)).toEqual([5, 1, [4, 5, 2, 15]]);
  128. expect(cb.calls.argsFor(2)).toEqual([2, 2, [4, 5, 2, 15]]);
  129. expect(cb.calls.argsFor(3)).toEqual([15, 3, [4, 5, 2, 15]]);
  130. });
  131. it('should call callback with proper arguments for input data passed as an object with implemented iterator protocol', () => {
  132. const cb = jasmine.createSpy('cb');
  133. cb.and.callFake(value => value);
  134. arrayMap(iterableObject, cb);
  135. expect(cb.calls.count()).toBe(4);
  136. expect(cb.calls.argsFor(0)).toEqual([2, 0, [2, 6, 3, 1]]);
  137. expect(cb.calls.argsFor(1)).toEqual([6, 1, [2, 6, 3, 1]]);
  138. expect(cb.calls.argsFor(2)).toEqual([3, 2, [2, 6, 3, 1]]);
  139. expect(cb.calls.argsFor(3)).toEqual([1, 3, [2, 6, 3, 1]]);
  140. });
  141. });
  142. //
  143. // Handsontable.helper.arrayMax
  144. //
  145. describe('arrayMax', () => {
  146. it('should returns the highest number from an array (array of numbers)', () => {
  147. expect(arrayMax([])).toBeUndefined();
  148. expect(arrayMax([0])).toBe(0);
  149. expect(arrayMax([0, 0, 0, -1, 3, 2])).toBe(3);
  150. expect(arrayMax(iterableObject)).toBe(6);
  151. });
  152. it('should returns the highest string from an array (array of strings)', () => {
  153. expect(arrayMax(['b', 'a', 'A', 'z', 'Z', '1'])).toBe('z');
  154. expect(arrayMax(['b', 'a', 'A', 'Z', '1'])).toBe('b');
  155. expect(arrayMax(['a', 'A', 'Z', '1'])).toBe('a');
  156. });
  157. });
  158. //
  159. // Handsontable.helper.arrayMin
  160. //
  161. describe('arrayMin', () => {
  162. it('should returns the lowest number from an array (array of numbers)', () => {
  163. expect(arrayMin([])).toBeUndefined();
  164. expect(arrayMin([0])).toBe(0);
  165. expect(arrayMin([0, 0, 0, -1, 3, 2])).toBe(-1);
  166. expect(arrayMin(iterableObject)).toBe(1);
  167. });
  168. it('should returns the lowest string from an array (array of strings)', () => {
  169. expect(arrayMin(['b', 'a', 'A', 'z', '1'])).toBe('1');
  170. expect(arrayMin(['b', 'a', 'A', 'z'])).toBe('A');
  171. expect(arrayMin(['b', 'a', 'z'])).toBe('a');
  172. });
  173. });
  174. //
  175. // Handsontable.helper.arrayReduce
  176. //
  177. describe('arrayReduce', () => {
  178. it('should call callback with proper arguments for input data passed as an array', () => {
  179. const cb = jasmine.createSpy('cb');
  180. cb.and.callFake((acc, value) => acc + value);
  181. arrayReduce([4, 5, 2, 15], cb, 0);
  182. expect(cb.calls.count()).toBe(4);
  183. expect(cb.calls.argsFor(0)).toEqual([0, 4, 0, [4, 5, 2, 15]]);
  184. expect(cb.calls.argsFor(1)).toEqual([4, 5, 1, [4, 5, 2, 15]]);
  185. expect(cb.calls.argsFor(2)).toEqual([9, 2, 2, [4, 5, 2, 15]]);
  186. expect(cb.calls.argsFor(3)).toEqual([11, 15, 3, [4, 5, 2, 15]]);
  187. });
  188. it('should return sum of all values', () => {
  189. const data = [4, 5, 2, 15];
  190. expect(arrayReduce(data, (acc, value) => acc + value, 0)).toBe(26);
  191. });
  192. it('should return an array with multipled values', () => {
  193. const data = [4, 5, 2, 15];
  194. expect(arrayReduce(data, (acc, value) => {
  195. acc.push(value * value);
  196. return acc;
  197. }, [])).toEqual([16, 25, 4, 225]);
  198. });
  199. it('should call callback with proper arguments for input data passed as an object with implemented iterator protocol', () => {
  200. const cb = jasmine.createSpy('cb');
  201. cb.and.callFake((acc, value) => acc + value);
  202. arrayReduce(iterableObject, cb, 0);
  203. expect(cb.calls.count()).toBe(4);
  204. expect(cb.calls.argsFor(0)).toEqual([0, 2, 0, [2, 6, 3, 1]]);
  205. expect(cb.calls.argsFor(1)).toEqual([2, 6, 1, [2, 6, 3, 1]]);
  206. expect(cb.calls.argsFor(2)).toEqual([8, 3, 2, [2, 6, 3, 1]]);
  207. expect(cb.calls.argsFor(3)).toEqual([11, 1, 3, [2, 6, 3, 1]]);
  208. });
  209. });
  210. //
  211. // Handsontable.helper.arraySum
  212. //
  213. describe('arraySum', () => {
  214. it('should returns the cumulative value', () => {
  215. expect(arraySum([1])).toBe(1);
  216. expect(arraySum([1, 1, 2, 3, 4])).toBe(11);
  217. expect(arraySum([1, 1, 0, 3.1, 4.2])).toBe(9.3);
  218. expect(arraySum(iterableObject)).toBe(12);
  219. });
  220. });
  221. });