From 8e8e6981e7159270e7c4341729b543a951fbc145 Mon Sep 17 00:00:00 2001 From: Nikita Grafov Date: Sat, 26 Dec 2015 16:08:55 +0300 Subject: [PATCH 1/4] Use action function as handleAction(s) type param --- src/createAction.js | 7 +++++-- src/handleAction.js | 6 +++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/createAction.js b/src/createAction.js index 7dfd752c..e53b1ac6 100644 --- a/src/createAction.js +++ b/src/createAction.js @@ -6,8 +6,7 @@ export default function createAction(type, actionCreator, metaCreator) { const finalActionCreator = typeof actionCreator === 'function' ? actionCreator : identity; - - return (...args) => { + let actionHandler = (...args) => { const action = { type, payload: finalActionCreator(...args) @@ -24,4 +23,8 @@ export default function createAction(type, actionCreator, metaCreator) { return action; }; + + actionHandler.toString = () => type; + + return actionHandler; } diff --git a/src/handleAction.js b/src/handleAction.js index 2ad75534..ef8968de 100644 --- a/src/handleAction.js +++ b/src/handleAction.js @@ -5,9 +5,13 @@ function isFunction(val) { } export default function handleAction(type, reducers) { + const typeValue = isFunction(type) + ? type.toString() + : type; + return (state, action) => { // If action type does not match, return previous state - if (action.type !== type) return state; + if (action.type !== typeValue) return state; const handlerKey = isError(action) ? 'throw' : 'next'; From 6da154fb8260d82131b60dab134ccd1a58defb35 Mon Sep 17 00:00:00 2001 From: Nikita Grafov Date: Sat, 21 May 2016 18:37:32 +0300 Subject: [PATCH 2/4] fix for review --- src/createAction.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/createAction.js b/src/createAction.js index e53b1ac6..4e09d82f 100644 --- a/src/createAction.js +++ b/src/createAction.js @@ -6,7 +6,7 @@ export default function createAction(type, actionCreator, metaCreator) { const finalActionCreator = typeof actionCreator === 'function' ? actionCreator : identity; - let actionHandler = (...args) => { + const actionHandler = (...args) => { const action = { type, payload: finalActionCreator(...args) From 65770ddf9e168acb51dace67e9e49286ab0486e9 Mon Sep 17 00:00:00 2001 From: Nikita Grafov Date: Sat, 21 May 2016 19:14:39 +0300 Subject: [PATCH 3/4] added tests --- src/__tests__/handleAction-test.js | 14 +++++++++++++- src/__tests__/handleActions-test.js | 16 +++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/__tests__/handleAction-test.js b/src/__tests__/handleAction-test.js index ba4a3f7f..d2cdfccf 100644 --- a/src/__tests__/handleAction-test.js +++ b/src/__tests__/handleAction-test.js @@ -1,4 +1,4 @@ -import { handleAction } from '../'; +import { handleAction, createAction } from '../'; describe('handleAction()', () => { const type = 'TYPE'; @@ -20,6 +20,18 @@ describe('handleAction()', () => { counter: 10 }); }); + + it('accepts action function as action type', () => { + const incrementAction = createAction(type); + const reducer = handleAction(incrementAction, (state, action) => ({ + counter: state.counter + action.payload + })); + + expect(reducer(prevState, incrementAction(7))) + .to.deep.equal({ + counter: 10 + }); + }); }); }); diff --git a/src/__tests__/handleActions-test.js b/src/__tests__/handleActions-test.js index 0a8b9409..950c2a89 100644 --- a/src/__tests__/handleActions-test.js +++ b/src/__tests__/handleActions-test.js @@ -1,4 +1,4 @@ -import { handleActions } from '../'; +import { handleActions, createAction } from '../'; describe('handleActions', () => { it('create a single handler from a map of multiple action handlers', () => { @@ -53,4 +53,18 @@ describe('handleActions', () => { counter: 10 }); }); + + it('accepts action function as action type', () => { + const incrementAction = createAction('INCREMENT'); + const reducer = handleActions({ + [incrementAction]: ({ counter }, { payload: amount }) => ({ + counter: counter + amount + }) + }); + + expect(reducer({ counter: 3 }, incrementAction(7))) + .to.deep.equal({ + counter: 10 + }); + }); }); From a03e9a7076d977f16a9e36a9a44e530b70039477 Mon Sep 17 00:00:00 2001 From: Nikita Grafov Date: Sat, 21 May 2016 20:06:56 +0300 Subject: [PATCH 4/4] fix lint errors --- src/__tests__/handleAction-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/__tests__/handleAction-test.js b/src/__tests__/handleAction-test.js index a11bd3ba..6d8ee0a4 100644 --- a/src/__tests__/handleAction-test.js +++ b/src/__tests__/handleAction-test.js @@ -40,7 +40,7 @@ describe('handleAction()', () => { counter: 10 }); }); - + it('accepts single function as handler and a default state', () => { const reducer = handleAction(type, (state, action) => ({ counter: state.counter + action.payload