Skip to content

Commit

Permalink
Add a warning for undefined properties passed to combineReducers (#1789)
Browse files Browse the repository at this point in the history
* Add warning to combineReducers for undefined properties

* Test that it warns for undefined reducer props

* Duh remove only

* add some newlines
  • Loading branch information
Erik Michaelson authored and timdorr committed Jul 19, 2016
1 parent 8636df8 commit 5fb26a3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/combineReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ export default function combineReducers(reducers) {
var finalReducers = {}
for (var i = 0; i < reducerKeys.length; i++) {
var key = reducerKeys[i]

if (process.env.NODE_ENV !== 'production') {
if (typeof reducers[key] === 'undefined') {
warning(`No reducer provided for key "${key}"`)
}
}

if (typeof reducers[key] === 'function') {
finalReducers[key] = reducers[key]
}
Expand Down
18 changes: 18 additions & 0 deletions test/combineReducers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ describe('Utils', () => {
).toEqual([ 'stack' ])
})

it('warns if a reducer prop is undefined', () => {
const spy = expect.spyOn(console, 'error')

let isNotDefined
combineReducers({ isNotDefined })
expect(spy.calls[0].arguments[0]).toMatch(
/No reducer provided for key "isNotDefined"/
)

spy.reset()
combineReducers({ thing: undefined })
expect(spy.calls[0].arguments[0]).toMatch(
/No reducer provided for key "thing"/
)

spy.restore()
})

it('throws an error if a reducer returns undefined handling an action', () => {
const reducer = combineReducers({
counter(state = 0, action) {
Expand Down

0 comments on commit 5fb26a3

Please sign in to comment.