redux 操作的玩笑单元测试

发布于 2025-01-09 08:07:12 字数 1528 浏览 4 评论 0原文

我试图通过为我的操作编写所有可能的单元测试来实现 100% 的代码覆盖率。 以下是我的操作代码

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;
  }
};

我为上述操作编写了一个单元测试,如下所示

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);
    });
  });
});

该测试仅覆盖了77.78%的代码覆盖率。我可以为此编写哪些其他测试来实现 100% 的代码覆盖率?

I'm trying to achieve 100% code coverage by writing all possible unit tests for my action.
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. What are the other tests i can write for this to achieve 100% code coverage?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骄傲 2025-01-16 08:07:12

您可以为 catch 块编写测试。

You can write test for the catch block.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文