TypeError:无法读取未定义的属性“ ACTION-NAME”

时间:2019-07-24 14:17:44

标签: javascript reactjs redux jestjs

我正在尝试在我的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",

应用基于Fuse - React Redux Material Design Admin Template

0 个答案:

没有答案