123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- describe('PluginHooks', () => {
- var id = 'testContainer';
- beforeEach(function() {
- this.$container = $(`<div id="${id}"></div>`).appendTo('body');
- });
- afterEach(function() {
- if (this.$container) {
- destroy();
- this.$container.remove();
- }
- });
- it('should add a many local hooks at init (as array)', () => {
- var handler1 = jasmine.createSpy('handler1');
- var handler2 = jasmine.createSpy('handler2');
- var handler3 = jasmine.createSpy('handler3');
- handsontable({
- afterInit: [handler1, handler2, handler3]
- });
- expect(handler1).toHaveBeenCalled();
- expect(handler2).toHaveBeenCalled();
- expect(handler3).toHaveBeenCalled();
- });
- it('should remove a global hook', () => {
- var
- test = 0,
- hook = function() {
- test = 5;
- };
- Handsontable.hooks.add('afterInit', hook);
- Handsontable.hooks.remove('afterInit', hook);
- handsontable();
- expect(test).toEqual(0);
- });
- it('should remove a local hook', () => {
- var
- test = 0,
- hook = function() {
- test = 5;
- };
- handsontable();
- getInstance().addHook('afterInit', hook);
- getInstance().removeHook('afterInit', hook);
- expect(test).toEqual(0);
- });
- it('should run global hook', () => {
- var test = 0;
- Handsontable.hooks.add('afterInit', () => {
- test = 5;
- });
- handsontable();
- expect(test).toEqual(5);
- });
- it('should run local hook', () => {
- var test = 0;
- handsontable();
- getInstance().addHook('myHook', () => {
- test += 5;
- });
- getInstance().runHooks('myHook');
- getInstance().runHooks('myHook');
- expect(test).toEqual(10);
- });
- it('should run local hook once', () => {
- var test = 0;
- handsontable();
- getInstance().addHookOnce('myHook', () => {
- test += 5;
- });
- getInstance().runHooks('myHook');
- getInstance().runHooks('myHook');
- expect(test).toEqual(5);
- });
- it('should run all hooks', () => {
- var test = 0;
- Handsontable.hooks.add('afterInit', () => {
- test += 5;
- });
- handsontable({
- afterInit() {
- test += 5;
- }
- });
- expect(test).toEqual(10);
- });
- it('list of all avaliable plugin hooks should be exposed as a public method', () => {
- var hooks = Handsontable.hooks.getRegistered(); // this is used in demo/callbacks.html
- expect(hooks.indexOf('beforeInit')).toBeGreaterThan(-1);
- });
- it('should add a local hook with addHooks method', () => {
- var hot1 = handsontable();
- var test = 0;
- hot1.addHook('myHook', () => {
- test += 5;
- });
- hot1.runHooks('myHook');
- expect(test).toEqual(5);
- });
- it('should remove a local hook with removeHook method', () => {
- var hot1 = handsontable();
- var test = 0;
- var handler = function() {
- test += 5;
- };
- hot1.addHook('myHook', handler);
- hot1.runHooks('myHook');
- hot1.runHooks('myHook');
- expect(test).toEqual(10);
- hot1.removeHook('myHook', handler);
- hot1.runHooks('myHook');
- expect(test).toEqual(10);
- });
- it('should add a local hook with addHookOnce method and run it just once', () => {
- var hot1 = handsontable();
- var test = 0;
- var handler = function() {
- test += 5;
- };
- hot1.addHookOnce('myHook', handler);
- hot1.runHooks('myHook');
- hot1.runHooks('myHook');
- expect(test).toEqual(5);
- });
- it('should run hook with runHooks and return value', () => {
- var hot = handsontable();
- var handler = function() {
- return 5;
- };
- hot.addHook('myHook', handler);
- expect(hot.runHooks('myHook')).toEqual(5);
- });
- it('should run two "once" hooks in desired order', () => {
- var hot = handsontable();
- var arr = [];
- hot.addHookOnce('myHook', () => {
- arr.push(1);
- });
- hot.addHookOnce('myHook', () => {
- arr.push(2);
- });
- hot.runHooks('myHook');
- expect(arr).toEqual([1, 2]);
- });
- it('should execute two "once" hooks in desired order', () => {
- var hot = handsontable();
- var str = 'a';
- hot.addHookOnce('myHook', (str) => `${str}b`);
- hot.addHookOnce('myHook', (str) => `${str}c`);
- expect(hot.runHooks('myHook', str)).toEqual('abc');
- });
- it('adding same hook twice should register it only once (without an error)', () => {
- var i = 0;
- var fn = function() {
- i++;
- };
- var hot = handsontable({
- afterOnCellMouseOver: fn
- });
- hot.getInstance().updateSettings({afterOnCellMouseOver: fn});
- hot.runHooks('afterOnCellMouseOver');
- expect(i).toEqual(1);
- });
- it('should mark the hook callbacks added with Handsontable initialization', function() {
- var fn = function() {};
- var fn2 = function() {};
- var hot = handsontable({
- afterChange: fn
- });
- hot.addHook('afterChange', fn2);
- expect(fn.initialHook).toEqual(true);
- expect(fn2.initialHook).toEqual(void 0);
- });
- it('should mark the hook callbacks added using the updateSettings method', function() {
- var fn = function() {};
- var fn2 = function() {};
- var hot = handsontable();
- hot.updateSettings({
- afterChange: fn
- });
- hot.addHook('afterChange', fn2);
- expect(fn.initialHook).toEqual(true);
- expect(fn2.initialHook).toEqual(void 0);
- });
- it('should replace the existing hook callbacks, if they\'re updated using the updateSettings method (when there was a hook ' +
- 'already declared in the initialization)', function() {
- var fn = function() {};
- var fn2 = function() {};
- var hot = handsontable({
- afterGetCellMeta: fn
- });
- var initialCallbackCount = hot.pluginHookBucket.afterGetCellMeta.length;
- hot.updateSettings({
- afterGetCellMeta: function() {
- var a = 'another function';
- }
- });
- hot.updateSettings({
- afterGetCellMeta: function() {
- var a = 'yet another function';
- }
- });
- hot.updateSettings({
- afterGetCellMeta: fn2
- });
- expect(hot.pluginHookBucket.afterGetCellMeta.length).toEqual(initialCallbackCount);
- });
- it('should replace the existing hook callbacks, if they\'re updated using the updateSettings method', function() {
- var fn = function() {};
- var fn2 = function() {};
- var hot = handsontable();
- hot.addHook('afterGetCellMeta', () => 'doesn\'t matter 1');
- hot.addHook('afterGetCellMeta', () => 'doesn\'t matter 2');
- hot.addHook('afterGetCellMeta', () => 'doesn\'t matter 3');
- hot.updateSettings({
- afterGetCellMeta: fn
- });
- var initialCallbackCount = hot.pluginHookBucket.afterGetCellMeta.length;
- hot.updateSettings({
- afterGetCellMeta: function() {
- var a = 'another function';
- }
- });
- hot.updateSettings({
- afterGetCellMeta: function() {
- var a = 'yet another function';
- }
- });
- hot.updateSettings({
- afterGetCellMeta: fn2
- });
- expect(hot.pluginHookBucket.afterGetCellMeta.length).toEqual(initialCallbackCount);
- });
- it('should NOT replace existing hook callbacks, if the\'re added using the addHook method', function() {
- var fn = function() {};
- var fn2 = function() {};
- var hot = handsontable();
- hot.updateSettings({
- afterGetCellMeta: fn
- });
- var initialCallbackCount = hot.pluginHookBucket.afterGetCellMeta.length;
- hot.addHook('afterGetCellMeta', function() {
- var a = 'another function';
- });
- hot.addHook('afterGetCellMeta', function() {
- var a = 'yet another function';
- });
- hot.addHook('afterGetCellMeta', fn2);
- // should not add this one, as it's a duplicate
- hot.addHook('afterGetCellMeta', fn);
- expect(hot.pluginHookBucket.afterGetCellMeta.length).toEqual(initialCallbackCount + 3);
- });
- describe('controlling handler queue execution', () => {
- it('should execute all handlers if none of them hasn\'t skipped', () => {
- var handler1 = jasmine.createSpy('handler1');
- var handler2 = jasmine.createSpy('handler2');
- var handler3 = jasmine.createSpy('handler3');
- var hot = handsontable();
- hot.addHook('fakeEvent', handler1);
- hot.addHook('fakeEvent', handler2);
- hot.addHook('fakeEvent', handler3);
- expect(handler1).not.toHaveBeenCalled();
- expect(handler2).not.toHaveBeenCalled();
- expect(handler3).not.toHaveBeenCalled();
- hot.runHooks('fakeEvent');
- expect(handler1).toHaveBeenCalled();
- expect(handler2).toHaveBeenCalled();
- expect(handler3).toHaveBeenCalled();
- });
- });
- });
|