我如何在React和测试库中的自定义挂钩上模拟数据

发布于 2025-02-10 17:46:30 字数 1995 浏览 0 评论 0原文

我有一个组件使用高级挂钩,已经测试了高级钩子,还可以,但是我不知道如何测试使用此挂钩的组件,并使用模拟数据进行测试。

钩子测试:

import { renderHook, act } from "@testing-library/react-hooks";
import {
  HookProvider,
  useHook
} from "hooks/useHook";
import { render } from "tests/test-utils";

export const wrapper = ({ children }) => (
  <HookProvider>
    {children}
  </HookProvider>
);

describe("Should advanced hook works", () => {
  it("Should hook has initial states", () => {
    const { result } = renderHook(() => useHook());
    expect(result.current.selectedProducts).toStrictEqual([]);
  });

  it("Test change states", async () => {
    const { result } = renderHook(() => useHook(), {
      wrapper
    });

    act(() => {
      result.current.setSelectedProducts([{_id: ""a14da5", title: "product test", description: "description of product test"}]);
    });

    expect(result.current.selectedProducts[0]._id).toBe("a14da5");
  });
});

组件测试:

import { renderHook, act } from "@testing-library/react-hooks";

import {
  HookProvider,
  useHook
} from "hooks/useHook";
import { render } from "tests/test-utils";
import SelectedProductCard from ".";
import { screen, waitFor } from "@testing-library/react";

export const wrapper = ({ children }) => (
  <HookProvider>
    {children}
  </HookProvider>
);

it("Renders correctly SelectedProductCard", async () => {
  const { result } = renderHook(() => useHook(), {
    wrapper
  });

  act(() => {
    result.current.setSelectedProducts([{_id: ""a14da5", title: "product test", description: "description of product test"}]);
  });

   render(
        <SelectedProductCard
          key={a14da5}
          selectedProduct={{_id: ""a14da5", title: "product test", description: "description of product test"}}
        />
  );

  expect(
    screen.getAllByTestId("div-selected-product-card-container")
  ).toMatchSnapshot();
});

但是组件的测试不起作用。 如何测试此组件模拟数据?

I have one component thats use a advanced hook, the advanced hook has been tested and its ok, but I don't know how test the component thats use this hook, with mocking data.

The hook test:

import { renderHook, act } from "@testing-library/react-hooks";
import {
  HookProvider,
  useHook
} from "hooks/useHook";
import { render } from "tests/test-utils";

export const wrapper = ({ children }) => (
  <HookProvider>
    {children}
  </HookProvider>
);

describe("Should advanced hook works", () => {
  it("Should hook has initial states", () => {
    const { result } = renderHook(() => useHook());
    expect(result.current.selectedProducts).toStrictEqual([]);
  });

  it("Test change states", async () => {
    const { result } = renderHook(() => useHook(), {
      wrapper
    });

    act(() => {
      result.current.setSelectedProducts([{_id: ""a14da5", title: "product test", description: "description of product test"}]);
    });

    expect(result.current.selectedProducts[0]._id).toBe("a14da5");
  });
});

The component test:

import { renderHook, act } from "@testing-library/react-hooks";

import {
  HookProvider,
  useHook
} from "hooks/useHook";
import { render } from "tests/test-utils";
import SelectedProductCard from ".";
import { screen, waitFor } from "@testing-library/react";

export const wrapper = ({ children }) => (
  <HookProvider>
    {children}
  </HookProvider>
);

it("Renders correctly SelectedProductCard", async () => {
  const { result } = renderHook(() => useHook(), {
    wrapper
  });

  act(() => {
    result.current.setSelectedProducts([{_id: ""a14da5", title: "product test", description: "description of product test"}]);
  });

   render(
        <SelectedProductCard
          key={a14da5}
          selectedProduct={{_id: ""a14da5", title: "product test", description: "description of product test"}}
        />
  );

  expect(
    screen.getAllByTestId("div-selected-product-card-container")
  ).toMatchSnapshot();
});

but the test of components not works.
How can I test this component mocking data?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文