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 check that reducer returns current state for unknown actions #1054

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/createStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import isPlainObject from './utils/isPlainObject'
* Do not reference these action types directly in your code.
*/
export var ActionTypes = {
INIT: '@@redux/INIT'
INIT: '@@redux/INIT_' + Math.random().toString(36).substring(7).split('').join('.')
}

/**
Expand Down
10 changes: 4 additions & 6 deletions src/utils/combineReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,11 @@ function assertReducerSanity(reducers) {
)
Copy link
Author

Choose a reason for hiding this comment

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

I changed the error message here because if the user doesn't have a default case in their code, this is the error that will be thrown.

}

var type = '@@redux/PROBE_UNKNOWN_ACTION_' + Math.random().toString(36).substring(7).split('').join('.')
if (typeof reducer(undefined, { type }) === 'undefined') {
var randomState = Math.random().toString(36).substring(7).split('').join('.')
if (reducer(randomState, { type: ActionTypes.INIT }) !== randomState) {
throw new Error(
`Reducer "${key}" returned undefined when probed with a random type. ` +
`Don't try to handle ${ActionTypes.INIT} or other actions in "redux/*" ` +
`namespace. They are considered private. Instead, you must return the ` +
`current state for any unknown actions, unless it is undefined, ` +
`Reducer "${key}" did not return the current state when probed with a random type. ` +
`You must return the current state for any unknown actions, unless it is undefined, ` +
`in which case you must return the initial state, regardless of the ` +
`action type. The initial state may not be undefined.`
)
Expand Down
12 changes: 5 additions & 7 deletions test/utils/combineReducers.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import expect from 'expect'
import { combineReducers } from '../../src'
import createStore, { ActionTypes } from '../../src/createStore'
import createStore from '../../src/createStore'

describe('Utils', () => {
describe('combineReducers', () => {
Expand Down Expand Up @@ -151,24 +151,22 @@ describe('Utils', () => {
expect(reducer(initialState, { type: 'increment' })).toNotBe(initialState)
})

it('throws an error on first call if a reducer attempts to handle a private action', () => {
it('throws an error if reducer does not return current state for all unknown action types', () => {
const reducer = combineReducers({
counter(state, action) {
switch (action.type) {
case 'increment':
return state + 1
case 'decrement':
return state - 1
// Never do this in your code:
case ActionTypes.INIT:
return 0
default:
return undefined
return 0
}
}
})
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we keep the test that if you forget a default case you'll get this warning? It's an important thing to remember to check.

Copy link
Author

Choose a reason for hiding this comment

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

Okay, I added it here.


expect(() => reducer()).toThrow(
/"counter".*private/
/"counter".*probed/
)
})

Expand Down