UnitTest Jest for Redatch中的调度Action

时间:2017-07-10 10:14:02

标签: redux jestjs

我是新手,用Jest对Redux进行单元测试。

我有以下行动:

export const stepDone = (step) => (dispatch) => {
    dispatch({type: STEP_DONE, payload: step});
}

如何测试此功能?

1 个答案:

答案 0 :(得分:0)

这样的事情应该有效:

//Mock the dispatch function with jest's built in mocking functions
const mockDispatch = jest.fn();
//Call the action
stepDone(999)(mockDispatch)
//Check it was called with the correct arguement
expect(mockDispatch).toHaveBeenCalledWith({type: STEP_DONE, payload: 999})

Redux的神奇之处在于,当您测试动作和缩减器时,您通常只是测试纯Javascript,因此它并不是特别复杂。