测试 redux saga 回调函数
testing redux saga callback function
在saga函数中,成功后,我在做回调。我试图在测试中涵盖这一点,但它没有被涵盖。谁能建议一些方法来涵盖这些领域。
export function* submitSavedDataWorker(action) {
try {
const response = yield call(suggestFunction, action.payload);
const { data } = action.payload;
yield put({
type: actionTypes.suggestType,
payload: response.data,
prevData: data,
});
if (action.successFn) {
action.successFn(response.data);
}
} catch (error) {
const { charityData } = action.payload;
yield put({
type: actionTypes.saveFailType,
payload: error,
prevData: data,
});
}
}
我写了这样的测试用例:
describe('submitSavedDataWorker success', ()=> {
const it = sagaHelper(
submitSavedDataWorker({ payload: { data: 1234 }, successFn: ()=> { return 0 } })
);
it("should get error as result", (result) => {
expect(result).toEqual(
call(suggestFunction, { data: 1234 })
);
return {data: true};
});
it("should get success as result", (result) => {
expect(result).toEqual(
put({
type: actionTypes.suggestType,
payload: true,
prevData: 1234,
})
);
});
});
sagaHelper
在调用 it
时,一次只会将 saga 推进一个效果 - 因此生成器在产生 put
效果后停止执行。为了让传奇 运行 完成(并因此执行未覆盖的行),需要再次调用 it
。您还可以检查结果是否为 undefined
以确保 saga 已 运行 完成,例如:
it("should complete", (result) => {
expect(result).toBeUndefined();
});
有了这个补充,这里是完整的测试:
describe('submitSavedDataWorker success', ()=> {
const it = sagaHelper(
submitSavedDataWorker({ payload: { data: 1234 }, successFn: ()=> { return 0 } })
);
it("should get error as result", (result) => {
expect(result).toEqual(
call(suggestFunction, { data: 1234 })
);
return {data: true};
});
it("should get success as result", (result) => {
expect(result).toEqual(
put({
type: actionTypes.suggestType,
payload: true,
prevData: 1234,
})
);
});
/* New code is here */
it("should complete", (result) => {
expect(result).toBeUndefined();
});
});
在saga函数中,成功后,我在做回调。我试图在测试中涵盖这一点,但它没有被涵盖。谁能建议一些方法来涵盖这些领域。
export function* submitSavedDataWorker(action) {
try {
const response = yield call(suggestFunction, action.payload);
const { data } = action.payload;
yield put({
type: actionTypes.suggestType,
payload: response.data,
prevData: data,
});
if (action.successFn) {
action.successFn(response.data);
}
} catch (error) {
const { charityData } = action.payload;
yield put({
type: actionTypes.saveFailType,
payload: error,
prevData: data,
});
}
}
我写了这样的测试用例:
describe('submitSavedDataWorker success', ()=> {
const it = sagaHelper(
submitSavedDataWorker({ payload: { data: 1234 }, successFn: ()=> { return 0 } })
);
it("should get error as result", (result) => {
expect(result).toEqual(
call(suggestFunction, { data: 1234 })
);
return {data: true};
});
it("should get success as result", (result) => {
expect(result).toEqual(
put({
type: actionTypes.suggestType,
payload: true,
prevData: 1234,
})
);
});
});
sagaHelper
在调用 it
时,一次只会将 saga 推进一个效果 - 因此生成器在产生 put
效果后停止执行。为了让传奇 运行 完成(并因此执行未覆盖的行),需要再次调用 it
。您还可以检查结果是否为 undefined
以确保 saga 已 运行 完成,例如:
it("should complete", (result) => {
expect(result).toBeUndefined();
});
有了这个补充,这里是完整的测试:
describe('submitSavedDataWorker success', ()=> {
const it = sagaHelper(
submitSavedDataWorker({ payload: { data: 1234 }, successFn: ()=> { return 0 } })
);
it("should get error as result", (result) => {
expect(result).toEqual(
call(suggestFunction, { data: 1234 })
);
return {data: true};
});
it("should get success as result", (result) => {
expect(result).toEqual(
put({
type: actionTypes.suggestType,
payload: true,
prevData: 1234,
})
);
});
/* New code is here */
it("should complete", (result) => {
expect(result).toBeUndefined();
});
});