Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce reducer type in handleAction #156

Merged
merged 9 commits into from
Nov 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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