PluginHooks.spec.js 8.4 KB

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