Function.spec.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import {
  2. throttle,
  3. throttleAfterHits,
  4. debounce,
  5. pipe,
  6. partial,
  7. curry,
  8. curryRight,
  9. isFunction,
  10. } from 'handsontable/helpers/function';
  11. import { sleep } from '../../helpers/common';
  12. describe('Function helper', () => {
  13. //
  14. // Handsontable.helper.throttle
  15. //
  16. describe('throttle', () => {
  17. it('should returns new function with applied throttling functionality', async() => {
  18. const spy = jasmine.createSpy();
  19. const throttled = throttle(spy, 200);
  20. throttled();
  21. throttled();
  22. throttled();
  23. throttled();
  24. throttled();
  25. expect(spy.calls.count()).toBe(1);
  26. await sleep(100);
  27. throttled();
  28. throttled();
  29. expect(spy.calls.count()).toBe(1);
  30. await sleep(300);
  31. throttled();
  32. throttled();
  33. throttled();
  34. throttled();
  35. expect(spy.calls.count()).toBe(3);
  36. await sleep(300);
  37. expect(spy.calls.count()).toBe(4);
  38. });
  39. });
  40. //
  41. // Handsontable.helper.throttleAfterHits
  42. //
  43. describe('throttleAfterHits', () => {
  44. it('should returns new function with applied throttling functionality', async() => {
  45. const spy = jasmine.createSpy();
  46. const throttled = throttleAfterHits(spy, 200, 5);
  47. throttled();
  48. throttled();
  49. throttled();
  50. throttled();
  51. throttled();
  52. expect(spy.calls.count()).toBe(5);
  53. await sleep(100);
  54. throttled();
  55. throttled();
  56. expect(spy.calls.count()).toBe(6);
  57. await sleep(300);
  58. throttled();
  59. throttled();
  60. throttled();
  61. throttled();
  62. expect(spy.calls.count()).toBe(8);
  63. await sleep(500);
  64. expect(spy.calls.count()).toBe(9);
  65. });
  66. });
  67. //
  68. // Handsontable.helper.debounce
  69. //
  70. describe('debounce', () => {
  71. it('should returns new function with applied debouncing functionality', async() => {
  72. const spy = jasmine.createSpy();
  73. const debounced = debounce(spy, 200);
  74. debounced();
  75. debounced();
  76. debounced();
  77. debounced();
  78. debounced();
  79. expect(spy.calls.count()).toBe(0);
  80. await sleep(100);
  81. debounced();
  82. debounced();
  83. expect(spy.calls.count()).toBe(0);
  84. await sleep(300);
  85. debounced();
  86. debounced();
  87. debounced();
  88. debounced();
  89. expect(spy.calls.count()).toBe(1);
  90. await sleep(500);
  91. expect(spy.calls.count()).toBe(2);
  92. });
  93. });
  94. //
  95. // Handsontable.helper.pipe
  96. //
  97. describe('pipe', () => {
  98. it('should returns new function with piped all passed functions', () => {
  99. const spyObject = {
  100. test1: a => a + 1,
  101. test2: a => a + 1,
  102. test3: a => a + 1,
  103. test4: a => a + 1,
  104. };
  105. const spyTest1 = jest.spyOn(spyObject, 'test1');
  106. const spyTest2 = jest.spyOn(spyObject, 'test2');
  107. const spyTest3 = jest.spyOn(spyObject, 'test3');
  108. const spyTest4 = jest.spyOn(spyObject, 'test4');
  109. const piped = pipe(spyTest1, spyTest2, spyTest3, spyTest4);
  110. const result = piped(1, 2, 'foo');
  111. expect(spyTest1).toHaveBeenCalledWith(1, 2, 'foo');
  112. expect(spyTest2).toHaveBeenCalledWith(2);
  113. expect(spyTest3).toHaveBeenCalledWith(3);
  114. expect(spyTest4).toHaveBeenCalledWith(4);
  115. expect(result).toBe(5);
  116. });
  117. });
  118. //
  119. // Handsontable.helper.partial
  120. //
  121. describe('partial', () => {
  122. it('should returns new function with cached arguments', () => {
  123. const spyObject = {
  124. test1: (a, b, c) => (a + b) + c,
  125. };
  126. const spyTest1 = jest.spyOn(spyObject, 'test1');
  127. let partialized = partial(spyTest1, 1, 2);
  128. expect(partialized('foo')).toBe('3foo');
  129. partialized = partial(spyTest1);
  130. expect(partialized(1, 2, 'foo')).toBe('3foo');
  131. partialized = partial(spyTest1, 1, 2, 3);
  132. expect(partialized('foo')).toBe(6);
  133. });
  134. });
  135. //
  136. // Handsontable.helper.curry
  137. //
  138. describe('curry', () => {
  139. it('should returns new function with cached arguments (collecting arguments from the left to the right)', () => {
  140. const fn = (a, b, c) => (a + b) + c;
  141. const curried = curry(fn);
  142. expect(curried(1, 2, 'foo')).toBe('3foo');
  143. expect(curried(1)(2)('foo')).toBe('3foo');
  144. expect(curried(1, 2)(3)).toBe(6);
  145. });
  146. });
  147. //
  148. // Handsontable.helper.curryRight
  149. //
  150. describe('curryRight', () => {
  151. it('should returns new function with cached arguments (collecting arguments from the right to the left)', () => {
  152. const fn = (a, b, c) => (a + b) + c;
  153. const curried = curryRight(fn);
  154. expect(curried('foo', 2, 1)).toBe('3foo');
  155. expect(curried(1, 2, 'foo')).toBe('foo21');
  156. expect(curried(1)(2)('foo')).toBe('3foo');
  157. expect(curried(1, 2)(3)).toBe(6);
  158. });
  159. });
  160. //
  161. // Handsontable.helper.isFunction
  162. //
  163. describe('isFunction', () => {
  164. it('should correctly detect function', () => {
  165. const toCheck = [
  166. function() {},
  167. { id() {} },
  168. 1,
  169. 'text',
  170. /^\d+$/,
  171. true
  172. ];
  173. function namedFunc() {}
  174. expect(isFunction(toCheck[0])).toBeTruthy();
  175. expect(isFunction(toCheck[1].id)).toBeTruthy();
  176. expect(isFunction(namedFunc)).toBeTruthy();
  177. expect(isFunction(() => {})).toBeTruthy();
  178. expect(isFunction(toCheck)).toBeFalsy();
  179. expect(isFunction(toCheck[1])).toBeFalsy();
  180. expect(isFunction(toCheck[2])).toBeFalsy();
  181. expect(isFunction(toCheck[3])).toBeFalsy();
  182. expect(isFunction(toCheck[4])).toBeFalsy();
  183. expect(isFunction(toCheck[5])).toBeFalsy();
  184. });
  185. });
  186. });