我一直在使用这个函数来测试我的传奇故事,但在这种情况下它不起作用。
我从调度函数内部的控制台得到响应,但调度的数组没有更新。
Saga
export function* releaseCommitsSaga({ release, product } ) {
if (release && product) {
const response = yield fetch(`${ApiEndpoint}/ReleaseCommits?${getURLQueryParams({ product, version: release })}`);
const releaseCommits = yield response.json()
.catch((error) => error);
if (response.status === 200) {
console.log(releaseCommits);
yield put(getReleaseCommitsSuccess(releaseCommits));
} else {
yield put(getReleaseCommitsFailure('release commits could not be fetched'));
}
} else {
yield put({
type: INVALID_PAGE_LOAD,
});
}
}
测验
it('releaseCommitsSaga success', async () => {
const response = responses.getReleaseCommitsResponse('success');
global.fetch = jest.fn(() => Promise.resolve({
status: 200,
json: () => Promise.resolve(response),
}));
const dispatched = [];
await runSaga({
dispatch: (action) => {
console.log(action);
dispatched.push(action);
},
}, releaseCommitsSaga, getReleaseCommits(release, product));
await expect(fetch).toHaveBeenCalled();
expect(dispatched).toEqual([getReleaseCommitsSuccess(releaseCommits)]);
fetch.mockClear();
});
控制台
console.log src/__tests__/sagas/release.test.js:204
{
type: 'GET_RELEASE_COMMITS_SUCCESS',
releaseCommits: [
{
package: 'package_abc',
id: 'long_id',
committer: 'someone@something.com',
message: 'commit msg'
}
]
}
1条答案
按热度按时间ax6ht2ek1#
您应该使用
task.toPromise()
将runSaga()
返回的任务转换为承诺,请参阅任务。然后使用async/await
等待任务完成。例如
saga.ts
:saga.test.ts
:试验结果:
软件包版本: