Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: Warn when bindActionCreators encounters non-function property #2279

Merged
merged 1 commit into from
Mar 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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