shoc-frontend-new/src/test/app/v/dispatch-detail-sections.test.tsx
2026-08-11 08:57:38 -03:00

100 lines
3.2 KiB
TypeScript

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: () => <div data-testid="checklist-mock" />,
}));
vi.mock("@/app/v/_components/vendor-portal-documents", () => ({
VendorPortalDocuments: () => <div data-testid="documents-mock" />,
}));
vi.mock("@/app/v/[token]/dispatch/_components/uplift-requests-section", () => ({
UpliftRequestsSection: () => <div data-testid="uplift-mock" />,
}));
vi.mock("@/app/v/[token]/dispatch/_components/comments-section", () => ({
CommentsSection: () => <div data-testid="comments-mock" />,
}));
function buildData(
overrides: Partial<VendorPortalDispatchDetail> = {},
): 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(
<DispatchDetailSections
token="portal-token"
dispatchId={9}
data={data}
locked={locked}
refresh={vi.fn()}
pollEvidenceDocument={vi.fn(async () => 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);
});
});