diff --git a/README.md b/README.md index 9859f45b..53f01dea 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/__tests__/createAction-test.js b/src/__tests__/createAction-test.js index 230266aa..fed1a50e 100644 --- a/src/__tests__/createAction-test.js +++ b/src/__tests__/createAction-test.js @@ -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); @@ -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({ @@ -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' }); @@ -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: { diff --git a/src/createAction.js b/src/createAction.js index 0b1d378d..e8db7daa 100644 --- a/src/createAction.js +++ b/src/createAction.js @@ -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; @@ -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; } @@ -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); }