Skip to content

Commit c51f62e

Browse files
Enforce reducer type in handleAction (#156)
1 parent 06c6dff commit c51f62e

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,17 @@ expect(actionThree(3)).to.deep.equal({
132132
});
133133
```
134134

135-
### `handleAction(type, reducer | reducerMap, defaultState)`
135+
### `handleAction(type, reducer | reducerMap = Identity, defaultState)`
136136

137137
```js
138138
import { handleAction } from 'redux-actions';
139139
```
140140

141141
Wraps a reducer so that it only handles Flux Standard Actions of a certain type.
142142

143-
If a single reducer 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.
143+
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.
144144

145-
Otherwise, you can specify separate reducers for `next()` and `throw()`. This API is inspired by the ES6 generator interface.
145+
Otherwise, you can specify separate reducers for `next()` and `throw()` using the `reducerMap` form. This API is inspired by the ES6 generator interface.
146146

147147
```js
148148
handleAction('FETCH_DATA', {
@@ -151,7 +151,9 @@ handleAction('FETCH_DATA', {
151151
}, defaultState);
152152
```
153153

154-
If either `next()` or `throw()` are `undefined` or `null`, then the identity function is used for that reducer.
154+
If either `next()` or `throw()` are `undefined` or `null`, then the identity function is used for that reducer.
155+
156+
If the reducer argument (`reducer | reducerMap`) is `undefined`, then the identity function is used.
155157

156158
The third parameter `defaultState` is required, and is used when `undefined` is passed to the reducer.
157159

src/__tests__/handleAction-test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ describe('handleAction()', () => {
77
const prevState = { counter: 3 };
88
const defaultState = { counter: 0 };
99

10+
it('should throw an error if the reducer is the wrong type', () => {
11+
const wrongTypeReducers = [1, 'string', [], null];
12+
13+
wrongTypeReducers.forEach(wrongTypeReducer => {
14+
expect(() => {
15+
handleAction(type, wrongTypeReducer, defaultState);
16+
}).to.throw(
17+
Error,
18+
'Expected reducer to be a function or object with next and throw reducers'
19+
);
20+
});
21+
});
22+
23+
it('uses the identity if the specified reducer is undefined', () => {
24+
const reducer = handleAction(type, undefined, defaultState);
25+
26+
expect(reducer(prevState, { type })).to.equal(prevState);
27+
expect(reducer(prevState, { type, error: true, payload: new Error })).to.equal(prevState);
28+
});
29+
1030
describe('single handler form', () => {
1131
it('should throw an error if defaultState is not specified', () => {
1232
expect(() => {

src/handleAction.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import isFunction from 'lodash/isFunction';
2+
import isPlainObject from 'lodash/isPlainObject';
23
import identity from 'lodash/identity';
34
import isNil from 'lodash/isNil';
45
import isUndefined from 'lodash/isUndefined';
@@ -7,16 +8,20 @@ import invariant from 'invariant';
78
import { isFSA } from 'flux-standard-action';
89
import { ACTION_TYPE_DELIMITER } from './combineActions';
910

10-
export default function handleAction(actionType, reducers, defaultState) {
11+
export default function handleAction(actionType, reducer = identity, defaultState) {
1112
const actionTypes = actionType.toString().split(ACTION_TYPE_DELIMITER);
1213
invariant(
1314
!isUndefined(defaultState),
1415
`defaultState for reducer handling ${actionTypes.join(', ')} should be defined`
1516
);
17+
invariant(
18+
isFunction(reducer) || isPlainObject(reducer),
19+
'Expected reducer to be a function or object with next and throw reducers'
20+
);
1621

17-
const [nextReducer, throwReducer] = isFunction(reducers)
18-
? [reducers, reducers]
19-
: [reducers.next, reducers.throw].map(reducer => (isNil(reducer) ? identity : reducer));
22+
const [nextReducer, throwReducer] = isFunction(reducer)
23+
? [reducer, reducer]
24+
: [reducer.next, reducer.throw].map(aReducer => (isNil(aReducer) ? identity : aReducer));
2025

2126
return (state = defaultState, action) => {
2227
invariant(

0 commit comments

Comments
 (0)