我如何在React和测试库中的自定义挂钩上模拟数据
我有一个组件使用高级挂钩,已经测试了高级钩子,还可以,但是我不知道如何测试使用此挂钩的组件,并使用模拟数据进行测试。
钩子测试:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论