-
Notifications
You must be signed in to change notification settings - Fork 1
/
store.test.js
153 lines (131 loc) · 4.75 KB
/
store.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const Store = require('./store');
const deepfreeze = require('deepfreeze');
describe('Store', function() {
const initialState = {
auth: { token: 'sessiontoken', lastSignedIn: '09/09/2018'},
};
it('should create a store', function() {
const store = new Store;
expect(store).toBeTruthy();
expect(store.state).toEqual({});
});
it('should create a store with some initial state', function() {
const store = new Store(initialState);
expect(store.state).toEqual(initialState);
});
it('should create a store with initial state and reducers', function() {
const reducers = {
auth: function(state, action) {
return state;
},
};
const store = new Store(initialState, reducers);
expect(store.state).toEqual(initialState);
expect(store.reducers).toEqual(reducers);
});
it('should dispatch an action to a reducer', function() {
const reducers = {
auth: jest.fn(),
};
const action = {
type: 'AUTH::DUMMY',
}
const store = new Store(initialState, reducers);
store.dispatch(action);
expect(reducers.auth).toHaveBeenCalledWith(initialState.auth, action);
});
it('should dispatch an action to all reducers', function() {
const reducers = {
auth: jest.fn(),
users: jest.fn(),
};
const action = {
type: 'AUTH::DUMMY',
}
const store = new Store(initialState, reducers);
store.dispatch(action);
expect(reducers.auth).toHaveBeenCalledWith(initialState.auth, action);
expect(reducers.users).toHaveBeenCalledWith(undefined, action);
});
it('should dispatch an action to reducers and update state', function() {
const reducers = {
auth: jest.fn(function(state, action) {
if (action.type === 'AUTH::SIGN_OUT') {
delete state.token;
}
return state;
}),
users: jest.fn(),
};
const action = {
type: 'AUTH::SIGN_OUT',
}
const stateAfter = {
auth: { lastSignedIn: '09/09/2018' },
users: undefined,
};
const store = new Store(initialState, reducers);
store.dispatch(action);
expect(reducers.auth).toHaveBeenCalledWith(initialState.auth, action);
expect(reducers.users).toHaveBeenCalledWith(undefined, action);
expect(store.state).toEqual(stateAfter);
});
it('should dispatch an action to reducers and update state immutably', function() {
const reducers = {
auth: jest.fn(function(state, action) {
if (action.type === 'AUTH::SIGN_OUT') {
return {
...state,
token: undefined,
}
}
return state;
}),
users: jest.fn(),
};
const action = {
type: 'AUTH::SIGN_OUT',
}
const stateAfter = {
auth: { token: undefined, lastSignedIn: '09/09/2018' },
users: undefined,
};
const store = new Store(initialState, reducers);
deepfreeze(initialState);
deepfreeze(action);
store.dispatch(action);
expect(reducers.auth).toHaveBeenCalledWith(initialState.auth, action);
expect(reducers.users).toHaveBeenCalledWith(undefined, action);
expect(store.state).toEqual(stateAfter);
});
it('should notify subscribers on state update', function() {
const reducers = {
auth: jest.fn(function(state, action) {
if (action.type === 'AUTH::SIGN_OUT') {
return {
...state,
token: undefined,
}
}
return state;
}),
users: jest.fn(),
};
const action = {
type: 'AUTH::SIGN_OUT',
}
const stateAfter = {
auth: { token: undefined, lastSignedIn: '09/09/2018' },
users: undefined,
};
const listener = jest.fn();
const store = new Store(initialState, reducers);
const unsubscribe = store.subscribe(listener);
store.dispatch(action);
expect(listener).toHaveBeenCalledWith(stateAfter);
expect(listener).toHaveBeenCalledTimes(1);
unsubscribe();
store.dispatch(action);
expect(listener).toHaveBeenCalledTimes(1);
});
});