Redux Jest调用异步函数
我在redux商店有一个功能Redux Jest调用异步函数,redux,jestjs,Redux,Jestjs,我在redux商店有一个功能 Func1(){ Dispatch(some action) Dispatch(some action1) result = Dispatch(some async action)\\promise retured result.then(()=> Dispatch(some other action) ) } 我的测试用例 Test(()=>{ Expectedactions= all 3 actions. Axios.post.mockim
Func1(){
Dispatch(some action)
Dispatch(some action1)
result = Dispatch(some async action)\\promise retured
result.then(()=>
Dispatch(some other action)
)
}
我的测试用例
Test(()=>{
Expectedactions= all 3 actions.
Axios.post.mockimplimetation(()=>
Promise.resolve(data))
calling Func1() from wrapper.
Expect(store.getActions()).toequal(expectedactions)
})
Test(()=>{
Expectedactions= all 3 actions.
Axios.post.mockimplimetation(()=>
Promise.resolve(data))
calling f1() from wrapper like
Wrapper.shallow(<TestContainer store=store/>)
Wrapper.find('Test').props().f1()
Expect(store.getActions()).toequal(expectedactions)
})
测试结果只显示了2个动作。
未捕获异步操作
请帮助我如何测试这种行为或我应该采取什么方法
编辑
TestConrainer.js
const mapstatetoprops= (state)=>{
return {}
}
const mergeprops =(stateprops,dispatchprops,ownprops)=>{
const {dispatch}= dispatchprops
const f1=()=>{
dispatch(configActions.pause("Pause"))
let result = dispatch(DataActions.save(stateProps.data)
dispatch(configActions.setClick(false));
result.then((result) => {
dispatch(configActions.show(false));
dispatch(DataActions.StatusValue(""))
})
return Object.assign({},stateprops,ownprops,{
f1:f1
})
}
export default connect(mapstatetoprops,null,mergeprops)(Test)
DataActions.js
export const save = (data) => {
return (dispatch,getState) =>{
return axios.post(url, payload))
.then(data => {
return data;
},error => {
return error
}).catch(err){
return err
}
我的测试用例
Test(()=>{
Expectedactions= all 3 actions.
Axios.post.mockimplimetation(()=>
Promise.resolve(data))
calling Func1() from wrapper.
Expect(store.getActions()).toequal(expectedactions)
})
Test(()=>{
Expectedactions= all 3 actions.
Axios.post.mockimplimetation(()=>
Promise.resolve(data))
calling f1() from wrapper like
Wrapper.shallow(<TestContainer store=store/>)
Wrapper.find('Test').props().f1()
Expect(store.getActions()).toequal(expectedactions)
})
测试(()=>{
Expectedactions=所有3个动作。
Axios.post.mockimpimulation(()=>
承诺。解决(数据)
从类包装器调用f1()
Wrapper.shallow()
Wrapper.find('Test').props().f1()
Expect(store.getActions()).toequal(expectedactions)
})
测试结果只显示了2个动作。
未捕获异步操作
请帮助我如何测试这种行为或我应该采取什么方法。
调度(其他一些操作)
等将导致语法错误。请提供工作代码,而不是伪代码。承诺应该被拴住,在你的情况下,它们可能不会。还不清楚具体在哪里。添加了一些工作代码。它可以帮助您调度(其他一些操作)
等将导致语法错误。请提供工作代码,而不是伪代码。承诺应该被拴住,在你的情况下,它们可能不会。还不清楚具体在哪里。添加了一些工作代码。这可能对你有帮助