PluginHooks.spec.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. import Hooks from 'handsontable/pluginHooks';
  2. describe('PluginHooks', () => {
  3. it('should create global empty bucket on construct', () => {
  4. const hooks = new Hooks();
  5. expect(hooks.globalBucket).toBeDefined();
  6. expect(hooks.globalBucket.afterInit).toEqual([]);
  7. expect(hooks.globalBucket.beforeInit).toEqual([]);
  8. expect(hooks.globalBucket.init).toEqual([]);
  9. });
  10. it('should create empty object (bucket) on createEmptyBucket call', () => {
  11. const hooks = new Hooks();
  12. const bucket = hooks.createEmptyBucket();
  13. expect(bucket.afterInit).toEqual([]);
  14. expect(bucket.beforeInit).toEqual([]);
  15. expect(bucket.init).toEqual([]);
  16. expect(bucket).not.toBe(hooks.createEmptyBucket());
  17. });
  18. it('should create and get local bucket when context is passed', () => {
  19. const hooks = new Hooks();
  20. const context = {};
  21. const bucket = hooks.getBucket(context);
  22. expect(context.pluginHookBucket).toBeDefined();
  23. expect(context.pluginHookBucket).toBe(bucket);
  24. });
  25. it('should get global bucket when context is empty', () => {
  26. const hooks = new Hooks();
  27. const bucket = hooks.getBucket();
  28. expect(bucket).toBe(hooks.globalBucket);
  29. });
  30. it('should add hooks as array', () => {
  31. const hooks = new Hooks();
  32. const fn1 = function() {};
  33. const fn2 = function() {};
  34. const fn3 = function() {};
  35. const context = {};
  36. const bucket = {};
  37. spyOn(hooks, 'getBucket').and.returnValue(bucket);
  38. spyOn(hooks, 'register');
  39. hooks.add('test', [fn1, fn2, fn3, fn3, fn3], context);
  40. expect(hooks.getBucket.calls.count()).toBe(5);
  41. expect(hooks.getBucket.calls.mostRecent()).toEqual({ object: hooks, args: [{}], returnValue: bucket });
  42. expect(hooks.register.calls.count()).toBe(1);
  43. expect(hooks.register.calls.mostRecent()).toEqual({ object: hooks, args: ['test'], returnValue: void 0 });
  44. expect(bucket.test.length).toBe(3);
  45. expect(bucket.test[0]).toBe(fn1);
  46. expect(bucket.test[1]).toBe(fn2);
  47. expect(bucket.test[2]).toBe(fn3);
  48. });
  49. it('should add hook as function', () => {
  50. const hooks = new Hooks();
  51. const fn1 = function() {};
  52. const fn2 = function() {};
  53. const context = {};
  54. const bucket = { test: [] };
  55. spyOn(hooks, 'getBucket').and.returnValue(bucket);
  56. spyOn(hooks, 'register');
  57. hooks.add('test', fn1, context);
  58. hooks.add('test', fn1);
  59. hooks.add('test', fn2, context);
  60. expect(hooks.getBucket.calls.count()).toBe(3);
  61. expect(hooks.getBucket.calls.argsFor(0)[0]).toBe(context);
  62. expect(hooks.getBucket.calls.argsFor(1)[0]).toBe(null);
  63. expect(hooks.getBucket.calls.argsFor(2)[0]).toBe(context);
  64. expect(hooks.register).not.toHaveBeenCalled();
  65. expect(bucket.test.length).toBe(2);
  66. expect(bucket.test[0]).toBe(fn1);
  67. expect(bucket.test[1]).toBe(fn2);
  68. });
  69. it('should add hook once as array', () => {
  70. const hooks = new Hooks();
  71. const fn1 = function() {};
  72. const fn2 = function() {};
  73. const fn3 = function() {};
  74. const context = {};
  75. spyOn(hooks, 'add');
  76. hooks.once('test', [fn1, fn2, fn3, fn3, fn3], context);
  77. expect(fn1.runOnce).toBe(true);
  78. expect(fn2.runOnce).toBe(true);
  79. expect(fn3.runOnce).toBe(true);
  80. expect(hooks.add.calls.count()).toBe(5);
  81. expect(hooks.add.calls.mostRecent()).toEqual({ object: hooks, args: ['test', fn3, context], returnValue: void 0 });
  82. });
  83. it('should add hook once as function', () => {
  84. const hooks = new Hooks();
  85. const fn1 = function() {};
  86. const fn2 = function() {};
  87. const context = {};
  88. spyOn(hooks, 'add');
  89. hooks.once('test', fn1, context);
  90. hooks.once('test', fn2);
  91. expect(fn1.runOnce).toBe(true);
  92. expect(fn2.runOnce).toBe(true);
  93. expect(hooks.add.calls.count()).toBe(2);
  94. expect(hooks.add.calls.argsFor(0)[0]).toBe('test');
  95. expect(hooks.add.calls.argsFor(0)[1]).toBe(fn1);
  96. expect(hooks.add.calls.argsFor(0)[2]).toBe(context);
  97. expect(hooks.add.calls.argsFor(1)[0]).toBe('test');
  98. expect(hooks.add.calls.argsFor(1)[1]).toBe(fn2);
  99. expect(hooks.add.calls.argsFor(1)[2]).toBe(null);
  100. });
  101. it('should remove hook', () => {
  102. const hooks = new Hooks();
  103. const fn1 = function() {};
  104. const fn2 = function() {};
  105. const fn3 = function() {};
  106. const bucket = { test: [fn1, fn2] };
  107. let result;
  108. spyOn(hooks, 'getBucket').and.returnValue(bucket);
  109. result = hooks.remove('test2', fn1);
  110. expect(result).toBe(false);
  111. expect(bucket.test.length).toBe(2);
  112. result = hooks.remove('test', fn3);
  113. expect(result).toBe(false);
  114. expect(bucket.test.length).toBe(2);
  115. result = hooks.remove('test', fn1);
  116. expect(result).toBe(true);
  117. expect(bucket.test[0].skip).toBe(true);
  118. expect(bucket.test.length).toBe(2);
  119. });
  120. it('should run hook', () => {
  121. const hooks = new Hooks();
  122. const fn1 = jasmine.createSpy('fn1').and.returnValue('Foo');
  123. const fn2 = jasmine.createSpy('fn2').and.returnValue('Bar');
  124. const fn3 = jasmine.createSpy('fn3');
  125. const context = {};
  126. const bucket = { test: [fn1, fn2] };
  127. let result;
  128. hooks.globalBucket.test = [fn3];
  129. spyOn(hooks, 'getBucket').and.returnValue(bucket);
  130. spyOn(hooks, 'remove');
  131. result = hooks.run(context, 'test');
  132. expect(result).toBe('Bar');
  133. expect(hooks.getBucket).toHaveBeenCalledWith(context);
  134. expect(hooks.remove).not.toHaveBeenCalled();
  135. expect(fn1).toHaveBeenCalled();
  136. expect(fn2).toHaveBeenCalled();
  137. expect(fn3).toHaveBeenCalled();
  138. fn1.calls.reset();
  139. fn1.runOnce = true;
  140. fn2.calls.reset();
  141. fn3.calls.reset();
  142. result = hooks.run(context, 'test', 1, 2, 'AB');
  143. expect(result).toBe('Bar');
  144. expect(hooks.remove).toHaveBeenCalledWith('test', fn1, context);
  145. expect(fn1).toHaveBeenCalledWith(1, 2, 'AB', void 0, void 0, void 0);
  146. expect(fn2).toHaveBeenCalledWith('Foo', 2, 'AB', void 0, void 0, void 0);
  147. expect(fn3).toHaveBeenCalledWith(1, 2, 'AB', void 0, void 0, void 0);
  148. });
  149. it('should run hooks added as once', () => {
  150. const hooks = new Hooks();
  151. const fn1 = jasmine.createSpy('fn1').and.returnValue('Foo');
  152. const fn2 = jasmine.createSpy('fn2').and.returnValue('Bar');
  153. const fn3 = jasmine.createSpy('fn3');
  154. const context = { pluginHookBucket: { test: [fn1, fn2] } };
  155. fn1.runOnce = true;
  156. fn2.runOnce = true;
  157. fn3.runOnce = true;
  158. hooks.globalBucket = { test: [fn3] };
  159. hooks.run(context, 'test');
  160. hooks.run(context, 'test');
  161. hooks.run(context, 'test');
  162. expect(fn1.calls.count()).toBe(1);
  163. expect(fn2.calls.count()).toBe(1);
  164. expect(fn3.calls.count()).toBe(1);
  165. });
  166. it('should destroy hooks', () => {
  167. const hooks = new Hooks();
  168. const fn1 = jasmine.createSpy('fn1').and.returnValue('Foo');
  169. const fn2 = jasmine.createSpy('fn2').and.returnValue('Bar');
  170. const fn3 = jasmine.createSpy('fn3');
  171. const context = {};
  172. const bucket = { test: [fn1, fn2, fn3], test2: [fn3] };
  173. spyOn(hooks, 'getBucket').and.returnValue(bucket);
  174. hooks.destroy(context);
  175. expect(hooks.getBucket).toHaveBeenCalledWith(context);
  176. expect(bucket.test.length).toBe(0);
  177. expect(bucket.test2.length).toBe(0);
  178. });
  179. it('should register hook', () => {
  180. const hooks = new Hooks();
  181. spyOn(hooks, 'isRegistered').and.returnValue(false);
  182. hooks.register('test');
  183. expect(hooks.isRegistered).toHaveBeenCalledWith('test');
  184. expect(hooks.getRegistered().indexOf('test')).toBeGreaterThan(-1);
  185. hooks.isRegistered.and.returnValue(true);
  186. hooks.register('test2');
  187. expect(hooks.isRegistered).toHaveBeenCalledWith('test2');
  188. expect(hooks.getRegistered().indexOf('test2')).toBe(-1);
  189. });
  190. it('should deregister hook', () => {
  191. const hooks = new Hooks();
  192. spyOn(hooks, 'isRegistered').and.returnValue(false);
  193. hooks.register('test');
  194. hooks.deregister('test');
  195. expect(hooks.isRegistered).toHaveBeenCalledWith('test');
  196. expect(hooks.getRegistered().indexOf('test')).toBeGreaterThan(-1);
  197. hooks.isRegistered.and.returnValue(true);
  198. hooks.deregister('test2');
  199. expect(hooks.isRegistered).toHaveBeenCalledWith('test2');
  200. expect(hooks.getRegistered().indexOf('test2')).toBe(-1);
  201. });
  202. it('should returns `true` if hooks is registered', () => {
  203. const hooks = new Hooks();
  204. hooks.register('test');
  205. expect(hooks.isRegistered('test')).toBe(true);
  206. expect(hooks.isRegistered('test2')).toBe(false);
  207. });
  208. it('should returns array of registered hooks', () => {
  209. const hooks = new Hooks();
  210. expect(hooks.getRegistered().length).toBeGreaterThan(0);
  211. });
  212. it('should returns `true` if at least one listener was added to the hook', () => {
  213. const hooks = new Hooks();
  214. const context = {};
  215. expect(hooks.has('beforeInit', context)).toBe(false);
  216. hooks.add('beforeInit', () => {}, context);
  217. expect(hooks.has('beforeInit', context)).toBe(true);
  218. });
  219. });