Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@
"eslint-config-airbnb-base": "^1.0.3",
"eslint-plugin-import": "^1.5.0",
"eslint-watch": "^2.1.13",
"flux-standard-action": "^0.6.0",
"mocha": "^2.2.5",
"rimraf": "^2.5.3",
"webpack": "^1.13.1"
},
"dependencies": {
"flux-standard-action": "^1.0.0",
"invariant": "^2.2.1",
"lodash": "^4.13.1",
"reduce-reducers": "^0.1.0"
}
Expand Down
33 changes: 33 additions & 0 deletions src/__tests__/handleAction-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import identity from 'lodash/identity';
import { handleAction, createAction, createActions, combineActions } from '../';

describe('handleAction()', () => {
Expand Down Expand Up @@ -195,4 +196,36 @@ describe('handleAction()', () => {
.to.deep.equal({ number: 3 });
});
});

describe('with invalid actions', () => {
it('should throw a descriptive error when the action object is missing', () => {
const reducer = handleAction(createAction('ACTION_1'), identity);
expect(
() => reducer(undefined)
).to.throw(
Error,
'The FSA spec mandates an action object with a type. Try using the createAction(s) method.'
);
});

it('should throw a descriptive error when the action type is missing', () => {
const reducer = handleAction(createAction('ACTION_1'), identity);
expect(
() => reducer(undefined, {})
).to.throw(
Error,
'The FSA spec mandates an action object with a type. Try using the createAction(s) method.'
);
});

it('should throw a descriptive error when the action type is not a string or symbol', () => {
const reducer = handleAction(createAction('ACTION_1'), identity);
expect(
() => reducer(undefined, { type: false })
).to.throw(
Error,
'The FSA spec mandates an action object with a type. Try using the createAction(s) method.'
);
});
});
});
7 changes: 7 additions & 0 deletions src/handleAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import isFunction from 'lodash/isFunction';
import identity from 'lodash/identity';
import isNil from 'lodash/isNil';
import includes from 'lodash/includes';
import invariant from 'invariant';
import { isFSA } from 'flux-standard-action';
import { ACTION_TYPE_DELIMITER } from './combineActions';

export default function handleAction(actionType, reducers, defaultState) {
Expand All @@ -12,6 +14,11 @@ export default function handleAction(actionType, reducers, defaultState) {
: [reducers.next, reducers.throw].map(reducer => (isNil(reducer) ? identity : reducer));

return (state = defaultState, action) => {
invariant(
isFSA(action),
'The FSA spec mandates an action object with a type. Try using the createAction(s) method.'
);

if (!includes(actionTypes, action.type.toString())) {
return state;
}
Expand Down