Skip to content

Commit

Permalink
fix(dva-core): throw error when action's type is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Apr 10, 2018
1 parent 33d6f35 commit 9f33b35
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
17 changes: 9 additions & 8 deletions packages/dva-core/src/handleActions.js
@@ -1,3 +1,4 @@
import invariant from 'invariant';

function identify(value) {
return value;
Expand All @@ -6,23 +7,23 @@ function identify(value) {
function handleAction(actionType, reducer = identify) {
return (state, action) => {
const { type } = action;
if (type && actionType !== type) {
return state;
invariant(type, 'dispatch: action should be a plain Object with type');
if (actionType === type) {
return reducer(state, action);
}
return reducer(state, action);
return state;
};
}

function reduceReducers(...reducers) {
return (previous, current) =>
reducers.reduce(
(p, r) => r(p, current),
previous,
);
reducers.reduce((p, r) => r(p, current), previous);
}

function handleActions(handlers, defaultState) {
const reducers = Object.keys(handlers).map(type => handleAction(type, handlers[type]));
const reducers = Object.keys(handlers).map(type =>
handleAction(type, handlers[type])
);
const reducer = reduceReducers(...reducers);
return (state = defaultState, action) => reducer(state, action);
}
Expand Down
7 changes: 7 additions & 0 deletions packages/dva-core/test/handleActions.test.js
@@ -1,3 +1,4 @@
import expect from 'expect';
import handleActions from '../src/handleActions';

describe('handleActions', () => {
Expand Down Expand Up @@ -47,4 +48,10 @@ describe('handleActions', () => {
it('uses the identity if the specified reducer is undefined', () => {
expect(reducers(initialState, { type: LOGIN_SAVE })).toBe(initialState);
});

it('dispatch not valid action', () => {
expect(() => {
reducers(initialState, { type: '' });
}).toThrow(/dispatch: action should be a plain Object with type/);
});
});

0 comments on commit 9f33b35

Please sign in to comment.