PluginHooks.spec.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. describe('PluginHooks', () => {
  2. const id = 'testContainer';
  3. beforeEach(function() {
  4. this.$container = $(`<div id="${id}"></div>`).appendTo('body');
  5. });
  6. afterEach(function() {
  7. if (this.$container) {
  8. destroy();
  9. this.$container.remove();
  10. }
  11. });
  12. it('should add a many local hooks at init (as array)', () => {
  13. const handler1 = jasmine.createSpy('handler1');
  14. const handler2 = jasmine.createSpy('handler2');
  15. const handler3 = jasmine.createSpy('handler3');
  16. handsontable({
  17. afterInit: [handler1, handler2, handler3]
  18. });
  19. expect(handler1).toHaveBeenCalled();
  20. expect(handler2).toHaveBeenCalled();
  21. expect(handler3).toHaveBeenCalled();
  22. });
  23. it('should remove a global hook', () => {
  24. let test = 0;
  25. const hook = function() {
  26. test = 5;
  27. };
  28. Handsontable.hooks.add('afterInit', hook);
  29. Handsontable.hooks.remove('afterInit', hook);
  30. handsontable();
  31. expect(test).toEqual(0);
  32. });
  33. it('should remove a local hook', () => {
  34. let test = 0;
  35. const hook = function() {
  36. test = 5;
  37. };
  38. handsontable();
  39. getInstance().addHook('afterInit', hook);
  40. getInstance().removeHook('afterInit', hook);
  41. expect(test).toEqual(0);
  42. });
  43. it('should run global hook', () => {
  44. let test = 0;
  45. Handsontable.hooks.add('afterInit', () => {
  46. test = 5;
  47. });
  48. handsontable();
  49. expect(test).toEqual(5);
  50. });
  51. it('should run local hook', () => {
  52. let test = 0;
  53. handsontable();
  54. getInstance().addHook('myHook', () => {
  55. test += 5;
  56. });
  57. getInstance().runHooks('myHook');
  58. getInstance().runHooks('myHook');
  59. expect(test).toEqual(10);
  60. });
  61. it('should run local hook once', () => {
  62. let test = 0;
  63. handsontable();
  64. getInstance().addHookOnce('myHook', () => {
  65. test += 5;
  66. });
  67. getInstance().runHooks('myHook');
  68. getInstance().runHooks('myHook');
  69. expect(test).toEqual(5);
  70. });
  71. it('should run all hooks', () => {
  72. let test = 0;
  73. Handsontable.hooks.add('afterInit', () => {
  74. test += 5;
  75. });
  76. handsontable({
  77. afterInit() {
  78. test += 5;
  79. }
  80. });
  81. expect(test).toEqual(10);
  82. });
  83. it('list of all avaliable plugin hooks should be exposed as a public method', () => {
  84. const hooks = Handsontable.hooks.getRegistered(); // this is used in demo/callbacks.html
  85. expect(hooks.indexOf('beforeInit')).toBeGreaterThan(-1);
  86. });
  87. it('should add a local hook with addHooks method', () => {
  88. const hot1 = handsontable();
  89. let test = 0;
  90. hot1.addHook('myHook', () => {
  91. test += 5;
  92. });
  93. hot1.runHooks('myHook');
  94. expect(test).toEqual(5);
  95. });
  96. it('should remove a local hook with removeHook method', () => {
  97. const hot1 = handsontable();
  98. let test = 0;
  99. const handler = function() {
  100. test += 5;
  101. };
  102. hot1.addHook('myHook', handler);
  103. hot1.runHooks('myHook');
  104. hot1.runHooks('myHook');
  105. expect(test).toEqual(10);
  106. hot1.removeHook('myHook', handler);
  107. hot1.runHooks('myHook');
  108. expect(test).toEqual(10);
  109. });
  110. it('should add a local hook with addHookOnce method and run it just once', () => {
  111. const hot1 = handsontable();
  112. let test = 0;
  113. const handler = function() {
  114. test += 5;
  115. };
  116. hot1.addHookOnce('myHook', handler);
  117. hot1.runHooks('myHook');
  118. hot1.runHooks('myHook');
  119. expect(test).toEqual(5);
  120. });
  121. it('should run hook with runHooks and return value', () => {
  122. const hot = handsontable();
  123. const handler = function() {
  124. return 5;
  125. };
  126. hot.addHook('myHook', handler);
  127. expect(hot.runHooks('myHook')).toEqual(5);
  128. });
  129. it('should run two "once" hooks in desired order', () => {
  130. const hot = handsontable();
  131. const arr = [];
  132. hot.addHookOnce('myHook', () => {
  133. arr.push(1);
  134. });
  135. hot.addHookOnce('myHook', () => {
  136. arr.push(2);
  137. });
  138. hot.runHooks('myHook');
  139. expect(arr).toEqual([1, 2]);
  140. });
  141. it('should execute two "once" hooks in desired order', () => {
  142. const hot = handsontable();
  143. const str = 'a';
  144. hot.addHookOnce('myHook', value => `${value}b`);
  145. hot.addHookOnce('myHook', value => `${value}c`);
  146. expect(hot.runHooks('myHook', str)).toEqual('abc');
  147. });
  148. it('adding same hook twice should register it only once (without an error)', () => {
  149. let i = 0;
  150. const fn = function() {
  151. i += 1;
  152. };
  153. const hot = handsontable({
  154. afterOnCellMouseOver: fn
  155. });
  156. hot.getInstance().updateSettings({ afterOnCellMouseOver: fn });
  157. hot.runHooks('afterOnCellMouseOver');
  158. expect(i).toEqual(1);
  159. });
  160. it('should mark the hook callbacks added with Handsontable initialization', () => {
  161. const fn = function() {};
  162. const fn2 = function() {};
  163. const hot = handsontable({
  164. afterChange: fn
  165. });
  166. hot.addHook('afterChange', fn2);
  167. expect(fn.initialHook).toEqual(true);
  168. expect(fn2.initialHook).toEqual(void 0);
  169. });
  170. it('should mark the hook callbacks added using the updateSettings method', () => {
  171. const fn = function() {};
  172. const fn2 = function() {};
  173. const hot = handsontable();
  174. hot.updateSettings({
  175. afterChange: fn
  176. });
  177. hot.addHook('afterChange', fn2);
  178. expect(fn.initialHook).toEqual(true);
  179. expect(fn2.initialHook).toEqual(void 0);
  180. });
  181. it('should replace the existing hook callbacks, if they\'re updated using the updateSettings method (when there was a hook ' +
  182. 'already declared in the initialization)', () => {
  183. const fn = function() {};
  184. const fn2 = function() {};
  185. const hot = handsontable({
  186. afterGetCellMeta: fn
  187. });
  188. const initialCallbackCount = hot.pluginHookBucket.afterGetCellMeta.length;
  189. hot.updateSettings({
  190. afterGetCellMeta() {
  191. return { a: 'another function' };
  192. }
  193. });
  194. hot.updateSettings({
  195. afterGetCellMeta() {
  196. return { a: 'yet another function' };
  197. }
  198. });
  199. hot.updateSettings({
  200. afterGetCellMeta: fn2
  201. });
  202. expect(hot.pluginHookBucket.afterGetCellMeta.length).toEqual(initialCallbackCount);
  203. });
  204. it('should replace the existing hook callbacks, if they\'re updated using the updateSettings method', () => {
  205. const fn = function() {};
  206. const fn2 = function() {};
  207. const hot = handsontable();
  208. hot.addHook('afterGetCellMeta', () => 'doesn\'t matter 1');
  209. hot.addHook('afterGetCellMeta', () => 'doesn\'t matter 2');
  210. hot.addHook('afterGetCellMeta', () => 'doesn\'t matter 3');
  211. hot.updateSettings({
  212. afterGetCellMeta: fn
  213. });
  214. const initialCallbackCount = hot.pluginHookBucket.afterGetCellMeta.length;
  215. hot.updateSettings({
  216. afterGetCellMeta() {
  217. return { a: 'another function' };
  218. }
  219. });
  220. hot.updateSettings({
  221. afterGetCellMeta() {
  222. return { a: 'yet another function' };
  223. }
  224. });
  225. hot.updateSettings({
  226. afterGetCellMeta: fn2
  227. });
  228. expect(hot.pluginHookBucket.afterGetCellMeta.length).toEqual(initialCallbackCount);
  229. });
  230. it('should NOT replace existing hook callbacks, if the\'re added using the addHook method', () => {
  231. const fn = function() {};
  232. const fn2 = function() {};
  233. const hot = handsontable();
  234. hot.updateSettings({
  235. afterGetCellMeta: fn
  236. });
  237. const initialCallbackCount = hot.pluginHookBucket.afterGetCellMeta.length;
  238. hot.addHook('afterGetCellMeta', () => ({ a: 'another function' }));
  239. hot.addHook('afterGetCellMeta', () => ({ a: 'yet another function' }));
  240. hot.addHook('afterGetCellMeta', fn2);
  241. // should not add this one, as it's a duplicate
  242. hot.addHook('afterGetCellMeta', fn);
  243. expect(hot.pluginHookBucket.afterGetCellMeta.length).toEqual(initialCallbackCount + 3);
  244. });
  245. describe('controlling handler queue execution', () => {
  246. it('should execute all handlers if none of them hasn\'t skipped', () => {
  247. const handler1 = jasmine.createSpy('handler1');
  248. const handler2 = jasmine.createSpy('handler2');
  249. const handler3 = jasmine.createSpy('handler3');
  250. const hot = handsontable();
  251. hot.addHook('fakeEvent', handler1);
  252. hot.addHook('fakeEvent', handler2);
  253. hot.addHook('fakeEvent', handler3);
  254. expect(handler1).not.toHaveBeenCalled();
  255. expect(handler2).not.toHaveBeenCalled();
  256. expect(handler3).not.toHaveBeenCalled();
  257. hot.runHooks('fakeEvent');
  258. expect(handler1).toHaveBeenCalled();
  259. expect(handler2).toHaveBeenCalled();
  260. expect(handler3).toHaveBeenCalled();
  261. });
  262. });
  263. });