|
| 1 | +import expect from 'expect'; |
| 2 | +import sinon from 'sinon'; |
| 3 | +import digestMiddleware from '../../src/components/digestMiddleware'; |
| 4 | + |
| 5 | + |
| 6 | +describe('digestMiddleware', () => { |
| 7 | + |
| 8 | + it('Should debounce the $evalAsync function if debounce is enabled', (done) => { |
| 9 | + const $evalAsync = sinon.spy(); |
| 10 | + const $rootScope = { |
| 11 | + $evalAsync, |
| 12 | + }; |
| 13 | + const firstAction = 1; |
| 14 | + const secondAction = 2; |
| 15 | + const debounceConfig = { |
| 16 | + wait: 10, |
| 17 | + }; |
| 18 | + const next = sinon.spy((action) => (action)); |
| 19 | + const middleware = digestMiddleware($rootScope, debounceConfig); |
| 20 | + middleware()(next)(firstAction); |
| 21 | + setTimeout(() => { |
| 22 | + middleware()(next)(secondAction); |
| 23 | + }, 1); |
| 24 | + setTimeout(() => { |
| 25 | + expect($evalAsync.calledOnce).toBe(true); |
| 26 | + expect(next.calledTwice).toBe(true); |
| 27 | + expect(next.firstCall.calledWithExactly(firstAction)).toBe(true); |
| 28 | + expect(next.secondCall.calledWithExactly(secondAction)).toBe(true); |
| 29 | + expect($evalAsync.firstCall.calledWithExactly(secondAction)).toBe(true); |
| 30 | + done(); |
| 31 | + }, debounceConfig.wait + 10); |
| 32 | + |
| 33 | + }); |
| 34 | + |
| 35 | + it('Should not debounce the $evalAsync function if debounce is disabled', () => { |
| 36 | + const disabledDebounceConfigs = [ |
| 37 | + null, |
| 38 | + undefined, |
| 39 | + {}, |
| 40 | + { wait: 0 }, |
| 41 | + ]; |
| 42 | + disabledDebounceConfigs.forEach(() => { |
| 43 | + const $evalAsync = sinon.spy(); |
| 44 | + const $rootScope = { |
| 45 | + $evalAsync, |
| 46 | + }; |
| 47 | + const firstAction = 1; |
| 48 | + const secondAction = 2; |
| 49 | + const debounceConfig = {}; |
| 50 | + |
| 51 | + const next = sinon.spy((action) => (action)); |
| 52 | + const middleware = digestMiddleware($rootScope, debounceConfig); |
| 53 | + middleware()(next)(firstAction); |
| 54 | + middleware()(next)(secondAction); |
| 55 | + expect($evalAsync.calledTwice).toBe(true); |
| 56 | + expect(next.calledTwice).toBe(true); |
| 57 | + expect(next.firstCall.calledWithExactly(firstAction)).toBe(true); |
| 58 | + expect(next.secondCall.calledWithExactly(secondAction)).toBe(true); |
| 59 | + expect($evalAsync.firstCall.calledWithExactly(firstAction)).toBe(true); |
| 60 | + expect($evalAsync.secondCall.calledWithExactly(secondAction)).toBe(true); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | +}); |
0 commit comments