// Guards the in-app invoice preview: every row opens its own PDF below the // table, the row button toggles it shut again, and the panel's close button // does the same. The frame points at the inline variant of the download route, // so the preview never bypasses the permission check the download runs. import { fireEvent, render, screen } from "@testing-library/react"; import "@testing-library/jest-dom/vitest"; import { describe, expect, it, vi } from "vitest"; import { buildBillingPaymentVM } from "@hivegrid/view-models"; import { BillingPaymentPage } from "./BillingPaymentPage"; vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (key: string, options?: Record) => (options?.invoice ? `${key}:${String(options.invoice)}` : key), i18n: { language: "de" } }), })); vi.mock("~/locale/format", () => ({ useLocaleFormatters: () => ({ number: (value: number) => String(value), date: (value: string) => value, currencyFromCents: (cents: number) => String(cents), }), })); function invoice(id: string, number: string, overrides: Record = {}) { return { id, number, issuingDate: "2026-07-10", amountCents: 4_900, currency: "eur", status: "finalized", paymentStatus: "succeeded", invoiceType: "subscription", downloadHref: `/acme/settings/subscription/invoices/${id}/download`, ...overrides, }; } function renderPage(invoices = [invoice("invoice_1", "LAG-001"), invoice("invoice_2", "LAG-002", { issuingDate: "2026-06-10" })]) { const onDownloadInvoice = vi.fn(); render(); return { onDownloadInvoice }; } function previewButtons(): HTMLElement[] { return Array.from(document.querySelectorAll("[data-ui-id='ui.billing.payment.invoicePreview']")); } function previewFrame(): HTMLIFrameElement | null { return document.querySelector("[data-ui-id='ui.billing.payment.invoicePreviewFrame']"); } describe("BillingPaymentPage invoice preview", () => { it("opens the picked invoice inline below the table", () => { renderPage(); expect(previewFrame()).toBeNull(); fireEvent.click(previewButtons()[1]!); expect(previewFrame()?.getAttribute("src")).toBe("/acme/settings/subscription/invoices/invoice_2/download?disposition=inline"); expect(screen.getByText("billing:billingPayment.invoices.previewTitle:LAG-002")).toBeInTheDocument(); }); it("switches the frame to another invoice without closing it", () => { renderPage(); fireEvent.click(previewButtons()[1]!); fireEvent.click(previewButtons()[0]!); expect(previewFrame()?.getAttribute("src")).toBe("/acme/settings/subscription/invoices/invoice_1/download?disposition=inline"); }); it("closes from the row button that opened it and from the panel", () => { renderPage(); fireEvent.click(previewButtons()[0]!); fireEvent.click(previewButtons()[0]!); expect(previewFrame()).toBeNull(); fireEvent.click(previewButtons()[0]!); fireEvent.click(document.querySelector("[data-ui-id='ui.billing.payment.invoicePreviewClose']")!); expect(previewFrame()).toBeNull(); }); it("offers neither preview nor download for a voided invoice", () => { renderPage([invoice("voided", "LAG-003", { status: "voided", downloadHref: null })]); expect(previewButtons()).toHaveLength(0); expect(screen.getByText("billing:billingPayment.invoices.unavailable")).toBeInTheDocument(); }); });