Array.spec.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {
  2. arrayFlatten,
  3. arrayAvg,
  4. arraySum,
  5. arrayMap,
  6. arrayMin,
  7. arrayMax,
  8. } from 'handsontable/helpers/array';
  9. describe('Array helper', () => {
  10. //
  11. // Handsontable.helper.arrayFlatten
  12. //
  13. describe('arrayFlatten', () => {
  14. it('should returns the flattened array', () => {
  15. expect(arrayFlatten([1])).toEqual([1]);
  16. expect(arrayFlatten([1, 2, 3, [4, 5, 6]])).toEqual([1, 2, 3, 4, 5, 6]);
  17. expect(arrayFlatten([1, [[[2]]], 3, [[4], [5], [6]]])).toEqual([1, 2, 3, 4, 5, 6]);
  18. });
  19. });
  20. //
  21. // Handsontable.helper.arrayAvg
  22. //
  23. describe('arrayAvg', () => {
  24. it('should returns the average value', () => {
  25. expect(arrayAvg([1])).toBe(1);
  26. expect(arrayAvg([1, 1, 2, 3, 4])).toBe(2.2);
  27. });
  28. });
  29. //
  30. // Handsontable.helper.arraySum
  31. //
  32. describe('arraySum', () => {
  33. it('should returns the cumulative value', () => {
  34. expect(arraySum([1])).toBe(1);
  35. expect(arraySum([1, 1, 2, 3, 4])).toBe(11);
  36. expect(arraySum([1, 1, 0, 3.1, 4.2])).toBe(9.3);
  37. });
  38. });
  39. //
  40. // Handsontable.helper.arrayMap
  41. //
  42. describe('arrayMap', () => {
  43. it('should returns the mapped array', () => {
  44. expect(arrayMap([1], (a) => a + 1)).toEqual([2]);
  45. expect(arrayMap([1, 2, 3], () => '')).toEqual(['', '', '']);
  46. });
  47. });
  48. //
  49. // Handsontable.helper.arrayMin
  50. //
  51. describe('arrayMin', () => {
  52. it('should returns the lowest number from an array (array of numbers)', () => {
  53. expect(arrayMin([])).toBeUndefined();
  54. expect(arrayMin([0])).toBe(0);
  55. expect(arrayMin([0, 0, 0, -1, 3, 2])).toBe(-1);
  56. });
  57. it('should returns the lowest string from an array (array of strings)', () => {
  58. expect(arrayMin(['b', 'a', 'A', 'z', '1'])).toBe('1');
  59. expect(arrayMin(['b', 'a', 'A', 'z'])).toBe('A');
  60. expect(arrayMin(['b', 'a', 'z'])).toBe('a');
  61. });
  62. });
  63. //
  64. // Handsontable.helper.arrayMax
  65. //
  66. describe('arrayMax', () => {
  67. it('should returns the highest number from an array (array of numbers)', () => {
  68. expect(arrayMax([])).toBeUndefined();
  69. expect(arrayMax([0])).toBe(0);
  70. expect(arrayMax([0, 0, 0, -1, 3, 2])).toBe(3);
  71. });
  72. it('should returns the highest string from an array (array of strings)', () => {
  73. expect(arrayMax(['b', 'a', 'A', 'z', 'Z', '1'])).toBe('z');
  74. expect(arrayMax(['b', 'a', 'A', 'Z', '1'])).toBe('b');
  75. expect(arrayMax(['a', 'A', 'Z', '1'])).toBe('a');
  76. });
  77. });
  78. });