Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for Modal #2731

Merged
merged 5 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/Modal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ SPDX-License-Identifier: AGPL-3.0-only
Please see LICENSE in the repository root for full details.
*/

import { afterAll, expect, test } from "vitest";
import { expect, test } from "vitest";
import { render } from "@testing-library/react";
import { act, ReactNode, useState } from "react";
import { afterEach } from "node:test";
import userEvent from "@testing-library/user-event";

import { Modal } from "./Modal";

const originalMatchMedia = window.matchMedia;
afterEach(() => {
window.matchMedia = originalMatchMedia;
});

test("that nothing is rendered when the modal is closed", () => {
const { queryByRole } = render(
<Modal title="My modal" open={false}>
Expand All @@ -29,7 +36,7 @@ test("the content is rendered when the modal is open", () => {
expect(queryByRole("dialog")).toMatchSnapshot();
});

test("the modal can be closed by clicking the close button", () => {
test("the modal can be closed by clicking the close button", async () => {
function ModalFn(): ReactNode {
const [isOpen, setOpen] = useState(true);
return (
Expand All @@ -38,19 +45,14 @@ test("the modal can be closed by clicking the close button", () => {
</Modal>
);
}
const { queryByRole, getByLabelText } = render(<ModalFn />);
act(() => {
getByLabelText("action.close").click();
const user = userEvent.setup();
const { queryByRole, getByRole } = render(<ModalFn />);
await act(async () => {
await user.click(getByRole("button", { name: "action.close" }));
Half-Shot marked this conversation as resolved.
Show resolved Hide resolved
});
expect(queryByRole("dialog")).toBeNull();
});

const originalMatchMedia = window.matchMedia;

afterAll(() => {
window.matchMedia = originalMatchMedia;
});

test("the modal renders as a drawer in mobile viewports", () => {
window.matchMedia = function (query): MediaQueryList {
return {
Expand Down
6 changes: 3 additions & 3 deletions src/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ export const Modal: FC<Props> = ({
styles.drawer,
{ [styles.tabbed]: tabbed },
)}
role="dialog"
// Suppress the warning about there being no description; the modal
// has an accessible title
role="dialog"
aria-describedby={undefined}
{...rest}
>
Expand All @@ -115,10 +115,10 @@ export const Modal: FC<Props> = ({
<DialogOverlay
className={classNames(overlayStyles.bg, overlayStyles.animate)}
/>
{/* Suppress the warning about there being no description; the modal
has an accessible title */}
<DialogContent
asChild
// Suppress the warning about there being no description; the modal
// has an accessible title
aria-describedby={undefined}
role="dialog"
{...rest}
Expand Down