2 回答
TA贡献1858条经验 获得超8个赞
在您的情况下,它与正面测试相同:您需要模拟ApiService.getTips,因此它会称为“正面反馈”或“负面反馈”。说,现在你没有测试成功fetchTips时调度的动作getTips。
这将是相似的(没有打字稿):
it('dispatches loadTips on success', () => {
const mockedResponse = [1,2,3];
ApiService.getTips.mockImplementation(
(successCallback, failureCallback) => successCallback(mockedResponse)
);
store.dispatch(actions.fetchTips());
expect(store.getActions()).toContainEqual(actions.loadTips(mockedResponse));
expect(store.getActions()).not.toContainEqual(
triggerToast('Tips are not loading. Try again later!', true)
);
});
it('dispatches toast message on failure', () => {
ApiService.getTips.mockImplementation(
(successCallback, failureCallback) => failureCallback()
);
store.dispatch(actions.fetchTips());
expect(store.getActions()).toContainEqual(
triggerToast('Tips are not loading. Try again later!', true)
);
expect(store.getActions()).not.toContainEqual(actions.loadTips(mockedResponse));
});
TA贡献1900条经验 获得超5个赞
由于这是负面测试,您必须使用负面数据调用 api。检查 ApiService.getTips 的实现并抛出错误。或模拟 ApiService.getTips 并抛出错误(调用错误回调)
ApiService.getTips(
tips => dispatch(loadTips(tips)),
() => dispatch(triggerToast('Tips are not loading. Try again later!', true))
)
添加回答
举报