如何使用 Jest 测试 Thunk 操作? [英] How can I test Thunk actions with Jest?
本文介绍了如何使用 Jest 测试 Thunk 操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是使用 Jest 对 Redux-Thunk 异步操作进行单元测试的新手.
I'm new to unit testing Redux-Thunk async actions using Jest.
这是我的代码:
export const functionA = (a, b) => (dispatch) => {
dispatch({ type: CONSTANT_A, payload: a });
dispatch({ type: CONSTANT_B, payload: b });
}
如何使用 Jest 测试此功能?
How can I test this function using Jest?
推荐答案
您在 Redux 文档中有一个示例:http://redux.js.org/docs/recipes/WritingTests.html#async-action-creators
You have an example in the Redux docs: http://redux.js.org/docs/recipes/WritingTests.html#async-action-creators
import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
const middlewares = [thunk]
const mockStore = configureMockStore(middleware)
describe('async actions', () => {
it('should dispatch actions of ConstantA and ConstantB', () => {
const expectedActions = [
{type: CONSTANT_A, payload: 'a'},
{type: CONSTANT_B, payload: 'b'}
]
const store = mockStore({ yourInitialState })
store.dispatch(actions.functionA('a', 'b'))
expect(store.getActions()).toEqual(expectedActions)
})
})
这篇关于如何使用 Jest 测试 Thunk 操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文