Function.spec.js 5.5 KB

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