Skip to content

Commit

Permalink
Add warning when bindActionCreators encounters non-function property (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
artgillespie authored and timdorr committed Mar 8, 2017
1 parent 55db7dd commit 1b154e0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/bindActionCreators.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warning from './utils/warning'

function bindActionCreator(actionCreator, dispatch) {
return (...args) => dispatch(actionCreator(...args))
}
Expand Down Expand Up @@ -42,6 +44,8 @@ export default function bindActionCreators(actionCreators, dispatch) {
const actionCreator = actionCreators[key]
if (typeof actionCreator === 'function') {
boundActionCreators[key] = bindActionCreator(actionCreator, dispatch)
} else {
warning(`bindActionCreators expected a function actionCreator for key '${key}', instead received type '${typeof actionCreator}'.`)
}
}
return boundActionCreators
Expand Down
9 changes: 9 additions & 0 deletions test/bindActionCreators.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ describe('bindActionCreators', () => {
})

it('wraps the action creators with the dispatch function', () => {
const _console = console
global.console = { error: jest.fn() }
const boundActionCreators = bindActionCreators(actionCreators, store.dispatch)
expect(
Object.keys(boundActionCreators)
Expand All @@ -31,9 +33,13 @@ describe('bindActionCreators', () => {
expect(store.getState()).toEqual([
{ id: 1, text: 'Hello' }
])
expect(console.error).toHaveBeenCalled()
global.console = _console
})

it('skips non-function values in the passed object', () => {
const _console = console
global.console = { error: jest.fn() }
const boundActionCreators = bindActionCreators({
...actionCreators,
foo: 42,
Expand All @@ -47,6 +53,9 @@ describe('bindActionCreators', () => {
).toEqual(
Object.keys(actionCreatorFunctions)
)
// 6 instead of 5 because of `__esModule: true` property from importing `actionCreators`
expect(console.error.mock.calls.length).toBe(6)
global.console = _console
})

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

0 comments on commit 1b154e0

Please sign in to comment.