ngrx-workshop
Search…
Testing Reducers
1
import {
2
User,
3
generateUser,
4
generateUserMap,
5
generateUserArray
6
} from './user.model';
7
import * as actions from './user.actions';
8
import {
9
userReducer,
10
initialUserState,
11
getSelectedId,
12
getLoading,
13
getError,
14
getQuery
15
} from './user.reducer';
16
import { Update } from '@ngrx/entity';
17
18
const INITIAL_STATE_WITH_ERROR = {
19
...initialUserState,
20
error: 'some error'
21
};
22
const BLANK_ERROR_MESSAGE = '';
23
24
describe('userReducer', () => {
25
describe('upon an undefined action', () => {
26
it('should return the default state upon an undefined action', () => {
27
const action = { type: 'NOT DEFINED' } as any;
28
29
expect(userReducer(initialUserState, action)).toEqual(initialUserState);
30
});
31
});
32
33
describe('upon CreateUser', () => {
34
it('should set loading to true and clear any error', () => {
35
const action = new actions.CreateUser({ user: generateUser() });
36
37
expect(userReducer(INITIAL_STATE_WITH_ERROR, action)).toEqual({
38
...initialUserState,
39
loading: true,
40
error: BLANK_ERROR_MESSAGE
41
});
42
});
43
});
44
45
describe('upon CreateUserSuccess', () => {
46
it('should add the given User, set loading to false, and clear any error', () => {
47
const result = generateUser();
48
const action = new actions.CreateUserSuccess({ result });
49
50
expect(userReducer(INITIAL_STATE_WITH_ERROR, action)).toEqual({
51
...initialUserState,
52
...generateUserMap([result]),
53
loading: false,
54
error: BLANK_ERROR_MESSAGE
55
});
56
});
57
});
58
59
describe('upon CreateUserFail', () => {
60
it('should set loading to true and echo the error', () => {
61
const error = 'test create error';
62
const action = new actions.CreateUserFail({ error });
63
64
expect(userReducer(initialUserState, action)).toEqual({
65
...initialUserState,
66
loading: false,
67
error: `User create failed: ${error}`
68
});
69
});
70
});
71
72
describe('upon SearchAllUserEntities', () => {
73
it('should remove User entities, set loading to true, and clear any error', () => {
74
const initialUserStateWithUserEntities = {
75
...INITIAL_STATE_WITH_ERROR,
76
...generateUserMap()
77
};
78
const action = new actions.SearchAllUserEntities();
79
80
expect(userReducer(initialUserStateWithUserEntities, action)).toEqual({
81
...initialUserState,
82
loading: true,
83
error: BLANK_ERROR_MESSAGE
84
});
85
});
86
});
87
88
describe('upon SearchAllUserEntitiesSuccess', () => {
89
it('should add User entities, set loading to false, and clear any error', () => {
90
const result = generateUserArray();
91
const action = new actions.SearchAllUserEntitiesSuccess({ result });
92
93
expect(userReducer(INITIAL_STATE_WITH_ERROR, action)).toEqual({
94
...initialUserState,
95
...generateUserMap(result),
96
loading: false,
97
error: BLANK_ERROR_MESSAGE
98
});
99
});
100
});
101
102
describe('upon SearchAllUserEntitiesFail', () => {
103
it('should set loading to false and echo the error', () => {
104
const error = 'test search error';
105
const action = new actions.SearchAllUserEntitiesFail({ error });
106
107
expect(userReducer(initialUserState, action)).toEqual({
108
...initialUserState,
109
loading: false,
110
error: `User search failed: ${error}`
111
});
112
});
113
});
114
115
describe('upon LoadUserById', () => {
116
it('should remove user entities, set selected id, and clear any error', () => {
117
const id = 8675309;
118
const initialUserStateWithUserEntities = {
119
...INITIAL_STATE_WITH_ERROR,
120
...generateUserMap()
121
};
122
const action = new actions.LoadUserById({ id });
123
124
expect(userReducer(initialUserStateWithUserEntities, action)).toEqual({
125
...initialUserState,
126
selectedId: id,
127
loading: true,
128
error: BLANK_ERROR_MESSAGE
129
});
130
});
131
});
132
133
describe('upon LoadUserByIdSuccess', () => {
134
it('should add the given User, set loading to false, and clear any error', () => {
135
const result = generateUser();
136
const action = new actions.LoadUserByIdSuccess({ result });
137
138
expect(userReducer(INITIAL_STATE_WITH_ERROR, action)).toEqual({
139
...initialUserState,
140
...generateUserMap([result]),
141
loading: false,
142
error: BLANK_ERROR_MESSAGE
143
});
144
});
145
});
146
147
describe('upon LoadUserByIdFail', () => {
148
it('should set loading to false and echo the error', () => {
149
const error = 'test load by id error';
150
const action = new actions.LoadUserByIdFail({ error });
151
152
expect(userReducer(initialUserState, action)).toEqual({
153
...initialUserState,
154
loading: false,
155
error: `User load failed: ${error}`
156
});
157
});
158
});
159
160
describe('upon UpdateUser', () => {
161
it('should set loading to true and clear any errior', () => {
162
const user = generateUser();
163
const action = new actions.UpdateUser({ user });
164
165
expect(userReducer(INITIAL_STATE_WITH_ERROR, action)).toEqual({
166
...initialUserState,
167
loading: true,
168
error: BLANK_ERROR_MESSAGE
169
});
170
});
171
});
172
173
describe('upon UpdateUserSuccess', () => {
174
it('should add the given User, set loading to false, and clear any error', () => {
175
const user = generateUser();
176
const initialUserStateWithUser = {
177
...INITIAL_STATE_WITH_ERROR,
178
...generateUserMap([user])
179
};
180
const updatedUser = {
181
...user,
182
name: user.name + ' EDITED',
183
description: user.description + ' EDITED'
184
};
185
const update = {
186
id: updatedUser.id,
187
changes: updatedUser
188
} as Update<User>;
189
const action = new actions.UpdateUserSuccess({ update });
190
191
expect(userReducer(initialUserStateWithUser, action)).toEqual({
192
...initialUserStateWithUser,
193
...generateUserMap([updatedUser]),
194
loading: false,
195
error: BLANK_ERROR_MESSAGE
196
});
197
});
198
});
199
200
describe('upon UpdateUserFail', () => {
201
it('should set loading to false and echo the error', () => {
202
const error = 'test update error';
203
const action = new actions.UpdateUserFail({ error });
204
205
expect(userReducer(initialUserState, action)).toEqual({
206
...initialUserState,
207
loading: false,
208
error: `User update failed: ${error}`
209
});
210
});
211
});
212
213
describe('upon DeleteUserById', () => {
214
it('should set the id, set loading to true, and clear any error', () => {
215
const id = 4815162342;
216
const action = new actions.DeleteUserById({ id });
217
218
expect(userReducer(INITIAL_STATE_WITH_ERROR, action)).toEqual({
219
...initialUserState,
220
selectedId: id,
221
loading: true,
222
error: BLANK_ERROR_MESSAGE
223
});
224
});
225
});
226
227
describe('upon DeleteUserByIdSuccess', () => {
228
it('should remove the id-given user, set loading to false, and clear any error', () => {
229
const id = 18009453669;
230
const userToBeRemoved = generateUser(id);
231
const expectedUserEntities = generateUserArray();
232
const userEntitiesWithUserToBeRemoved = [
233
...expectedUserEntities,
234
userToBeRemoved
235
];
236
const initialUserStateWithAllUserEntities = {
237
...INITIAL_STATE_WITH_ERROR,
238
...generateUserMap(userEntitiesWithUserToBeRemoved)
239
};
240
const action = new actions.DeleteUserByIdSuccess({ id });
241
242
expect(
243
userReducer(initialUserStateWithAllUserEntities, action)
244
).toEqual({
245
...initialUserStateWithAllUserEntities,
246
...generateUserMap(expectedUserEntities),
247
loading: false,
248
error: BLANK_ERROR_MESSAGE
249
});
250
});
251
});
252
253
describe('upon DeleteUserByIdFail', () => {
254
it('should set loading to false and echo the error', () => {
255
const error = 'test delete error';
256
const action = new actions.DeleteUserByIdFail({ error });
257
258
expect(userReducer(initialUserState, action)).toEqual({
259
...initialUserState,
260
loading: false,
261
error: `User delete failed: ${error}`
262
});
263
});
264
});
265
266
describe('upon SetSearchQuery', () => {
267
it('should set the query', () => {
268
const query = {
269
filter: 'someFilter',
270
sorting: 'someSort',
271
limit: 1000000000000,
272
page: 888888
273
};
274
const action = new actions.SetSearchQuery(query);
275
276
expect(userReducer(initialUserState, action)).toEqual({
277
...initialUserState,
278
query
279
});
280
});
281
});
282
283
describe('upon SelectUserById', () => {
284
it('should set the id and clear any error', () => {
285
const id = 73;
286
const action = new actions.SelectUserById({ id });
287
288
expect(userReducer(INITIAL_STATE_WITH_ERROR, action)).toEqual({
289
...initialUserState,
290
selectedId: id,
291
error: BLANK_ERROR_MESSAGE
292
});
293
});
294
});
295
});
296
297
describe('getters', () => {
298
describe('getSelectedId', () => {
299
it('should return the selected id', () => {
300
expect(getSelectedId(initialUserState)).toEqual(initialUserState.selectedId);
301
});
302
});
303
describe('getLoading', () => {
304
it('should return the selected id', () => {
305
expect(getLoading(initialUserState)).toEqual(initialUserState.loading);
306
});
307
});
308
describe('getError', () => {
309
it('should return the selected id', () => {
310
expect(getError(INITIAL_STATE_WITH_ERROR))
311
.toEqual(INITIAL_STATE_WITH_ERROR.error);
312
});
313
});
314
describe('getQuery', () => {
315
it('should return the selected id', () => {
316
expect(getQuery(initialUserState))
317
.toEqual(initialUserState.query);
318
});
319
});
320
});
321
Copied!
Last modified 2yr ago
Copy link