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 3ea269a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
43 changes: 32 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
21 changes: 21 additions & 0 deletions test/createStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -761,4 +761,25 @@ 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 3ea269a

Please sign in to comment.