小编cro*_*lat的帖子

如何在 React 测试库中的多个测试中使用单个渲染?

使用 Jest 和 React 测试库是否可以在多个测试中维护相同的 render() ?我正在构建一个 Trivia 应用程序,并且有一个组件可以在用户完成测验时显示不同的问题。为了测试选择按钮、提交按钮的功能,并检查是否在正确的时间显示正确的屏幕,我需要在测验的不同阶段对同一渲染组件执行测试。例如:

describe("Question Screen", () => {

    it("should render the first question when difficulty button is clicked", async () => {
        render(<TriviaBox/>);
        const btn = screen.getByRole("button", {name: /easy/i});
        fireEvent.click(btn);

        const heading = await screen.findByText("Question 1");
        expect(heading).toBeInTheDocument();
    });

    it("should display the next question when the current question is answered", async () => {
        render(<TriviaBox/>);
        const btn = screen.getByRole("button", {name: /easy/i});
        fireEvent.click(btn);

        const correctAnswer = await screen.findByRole("button", {name: /Nevada/i});
        const submit = await screen.findByRole("button", {name: …
Run Code Online (Sandbox Code Playgroud)

reactjs jestjs react-testing-library

5
推荐指数
2
解决办法
9872
查看次数

标签 统计

jestjs ×1

react-testing-library ×1

reactjs ×1