[Bug]: Unintended result of not.toBeVisible() when testing modal components using testing-library. #1655
Closed
Description
Browser
No response
Package version
v3.0.407
React version
v18.2.0
Description
The following test code expect(modal).not.toBeVisible();
should not generate an error.
Source code
- test code
import "@testing-library/jest-dom";
import { render, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { screen, waitFor } from "@testing-library/react";
import { describe } from "node:test";
import { SampleModal } from "./SampleModal";
describe("SampleModal", () => {
test("open modal and close", async () => {
const tree = render(<SampleModal />)
const button = screen.getByRole("button", { name: "Open" })
const modal = within(tree.container.ownerDocument.body).getByRole("dialog")
// const modal = screen.getByRole("dialog")
await userEvent.click(button)
await waitFor(() => {
expect(modal).toBeVisible();
});
const closeButton = within(modal).getByRole("button", { name: "Close" })
await userEvent.click(closeButton)
await waitFor(() => {
expect(modal).not.toBeVisible();
// - display: none;
// + display: block;
// expect(modal).toHaveStyle({ display: "none" });
});
})
})
- component
import Modal from "@cloudscape-design/components/modal";
import Box from "@cloudscape-design/components/box";
import SpaceBetween from "@cloudscape-design/components/space-between";
import Button from "@cloudscape-design/components/button";
import { useState } from "react";
export const SampleModal = () => {
const [visible, setVisible] = useState(false);
return (
<>
<Button onClick={() => setVisible(true)}>Open</Button>
<Modal
onDismiss={() => setVisible(false)}
visible={visible}
closeAriaLabel="Close"
footer={
<Box float="right">
<SpaceBetween direction="horizontal" size="xs">
<Button variant="link">Cancel</Button>
<Button variant="primary">Ok</Button>
</SpaceBetween>
</Box>
}
header="Modal title"
>
Your description should go here
</Modal>
</>
);
};
The entire source code is as follows:
Reproduction
No response
Code of Conduct
- I agree to follow this project's Code of Conduct
- I checked the current issues for duplicate problems