Skip to content

Commit f9bf59e

Browse files
yangmillstheorytimche
authored andcommitted
Check type of payload creator (#129)
1 parent 69f69ec commit f9bf59e

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ If you don’t use [npm](https://www.npmjs.com), you may grab the latest [UMD](h
2222
import { createAction } from 'redux-actions';
2323
```
2424

25-
Wraps an action creator so that its return value is the payload of a Flux Standard Action. If no payload creator is passed, or if it's not a function, the identity function is used.
25+
Wraps an action creator so that its return value is the payload of a Flux Standard Action.
26+
27+
`payloadCreator` must be a function or `undefined`. If `payloadCreator` is `undefined`, the identity function is used.
2628

2729
Example:
2830

src/__tests__/createAction-test.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,21 @@ describe('createAction()', () => {
2323
});
2424
});
2525

26-
it('uses identity function if payloadCreator is not a function', () => {
26+
it('should throw an error if payloadCreator is not a function or undefined', () => {
27+
const wrongTypePayloadCreators = [1, false, null, 'string', {}, []];
28+
29+
wrongTypePayloadCreators.forEach(wrongTypePayloadCreator => {
30+
expect(() => {
31+
createAction(type, wrongTypePayloadCreator);
32+
})
33+
.to.throw(
34+
Error,
35+
'Expected payloadCreator to be a function or undefined'
36+
);
37+
});
38+
});
39+
40+
it('uses identity function if payloadCreator is undefined', () => {
2741
const actionCreator = createAction(type);
2842
const foobar = { foo: 'bar' };
2943
const action = actionCreator(foobar);
@@ -35,7 +49,7 @@ describe('createAction()', () => {
3549
});
3650

3751
it('accepts a second parameter for adding meta to object', () => {
38-
const actionCreator = createAction(type, null, ({ cid }) => ({ cid }));
52+
const actionCreator = createAction(type, undefined, ({ cid }) => ({ cid }));
3953
const foobar = { foo: 'bar', cid: 5 };
4054
const action = actionCreator(foobar);
4155
expect(action).to.deep.equal({
@@ -70,7 +84,7 @@ describe('createAction()', () => {
7084
});
7185

7286
it('sets error to true if payload is an Error object and meta is provided', () => {
73-
const actionCreator = createAction(type, null, (_, meta) => meta);
87+
const actionCreator = createAction(type, undefined, (_, meta) => meta);
7488
const errObj = new TypeError('this is an error');
7589

7690
const errAction = actionCreator(errObj, { foo: 'bar' });
@@ -94,7 +108,7 @@ describe('createAction()', () => {
94108
});
95109

96110
const baz = '1';
97-
const actionCreator = createAction(type, null, () => ({ bar: baz }));
111+
const actionCreator = createAction(type, undefined, () => ({ bar: baz }));
98112
expect(actionCreator()).to.deep.equal({
99113
type,
100114
meta: {

src/createAction.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import identity from 'lodash/identity';
2+
import isFunction from 'lodash/isFunction';
23
import isUndefined from 'lodash/isUndefined';
4+
import invariant from 'invariant';
35

4-
export default function createAction(type, payloadCreator, metaCreator) {
5-
const finalPayloadCreator = typeof payloadCreator === 'function'
6-
? payloadCreator
7-
: identity;
6+
export default function createAction(type, payloadCreator = identity, metaCreator) {
7+
invariant(
8+
isFunction(payloadCreator),
9+
'Expected payloadCreator to be a function or undefined'
10+
);
811

912
const actionCreator = (...args) => {
1013
const hasError = args[0] instanceof Error;
@@ -13,7 +16,7 @@ export default function createAction(type, payloadCreator, metaCreator) {
1316
type
1417
};
1518

16-
const payload = hasError ? args[0] : finalPayloadCreator(...args);
19+
const payload = hasError ? args[0] : payloadCreator(...args);
1720
if (!isUndefined(payload)) {
1821
action.payload = payload;
1922
}
@@ -23,7 +26,7 @@ export default function createAction(type, payloadCreator, metaCreator) {
2326
action.error = true;
2427
}
2528

26-
if (typeof metaCreator === 'function') {
29+
if (isFunction(metaCreator)) {
2730
action.meta = metaCreator(...args);
2831
}
2932

0 commit comments

Comments
 (0)