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

fix: Throw error if createStore is passed several enhancers #3151

Merged
merged 1 commit into from
Sep 27, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/createStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ import isPlainObject from './utils/isPlainObject'
* and subscribe to changes.
*/
export default function createStore(reducer, preloadedState, enhancer) {
if (
(typeof preloadedState === 'function' && typeof enhancer === 'function') ||
(typeof enhancer === 'function' && typeof arguments[3] === 'function')
) {
throw new Error(
'It looks like you are passing several store enhancers to ' +
'createStore(). This is not supported. Instead, compose them ' +
'together to a single function'
)
}

if (typeof preloadedState === 'function' && typeof enhancer === 'undefined') {
enhancer = preloadedState
preloadedState = undefined
Expand Down
16 changes: 16 additions & 0 deletions test/createStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -761,4 +761,20 @@ describe('createStore', () => {
expect(console.error.mock.calls.length).toBe(0)
console.error = originalConsoleError
})

it('throws if passing several enhancer functions without preloaded state', () => {
const rootReducer = combineReducers(reducers)
const dummyEnhancer = f => f
expect(() =>
createStore(rootReducer, dummyEnhancer, dummyEnhancer)
).toThrow()
})

it('throws if passing several enhancer functions with preloaded state', () => {
const rootReducer = combineReducers(reducers)
const dummyEnhancer = f => f
expect(() =>
createStore(rootReducer, { todos: [] }, dummyEnhancer, dummyEnhancer)
).toThrow()
})
})