Skip to content

Commit 5fb26a3

Browse files
Erik Michaelsontimdorr
Erik Michaelson
authored andcommitted
Add a warning for undefined properties passed to combineReducers (#1789)
* Add warning to combineReducers for undefined properties * Test that it warns for undefined reducer props * Duh remove only * add some newlines
1 parent 8636df8 commit 5fb26a3

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/combineReducers.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ export default function combineReducers(reducers) {
102102
var finalReducers = {}
103103
for (var i = 0; i < reducerKeys.length; i++) {
104104
var key = reducerKeys[i]
105+
106+
if (process.env.NODE_ENV !== 'production') {
107+
if (typeof reducers[key] === 'undefined') {
108+
warning(`No reducer provided for key "${key}"`)
109+
}
110+
}
111+
105112
if (typeof reducers[key] === 'function') {
106113
finalReducers[key] = reducers[key]
107114
}

test/combineReducers.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ describe('Utils', () => {
3131
).toEqual([ 'stack' ])
3232
})
3333

34+
it('warns if a reducer prop is undefined', () => {
35+
const spy = expect.spyOn(console, 'error')
36+
37+
let isNotDefined
38+
combineReducers({ isNotDefined })
39+
expect(spy.calls[0].arguments[0]).toMatch(
40+
/No reducer provided for key "isNotDefined"/
41+
)
42+
43+
spy.reset()
44+
combineReducers({ thing: undefined })
45+
expect(spy.calls[0].arguments[0]).toMatch(
46+
/No reducer provided for key "thing"/
47+
)
48+
49+
spy.restore()
50+
})
51+
3452
it('throws an error if a reducer returns undefined handling an action', () => {
3553
const reducer = combineReducers({
3654
counter(state = 0, action) {

0 commit comments

Comments
 (0)