import type { GridColDef } from "@mui/x-data-grid"; import { describe, expect, it } from "vitest"; import { withEditablePayDate } from "@/dashboard/approved-columns"; import type { MockInvoice } from "@/mocks"; import { formatRelativeFromIsoDate, formatSlashDate } from "@/dashboard/invoice-date-format"; describe("invoice column date helpers (AP-47)", () => { it("formats ISO dates as MM/DD/YYYY", () => { expect(formatSlashDate("2026-04-10")).toBe("04/10/2026"); }); it("formats relative ages from a fixed now", () => { const now = new Date("2026-08-11T12:00:00"); expect(formatRelativeFromIsoDate("2026-08-11", now)).toBe("today"); expect(formatRelativeFromIsoDate("2026-08-10", now)).toBe("1 day ago"); expect(formatRelativeFromIsoDate("2026-02-11", now)).toBe("6 months ago"); }); it("uses calendar days so early hours still treat yesterday as 1 day ago", () => { const earlyMorning = new Date("2026-08-11T01:15:00"); expect(formatRelativeFromIsoDate("2026-08-10", earlyMorning)).toBe("1 day ago"); expect(formatRelativeFromIsoDate("2026-08-11", earlyMorning)).toBe("today"); }); it("formats future invoice and due dates instead of clamping to today", () => { const now = new Date("2026-08-11T12:00:00"); expect(formatRelativeFromIsoDate("2026-08-12", now)).toBe("in 1 day"); expect(formatRelativeFromIsoDate("2026-08-16", now)).toBe("in 5 days"); expect(formatRelativeFromIsoDate("2026-10-11", now)).toBe("in 2 months"); }); }); describe("withEditablePayDate", () => { const hidden: GridColDef = { field: "vendorName", headerName: "Vendor" }; const payDate: GridColDef = { field: "payDate", headerName: "Payment Date" }; it("appends an editable pay date when the catalog hides Payment Date", () => { const columns = withEditablePayDate([hidden], payDate); expect(columns.filter((column) => column.field === "payDate")).toEqual([ { field: "payDate", headerName: "Payment Date", editable: true }, ]); }); it("keeps a single editable pay date when Payment Date is already visible", () => { const columns = withEditablePayDate([hidden, { ...payDate, editable: false }], payDate); const matches = columns.filter((column) => column.field === "payDate"); expect(matches).toHaveLength(1); expect(matches[0]?.editable).toBe(true); }); });