import { render, screen, fireEvent } from "@testing-library/react";
import { vi } from "vitest";
import { Dialog, DialogClose, DialogContent, DialogFooter, DialogHeader, DialogTrigger } from "./Dialog";
vi.mock("@calcom/lib/hooks/useCompatSearchParams", () => ({
useCompatSearchParams() {
return new URLSearchParams();
},
}));
vi.mock("next/navigation", () => ({
usePathname() {
return "";
},
useSearchParams() {
return new URLSearchParams();
},
useRouter() {
return {
push: vi.fn(),
beforePopState: vi.fn(() => null),
prefetch: vi.fn(() => null),
};
},
}));
describe("Dialog", () => {
const title = "Dialog Title";
const subtitle = "Dialog Subtitle";
const content = "Dialog Content";
const footerContent = "Dialog Footer";
const TestDialog = (props: {
open?: boolean;
title?: string;
subtitle?: string;
type?: "creation" | "confirmation";
showDivider?: boolean;
color?: "primary" | "secondary" | "minimal" | "destructive";
preventCloseOnOutsideClick?: boolean;
enableOverflow?: boolean;
}) => (
);
describe("Rendering", () => {
it("renders without header when no title provided", () => {
render();
expect(screen.queryByTestId("dialog-title")).not.toBeInTheDocument();
expect(screen.getByText(content)).toBeInTheDocument();
});
it("renders creation type dialog", () => {
render();
expect(screen.getByTestId("dialog-creation")).toBeInTheDocument();
});
it("renders confirmation type dialog", () => {
render();
expect(screen.getByTestId("dialog-confirmation")).toBeInTheDocument();
});
});
describe("Visibility", () => {
it("shows content when open", () => {
render();
expect(screen.getByText(content)).toBeInTheDocument();
});
it("hides content when closed", () => {
render();
expect(screen.queryByText(content)).not.toBeInTheDocument();
});
it("toggles visibility with DialogTrigger", () => {
render(
);
expect(screen.queryByText(content)).not.toBeInTheDocument();
fireEvent.click(screen.getByText("Open Dialog"));
expect(screen.getByText(content)).toBeInTheDocument();
});
});
describe("Footer", () => {
it("shows divider when showDivider is true", () => {
render();
expect(screen.getByTestId("divider")).toBeInTheDocument();
});
});
describe("Behavior", () => {
it("enables overflow when enableOverflow is true", () => {
render();
const dialogContent = screen.getByRole("dialog");
expect(dialogContent.className).toContain("overflow-y-auto");
});
});
});