From 1b154e0ba79ff3428342618f5e437363f9e73569 Mon Sep 17 00:00:00 2001 From: Art Gillespie Date: Tue, 7 Mar 2017 16:56:28 -0800 Subject: [PATCH] Add warning when bindActionCreators encounters non-function property (#2279) --- src/bindActionCreators.js | 4 ++++ test/bindActionCreators.spec.js | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/src/bindActionCreators.js b/src/bindActionCreators.js index 9b0cc061eb..d38c2d5670 100644 --- a/src/bindActionCreators.js +++ b/src/bindActionCreators.js @@ -1,3 +1,5 @@ +import warning from './utils/warning' + function bindActionCreator(actionCreator, dispatch) { return (...args) => dispatch(actionCreator(...args)) } @@ -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 diff --git a/test/bindActionCreators.spec.js b/test/bindActionCreators.spec.js index f4a8cb2b44..14ceb359be 100644 --- a/test/bindActionCreators.spec.js +++ b/test/bindActionCreators.spec.js @@ -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) @@ -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, @@ -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', () => {