The element(s) given to waitForElementToBeRemoved are already removed.

huangapple go评论71阅读模式
英文:

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(&quot;popover responses to hover&quot;, async () =&gt; {
  render(&lt;SummaryForm /&gt;);

  //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(() =&gt; {
    nullPopoverAgain = screen.queryByText(
      /no ice cream will actually be delivered/i
    );
  });

  expect(nullPopoverAgain).not.toBeInTheDocument();
});

Summary Form Code

import React, { useState } from &quot;react&quot;;
import Form from &quot;react-bootstrap/Form&quot;;
import Button from &quot;react-bootstrap/Button&quot;;
import OverlayTrigger from &quot;react-bootstrap/OverlayTrigger&quot;;
import Popover from &quot;react-bootstrap/Popover&quot;;

export default function SummaryForm() {
  const [tcChecked, setTcChecked] = useState(false);

  const popover = (
    &lt;Popover id=&quot;popover-basic&quot;&gt;
      &lt;Popover.Body&gt;No ice cream will actually be delivered&lt;/Popover.Body&gt;
    &lt;/Popover&gt;
  );

  const checkboxLabel = (
    &lt;span&gt;
      I agree to
      &lt;OverlayTrigger placement=&quot;right&quot; overlay={popover}&gt;
        &lt;span style={{ color: &quot;blue&quot; }}&gt; Terms and Conditions&lt;/span&gt;
      &lt;/OverlayTrigger&gt;
    &lt;/span&gt;
  );

  return (
    &lt;Form&gt;
      &lt;Form.Group controlId=&quot;terms-and-conditions&quot;&gt;
        &lt;Form.Check
          type=&quot;checkbox&quot;
          checked={tcChecked}
          onChange={(e) =&gt; setTcChecked(e.target.checked)}
          label={checkboxLabel}
        /&gt;
      &lt;/Form.Group&gt;
      &lt;Button variant=&quot;primary&quot; type=&quot;submit&quot; disabled={!tcChecked}&gt;
        Confirm order
      &lt;/Button&gt;
    &lt;/Form&gt;
  );
}

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;
    &gt; 51 |   await waitForElementToBeRemoved(() =&gt; {
         |                                  ^
      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(&quot;popover responses to hover&quot;, async () =&gt; {
  render(&lt;SummaryForm /&gt;);

  //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();
});

huangapple
  • 本文由 发表于 2023年4月11日 13:50:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定