Skip to content

Commit d1aa7d6

Browse files
nikita-graftimche
authored andcommitted
Use action function as handleAction(s) type param (#51)
* Use action function as handleAction(s) type param * fix for review * added tests * fix lint errors
1 parent 588692d commit d1aa7d6

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

src/__tests__/handleAction-test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { handleAction } from '../';
1+
import { handleAction, createAction } from '../';
22

33
describe('handleAction()', () => {
44
const type = 'TYPE';
@@ -29,6 +29,18 @@ describe('handleAction()', () => {
2929
});
3030
});
3131

32+
it('accepts action function as action type', () => {
33+
const incrementAction = createAction(type);
34+
const reducer = handleAction(incrementAction, (state, action) => ({
35+
counter: state.counter + action.payload
36+
}));
37+
38+
expect(reducer(prevState, incrementAction(7)))
39+
.to.deep.equal({
40+
counter: 10
41+
});
42+
});
43+
3244
it('accepts single function as handler and a default state', () => {
3345
const reducer = handleAction(type, (state, action) => ({
3446
counter: state.counter + action.payload

src/__tests__/handleActions-test.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { handleActions } from '../';
1+
import { handleActions, createAction } from '../';
22

33
describe('handleActions', () => {
44
it('create a single handler from a map of multiple action handlers', () => {
@@ -53,4 +53,18 @@ describe('handleActions', () => {
5353
counter: 10
5454
});
5555
});
56+
57+
it('accepts action function as action type', () => {
58+
const incrementAction = createAction('INCREMENT');
59+
const reducer = handleActions({
60+
[incrementAction]: ({ counter }, { payload: amount }) => ({
61+
counter: counter + amount
62+
})
63+
});
64+
65+
expect(reducer({ counter: 3 }, incrementAction(7)))
66+
.to.deep.equal({
67+
counter: 10
68+
});
69+
});
5670
});

src/createAction.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ export default function createAction(type, actionCreator, metaCreator) {
66
const finalActionCreator = typeof actionCreator === 'function'
77
? actionCreator
88
: identity;
9-
10-
return (...args) => {
9+
const actionHandler = (...args) => {
1110
const action = {
1211
type
1312
};
@@ -28,4 +27,8 @@ export default function createAction(type, actionCreator, metaCreator) {
2827

2928
return action;
3029
};
30+
31+
actionHandler.toString = () => type;
32+
33+
return actionHandler;
3134
}

src/handleAction.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ function isFunction(val) {
33
}
44

55
export default function handleAction(type, reducers, defaultState) {
6+
const typeValue = isFunction(type)
7+
? type.toString()
8+
: type;
9+
610
return (state = defaultState, action) => {
711
// If action type does not match, return previous state
8-
if (action.type !== type) return state;
12+
if (action.type !== typeValue) return state;
913

1014
const handlerKey = action.error === true ? 'throw' : 'next';
1115

0 commit comments

Comments
 (0)