我正在使用jest进行测试。我正在使用react和redux,我有这个动作:
function getData(id, notify) {
return (dispatch, ...) => {
dispatch(anotherFunction());
Promise.all(['resource1', 'resource2', 'resource3'])
.then(([response1,response2,response3]) => {
... handle responses
})
.catch(error => { dispatch(handleError(error)); }
};
}
我一直在寻找如何为此操作设置测试的jest文档,但我无法找到方法。我尝试过这样的事情:
it('test description', (done) => {
const expectedActions = [{type: {...}, payload: {...}},{type: {...}, payload: {...}},...];
fetchMock.get('resource1', ...);
fetchMock.get('resource2', ...);
fetchMock.get('resource3', ...);
... then the rest of the test calls
});
未成功。那我该怎么办?
答案 0 :(得分:1)
您可以通过在回调中返回诺言来告诉Jest等待诺言解决。有关更多信息,请参见here部分。
it('should fetch some food', () => {
const fetchItem1 = () => fetchData1().then(data => {
expect(data).toBe('peanut butter');
})
const fetchItem2 = () => fetchData2().then(data => {
expect(data).toBe('boiled egg');
})
const fetchItem3 = () => fetchData3().then(data => {
expect(data).toBe('fried salad');
})
return Promise.all([fetchItem1(), fetchItem2(), fetchItem3()])
.then(() => runOtherTests());
});