File tree 2 files changed +23
-4
lines changed 2 files changed +23
-4
lines changed Original file line number Diff line number Diff line change @@ -59,9 +59,9 @@ function assertReducerSanity(reducers) {
59
59
if ( typeof initialState === 'undefined' ) {
60
60
throw new Error (
61
61
`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.`
65
65
)
66
66
}
67
67
Original file line number Diff line number Diff line change @@ -84,6 +84,25 @@ describe('Utils', () => {
84
84
)
85
85
} )
86
86
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
+ / " c o u n t e r " .* i n i t i a l i z a t i o n /
103
+ )
104
+ } )
105
+
87
106
it ( 'catches error thrown in reducer when initializing and re-throw' , ( ) => {
88
107
const reducer = combineReducers ( {
89
108
throwingReducer ( ) {
@@ -151,7 +170,7 @@ describe('Utils', () => {
151
170
expect ( reducer ( initialState , { type : 'increment' } ) ) . toNotBe ( initialState )
152
171
} )
153
172
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' , ( ) => {
155
174
const reducer = combineReducers ( {
156
175
counter ( state , action ) {
157
176
switch ( action . type ) {
You can’t perform that action at this time.
0 commit comments