Skip to content

Commit

Permalink
Merge pull request #411 from jarsbe/bind-warning
Browse files Browse the repository at this point in the history
Bind warning
  • Loading branch information
gaearon committed Aug 6, 2015
2 parents 4efcfcb + 22ca4be commit f614bf6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/utils/bindActionCreators.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import invariant from 'invariant';
import mapValues from '../utils/mapValues';

function bindActionCreator(actionCreator, dispatch) {
Expand Down Expand Up @@ -25,7 +26,15 @@ export default function bindActionCreators(actionCreators, dispatch) {
return bindActionCreator(actionCreators, dispatch);
}

invariant(
typeof actionCreators === 'object' && actionCreators != null,
'bindActionCreators expected an object or a function, instead received %s. ' +
'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?',
typeof actionCreators
);

return mapValues(actionCreators, actionCreator =>
bindActionCreator(actionCreator, dispatch)
);
}

27 changes: 27 additions & 0 deletions test/utils/bindActionCreators.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,31 @@ describe('bindActionCreators', () => {
{ id: 1, text: 'Hello' }
]);
});

it('should throw an invariant violation for an undefined actionCreator', () => {
expect(() => {
bindActionCreators(undefined, store.dispatch);
}).toThrow(
'bindActionCreators expected an object or a function, instead received undefined. ' +
'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?'
);
});

it('should throw an invariant violation for a null actionCreator', () => {
expect(() => {
bindActionCreators(null, store.dispatch);
}).toThrow(
'bindActionCreators expected an object or a function, instead received null. ' +
'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?'
);
});

it('should throw an invariant violation for a primitive actionCreator', () => {
expect(() => {
bindActionCreators('string', store.dispatch);
}).toThrow(
'bindActionCreators expected an object or a function, instead received string. ' +
'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?'
);
});
});

0 comments on commit f614bf6

Please sign in to comment.