Skip to content

Check type of payload creator #129

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

Merged
merged 5 commits into from
Nov 4, 2016
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: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ If you don’t use [npm](https://www.npmjs.com), you may grab the latest [UMD](h
import { createAction } from 'redux-actions';
```

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.
Wraps an action creator so that its return value is the payload of a Flux Standard Action.

`payloadCreator` must be a function or `undefined`. If `payloadCreator` is `undefined`, the identity function is used.

Example:

Expand Down
22 changes: 18 additions & 4 deletions src/__tests__/createAction-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,21 @@ describe('createAction()', () => {
});
});

it('uses identity function if payloadCreator is not a function', () => {
it('should throw an error if payloadCreator is not a function or undefined', () => {
const wrongTypePayloadCreators = [1, false, null, 'string', {}, []];

wrongTypePayloadCreators.forEach(wrongTypePayloadCreator => {
expect(() => {
createAction(type, wrongTypePayloadCreator);
})
.to.throw(
Error,
'Expected payloadCreator to be a function or undefined'
);
});
});

it('uses identity function if payloadCreator is undefined', () => {
const actionCreator = createAction(type);
const foobar = { foo: 'bar' };
const action = actionCreator(foobar);
Expand All @@ -35,7 +49,7 @@ describe('createAction()', () => {
});

it('accepts a second parameter for adding meta to object', () => {
const actionCreator = createAction(type, null, ({ cid }) => ({ cid }));
const actionCreator = createAction(type, undefined, ({ cid }) => ({ cid }));
const foobar = { foo: 'bar', cid: 5 };
const action = actionCreator(foobar);
expect(action).to.deep.equal({
Expand Down Expand Up @@ -70,7 +84,7 @@ describe('createAction()', () => {
});

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

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

const baz = '1';
const actionCreator = createAction(type, null, () => ({ bar: baz }));
const actionCreator = createAction(type, undefined, () => ({ bar: baz }));
expect(actionCreator()).to.deep.equal({
type,
meta: {
Expand Down
15 changes: 9 additions & 6 deletions src/createAction.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import identity from 'lodash/identity';
import isFunction from 'lodash/isFunction';
import isUndefined from 'lodash/isUndefined';
import invariant from 'invariant';

export default function createAction(type, payloadCreator, metaCreator) {
const finalPayloadCreator = typeof payloadCreator === 'function'
? payloadCreator
: identity;
export default function createAction(type, payloadCreator = identity, metaCreator) {
invariant(
isFunction(payloadCreator),
'Expected payloadCreator to be a function or undefined'
);

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

const payload = hasError ? args[0] : finalPayloadCreator(...args);
const payload = hasError ? args[0] : payloadCreator(...args);
if (!isUndefined(payload)) {
action.payload = payload;
}
Expand All @@ -23,7 +26,7 @@ export default function createAction(type, payloadCreator, metaCreator) {
action.error = true;
}

if (typeof metaCreator === 'function') {
if (isFunction(metaCreator)) {
action.meta = metaCreator(...args);
}

Expand Down