Build Seth's changes C1 to C11 (Gitea issues 1, 8, 18, 23, 24, 25, 27, 31, 32, 33, 36)
2026-10-30 13:33:51 -06:00
|
|
|
import { fireEvent, screen, waitFor } from "@testing-library/react";
|
2026-10-17 23:49:14 -06:00
|
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
Build Seth's changes C1 to C11 (Gitea issues 1, 8, 18, 23, 24, 25, 27, 31, 32, 33, 36)
2026-10-30 13:33:51 -06:00
|
|
|
import { printPdf } from "../../lib/printPdf";
|
2026-10-17 23:49:14 -06:00
|
|
|
import type { Itp } from "../../api/qc";
|
|
|
|
|
import { DEPARTMENTS, renderApp } from "../../test/auth";
|
|
|
|
|
import { mockFetch } from "../../test/fetchMock";
|
|
|
|
|
import { makeItp, makeUnitFolder, QUINN } from "./fixtures";
|
|
|
|
|
|
|
|
|
|
function base(itp: Itp) {
|
|
|
|
|
return {
|
|
|
|
|
"/api/auth/me": { body: QUINN },
|
|
|
|
|
"/api/departments": { body: DEPARTMENTS },
|
|
|
|
|
"/api/units/12/itp": { body: itp },
|
|
|
|
|
"/api/qc/folders/units/12": { body: makeUnitFolder() },
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
Build Seth's changes C1 to C11 (Gitea issues 1, 8, 18, 23, 24, 25, 27, 31, 32, 33, 36)
2026-10-30 13:33:51 -06:00
|
|
|
vi.mock("../../lib/printPdf", () => ({ printPdf: vi.fn(() => Promise.resolve()) }));
|
|
|
|
|
|
2026-10-17 23:49:14 -06:00
|
|
|
afterEach(() => {
|
|
|
|
|
vi.restoreAllMocks();
|
Build Seth's changes C1 to C11 (Gitea issues 1, 8, 18, 23, 24, 25, 27, 31, 32, 33, 36)
2026-10-30 13:33:51 -06:00
|
|
|
vi.mocked(printPdf).mockReset();
|
|
|
|
|
vi.mocked(printPdf).mockImplementation(() => Promise.resolve());
|
2026-10-17 23:49:14 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("ItpPage PDF and print", () => {
|
Build Seth's changes C1 to C11 (Gitea issues 1, 8, 18, 23, 24, 25, 27, 31, 32, 33, 36)
2026-10-30 13:33:51 -06:00
|
|
|
it("opens the ITP PDF, offers the download, and prints on the page", async () => {
|
2026-10-17 23:49:14 -06:00
|
|
|
const open = vi.spyOn(window, "open").mockImplementation(() => null);
|
|
|
|
|
mockFetch(base(makeItp()));
|
|
|
|
|
renderApp("/qc/units/12/itp");
|
|
|
|
|
fireEvent.click(await screen.findByRole("button", { name: "ITP (PDF)" }));
|
|
|
|
|
expect(open).toHaveBeenLastCalledWith("/api/itps/5/pdf", "_blank", "noopener");
|
|
|
|
|
const dl = screen.getByRole("link", { name: "Download" });
|
|
|
|
|
expect(dl).toHaveAttribute("href", "/api/itps/5/pdf?download=1");
|
|
|
|
|
expect(dl).toHaveAttribute("download");
|
Build Seth's changes C1 to C11 (Gitea issues 1, 8, 18, 23, 24, 25, 27, 31, 32, 33, 36)
2026-10-30 13:33:51 -06:00
|
|
|
open.mockClear();
|
2026-10-17 23:49:14 -06:00
|
|
|
fireEvent.click(screen.getByRole("button", { name: "Print" }));
|
Build Seth's changes C1 to C11 (Gitea issues 1, 8, 18, 23, 24, 25, 27, 31, 32, 33, 36)
2026-10-30 13:33:51 -06:00
|
|
|
await waitFor(() => expect(printPdf).toHaveBeenCalledWith("/api/itps/5/print"));
|
|
|
|
|
expect(open).not.toHaveBeenCalled();
|
|
|
|
|
expect(await screen.findByRole("button", { name: "Print" })).toBeEnabled();
|
2026-10-17 23:49:14 -06:00
|
|
|
expect(screen.queryByRole("link", { name: "Signed PDF" })).not.toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
Build Seth's changes C1 to C11 (Gitea issues 1, 8, 18, 23, 24, 25, 27, 31, 32, 33, 36)
2026-10-30 13:33:51 -06:00
|
|
|
it("says so when the print cannot be prepared", async () => {
|
|
|
|
|
vi.mocked(printPdf).mockRejectedValueOnce(new Error("no pdf"));
|
|
|
|
|
mockFetch(base(makeItp()));
|
|
|
|
|
renderApp("/qc/units/12/itp");
|
|
|
|
|
fireEvent.click(await screen.findByRole("button", { name: "Print" }));
|
|
|
|
|
expect(await screen.findByRole("alert")).toHaveTextContent("The print could not be prepared.");
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-17 23:49:14 -06:00
|
|
|
it("links the signed PDF on a signed-off ITP", async () => {
|
|
|
|
|
mockFetch(
|
|
|
|
|
base(
|
|
|
|
|
makeItp({
|
|
|
|
|
status: "complete",
|
|
|
|
|
can: { edit: false, sign_off: false, reopen: false },
|
|
|
|
|
signoff: { at: "2026-10-17T12:00:00Z", by: { id: "u-quinn", display_name: "Quinn QC" }, asset_id: "a-signed" },
|
|
|
|
|
}),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
renderApp("/qc/units/12/itp");
|
|
|
|
|
const link = await screen.findByRole("link", { name: "Signed PDF" });
|
|
|
|
|
expect(link).toHaveAttribute("href", "/_blob/a-signed");
|
|
|
|
|
expect(link).toHaveAttribute("target", "_blank");
|
|
|
|
|
expect(screen.getByRole("note")).toHaveTextContent(/a signed PDF copy is filed in this unit's documents\. Signed PDF$/);
|
|
|
|
|
});
|
|
|
|
|
});
|