import { render, screen } from "@testing-library/react";
import { vi } from "vitest";
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogClose } from "./Dialog";
vi.mock("next/router", () => ({
useRouter() {
return {
route: "/",
pathname: "",
query: "",
asPath: "",
push: vi.fn(),
events: {
on: vi.fn(),
off: vi.fn(),
},
beforePopState: vi.fn(() => null),
prefetch: vi.fn(() => null),
};
},
}));
const title = "Dialog Header";
const subtitle = "Dialog Subtitle";
const DialogComponent = (props: {
open: boolean;
title?: string;
subtitle?: string;
type?: "creation" | "confirmation";
showDivider?: boolean;
color?: "primary" | "secondary" | "minimal" | "destructive";
}) => {
return (
);
};
describe("Tests for Dialog component", () => {
test("Should render Dialog with header", () => {
render();
expect(screen.queryByText("Dialog Header")).toBeInTheDocument();
expect(screen.getByText("Dialog Content")).toBeInTheDocument();
expect(screen.getByText("Dialog Footer")).toBeInTheDocument();
});
test("Should render Dialog without header", () => {
render();
expect(screen.queryByTestId("dialog-title")).toBeNull();
expect(screen.getByText("Dialog Content")).toBeInTheDocument();
expect(screen.getByText("Dialog Footer")).toBeInTheDocument();
});
test("Should render Dialog with header and subtitle", () => {
render();
expect(screen.queryByText("Dialog Header")).toBeInTheDocument();
expect(screen.queryByText("Dialog Subtitle")).toBeInTheDocument();
expect(screen.getByText("Dialog Content")).toBeInTheDocument();
expect(screen.getByText("Dialog Footer")).toBeInTheDocument();
});
test("Should render Dialog with default type creation", () => {
render();
expect(screen.getByTestId("dialog-creation")).toBeInTheDocument();
expect(screen.getByText("Dialog Content")).toBeInTheDocument();
expect(screen.getByText("Dialog Footer")).toBeInTheDocument();
});
test("Should render Dialog with type creation", () => {
render();
expect(screen.getByTestId("dialog-creation")).toBeInTheDocument();
expect(screen.getByText("Dialog Content")).toBeInTheDocument();
expect(screen.getByText("Dialog Footer")).toBeInTheDocument();
});
test("Should render Dialog with type confirmation", () => {
render();
expect(screen.getByTestId("dialog-confirmation")).toBeInTheDocument();
expect(screen.getByText("Dialog Content")).toBeInTheDocument();
expect(screen.getByText("Dialog Footer")).toBeInTheDocument();
});
test("Should open Dialog", async () => {
const { rerender } = render();
expect(screen.queryByText("Dialog Content")).not.toBeInTheDocument();
expect(screen.queryByText("Dialog Footer")).not.toBeInTheDocument();
rerender();
expect(screen.getByText("Dialog Content")).toBeInTheDocument();
expect(screen.getByText("Dialog Footer")).toBeInTheDocument();
});
test("Should close Dialog", async () => {
const { rerender } = render();
expect(screen.getByText("Dialog Content")).toBeInTheDocument();
expect(screen.getByText("Dialog Footer")).toBeInTheDocument();
rerender();
expect(screen.queryByText("Dialog Content")).not.toBeInTheDocument();
expect(screen.queryByText("Dialog Footer")).not.toBeInTheDocument();
});
test("Should use color from props in CloseDialog", async () => {
render();
const closeBtn = screen.getByText("Close");
expect(closeBtn.classList.toString()).toContain("hover:text-red-700");
});
test("Should show divider with showDivider", async () => {
render();
expect(screen.getByTestId("divider")).toBeInTheDocument();
});
});