Skip to content

Commit e240626

Browse files
committed
Add test case & improve error message wording
* Add a test case to verify reducer has a catch-all clause for unknown action types * Improve error message wording
1 parent ae16bad commit e240626

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/utils/combineReducers.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ function assertReducerSanity(reducers) {
5959
if (typeof initialState === 'undefined') {
6060
throw new Error(
6161
`Reducer "${key}" returned undefined during initialization. ` +
62-
`If the state passed to the reducer is undefined, you must ` +
63-
`explicitly return the initial state. The initial state may ` +
64-
`not be undefined.`
62+
`Reducers should never return undefined. Make sure this reducer ` +
63+
`has a catch-all clause for unknown action types and that it returns a ` +
64+
`default initial state if the state passed to it is undefined.`
6565
)
6666
}
6767

test/utils/combineReducers.spec.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,25 @@ describe('Utils', () => {
8484
)
8585
})
8686

87+
it('throws an error if reducer does not have a catch-all clause for unknown action types', () => {
88+
const reducer = combineReducers({
89+
counter(state = 0, action) {
90+
switch (action.type) {
91+
case 'increment':
92+
return state + 1
93+
case 'decrement':
94+
return state - 1
95+
case undefined:
96+
return state
97+
}
98+
}
99+
})
100+
101+
expect(() => reducer()).toThrow(
102+
/"counter".*initialization/
103+
)
104+
})
105+
87106
it('catches error thrown in reducer when initializing and re-throw', () => {
88107
const reducer = combineReducers({
89108
throwingReducer() {
@@ -151,7 +170,7 @@ describe('Utils', () => {
151170
expect(reducer(initialState, { type: 'increment' })).toNotBe(initialState)
152171
})
153172

154-
it('throws an error if reducer does not return current state for all unknown action types', () => {
173+
it('throws an error if reducer does not return current state for unknown action types', () => {
155174
const reducer = combineReducers({
156175
counter(state, action) {
157176
switch (action.type) {

0 commit comments

Comments
 (0)