我在尝试执行基本的reducer测试时遇到错误,例如在redux文档中找到的测试:https://redux.js.org/recipes/writing-tests#reducers
TypeError: Cannot read property 'GROUP_LIST_REQUEST' of undefined
22 |
23 | switch (action.type) {
> 24 | case GROUP_LIST_REQUEST:
25 | return {
26 | ...state,
27 | fetching: true,
at group (app/reducers/group.js:24:10)
at node_modules/redux/lib/combineReducers.js:53:24
at Array.forEach (native)
at assertReducerShape (node_modules/redux/lib/combineReducers.js:51:25)
at combineReducers (node_modules/redux/lib/combineReducers.js:107:5)
at Object.<anonymous> (app/reducers/all.js:23:16)
at Object.<anonymous> (app/store.js:31:1)
at Object.<anonymous> (app/models/api.js:31:1)
at Object.<anonymous> (app/actions/group.js:1:1)
at Object.<anonymous> (app/reducers/group.js:1:1)
at Object.<anonymous> (__tests__/groups/reducer.js:6:1)
当我只想单独运行我的reducer方法时,堆栈跟踪似乎意味着整个应用程序正在运行。
但更奇怪的是,它无法在减速器开关中找到我的动作类型,而我甚至没有在测试中使用。
import reducer from '../../app/reducers/group';
import * as actions from '../../app/actions/group';
describe('group reducer', () => {
test('should return the initial state', () => {
expect(reducer(undefined, {})).toEqual({
selected: null,
fetching: false,
fetched: false,
error: null,
items: [],
});
});
});
减速机:
import {
GROUP_LIST_REQUEST,
GROUP_LIST_SUCCESS,
GROUP_LIST_FAILURE,
} from '../actions/group';
export const initialState = {
selected: null,
fetching: false,
fetched: false,
error: null,
items: [],
};
export default function group(state = initialState, action) {
console.log(action);
switch (action.type) {
case GROUP_LIST_REQUEST:
return {
...state,
fetching: true,
fetched: false,
};
case GROUP_LIST_SUCCESS:
return {
...state,
fetching: false,
fetched: true,
items: action.groupList === undefined ? [] : action.groupList,
};
case GROUP_LIST_FAILURE:
return {
...state,
fetching: false,
fetched: false,
};
default:
return state;
}
}
当我记录动作时,我得到{ type: '@@redux/INIT' }
这很奇怪,因为测试我传递的是一个空对象。