Skip to content

Commit

Permalink
Enforce reducer type in handleAction (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
yangmillstheory committed Nov 4, 2016
1 parent 06c6dff commit c51f62e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,17 @@ expect(actionThree(3)).to.deep.equal({
});
```

### `handleAction(type, reducer | reducerMap, defaultState)`
### `handleAction(type, reducer | reducerMap = Identity, defaultState)`

```js
import { handleAction } from 'redux-actions';
```

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

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.
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.

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

```js
handleAction('FETCH_DATA', {
Expand All @@ -151,7 +151,9 @@ handleAction('FETCH_DATA', {
}, defaultState);
```

If either `next()` or `throw()` are `undefined` or `null`, then the identity function is used for that reducer.
If either `next()` or `throw()` are `undefined` or `null`, then the identity function is used for that reducer.

If the reducer argument (`reducer | reducerMap`) is `undefined`, then the identity function is used.

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

Expand Down
20 changes: 20 additions & 0 deletions src/__tests__/handleAction-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@ describe('handleAction()', () => {
const prevState = { counter: 3 };
const defaultState = { counter: 0 };

it('should throw an error if the reducer is the wrong type', () => {
const wrongTypeReducers = [1, 'string', [], null];

wrongTypeReducers.forEach(wrongTypeReducer => {
expect(() => {
handleAction(type, wrongTypeReducer, defaultState);
}).to.throw(
Error,
'Expected reducer to be a function or object with next and throw reducers'
);
});
});

it('uses the identity if the specified reducer is undefined', () => {
const reducer = handleAction(type, undefined, defaultState);

expect(reducer(prevState, { type })).to.equal(prevState);
expect(reducer(prevState, { type, error: true, payload: new Error })).to.equal(prevState);
});

describe('single handler form', () => {
it('should throw an error if defaultState is not specified', () => {
expect(() => {
Expand Down
13 changes: 9 additions & 4 deletions src/handleAction.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import isFunction from 'lodash/isFunction';
import isPlainObject from 'lodash/isPlainObject';
import identity from 'lodash/identity';
import isNil from 'lodash/isNil';
import isUndefined from 'lodash/isUndefined';
Expand All @@ -7,16 +8,20 @@ import invariant from 'invariant';
import { isFSA } from 'flux-standard-action';
import { ACTION_TYPE_DELIMITER } from './combineActions';

export default function handleAction(actionType, reducers, defaultState) {
export default function handleAction(actionType, reducer = identity, defaultState) {
const actionTypes = actionType.toString().split(ACTION_TYPE_DELIMITER);
invariant(
!isUndefined(defaultState),
`defaultState for reducer handling ${actionTypes.join(', ')} should be defined`
);
invariant(
isFunction(reducer) || isPlainObject(reducer),
'Expected reducer to be a function or object with next and throw reducers'
);

const [nextReducer, throwReducer] = isFunction(reducers)
? [reducers, reducers]
: [reducers.next, reducers.throw].map(reducer => (isNil(reducer) ? identity : reducer));
const [nextReducer, throwReducer] = isFunction(reducer)
? [reducer, reducer]
: [reducer.next, reducer.throw].map(aReducer => (isNil(aReducer) ? identity : aReducer));

return (state = defaultState, action) => {
invariant(
Expand Down

0 comments on commit c51f62e

Please sign in to comment.