From 6da2e71425264b7eea452be2620d940b78fafac2 Mon Sep 17 00:00:00 2001 From: Victor Alvarez Date: Wed, 23 Nov 2016 08:29:59 -0800 Subject: [PATCH 1/3] WIP --- README.md | 2 +- src/__tests__/createAction-test.js | 17 ++++++++++++++--- src/createAction.js | 9 ++++++--- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 53f01dea..06f4bd6d 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ import { createAction } from 'redux-actions'; 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. +`payloadCreator` must be a function, `undefined`, or `null`. If `payloadCreator` is `undefined` or `null`, the identity function is used. Example: diff --git a/src/__tests__/createAction-test.js b/src/__tests__/createAction-test.js index fed1a50e..8ba753d8 100644 --- a/src/__tests__/createAction-test.js +++ b/src/__tests__/createAction-test.js @@ -23,8 +23,8 @@ describe('createAction()', () => { }); }); - it('should throw an error if payloadCreator is not a function or undefined', () => { - const wrongTypePayloadCreators = [1, false, null, 'string', {}, []]; + it('should throw an error if payloadCreator is not a function, undefined, null', () => { + const wrongTypePayloadCreators = [1, false, 'string', {}, []]; wrongTypePayloadCreators.forEach(wrongTypePayloadCreator => { expect(() => { @@ -32,7 +32,7 @@ describe('createAction()', () => { }) .to.throw( Error, - 'Expected payloadCreator to be a function or undefined' + 'Expected payloadCreator to be a function, undefined, or null' ); }); }); @@ -48,6 +48,17 @@ describe('createAction()', () => { expect(isFSA(action)).to.be.true; }); + it('uses identity function if payloadCreator is null', () => { + const actionCreator = createAction(type, null); + const foobar = { foo: 'bar' }; + const action = actionCreator(foobar); + expect(action).to.deep.equal({ + type, + payload: foobar + }); + expect(isFSA(action)).to.be.true; + }); + it('accepts a second parameter for adding meta to object', () => { const actionCreator = createAction(type, undefined, ({ cid }) => ({ cid })); const foobar = { foo: 'bar', cid: 5 }; diff --git a/src/createAction.js b/src/createAction.js index e8db7daa..f7e8599f 100644 --- a/src/createAction.js +++ b/src/createAction.js @@ -1,14 +1,17 @@ import identity from 'lodash/identity'; import isFunction from 'lodash/isFunction'; import isUndefined from 'lodash/isUndefined'; +import isNull from 'lodash/isNull'; import invariant from 'invariant'; export default function createAction(type, payloadCreator = identity, metaCreator) { invariant( - isFunction(payloadCreator), - 'Expected payloadCreator to be a function or undefined' + isFunction(payloadCreator) || isNull(payloadCreator), + 'Expected payloadCreator to be a function, undefined, or null' ); + const finalPayloadCreator = isNull(payloadCreator) ? identity : payloadCreator; + const actionCreator = (...args) => { const hasError = args[0] instanceof Error; @@ -16,7 +19,7 @@ export default function createAction(type, payloadCreator = identity, metaCreato type }; - const payload = hasError ? args[0] : payloadCreator(...args); + const payload = hasError ? args[0] : finalPayloadCreator(...args); if (!isUndefined(payload)) { action.payload = payload; } From 32bd22a997ecac16fee5b6ed5390731d58fe7499 Mon Sep 17 00:00:00 2001 From: Victor Alvarez Date: Wed, 23 Nov 2016 08:33:05 -0800 Subject: [PATCH 2/3] WIP --- src/createAction.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/createAction.js b/src/createAction.js index f7e8599f..55b1426d 100644 --- a/src/createAction.js +++ b/src/createAction.js @@ -10,7 +10,9 @@ export default function createAction(type, payloadCreator = identity, metaCreato 'Expected payloadCreator to be a function, undefined, or null' ); - const finalPayloadCreator = isNull(payloadCreator) ? identity : payloadCreator; + const finalPayloadCreator = isNull(payloadCreator) + ? identity + : payloadCreator; const actionCreator = (...args) => { const hasError = args[0] instanceof Error; From 06ec2b08189748f9d7902c9bbc6f7eee67bdea38 Mon Sep 17 00:00:00 2001 From: Victor Alvarez Date: Wed, 23 Nov 2016 08:42:35 -0800 Subject: [PATCH 3/3] WIP --- src/__tests__/createAction-test.js | 2 +- src/createAction.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/__tests__/createAction-test.js b/src/__tests__/createAction-test.js index 8ba753d8..4ca5af4d 100644 --- a/src/__tests__/createAction-test.js +++ b/src/__tests__/createAction-test.js @@ -32,7 +32,7 @@ describe('createAction()', () => { }) .to.throw( Error, - 'Expected payloadCreator to be a function, undefined, or null' + 'Expected payloadCreator to be a function, undefined or null' ); }); }); diff --git a/src/createAction.js b/src/createAction.js index 55b1426d..a6f7f9f2 100644 --- a/src/createAction.js +++ b/src/createAction.js @@ -7,7 +7,7 @@ import invariant from 'invariant'; export default function createAction(type, payloadCreator = identity, metaCreator) { invariant( isFunction(payloadCreator) || isNull(payloadCreator), - 'Expected payloadCreator to be a function, undefined, or null' + 'Expected payloadCreator to be a function, undefined or null' ); const finalPayloadCreator = isNull(payloadCreator)