From 92555a82c505e1c18bb6526b1dfc1b1a0ee192a2 Mon Sep 17 00:00:00 2001 From: Jose R Villalon Soler Date: Fri, 7 Jul 2017 16:27:40 -0400 Subject: [PATCH] pass shorthand arguments to reducers --- docs/api/handleAction.md | 13 ++++++++++++- src/__tests__/handleAction-test.js | 19 +++++++++++++++++++ src/handleAction.js | 8 +++++++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/docs/api/handleAction.md b/docs/api/handleAction.md index a1be6e4e..c6af56d2 100644 --- a/docs/api/handleAction.md +++ b/docs/api/handleAction.md @@ -29,13 +29,24 @@ import { handleAction } from 'redux-actions'; If a `reducer` function is passed, it is used to handle both normal actions and failed actions. (A failed action is analogous to a rejected promise.) You can use this form if you know a certain type of action will never fail, like the increment example above. +The reducer function gets recieves the following arguments + +1. `state`: The current redux state + +1. `action`: The redux action + + and as a shorthand, for easier desctructuring: +3. `payload`: The fsa payload of the action, *action.payload* +4. `meta`: The metadata of the action, *action.meta* +5. `error`: Boolean flag determining if this is an error, *action.error* + If the reducer argument (`reducer`) is `undefined`, then the identity function is used. The third parameter `defaultState` is required, and is used when `undefined` is passed to the reducer. ###### EXAMPLE ```js -handleAction('APP/COUNTER/INCREMENT', (state, action) => ({ +handleAction('APP/COUNTER/INCREMENT', (state, action, payload, meta, error) => ({ counter: state.counter + action.payload.amount, }), defaultState); ``` diff --git a/src/__tests__/handleAction-test.js b/src/__tests__/handleAction-test.js index cdadf1a4..261b01b7 100644 --- a/src/__tests__/handleAction-test.js +++ b/src/__tests__/handleAction-test.js @@ -252,4 +252,23 @@ describe('handleAction()', () => { .to.deep.equal({ number: 3 }); }); }); + + describe('with shorthand arguments', () => { + it('recieves shorthand arguments', () => { + const reducer = handleAction( + type, + (state, action, payloadArg, metaArg, errorArg) => + ({ ...state, payloadArg, metaArg, errorArg }), + prevState, + ); + + const error = new Error(); + expect(reducer(prevState, { type, payload: 123 })) + .to.deep.equal({ ...prevState, payloadArg: 123, metaArg: undefined, errorArg: false }); + expect(reducer(prevState, { type, payload: 123, meta: 456 })) + .to.deep.equal({ ...prevState, payloadArg: 123, metaArg: 456, errorArg: false }); + expect(reducer(prevState, { type, payload: error, meta: 456, error: true })) + .to.deep.equal({ ...prevState, payloadArg: error, metaArg: 456, errorArg: true }); + }); + }); }); diff --git a/src/handleAction.js b/src/handleAction.js index 83456fc6..ba12bfb0 100644 --- a/src/handleAction.js +++ b/src/handleAction.js @@ -28,6 +28,12 @@ export default function handleAction(type, reducer = identity, defaultState) { return state; } - return (action.error === true ? throwReducer : nextReducer)(state, action); + return (action.error === true ? throwReducer : nextReducer)( + state, + action, + action.payload, + action.meta, + action.error === true + ); }; }