-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactionWrapper.test.js
74 lines (63 loc) · 2.56 KB
/
actionWrapper.test.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
65
66
67
68
69
70
71
72
73
74
import reduxMill from '../index';
describe('use actionWrapper', () => {
let wrapperCalls = [];
let wrapperResults = [];
let reducer = {};
const actionWrapper = jest.fn((actionCB) => {
return jest.fn((currentState, payload, action) => {
wrapperCalls.push([currentState, payload, action]);
const r = actionCB(currentState, payload, action);
wrapperResults.push(r);
return r;
});
});
beforeEach(() => {
actionWrapper.mockClear();
wrapperCalls = [];
wrapperResults = [];
reducer = {
SET: (state, name) => ({ ...state, name }),
GET: {
0: (state, payload) => ({ ...state, ...payload }),
X: (state, { value }) => ({ ...state, value }),
LOAD: (state) => ({ ...state, loading: true }),
}
};
});
it('should not call actionWrapper in initialization for empty reducer', () => {
reduxMill({}, {}, 'myName', { actionWrapper });
expect(actionWrapper.mock.calls.length).toBe(0);
expect(wrapperCalls.length).toBe(0);
});
it('should call actionWrapper in initialization for each action', () => {
reduxMill({}, reducer, 'myName', { actionWrapper });
expect(actionWrapper.mock.calls.length).toBe(4);
expect(wrapperCalls.length).toBe(0);
});
it('should not call wrapped action for undefined action type', () => {
const result = reduxMill({ a: 10 }, reducer, 'myName', { actionWrapper });
expect(actionWrapper.mock.calls.length).toBe(4);
expect(wrapperCalls.length).toBe(0);
result.myName({ b: 100 }, { type: 'some' });
expect(actionWrapper.mock.calls.length).toBe(4);
expect(wrapperCalls.length).toBe(0);
});
it('should not call wrapped action for defined action type', () => {
const result = reduxMill({ a: 10 }, reducer, 'myName', { actionWrapper });
expect(actionWrapper.mock.calls.length).toBe(4);
expect(wrapperCalls.length).toBe(0);
result.myName({ b: 1 }, { type: 'some' });
expect(actionWrapper.mock.calls.length).toBe(4);
expect(wrapperCalls.length).toBe(0);
result.myName({ c: 2 }, { type: 'SET' });
expect(actionWrapper.mock.calls.length).toBe(4);
expect(wrapperCalls.length).toBe(1);
expect(wrapperCalls[0]).toEqual([{ c: 2 }, undefined, { type: 'SET' }]);
const payload = { value: 100 };
result.myName({ d: 3 }, { type: 'GET_X', payload });
expect(wrapperCalls.length).toBe(2);
expect(wrapperCalls[1]).toEqual([{ d: 3 }, payload, { type: 'GET_X', payload }]);
expect(wrapperResults[0]).toEqual({ c: 2, name: undefined });
expect(wrapperResults[1]).toEqual({ d: 3, value: 100 });
});
});