Reactjs 如何在React with Jest中测试链式API调用?
我的react/redux应用程序中有一个保存工作流,需要两个API调用。第二次调用仅在第一次调用成功完成后发生。该应用程序运行良好,我现在正在添加一些自动端到端测试 我能够触发Reactjs 如何在React with Jest中测试链式API调用?,reactjs,jestjs,jest-mock-axios,Reactjs,Jestjs,Jest Mock Axios,我的react/redux应用程序中有一个保存工作流,需要两个API调用。第二次调用仅在第一次调用成功完成后发生。该应用程序运行良好,我现在正在添加一些自动端到端测试 我能够触发onSave函数并验证对saveRates的调用,但在测试中无法启动第二个API调用。这是密码 function onSave() { // PUT to /rates/<id> dispatchProps.saveRates() .then(() => { // PUT to /a
onSave
函数并验证对saveRates
的调用,但在测试中无法启动第二个API调用。这是密码
function onSave() {
// PUT to /rates/<id>
dispatchProps.saveRates()
.then(() => {
// PUT to /apps/<id>
dispatchProps.saveApplications()
.then(() => {
// Done
});
});
}
第一次API调用的承诺,saveRates
似乎还没有得到解决。调用mockResponse
可以解析承诺,但是,使用jest调试器,我可以看到对saveApplications
的调用从未发生过
如何让jest等到下一个API调用发生?我是否需要从onSave
通话中返回承诺
更新
这是有效的…所以我怀疑它可以通过等待
在某个地方解决
expect(mockAxios.put).toHaveBeenCalledWith(`taxes/event/${window.storeAdminTaxEventUuid}/rates/customer`, taxRatesResponse.data.customer);
mockAxios.mockResponse({ status: 200 }, mockAxios.lastPromiseGet());
setTimeout(() => {
expect(mockAxios.put).toHaveBeenCalledWith(`taxes/event/${window.storeAdminTaxEventUuid}/application/products`, taxApplicationsResponse.data);
mockAxios.mockResponse({ status: 200 });
}, 0);
expect(mockAxios.put).toHaveBeenCalledWith(`taxes/event/${window.storeAdminTaxEventUuid}/rates/customer`, taxRatesResponse.data.customer);
mockAxios.mockResponse({ status: 200 }, mockAxios.lastPromiseGet());
setTimeout(() => {
expect(mockAxios.put).toHaveBeenCalledWith(`taxes/event/${window.storeAdminTaxEventUuid}/application/products`, taxApplicationsResponse.data);
mockAxios.mockResponse({ status: 200 });
}, 0);