Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a warning for undefined properties passed to combineReducers #1789

Merged
merged 4 commits into from
Jul 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/combineReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,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}"`)
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Do you mind adding newlines before and after this new block? It makes it easier to spot the dev-only code.


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