/* Copyright 2024 New Vector Ltd. SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial Please see LICENSE in the repository root for full details. */ import { expect, test, afterEach } from "vitest"; import { render } from "@testing-library/react"; import { type ReactNode, useState } from "react"; 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(

This is the content.

, ); expect(queryByRole("dialog")).toBeNull(); }); test("the content is rendered when the modal is open", () => { const { queryByRole } = render(

This is the content.

, ); expect(queryByRole("dialog")).toMatchSnapshot(); }); test("the modal can be closed by clicking the close button", async () => { function ModalFn(): ReactNode { const [isOpen, setOpen] = useState(true); return ( setOpen(false)}>

This is the content.

); } const user = userEvent.setup(); const { queryByRole, getByRole } = render(); await user.click(getByRole("button", { name: "Close" })); expect(queryByRole("dialog")).toBeNull(); }); test("the modal renders as a drawer in mobile viewports", () => { window.matchMedia = function (query): MediaQueryList { return { matches: query.includes("hover: none"), addEventListener(): MediaQueryList { return this as MediaQueryList; }, removeEventListener(): MediaQueryList { return this as MediaQueryList; }, } as unknown as MediaQueryList; }; const { queryByRole } = render(

This is the content.

, ); expect(queryByRole("dialog")).toMatchSnapshot(); });