Files
co-lite/solvway-admin-portal/app/components/ui/date-picker.test.tsx
T
ngqileandClaude Opus 5.5 c908fbe9fc feat(admin-portal): port babyboo's date picker and calendar
Adds react-day-picker 10.0.1 (it brings date-fns). The portal bans native date
inputs, and the events and leads forms need a date field.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-27 00:23:56 +08:00

123 lines
5.6 KiB
TypeScript

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 (
<>
<DatePicker aria-label="Expiry date" value={value} min={props.min} max={props.max} disabled={props.disabled}
onChange={(v) => { setValue(v); props.onChange?.(v); }} />
<button type="button" onClick={() => setValue("")}>reset</button>
</>
);
}
describe("DatePicker", () => {
it("opens the calendar from the keyboard", () => {
render(<Harness />);
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(<Harness />);
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(<Harness initial="2026-09-15" min="2026-09-10" />);
expect(() => pickDate("Expiry date", "2026-09-01")).toThrow(/not an enabled day/);
});
it("opens the calendar when the box is clicked", () => {
render(<Harness />);
fireEvent.click(screen.getByLabelText("Expiry date"));
expect(screen.getByRole("dialog")).toBeInTheDocument();
});
it("ignores typing", () => {
const onChange = vi.fn();
render(<Harness onChange={onChange} />);
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(<Harness onChange={onChange} />);
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(<Harness initial="2026-09-01" onChange={onChange} />);
fireEvent.click(screen.getByLabelText("Expiry date"));
fireEvent.click(screen.getByRole("button", { name: "Clear" }));
expect(onChange).toHaveBeenLastCalledWith("");
unmount();
render(<DatePicker aria-label="Due" required value="2026-09-01" onChange={() => {}} />);
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(<Harness initial="2026-09-15" onChange={onChange} />);
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(<Harness initial="2026-09-15" onChange={onChange} />);
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(<Harness initial="2026-09-15" min="2026-09-10" max="2026-09-20" />);
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(<Harness disabled />);
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 <Label htmlFor> and native validation keep working", () => {
render(<><label htmlFor="exp">Expiry</label><DatePicker id="exp" required value="" onChange={() => {}} /></>);
expect(screen.getByLabelText("Expiry")).toBeRequired();
});
});
// sales-order-no-contains-search: Input is text-base below md, which cut "DD/MM/YYYY" to "DD/MM/YYY"
// in the 120px range fields at 375px. The box is read-only, so text-sm cannot trigger iOS focus zoom.
describe("DatePicker text size", () => {
it("uses text-sm at every width so the placeholder fits", () => {
render(<Harness />);
const box = screen.getByLabelText("Expiry date");
expect(box).toHaveClass("text-sm");
expect(box).not.toHaveClass("text-base");
});
});