import { createRef, useState } from "react";
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { Modal } from ".";
import { htmlDialogPolyfill } from "../../utils/htmlDialogPolyfill";
import { mockMatchMedia } from "../../utils/mockMatchMedia";
htmlDialogPolyfill();
const MODAL_CLASS_NAME = "mobius-modal";
const MODAL_CLOSE_CLASS_NAME = "mobius-modal__close";
const MODAL_TEST_CLASS_NAME = "my-test-class-name";
describe("Modal", () => {
beforeEach(() => {
mockMatchMedia(true);
});
afterAll(() => {
vi.resetAllMocks();
});
it("should render without errors", () => {
const { container } = render();
expect(container).toBeTruthy();
});
it("should render with default class names", async () => {
render(
,
);
await screen.findByRole("dialog");
const dialog = screen.getByRole("dialog");
const closeButton = screen.getByLabelText("Close");
expect(dialog).toHaveClass(MODAL_CLASS_NAME);
expect(closeButton).toHaveClass(MODAL_CLOSE_CLASS_NAME);
});
it("should render with custom class name", async () => {
render(
,
);
await screen.findByRole("dialog");
const dialog = screen.getByRole("dialog");
const closeButton = screen.getByLabelText("Close");
expect(dialog).toHaveClass(MODAL_CLASS_NAME);
expect(dialog).toHaveClass(MODAL_TEST_CLASS_NAME);
expect(closeButton).toHaveClass(MODAL_CLOSE_CLASS_NAME);
});
it("should render additional attributes", async () => {
const testId = "test-modal";
render(
,
);
await screen.findByRole("dialog");
const dialog = screen.getByTestId(testId);
expect(dialog).toBeInTheDocument();
});
describe("given the animation prop is set to slideUp", () => {
it("should render with appropriate class name", async () => {
render();
await screen.findByRole("dialog");
const dialog = screen.getByRole("dialog");
expect(dialog).toHaveClass("--slide-up");
});
});
describe("given the animation prop is set to fade", () => {
it("should render with appropriate class name", async () => {
render();
await screen.findByRole("dialog");
const dialog = screen.getByRole("dialog");
expect(dialog).toHaveClass("--fade");
});
});
it("has open attribute when isOpen is true", async () => {
render(
Title
Content
,
);
const dialog = await screen.findByRole("dialog");
expect(dialog).toHaveAttribute("open");
});
describe("pressing close button", () => {
it("should close the modal", async () => {
const App = () => {
const [isOpen, setIsOpen] = useState(true);
return (
setIsOpen(false)}>
Header
Sample content
);
};
render();
await screen.findByRole("dialog");
const dialog = screen.getByRole("dialog");
const drawerContent = screen.getByText("Sample content");
expect(dialog).toHaveAttribute("open");
expect(drawerContent).toBeInTheDocument();
const closeButton = screen.getByLabelText("Close");
await userEvent.click(closeButton);
waitFor(() => expect(dialog).not.toHaveAttribute("open"));
});
});
describe("closeLabel prop", () => {
it("should render the label", () => {
const closeLabel = "Leave";
render(
Sample content
,
);
const closeButton = screen.getByText(closeLabel);
expect(closeButton).toBeInTheDocument();
});
it("should render the default close button", () => {
render(
,
);
const closeButton = screen.getByLabelText("Close");
expect(closeButton).toBeInTheDocument();
});
it("should fire onOpen callback if supplied", async () => {
const onOpen = vi.fn();
const App = () => {
const [isOpen, setIsOpen] = useState(false);
return (
<>
setIsOpen(false)}
onOpen={onOpen}
>
Header
Sample content
>
);
};
render();
const showBtn = screen.getByText("Show modal");
expect(showBtn).toBeInTheDocument();
await userEvent.click(showBtn);
expect(onOpen).toHaveBeenCalled();
});
});
it("forwards ref", async () => {
const ref = createRef();
render();
await screen.findByRole("dialog");
expect(ref.current).toBeInstanceOf(HTMLDialogElement);
});
});