我正在尝试在我的React应用中使用Jest编写一些reducer测试
下面是示例代码
动作
export const testActions ={
DO_SOMETHING : 'DO_SOMETHING'
}
export function doSomething(){
return (dispatch) => {
dispatch({
type: testActions.DO_SOMETHING
});
}
}
减速器
export const handlers ={
[testActions.DO_SOMETHING]: (state, payload) => ({
...state,
didSomething: true
})
}
const testReducer = (state = { ...defaultState }, action) => {
return handlers.hasOwnProperty(action.type) ? handlers[action.type](state, action.payload) : state;
}
export default testReducer;
减速器测试
describe('test-Reducer', () => {
it('DO_SOMETHING', () => {
const defaultTestState = defaultState;
const expectedAction = {
type: testActions.DO_SOMETHING
};
let leadState = testReducer(defaultTestState, expectedAction);
expect(leadState).toEqual({
...defaultTestState,
didSomething: true
});
});
});
当我尝试运行测试时,出现以下错误消息
TypeError: Cannot read property 'DO_SOMETHING' of undefined
这指向线下的减速器
[testActions.DO_SOMETHING]: (state, payload) => ({...
版本如下
"react": "16.5.2",
"jest": "^23.6.0",