Skip to content

Commit 3ea269a

Browse files
author
Kristofer Selbekk
committed
fix: Throw error if createStore is passed several enhancers
This commit adds a check for whether the user is passing several enhancers to the `createStore` function. Fixes #3114.
1 parent 5345a9a commit 3ea269a

File tree

3 files changed

+64
-11
lines changed

3 files changed

+64
-11
lines changed

package-lock.json

Lines changed: 32 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/createStore.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ import isPlainObject from './utils/isPlainObject'
2929
* and subscribe to changes.
3030
*/
3131
export default function createStore(reducer, preloadedState, enhancer) {
32+
if (
33+
(typeof preloadedState === 'function' && typeof enhancer === 'function') ||
34+
(typeof enhancer === 'function' && typeof arguments[3] === 'function')
35+
) {
36+
throw new Error(
37+
'It looks like you are passing several store enhancers to ' +
38+
'createStore(). This is not supported. Instead, compose them ' +
39+
'together to a single function'
40+
)
41+
}
42+
3243
if (typeof preloadedState === 'function' && typeof enhancer === 'undefined') {
3344
enhancer = preloadedState
3445
preloadedState = undefined

test/createStore.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,4 +761,25 @@ describe('createStore', () => {
761761
expect(console.error.mock.calls.length).toBe(0)
762762
console.error = originalConsoleError
763763
})
764+
765+
it('throws if passing several enhancer functions without preloaded state', () => {
766+
const rootReducer = combineReducers(reducers)
767+
const dummyEnhancer = f => f
768+
expect(() =>
769+
createStore(rootReducer, dummyEnhancer, dummyEnhancer)
770+
).toThrow()
771+
})
772+
773+
it('throws if passing several enhancer functions with preloaded state', () => {
774+
const rootReducer = combineReducers(reducers)
775+
const dummyEnhancer = f => f
776+
expect(() =>
777+
createStore(
778+
rootReducer,
779+
{ todos: [] },
780+
dummyEnhancer,
781+
dummyEnhancer
782+
)
783+
).toThrow()
784+
})
764785
})

0 commit comments

Comments
 (0)