import { useState } from "react"; import { describe, expect, it, vi } from "vitest"; import { fireEvent, render, screen, within } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { DatePicker } from "./date-picker"; import { pickDate } from "@/test/pickDate"; function Harness(props: { initial?: string; onChange?: (v: string) => void; min?: string; max?: string; disabled?: boolean }) { const [value, setValue] = useState(props.initial ?? ""); return ( <> { setValue(v); props.onChange?.(v); }} /> ); } describe("DatePicker", () => { it("opens the calendar from the keyboard", () => { render(); fireEvent.keyDown(screen.getByLabelText("Expiry date"), { key: "Enter" }); expect(screen.getByRole("dialog")).toBeInTheDocument(); }); it("stays open when the box is clicked again", async () => { const user = userEvent.setup(); render(); await user.click(screen.getByLabelText("Expiry date")); await user.click(screen.getByLabelText("Expiry date")); expect(screen.getByRole("dialog")).toBeInTheDocument(); }); it("the pickDate helper refuses a disabled day instead of silently doing nothing", () => { render(); expect(() => pickDate("Expiry date", "2026-09-01")).toThrow(/not an enabled day/); }); it("opens the calendar when the box is clicked", () => { render(); fireEvent.click(screen.getByLabelText("Expiry date")); expect(screen.getByRole("dialog")).toBeInTheDocument(); }); it("ignores typing", () => { const onChange = vi.fn(); render(); expect(screen.getByLabelText("Expiry date")).toHaveAttribute("readonly"); fireEvent.change(screen.getByLabelText("Expiry date"), { target: { value: "01/09/2026" } }); expect(onChange).not.toHaveBeenCalled(); expect(screen.getByLabelText("Expiry date")).toHaveValue(""); }); it("picks a date through the helper", () => { const onChange = vi.fn(); render(); pickDate("Expiry date", "2026-09-01"); expect(onChange).toHaveBeenLastCalledWith("2026-09-01"); expect(screen.getByLabelText("Expiry date")).toHaveValue("01/09/2026"); }); it("Clear empties an optional field; a required one has no Clear", () => { const onChange = vi.fn(); const { unmount } = render(); fireEvent.click(screen.getByLabelText("Expiry date")); fireEvent.click(screen.getByRole("button", { name: "Clear" })); expect(onChange).toHaveBeenLastCalledWith(""); unmount(); render( {}} />); fireEvent.click(screen.getByLabelText("Due")); expect(screen.queryByRole("button", { name: "Clear" })).not.toBeInTheDocument(); }); it("fills the field from a picked day", async () => { const onChange = vi.fn(); render(); await userEvent.click(screen.getByRole("button", { name: "Open calendar for Expiry date" })); await userEvent.click(within(screen.getByRole("dialog")).getByRole("button", { name: /September 1st, 2026/ })); expect(onChange).toHaveBeenLastCalledWith("2026-09-01"); expect(screen.getByLabelText("Expiry date")).toHaveValue("01/09/2026"); }); it("does not wipe the value when the selected day is clicked again", async () => { const onChange = vi.fn(); render(); await userEvent.click(screen.getByRole("button", { name: "Open calendar for Expiry date" })); await userEvent.click(within(screen.getByRole("dialog")).getByRole("button", { name: /September 15th, 2026/ })); expect(onChange).not.toHaveBeenCalledWith(""); }); it("disables days outside min and max", async () => { render(); await userEvent.click(screen.getByRole("button", { name: "Open calendar for Expiry date" })); const dialog = screen.getByRole("dialog"); expect(within(dialog).getByRole("button", { name: /September 9th, 2026/ })).toBeDisabled(); expect(within(dialog).getByRole("button", { name: /September 21st, 2026/ })).toBeDisabled(); expect(within(dialog).getByRole("button", { name: /September 10th, 2026/ })).toBeEnabled(); }); it("disables the calendar button when the field is disabled", () => { render(); expect(screen.getByLabelText("Expiry date")).toBeDisabled(); expect(screen.getByRole("button", { name: "Open calendar for Expiry date" })).toBeDisabled(); }); it("passes id and required to the text box so