[英]jest unit tests for redux action
I'm trying to achieve 100% code coverage by writing all possible unit tests for my action.我试图通过为我的操作编写所有可能的单元测试来实现 100% 的代码覆盖率。 The following is my action code以下是我的动作代码
export const getData = async (
requestUUID: string,
dispatch?: (type?: { type: string, payload?: unknown }) => {
type: string,
payload?: unknown,
}
): Promise<Cart> => {
const url = `${getApiUrl()}${API_V1_CART}`;
dispatch({ type: GET_REQUEST });
try {
const result = await apiCall(
{
method: "GET",
withCredentials: true,
url,
timeout: 1000,
},
dispatch,
requestUUID
);
dispatch({ type: GET_SUCCESS, payload: result });
return result;
} catch (e) {
dispatch({ type: GET_FAILED, payload: e });
return e;
}
};
I have written a unit test for the above as action as follows我已经为上面的操作编写了一个单元测试,如下所示
describe('getData', () => {
describe('success', () => {
let result;
const mockDispatch = jest.fn();
const mockData = {};
beforeAll(async () => {
(apiCall as jest.Mock).mockResolvedValueOnce(mockData);
result = await getBasketData('uuid', mockDispatch);
});
it('should call the correct api', () => {
expect(apiCall).toHaveBeenCalledWith(
{
method: 'GET',
timeout: 1000,
url: 'http://localhost/api/v1/data',
withCredentials: true,
},
mockDispatch,
'uuid',
);
});
it('should return the correct result', () => {
expect(result).toEqual(mockData);
});
});
});
This test only covers 77.78% of code coverage.该测试仅覆盖了 77.78% 的代码覆盖率。 What are the other tests i can write for this to achieve 100% code coverage?我可以为此编写哪些其他测试来实现 100% 的代码覆盖率?
You can write test for the catch block.您可以为 catch 块编写测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.