Skip to content

Commit 1b154e0

Browse files
artgillespietimdorr
authored andcommitted
Add warning when bindActionCreators encounters non-function property (#2279)
1 parent 55db7dd commit 1b154e0

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/bindActionCreators.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import warning from './utils/warning'
2+
13
function bindActionCreator(actionCreator, dispatch) {
24
return (...args) => dispatch(actionCreator(...args))
35
}
@@ -42,6 +44,8 @@ export default function bindActionCreators(actionCreators, dispatch) {
4244
const actionCreator = actionCreators[key]
4345
if (typeof actionCreator === 'function') {
4446
boundActionCreators[key] = bindActionCreator(actionCreator, dispatch)
47+
} else {
48+
warning(`bindActionCreators expected a function actionCreator for key '${key}', instead received type '${typeof actionCreator}'.`)
4549
}
4650
}
4751
return boundActionCreators

test/bindActionCreators.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ describe('bindActionCreators', () => {
1717
})
1818

1919
it('wraps the action creators with the dispatch function', () => {
20+
const _console = console
21+
global.console = { error: jest.fn() }
2022
const boundActionCreators = bindActionCreators(actionCreators, store.dispatch)
2123
expect(
2224
Object.keys(boundActionCreators)
@@ -31,9 +33,13 @@ describe('bindActionCreators', () => {
3133
expect(store.getState()).toEqual([
3234
{ id: 1, text: 'Hello' }
3335
])
36+
expect(console.error).toHaveBeenCalled()
37+
global.console = _console
3438
})
3539

3640
it('skips non-function values in the passed object', () => {
41+
const _console = console
42+
global.console = { error: jest.fn() }
3743
const boundActionCreators = bindActionCreators({
3844
...actionCreators,
3945
foo: 42,
@@ -47,6 +53,9 @@ describe('bindActionCreators', () => {
4753
).toEqual(
4854
Object.keys(actionCreatorFunctions)
4955
)
56+
// 6 instead of 5 because of `__esModule: true` property from importing `actionCreators`
57+
expect(console.error.mock.calls.length).toBe(6)
58+
global.console = _console
5059
})
5160

5261
it('supports wrapping a single function only', () => {

0 commit comments

Comments
 (0)