PluginHooks.spec.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import Hooks from 'handsontable/pluginHooks';
  2. describe('PluginHooks', () => {
  3. it('should create global empty bucket on construct', () => {
  4. var 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. var hooks = new Hooks();
  12. var 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. var hooks = new Hooks();
  20. var context = {};
  21. var 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. var hooks = new Hooks();
  27. var bucket = hooks.getBucket();
  28. expect(bucket).toBe(hooks.globalBucket);
  29. });
  30. it('should add hooks as array', () => {
  31. var hooks = new Hooks();
  32. var fn1 = function() {};
  33. var fn2 = function() {};
  34. var fn3 = function() {};
  35. var context = {};
  36. var 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. var hooks = new Hooks();
  51. var fn1 = function() {};
  52. var fn2 = function() {};
  53. var context = {};
  54. var 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. var hooks = new Hooks();
  71. var fn1 = function() {};
  72. var fn2 = function() {};
  73. var fn3 = function() {};
  74. var context = {};
  75. var bucket = {};
  76. spyOn(hooks, 'add');
  77. hooks.once('test', [fn1, fn2, fn3, fn3, fn3], context);
  78. expect(fn1.runOnce).toBe(true);
  79. expect(fn2.runOnce).toBe(true);
  80. expect(fn3.runOnce).toBe(true);
  81. expect(hooks.add.calls.count()).toBe(5);
  82. expect(hooks.add.calls.mostRecent()).toEqual({object: hooks, args: ['test', fn3, context], returnValue: void 0});
  83. });
  84. it('should add hook once as function', () => {
  85. var hooks = new Hooks();
  86. var fn1 = function() {};
  87. var fn2 = function() {};
  88. var context = {};
  89. var bucket = {};
  90. spyOn(hooks, 'add');
  91. hooks.once('test', fn1, context);
  92. hooks.once('test', fn2);
  93. expect(fn1.runOnce).toBe(true);
  94. expect(fn2.runOnce).toBe(true);
  95. expect(hooks.add.calls.count()).toBe(2);
  96. expect(hooks.add.calls.argsFor(0)[0]).toBe('test');
  97. expect(hooks.add.calls.argsFor(0)[1]).toBe(fn1);
  98. expect(hooks.add.calls.argsFor(0)[2]).toBe(context);
  99. expect(hooks.add.calls.argsFor(1)[0]).toBe('test');
  100. expect(hooks.add.calls.argsFor(1)[1]).toBe(fn2);
  101. expect(hooks.add.calls.argsFor(1)[2]).toBe(null);
  102. });
  103. it('should remove hook', () => {
  104. var hooks = new Hooks();
  105. var fn1 = function() {};
  106. var fn2 = function() {};
  107. var fn3 = function() {};
  108. var context = {};
  109. var bucket = {test: [fn1, fn2]};
  110. var result;
  111. spyOn(hooks, 'getBucket').and.returnValue(bucket);
  112. result = hooks.remove('test2', fn1);
  113. expect(result).toBe(false);
  114. expect(bucket.test.length).toBe(2);
  115. result = hooks.remove('test', fn3);
  116. expect(result).toBe(false);
  117. expect(bucket.test.length).toBe(2);
  118. result = hooks.remove('test', fn1);
  119. expect(result).toBe(true);
  120. expect(bucket.test[0].skip).toBe(true);
  121. expect(bucket.test.length).toBe(2);
  122. });
  123. it('should run hook', () => {
  124. var hooks = new Hooks();
  125. var fn1 = jasmine.createSpy('fn1').and.returnValue('Foo');
  126. var fn2 = jasmine.createSpy('fn2').and.returnValue('Bar');
  127. var fn3 = jasmine.createSpy('fn3');
  128. var context = {};
  129. var bucket = {test: [fn1, fn2]};
  130. var result;
  131. hooks.globalBucket.test = [fn3];
  132. spyOn(hooks, 'getBucket').and.returnValue(bucket);
  133. spyOn(hooks, 'remove');
  134. result = hooks.run(context, 'test');
  135. expect(result).toBe('Bar');
  136. expect(hooks.getBucket).toHaveBeenCalledWith(context);
  137. expect(hooks.remove).not.toHaveBeenCalled();
  138. expect(fn1).toHaveBeenCalled();
  139. expect(fn2).toHaveBeenCalled();
  140. expect(fn3).toHaveBeenCalled();
  141. fn1.calls.reset();
  142. fn1.runOnce = true;
  143. fn2.calls.reset();
  144. fn3.calls.reset();
  145. result = hooks.run(context, 'test', 1, 2, 'AB');
  146. expect(result).toBe('Bar');
  147. expect(hooks.remove).toHaveBeenCalledWith('test', fn1, context);
  148. expect(fn1).toHaveBeenCalledWith(1, 2, 'AB', void 0, void 0, void 0);
  149. expect(fn2).toHaveBeenCalledWith('Foo', 2, 'AB', void 0, void 0, void 0);
  150. expect(fn3).toHaveBeenCalledWith(1, 2, 'AB', void 0, void 0, void 0);
  151. });
  152. it('should run hooks added as once', () => {
  153. var hooks = new Hooks();
  154. var fn1 = jasmine.createSpy('fn1').and.returnValue('Foo');
  155. var fn2 = jasmine.createSpy('fn2').and.returnValue('Bar');
  156. var fn3 = jasmine.createSpy('fn3');
  157. var context = {pluginHookBucket: {test: [fn1, fn2]}};
  158. var result;
  159. fn1.runOnce = true;
  160. fn2.runOnce = true;
  161. fn3.runOnce = true;
  162. hooks.globalBucket = {test: [fn3]};
  163. hooks.run(context, 'test');
  164. hooks.run(context, 'test');
  165. hooks.run(context, 'test');
  166. expect(fn1.calls.count()).toBe(1);
  167. expect(fn2.calls.count()).toBe(1);
  168. expect(fn3.calls.count()).toBe(1);
  169. });
  170. it('should destroy hooks', () => {
  171. var hooks = new Hooks();
  172. var fn1 = jasmine.createSpy('fn1').and.returnValue('Foo');
  173. var fn2 = jasmine.createSpy('fn2').and.returnValue('Bar');
  174. var fn3 = jasmine.createSpy('fn3');
  175. var context = {};
  176. var bucket = {test: [fn1, fn2, fn3], test2: [fn3]};
  177. spyOn(hooks, 'getBucket').and.returnValue(bucket);
  178. hooks.destroy(context);
  179. expect(hooks.getBucket).toHaveBeenCalledWith(context);
  180. expect(bucket.test.length).toBe(0);
  181. expect(bucket.test2.length).toBe(0);
  182. });
  183. it('should register hook', () => {
  184. var hooks = new Hooks();
  185. spyOn(hooks, 'isRegistered').and.returnValue(false);
  186. hooks.register('test');
  187. expect(hooks.isRegistered).toHaveBeenCalledWith('test');
  188. expect(hooks.getRegistered().indexOf('test')).toBeGreaterThan(-1);
  189. hooks.isRegistered.and.returnValue(true);
  190. hooks.register('test2');
  191. expect(hooks.isRegistered).toHaveBeenCalledWith('test2');
  192. expect(hooks.getRegistered().indexOf('test2')).toBe(-1);
  193. });
  194. it('should deregister hook', () => {
  195. var hooks = new Hooks();
  196. spyOn(hooks, 'isRegistered').and.returnValue(false);
  197. hooks.register('test');
  198. hooks.deregister('test');
  199. expect(hooks.isRegistered).toHaveBeenCalledWith('test');
  200. expect(hooks.getRegistered().indexOf('test')).toBeGreaterThan(-1);
  201. hooks.isRegistered.and.returnValue(true);
  202. hooks.deregister('test2');
  203. expect(hooks.isRegistered).toHaveBeenCalledWith('test2');
  204. expect(hooks.getRegistered().indexOf('test2')).toBe(-1);
  205. });
  206. it('should returns `true` if hooks is registered', () => {
  207. var hooks = new Hooks();
  208. hooks.register('test');
  209. expect(hooks.isRegistered('test')).toBe(true);
  210. expect(hooks.isRegistered('test2')).toBe(false);
  211. });
  212. it('should returns array of registered hooks', () => {
  213. var hooks = new Hooks();
  214. expect(hooks.getRegistered().length).toBeGreaterThan(0);
  215. });
  216. it('should returns `true` if at least one listener was added to the hook', () => {
  217. var hooks = new Hooks();
  218. var context = {};
  219. expect(hooks.has('beforeInit', context)).toBe(false);
  220. hooks.add('beforeInit', () => {}, context);
  221. expect(hooks.has('beforeInit', context)).toBe(true);
  222. });
  223. });