Skip to content

Commit ea04ccf

Browse files
JaKXztimche
authored andcommitted
fix(handleAction): Add descriptive error for missing or invalid actions (#141)
close #145
1 parent aaaa110 commit ea04ccf

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@
5050
"eslint-config-airbnb-base": "^1.0.3",
5151
"eslint-plugin-import": "^1.5.0",
5252
"eslint-watch": "^2.1.13",
53-
"flux-standard-action": "^0.6.0",
5453
"mocha": "^2.2.5",
5554
"rimraf": "^2.5.3",
5655
"webpack": "^1.13.1"
5756
},
5857
"dependencies": {
58+
"flux-standard-action": "^1.0.0",
59+
"invariant": "^2.2.1",
5960
"lodash": "^4.13.1",
6061
"reduce-reducers": "^0.1.0"
6162
}

src/__tests__/handleAction-test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect } from 'chai';
2+
import identity from 'lodash/identity';
23
import { handleAction, createAction, createActions, combineActions } from '../';
34

45
describe('handleAction()', () => {
@@ -195,4 +196,36 @@ describe('handleAction()', () => {
195196
.to.deep.equal({ number: 3 });
196197
});
197198
});
199+
200+
describe('with invalid actions', () => {
201+
it('should throw a descriptive error when the action object is missing', () => {
202+
const reducer = handleAction(createAction('ACTION_1'), identity);
203+
expect(
204+
() => reducer(undefined)
205+
).to.throw(
206+
Error,
207+
'The FSA spec mandates an action object with a type. Try using the createAction(s) method.'
208+
);
209+
});
210+
211+
it('should throw a descriptive error when the action type is missing', () => {
212+
const reducer = handleAction(createAction('ACTION_1'), identity);
213+
expect(
214+
() => reducer(undefined, {})
215+
).to.throw(
216+
Error,
217+
'The FSA spec mandates an action object with a type. Try using the createAction(s) method.'
218+
);
219+
});
220+
221+
it('should throw a descriptive error when the action type is not a string or symbol', () => {
222+
const reducer = handleAction(createAction('ACTION_1'), identity);
223+
expect(
224+
() => reducer(undefined, { type: false })
225+
).to.throw(
226+
Error,
227+
'The FSA spec mandates an action object with a type. Try using the createAction(s) method.'
228+
);
229+
});
230+
});
198231
});

src/handleAction.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import isFunction from 'lodash/isFunction';
22
import identity from 'lodash/identity';
33
import isNil from 'lodash/isNil';
44
import includes from 'lodash/includes';
5+
import invariant from 'invariant';
6+
import { isFSA } from 'flux-standard-action';
57
import { ACTION_TYPE_DELIMITER } from './combineActions';
68

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

1416
return (state = defaultState, action) => {
17+
invariant(
18+
isFSA(action),
19+
'The FSA spec mandates an action object with a type. Try using the createAction(s) method.'
20+
);
21+
1522
if (!includes(actionTypes, action.type.toString())) {
1623
return state;
1724
}

0 commit comments

Comments
 (0)