import { screen } from "@testing-library/react"; import { describe, expect, it, vi } from "vitest"; import { DispatchDetailSections } from "@/app/v/[token]/dispatch/_components/dispatch-detail-sections"; import { isVendorPortalLockedStatus } from "@/domain/vendor-portal/lib/status-helpers"; import type { VendorPortalDispatchDetail, VendorPortalSignoff, } from "@/domain/vendor-portal/types/vendor-portal"; import { renderWithProviders } from "@/test/test-utils"; vi.mock("@/app/v/[token]/dispatch/_components/checklist-section", () => ({ ChecklistSection: () =>
, })); vi.mock("@/app/v/_components/vendor-portal-documents", () => ({ VendorPortalDocuments: () =>
, })); vi.mock("@/app/v/[token]/dispatch/_components/uplift-requests-section", () => ({ UpliftRequestsSection: () =>
, })); vi.mock("@/app/v/[token]/dispatch/_components/comments-section", () => ({ CommentsSection: () =>
, })); function buildData( overrides: Partial = {}, ): VendorPortalDispatchDetail { return { id: 9, dispatchNumber: "DSP-9", status: "In Progress", nteAmount: 500, checklist: [], comments: [], upliftRequests: [], signoffs: [], documents: [], ...overrides, }; } function renderSections({ status, signoffs = [], }: { status: string; signoffs?: VendorPortalSignoff[]; }) { const data = buildData({ status, signoffs }); const locked = isVendorPortalLockedStatus(status); return renderWithProviders( undefined)} updateLocal={vi.fn()} />, { withAuth: false }, ); } describe("DispatchDetailSections signoff locking", () => { it.each(["Verified", "Cancelled", "Refused"])( "locks both customer and vendor signoff forms when status is %s", (status) => { renderSections({ status }); expect(screen.getAllByText(/Signoff is unavailable for this dispatch/i)).toHaveLength(2); expect(screen.queryAllByRole("button", { name: "Submit Signoff" })).toHaveLength(0); }, ); it.each(["Completed", "In Progress"])( "keeps both signoff forms available when status is %s", (status) => { renderSections({ status }); expect(screen.getAllByRole("button", { name: "Submit Signoff" })).toHaveLength(2); expect(screen.queryByText(/Signoff is unavailable/i)).not.toBeInTheDocument(); }, ); it("keeps an existing signoff visible while the dispatch is locked", () => { const customerSignoff: VendorPortalSignoff = { signoffType: "customer", name: "Ada Lovelace", signatureMethod: "drawn", signedAt: "2024-01-02T03:04:05Z", }; renderSections({ status: "Refused", signoffs: [customerSignoff] }); expect(screen.getByText("Ada Lovelace")).toBeInTheDocument(); expect(screen.getByText("drawn")).toBeInTheDocument(); expect(screen.getByText(/Signoff is unavailable/i)).toBeInTheDocument(); expect(screen.queryAllByRole("button", { name: "Submit Signoff" })).toHaveLength(0); }); });