shoc-frontend-new/src/test/app/v/vendor-portal-documents.test.tsx
Alexandre Brandizzi 45909e7aa6
Complete vendor operations roadmap frontend (#52)
* feat(vendors): complete operations roadmap frontend

* test(vendors): cover work order assignment flow

* fix(vendors): address review feedback

* fix(vendors): prevent stale preference saves

* fix(vendors): require truthful appointment bounds
2026-07-24 18:10:45 -03:00

100 lines
3 KiB
TypeScript

import { fireEvent, screen, waitFor } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { VendorPortalDocuments } from "@/app/v/_components/vendor-portal-documents";
import { vendorPortalApi } from "@/domain/vendor-portal/api/vendor-portal-api";
import type { VendorPortalDocument } from "@/domain/vendor-portal/types/vendor-portal";
import { renderWithProviders } from "@/test/test-utils";
const quarantinedDocument: VendorPortalDocument = {
id: 12,
originalFileName: "completion.pdf",
contentType: "application/pdf",
sizeBytes: 1024,
scanStatus: "Pending",
reviewStatus: "Processing",
version: 1,
canDownload: false,
};
afterEach(() => {
vi.restoreAllMocks();
});
describe("VendorPortalDocuments", () => {
it("rejects unsupported files before starting an upload", async () => {
const upload = vi.spyOn(vendorPortalApi, "uploadDocument");
renderWithProviders(
<VendorPortalDocuments
token="portal-token"
dispatchId={7}
documents={[]}
locked={false}
onChanged={vi.fn()}
/>,
{ withAuth: false },
);
fireEvent.change(screen.getByLabelText("Choose completion document"), {
target: { files: [new File(["text"], "notes.txt", { type: "text/plain" })] },
});
expect(await screen.findByRole("alert")).toHaveTextContent(
"Only PDF, JPG, and PNG files are allowed.",
);
expect(upload).not.toHaveBeenCalled();
});
it("keeps pending documents quarantined", () => {
renderWithProviders(
<VendorPortalDocuments
token="portal-token"
dispatchId={7}
documents={[quarantinedDocument]}
locked={false}
onChanged={vi.fn()}
/>,
{ withAuth: false },
);
expect(screen.getByRole("button", { name: "Quarantined" })).toBeDisabled();
expect(screen.getByText("Pending")).toBeInTheDocument();
expect(screen.getByText("Processing")).toBeInTheDocument();
});
it("reports upload progress and refreshes the dispatch after success", async () => {
const onChanged = vi.fn().mockResolvedValue(undefined);
let finishUpload: (() => void) | undefined;
vi.spyOn(vendorPortalApi, "uploadDocument").mockImplementation(async ({ onProgress }) => {
onProgress(65);
await new Promise<void>((resolve) => {
finishUpload = resolve;
});
return quarantinedDocument;
});
renderWithProviders(
<VendorPortalDocuments
token="portal-token"
dispatchId={7}
documents={[]}
locked={false}
onChanged={onChanged}
/>,
{ withAuth: false },
);
fireEvent.change(screen.getByLabelText("Choose completion document"), {
target: {
files: [
new File(["%PDF-1.4"], "completion.pdf", {
type: "application/pdf",
}),
],
},
});
expect(await screen.findByText("Uploading… 65%")).toBeInTheDocument();
finishUpload?.();
await waitFor(() => expect(onChanged).toHaveBeenCalledOnce());
});
});