import { fireEvent, render, screen, within } from "@testing-library/react"; import { describe, expect, it, vi } from "vitest"; import { DatePicker } from "./date-picker"; function getCalendarDayButtons(dialogName: string) { return within(screen.getByRole("dialog", { name: dialogName })) .getAllByRole("button") .filter((button) => button.getAttribute("data-slot") === "day"); } describe("DatePicker", () => { it("renders a placeholder and selects a date in uncontrolled mode", async () => { render( ); const field = screen.getByRole("combobox", { name: "Launch date" }); expect(field.closest('[data-slot="root"]')).toHaveAttribute("data-placeholder", ""); const dayButton = getCalendarDayButtons("Launch date calendar")[10]; fireEvent.click(dayButton); expect(field.closest('[data-slot="root"]')).not.toHaveAttribute("data-placeholder"); expect(field).not.toHaveValue(""); }); it("supports controlled values and emits changes", async () => { const onValueChange = vi.fn(); render( ); fireEvent.click( screen.getByRole("button", { name: /Apr 20, 2026|20 Apr 2026|Apr 20 2026/i }) ); expect(onValueChange).toHaveBeenCalled(); }); it("supports clearing the current value and choosing today", async () => { render( ); const field = screen.getByRole("combobox", { name: "Review date" }); fireEvent.click(screen.getByRole("button", { name: "Clear date" })); expect(field).toHaveValue(""); fireEvent.click(screen.getByRole("button", { name: "Today" })); expect(field).not.toHaveValue(""); }); it("supports month switching via controls and year selection", async () => { render( ); expect(screen.getByText("March 2026")).toBeInTheDocument(); fireEvent.click(screen.getByRole("button", { name: "Next month" })); expect(screen.getByText("April 2026")).toBeInTheDocument(); fireEvent.click(screen.getByRole("combobox", { name: "Year" })); fireEvent.click(screen.getByRole("option", { name: "2028" })); expect(screen.getByText("April 2028")).toBeInTheDocument(); }); it("defaults to a monday-first calendar grid", () => { render( ); const firstDay = getCalendarDayButtons("Monday-first date calendar")[0]; expect(firstDay).toHaveAccessibleName("Feb 23, 2026"); expect(screen.getByText("Mon")).toBeInTheDocument(); }); it("supports sunday-first calendar grids when requested", () => { render( ); const firstDay = getCalendarDayButtons("Sunday-first date calendar")[0]; expect(firstDay).toHaveAccessibleName("Mar 1, 2026"); expect(screen.getByText("Sun")).toBeInTheDocument(); }); it("respects min and max dates", async () => { render( ); const disabledDays = getCalendarDayButtons("Guardrailed date calendar").filter((cell) => cell.hasAttribute("data-disabled") ); expect(disabledDays.length).toBeGreaterThan(0); }); });