Skip to content

Commit

Permalink
fix: Throw error if createStore is passed several enhancers
Browse files Browse the repository at this point in the history
This commit adds a check for whether the user is passing several
enhancers to the `createStore` function.

Fixes #3114.
  • Loading branch information
Kristofer Selbekk committed Sep 27, 2018
1 parent 5345a9a commit 32acbea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
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()
})
})

0 comments on commit 32acbea

Please sign in to comment.