Skip to content

Commit a410afd

Browse files
committed
Warn when reducer is not a function
1 parent b02310b commit a410afd

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/combineReducers.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ export default function combineReducers(reducers) {
121121
if (process.env.NODE_ENV !== 'production') {
122122
if (typeof reducers[key] === 'undefined') {
123123
warning(`No reducer provided for key "${key}"`)
124+
} else if (typeof reducers[key] !== 'function') {
125+
warning(`The reducer for "${key}" is not a function`)
124126
}
125127
}
126128

test/combineReducers.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,31 @@ describe('Utils', () => {
196196
console.error = preSpy
197197
})
198198

199+
it('warns if a reducer is not a function', () => {
200+
const preSpy = console.error
201+
const spy = jest.fn()
202+
console.error = spy
203+
204+
let isFunction = state => null
205+
let isNotFunction = 'isNotFunction'
206+
let reducer = combineReducers({ isFunction, isNotFunction })
207+
reducer({})
208+
expect(spy.mock.calls[0][0]).toMatch(
209+
/The reducer for "isNotFunction" is not a function/
210+
)
211+
212+
spy.mockClear()
213+
isNotFunction = []
214+
reducer = combineReducers({ isFunction, isNotFunction })
215+
reducer({})
216+
expect(spy.mock.calls[0][0]).toMatch(
217+
/The reducer for "isNotFunction" is not a function/
218+
)
219+
220+
spy.mockClear()
221+
console.error = preSpy
222+
})
223+
199224
it('warns if input state does not match reducer shape', () => {
200225
const preSpy = console.error
201226
const spy = jest.fn()

0 commit comments

Comments
 (0)