-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
/
Copy pathcombineReducers.spec.ts
384 lines (318 loc) · 11.4 KB
/
combineReducers.spec.ts
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* eslint-disable no-console */
import type { Reducer, Action } from 'redux'
import {
createStore,
combineReducers,
__DO_NOT_USE__ActionTypes as ActionTypes
} from 'redux'
import { vi } from 'vitest'
describe('Utils', () => {
describe('combineReducers', () => {
it('returns a composite reducer that maps the state keys to given reducers', () => {
type IncrementAction = { type: 'increment' }
type PushAction = { type: 'push'; value: unknown }
const reducer = combineReducers({
counter: (state: number = 0, action: IncrementAction) =>
action.type === 'increment' ? state + 1 : state,
stack: (state: any[] = [], action: PushAction) =>
action.type === 'push' ? [...state, action.value] : state
})
const s1 = reducer(undefined, { type: 'increment' })
expect(s1).toEqual({ counter: 1, stack: [] })
const s2 = reducer(s1, { type: 'push', value: 'a' })
expect(s2).toEqual({ counter: 1, stack: ['a'] })
})
it('ignores all props which are not a function', () => {
// we double-cast because these conditions can only happen in a javascript setting
const reducer = combineReducers({
fake: true as unknown as Reducer,
broken: 'string' as unknown as Reducer,
another: { nested: 'object' } as unknown as Reducer,
stack: (state = []) => state
})
expect(Object.keys(reducer(undefined, { type: 'push' }))).toEqual([
'stack'
])
})
it('warns if a reducer prop is undefined', () => {
const preSpy = console.error
const spy = vi.fn()
console.error = spy
let isNotDefined: any
combineReducers({ isNotDefined })
expect(spy.mock.calls[0][0]).toMatch(
/No reducer provided for key "isNotDefined"/
)
spy.mockClear()
combineReducers({ thing: undefined })
expect(spy.mock.calls[0][0]).toMatch(
/No reducer provided for key "thing"/
)
spy.mockClear()
console.error = preSpy
})
it('throws an error if a reducer returns undefined handling an action', () => {
const reducer = combineReducers({
counter(state: number = 0, action: Action) {
switch (action && action.type) {
case 'increment':
return state + 1
case 'decrement':
return state - 1
case 'whatever':
case null:
case undefined:
return undefined
default:
return state
}
}
})
expect(() => reducer({ counter: 0 }, { type: 'whatever' })).toThrow(
/"whatever".*"counter"/
)
// @ts-expect-error
expect(() => reducer({ counter: 0 }, null)).toThrow(
/"counter".*an action/
)
expect(() => reducer({ counter: 0 }, {} as unknown as Action)).toThrow(
/"counter".*an action/
)
})
it('throws an error on first call if a reducer returns undefined initializing', () => {
const reducer = combineReducers({
counter(state: number, action: Action) {
switch (action.type) {
case 'increment':
return state + 1
case 'decrement':
return state - 1
default:
return state
}
}
})
expect(() => reducer(undefined, { type: '' })).toThrow(
/"counter".*initialization/
)
})
it('catches error thrown in reducer when initializing and re-throw', () => {
const reducer = combineReducers({
throwingReducer() {
throw new Error('Error thrown in reducer')
}
})
expect(() => reducer(undefined, undefined as unknown as Action)).toThrow(
/Error thrown in reducer/
)
})
it('maintains referential equality if the reducers it is combining do', () => {
const reducer = combineReducers({
child1(state = {}) {
return state
},
child2(state = {}) {
return state
},
child3(state = {}) {
return state
}
})
const initialState = reducer(undefined, { type: '@@INIT' })
expect(reducer(initialState, { type: 'FOO' })).toBe(initialState)
})
it('does not have referential equality if one of the reducers changes something', () => {
const reducer = combineReducers({
child1(state = {}) {
return state
},
child2(state: { count: number } = { count: 0 }, action: Action) {
switch (action.type) {
case 'increment':
return { count: state.count + 1 }
default:
return state
}
},
child3(state = {}) {
return state
}
})
const initialState = reducer(undefined, { type: '@@INIT' })
expect(reducer(initialState, { type: 'increment' })).not.toBe(
initialState
)
})
it('throws an error on first call if a reducer attempts to handle a private action', () => {
const reducer = combineReducers({
counter(state: number, action: Action) {
switch (action.type) {
case 'increment':
return state + 1
case 'decrement':
return state - 1
// Never do this in your code:
case ActionTypes.INIT:
return 0
default:
return undefined
}
}
})
expect(() => reducer(undefined, undefined as unknown as Action)).toThrow(
/"counter".*private/
)
})
it('warns if no reducers are passed to combineReducers', () => {
const preSpy = console.error
const spy = vi.fn()
console.error = spy
const reducer = combineReducers({})
// @ts-ignore
reducer(undefined, { type: '' })
expect(spy.mock.calls[0][0]).toMatch(
/Store does not have a valid reducer/
)
spy.mockClear()
console.error = preSpy
})
it('warns if input state does not match reducer shape', () => {
const preSpy = console.error
const spy = vi.fn()
const nullAction = undefined as unknown as Action
console.error = spy
interface ShapeState {
foo: { bar: number }
baz: { qux: number }
}
const reducer = combineReducers({
foo(state = { bar: 1 }) {
return state
},
baz(state = { qux: 3 }) {
return state
}
})
reducer(undefined, nullAction)
expect(spy.mock.calls.length).toBe(0)
reducer({ foo: { bar: 2 } } as unknown as ShapeState, nullAction)
expect(spy.mock.calls.length).toBe(0)
reducer(
{
foo: { bar: 2 },
baz: { qux: 4 }
},
nullAction
)
expect(spy.mock.calls.length).toBe(0)
createStore(reducer, { bar: 2 } as unknown as ShapeState)
expect(spy.mock.calls[0][0]).toMatch(
/Unexpected key "bar".*createStore.*instead: "foo", "baz"/
)
createStore(reducer, {
bar: 2,
qux: 4,
thud: 5
} as unknown as ShapeState)
expect(spy.mock.calls[1][0]).toMatch(
/Unexpected keys "qux", "thud".*createStore.*instead: "foo", "baz"/
)
createStore(reducer, 1 as unknown as ShapeState)
expect(spy.mock.calls[2][0]).toMatch(
/createStore has unexpected type of "number".*keys: "foo", "baz"/
)
reducer({ corge: 2 } as unknown as ShapeState, nullAction)
expect(spy.mock.calls[3][0]).toMatch(
/Unexpected key "corge".*reducer.*instead: "foo", "baz"/
)
reducer({ fred: 2, grault: 4 } as unknown as ShapeState, nullAction)
expect(spy.mock.calls[4][0]).toMatch(
/Unexpected keys "fred", "grault".*reducer.*instead: "foo", "baz"/
)
reducer(1 as unknown as ShapeState, nullAction)
expect(spy.mock.calls[5][0]).toMatch(
/reducer has unexpected type of "number".*keys: "foo", "baz"/
)
spy.mockClear()
console.error = preSpy
})
it('only warns for unexpected keys once', () => {
const preSpy = console.error
const spy = vi.fn()
console.error = spy
const nullAction = { type: '' }
const foo = (state = { foo: 1 }) => state
const bar = (state = { bar: 2 }) => state
expect(spy.mock.calls.length).toBe(0)
interface WarnState {
foo: { foo: number }
bar: { bar: number }
}
const reducer = combineReducers({ foo, bar })
const state = { foo: 1, bar: 2, qux: 3 } as unknown as WarnState
const bazState = { ...state, baz: 5 } as unknown as WarnState
reducer(state, nullAction)
reducer(state, nullAction)
reducer(state, nullAction)
reducer(state, nullAction)
expect(spy.mock.calls.length).toBe(1)
reducer(bazState, nullAction)
reducer({ ...bazState }, nullAction)
reducer({ ...bazState }, nullAction)
reducer({ ...bazState }, nullAction)
expect(spy.mock.calls.length).toBe(2)
spy.mockClear()
console.error = preSpy
})
describe('With Replace Reducers', function () {
const foo = (state = {}) => state
const bar = (state = {}) => state
const ACTION = { type: 'ACTION' }
it('should return an updated state when additional reducers are passed to combineReducers', function () {
type State = { foo: {}; bar?: {} }
const originalCompositeReducer = combineReducers<State>({ foo })
const store = createStore(originalCompositeReducer)
store.dispatch(ACTION)
const initialState = store.getState()
store.replaceReducer(combineReducers<State>({ foo, bar }))
store.dispatch(ACTION)
const nextState = store.getState()
expect(nextState).not.toBe(initialState)
})
it('should return an updated state when reducers passed to combineReducers are changed', function () {
type State = { foo?: {}; bar: {}; baz?: {} }
const baz = (state = {}) => state
const originalCompositeReducer = combineReducers<State>({ foo, bar })
const store = createStore(originalCompositeReducer)
store.dispatch(ACTION)
const initialState = store.getState()
store.replaceReducer(combineReducers<State>({ baz, bar }))
store.dispatch(ACTION)
const nextState = store.getState()
expect(nextState).not.toBe(initialState)
})
it('should return the same state when reducers passed to combineReducers not changed', function () {
const originalCompositeReducer = combineReducers({ foo, bar })
const store = createStore(originalCompositeReducer)
store.dispatch(ACTION)
const initialState = store.getState()
store.replaceReducer(combineReducers({ foo, bar }))
store.dispatch(ACTION)
const nextState = store.getState()
expect(nextState).toBe(initialState)
})
it('should return an updated state when one of more reducers passed to the combineReducers are removed', function () {
const originalCompositeReducer = combineReducers<{
foo?: typeof foo
bar: typeof bar
}>({ foo, bar })
const store = createStore(originalCompositeReducer)
store.dispatch(ACTION)
const initialState = store.getState()
store.replaceReducer(combineReducers({ bar }))
const nextState = store.getState()
expect(nextState).not.toBe(initialState)
})
})
})
})