我已经为异步操作编写了一个单元测试用例。为此编写的单元测试用例通过了,但是在覆盖率报告中,该动作未正确覆盖。调用API之后,我将调度一个操作来设置响应。
动作:
export const prefilledBonusDataPending = () => {
return async (dispatch) => {
dispatch({ type: configBonusPeriodActionTypes.PREFILLED_BONUS_DATA_PENDING });
try {
const response = await getBonusPeriodData();
if (response)
dispatch(prefilledBonusDataSuccess(response.data[0]));
}
catch (error) {
dispatch(prefilledBonusDataError(error));
}
}
}
此操作的单元测试用例:
it('dispatches PREFILLED_BONUS_DATA_PENDING to request data', () => {
mock.onGet('api/todos').reply(200, { response: [] });
store.dispatch(configBonusPeriodActions.prefilledBonusDataPending()).then(() => {
let expectedActions = [
{
type: configBonusPeriodActionTypes.PREFILLED_BONUS_DATA_PENDING
},
{
type: configBonusPeriodActionTypes.PREFILLED_BONUS_DATA_SUCCESS,
payload: [{ item: '1' }, { item: 'item2' }]
}
];
expect(store.getActions()).toEqual(expectedActions);
});
});
此测试用例成功通过,但覆盖率报告显示未涵盖API调用后分派的操作: