mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-03 19:03:15 +00:00
Share Week/Date/Range on inline, board, and slide-over DateCell. A selected range lands on the start day, matching the wizard.
120 lines
4.3 KiB
TypeScript
120 lines
4.3 KiB
TypeScript
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(
|
|
<DateCell
|
|
value=""
|
|
allowWeekMode
|
|
onSave={vi.fn()}
|
|
onSaveWeek={vi.fn()}
|
|
onSaveRange={vi.fn()}
|
|
/>,
|
|
);
|
|
|
|
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(<DateCell value="" allowWeekMode onSave={vi.fn()} onSaveWeek={vi.fn()} />);
|
|
|
|
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(
|
|
<DateCell
|
|
value=""
|
|
allowWeekMode
|
|
onSave={vi.fn()}
|
|
onSaveWeek={vi.fn()}
|
|
onSaveRange={onSaveRange}
|
|
/>,
|
|
);
|
|
|
|
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(
|
|
<DateCell
|
|
value=""
|
|
allowWeekMode
|
|
onSave={vi.fn()}
|
|
onSaveWeek={vi.fn()}
|
|
onSaveRange={onSaveRange}
|
|
/>,
|
|
);
|
|
|
|
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(
|
|
<DateCell
|
|
value=""
|
|
allowWeekMode
|
|
onSave={onSave}
|
|
onSaveWeek={vi.fn()}
|
|
onSaveRange={onSaveRange}
|
|
/>,
|
|
);
|
|
|
|
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(<DueDateCell type="PM" value="2026-07-15" onSave={vi.fn()} />);
|
|
|
|
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();
|
|
});
|
|
});
|