英文:
The element(s) given to waitForElementToBeRemoved are already removed
问题
Testing Code
test("popover responses to hover", async () => {
render(<SummaryForm />);
//popover appears upon mouseover of checkbox label
const termsAndConditions = screen.getByText(/terms and conditions/i);
userEvent.hover(termsAndConditions);
userEvent.unhover(termsAndConditions);
let nullPopoverAgain;
await waitForElementToBeRemoved(() => {
nullPopoverAgain = screen.queryByText(
/no ice cream will actually be delivered/i
);
});
expect(nullPopoverAgain).not.toBeInTheDocument();
});
Summary Form Code
import React, { useState } from "react";
import Form from "react-bootstrap/Form";
import Button from "react-bootstrap/Button";
import OverlayTrigger from "react-bootstrap/OverlayTrigger";
import Popover from "react-bootstrap/Popover";
export default function SummaryForm() {
const [tcChecked, setTcChecked] = useState(false);
const popover = (
<Popover id="popover-basic">
<Popover.Body>No ice cream will actually be delivered</Popover.Body>
</Popover>
);
const checkboxLabel = (
<span>
I agree to
<OverlayTrigger placement="right" overlay={popover}>
<span style={{ color: "blue" }}> Terms and Conditions</span>
</OverlayTrigger>
</span>
);
return (
<Form>
<Form.Group controlId="terms-and-conditions">
<Form.Check
type="checkbox"
checked={tcChecked}
onChange={(e) => setTcChecked(e.target.checked)}
label={checkboxLabel}
/>
</Form.Group>
<Button variant="primary" type="submit" disabled={!tcChecked}>
Confirm order
</Button>
</Form>
);
}
Getting error
● popover responses to hover
The element(s) given to waitForElementToBeRemoved are already removed. waitForElementToBeRemoved requires that the element(s) exist(s) before waiting for removal.
49 | userEvent.unhover(termsAndConditions);
50 | let nullPopoverAgain;
> 51 | await waitForElementToBeRemoved(() => {
| ^
52 | nullPopoverAgain = screen.queryByText(
53 | /no ice cream will actually be delivered/i
54 | );
I move userEvent.unhover(termsAndConditions);
after waitForElementToBeRemoved
but it throws the same error despite that. I also commented the code of unhover despite of that getting the same error that the given element had already been removed. Without unhovering, it should not go from the DOM. How is this possible?
英文:
Testing Code
test("popover responses to hover", async () => {
render(<SummaryForm />);
//popover appears upon mouseover of checkbox label
const termsAndConditions = screen.getByText(/terms and conditions/i);
userEvent.hover(termsAndConditions);
userEvent.unhover(termsAndConditions);
let nullPopoverAgain;
await waitForElementToBeRemoved(() => {
nullPopoverAgain = screen.queryByText(
/no ice cream will actually be delivered/i
);
});
expect(nullPopoverAgain).not.toBeInTheDocument();
});
Summary Form Code
import React, { useState } from "react";
import Form from "react-bootstrap/Form";
import Button from "react-bootstrap/Button";
import OverlayTrigger from "react-bootstrap/OverlayTrigger";
import Popover from "react-bootstrap/Popover";
export default function SummaryForm() {
const [tcChecked, setTcChecked] = useState(false);
const popover = (
<Popover id="popover-basic">
<Popover.Body>No ice cream will actually be delivered</Popover.Body>
</Popover>
);
const checkboxLabel = (
<span>
I agree to
<OverlayTrigger placement="right" overlay={popover}>
<span style={{ color: "blue" }}> Terms and Conditions</span>
</OverlayTrigger>
</span>
);
return (
<Form>
<Form.Group controlId="terms-and-conditions">
<Form.Check
type="checkbox"
checked={tcChecked}
onChange={(e) => setTcChecked(e.target.checked)}
label={checkboxLabel}
/>
</Form.Group>
<Button variant="primary" type="submit" disabled={!tcChecked}>
Confirm order
</Button>
</Form>
);
}
Getting error
● popover responses to hover
The element(s) given to waitForElementToBeRemoved are already removed. waitForElementToBeRemoved requires that the element(s) exist(s) before waiting for removal.
49 | userEvent.unhover(termsAndConditions);
50 | let nullPopoverAgain;
> 51 | await waitForElementToBeRemoved(() => {
| ^
52 | nullPopoverAgain = screen.queryByText(
53 | /no ice cream will actually be delivered/i
54 | );
I move userEvent.unhover(termsAndConditions);
after waitForElementToBeRemoved
but it throws the same error despite that. I also commented the code of unhover despite of that getting the same error that the given element had already been removed. Without un hovering, it should not go from the dom. How is this possible?
答案1
得分: 2
你应该直接将要等待其移除的元素传递给waitForElementToBeRemoved
函数,而不是将其包装在回调函数中。
test("鼠标悬停时弹出窗口的响应", async () => {
render(<SummaryForm />);
// 鼠标悬停在复选框标签上时弹出窗口
const termsAndConditions = screen.getByText(/terms and conditions/i);
userEvent.hover(termsAndConditions);
userEvent.unhover(termsAndConditions);
await waitForElementToBeRemoved(
screen.queryByText(/no ice cream will actually be delivered/i)
);
expect(
screen.queryByText(/no ice cream will actually be delivered/i)
).not.toBeInTheDocument();
});
英文:
You should pass the element that you want to wait for its removal directly to the waitForElementToBeRemoved
function, instead of wrapping it in a callback.
test("popover responses to hover", async () => {
render(<SummaryForm />);
//popover appears upon mouseover of checkbox label
const termsAndConditions = screen.getByText(/terms and conditions/i);
userEvent.hover(termsAndConditions);
userEvent.unhover(termsAndConditions);
await waitForElementToBeRemoved(
screen.queryByText(/no ice cream will actually be delivered/i)
);
expect(
screen.queryByText(/no ice cream will actually be delivered/i)
).not.toBeInTheDocument();
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论