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