Dev*_*vEB 23 reactjs react-testing-library
我在悬停上显示了一个弹出窗口,我想使用 Jest 和 React 测试库对其进行测试,以查看该元素是否默认隐藏。
当我手动测试时一切正常,而当我使用 RTL 测试时一切正常。
我尝试使用 not.toBeInTheDocument 和 not.toBeVisible 但似乎该元素始终存在于 DOM 中,不知道如何从 DOM 中删除它
JSX 代码:
<label
htmlFor="terms_and_conditions"
className="terms_and_conditions_box"
>
I agree with{" "}
<span className="terms_and_conditions_text" style={{ color: "blue" }}>
Terms and conditions
</span>
<div className="pop_over">No ice cream will be delivered</div>
</label>
Run Code Online (Sandbox Code Playgroud)
CSS代码:
.terms_and_conditions_text:hover + .pop_over {
display: block;
}
.pop_over {
background: rgb(199, 196, 196);
padding: 2rem;
width: 14rem;
border-radius: 15px;
position: absolute;
right: -18rem;
top: -2rem;
display: none;
}
Run Code Online (Sandbox Code Playgroud)
测试代码:
test("popover responds to hover", () => {
render(<SummaryForm />);
//* popover initially is hidden
const nullPopover = screen.queryByText(/No ice cream will be delivered/i);
expect(nullPopover).not.toBeInTheDocument();
});
Run Code Online (Sandbox Code Playgroud)
Rom*_*ian 36
我已经复制了您的代码,对我来说,测试不适用于,但只要存在就toBeInTheDocument可以使用。toBeVisibledisplay: none
这是我的测试文件的代码,第一个测试未通过,第二个测试通过:
import React from "react";
import { render, screen } from "@testing-library/react";
import "@testing-library/jest-dom/extend-expect";
import SummaryForm from "./App";
describe("popover responds to hover", () => {
test("popover initially is hidden (test with toBeInTheDocument)", () => {
render(<SummaryForm />);
const nullPopover = screen.queryByText(/No ice cream will be delivered/i);
expect(nullPopover).not.toBeInTheDocument();
});
test("popover initially is hidden (test with toBeVisible)", () => {
render(<SummaryForm />);
const nullPopover = screen.queryByText(/No ice cream will be delivered/i);
expect(nullPopover).not.toBeVisible();
});
});
Run Code Online (Sandbox Code Playgroud)
这里是codesandbox,您可以在其中看到它的实际情况。
| 归档时间: |
|
| 查看次数: |
54599 次 |
| 最近记录: |