import { fireEvent, screen } from "@testing-library/react"; import { describe, expect, it, vi } from "vitest"; import { DateCell } from "@/app/(protected)/workorders/_components/list/table/cells/date-cell"; import { DueDateCell } from "@/app/(protected)/workorders/_components/list/table/cells/due-date-cell"; import { pad2, todayIso } from "@/domain/work-orders/utils/wizard-date-utils"; import { renderWithProviders } from "@/test/test-utils"; function currentMonthIso(day: number): string { const [y, m] = todayIso().split("-").map(Number); return `${y}-${pad2(m)}-${pad2(day)}`; } async function openRangeTab(triggerName: RegExp | string) { fireEvent.click(screen.getByRole("button", { name: triggerName })); const rangeTab = await screen.findByRole("button", { name: "Range" }); fireEvent.click(rangeTab); } describe("DateCell — Schedule On range", () => { it("shows Week, Date, and Range when onSaveRange is provided", async () => { renderWithProviders( , ); fireEvent.click(screen.getByRole("button", { name: "—" })); expect(await screen.findByRole("button", { name: "Week" })).toBeInTheDocument(); expect(screen.getByRole("button", { name: "Date" })).toBeInTheDocument(); expect(screen.getByRole("button", { name: "Range" })).toBeInTheDocument(); expect(screen.queryByRole("button", { name: "Specific date" })).not.toBeInTheDocument(); }, 10_000); it("keeps Week / Specific date and hides Range without onSaveRange", async () => { renderWithProviders(); fireEvent.click(screen.getByRole("button", { name: "—" })); expect(await screen.findByRole("button", { name: "Week" })).toBeInTheDocument(); expect(screen.getByRole("button", { name: "Specific date" })).toBeInTheDocument(); expect(screen.queryByRole("button", { name: "Range" })).not.toBeInTheDocument(); }); it("submits start and end via Done, including inverted clicks", async () => { const onSaveRange = vi.fn(); renderWithProviders( , ); await openRangeTab("—"); fireEvent.click(screen.getByRole("button", { name: "20" })); fireEvent.click(screen.getByRole("button", { name: "5" })); fireEvent.click(screen.getByRole("button", { name: /^done$/i })); expect(onSaveRange).toHaveBeenCalledWith(currentMonthIso(5), currentMonthIso(20)); }); it("does not submit until both ends are picked", async () => { const onSaveRange = vi.fn(); renderWithProviders( , ); await openRangeTab("—"); fireEvent.click(screen.getByRole("button", { name: "5" })); expect(screen.getByRole("button", { name: /^done$/i })).toBeDisabled(); expect(onSaveRange).not.toHaveBeenCalled(); }); it("does not persist when switching tabs without confirming", async () => { const onSave = vi.fn(); const onSaveRange = vi.fn(); renderWithProviders( , ); await openRangeTab("—"); fireEvent.click(screen.getByRole("button", { name: "5" })); fireEvent.click(screen.getByRole("button", { name: "Date" })); expect(onSave).not.toHaveBeenCalled(); expect(onSaveRange).not.toHaveBeenCalled(); }); }); describe("DueDateCell — no Schedule On range tabs", () => { it("does not offer Week / Date / Range on a PM due date", () => { renderWithProviders(); fireEvent.click(screen.getByRole("button", { name: /07-15/ })); expect(screen.queryByRole("button", { name: "Week" })).not.toBeInTheDocument(); expect(screen.queryByRole("button", { name: "Range" })).not.toBeInTheDocument(); expect(screen.queryByRole("button", { name: "Specific date" })).not.toBeInTheDocument(); }); });