Skip to content

Commit 78e5beb

Browse files
committed
Merge pull request #1321 from dzhiriki/feature/patch-1
replaceReducer() should throw if argument is not a function
2 parents 03134a8 + 7fa8e86 commit 78e5beb

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/createStore.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ export default function createStore(reducer, initialState, enhancer) {
159159
* @returns {void}
160160
*/
161161
function replaceReducer(nextReducer) {
162+
if (typeof nextReducer !== 'function') {
163+
throw new Error('Expected the nextReducer to be a function.')
164+
}
165+
162166
currentReducer = nextReducer
163167
dispatch({ type: ActionTypes.INIT })
164168
}

test/createStore.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,4 +536,16 @@ describe('createStore', () => {
536536
createStore(reducers.todos, {})
537537
).toNotThrow()
538538
})
539+
540+
it('throws if nextReducer is not a function', () => {
541+
const store = createStore(reducers.todos)
542+
543+
expect(() =>
544+
store.replaceReducer()
545+
).toThrow('Expected the nextReducer to be a function.')
546+
547+
expect(() =>
548+
store.replaceReducer(() => {})
549+
).toNotThrow()
550+
})
539551
})

0 commit comments

Comments
 (0)