// @vitest-environment jsdom import { render, screen, fireEvent, cleanup } from "@testing-library/react"; import { afterEach, expect, test, vi } from "vitest"; vi.mock("next/navigation", () => ({ useRouter: () => ({ refresh: vi.fn(), push: vi.fn() }) })); import { BillingSelection } from "./BillingSelection"; import type { FinalizedReportSummary } from "./page-content"; import type { BillingPosition, RepeatDownloadPosition } from "@/modules/billing/queries"; afterEach(cleanup); const openPosition: BillingPosition = { id: "u1", assetId: "asset-1", filename: "jubel.jpg", albumName: "Presse 2026", purpose: "Titelgeschichte", reference: "Ausgabe 8/26", priceCents: 6_000, collab: false, status: "open", selectable: true, createdAt: new Date("2026-08-05T10:00:00.000Z"), reportNumber: null, invoiceNumber: null, }; const billedPosition: BillingPosition = { id: "u2", assetId: "asset-2", filename: "sport.jpg", albumName: "Presse 2026", purpose: "Sportbericht", reference: null, priceCents: 1_500, collab: false, status: "billed", selectable: false, createdAt: new Date("2026-08-06T10:00:00.000Z"), reportNumber: 42, invoiceNumber: "RE-2026-042", }; const reopenedPosition: BillingPosition = { id: "u3", assetId: "asset-3", filename: "redaktion.jpg", albumName: "Presse 2026", purpose: "Reopened", reference: null, priceCents: 1_500, collab: false, status: "reopened", selectable: true, createdAt: new Date("2026-08-07T10:00:00.000Z"), reportNumber: null, invoiceNumber: null, }; const repeatDownload: RepeatDownloadPosition = { id: "d1", assetId: "asset-1", usageId: "u1", authorizedAt: new Date("2026-08-10T12:00:00.000Z"), status: "completed", repeatDownloadNote: "Zweite Nutzung", }; const defaultProps = { editorialId: "e1", repeatDownloads: [] as RepeatDownloadPosition[], year: 2026, quarter: 3, periodLabel: "Q3 2026", reports: [] as FinalizedReportSummary[], }; function getCheckbox(filename: string): HTMLInputElement { return screen.getByRole("checkbox", { name: `${filename} auswählen`, }) as HTMLInputElement; } test("renders billable positions with individual checkboxes", () => { render( , ); expect(screen.getByText("jubel.jpg")).toBeTruthy(); expect(screen.getByText("sport.jpg")).toBeTruthy(); expect(getCheckbox("jubel.jpg").checked).toBe(false); }); test("disables the checkbox for billed positions and shows report reference", () => { render( , ); const checkbox = getCheckbox("sport.jpg"); expect(checkbox.disabled).toBe(true); expect(screen.getByText(/Bericht Nr\. 42/)).toBeTruthy(); expect(screen.getByText(/RE-2026-042/)).toBeTruthy(); }); test("selects all open positions via Alle offenen im Quartal", () => { render( , ); fireEvent.click(screen.getByRole("button", { name: /Alle offenen im Quartal/i })); expect(getCheckbox("jubel.jpg").checked).toBe(true); expect(getCheckbox("redaktion.jpg").checked).toBe(true); expect(getCheckbox("sport.jpg").checked).toBe(false); }); test("clears the selection via Auswahl aufheben", () => { render( , ); const checkbox = getCheckbox("jubel.jpg"); fireEvent.click(checkbox); expect(checkbox.checked).toBe(true); fireEvent.click(screen.getByRole("button", { name: /Auswahl aufheben/i })); expect(checkbox.checked).toBe(false); }); test("displays selected count and cents-derived EUR total", () => { const { container } = render( , ); fireEvent.click(screen.getByRole("button", { name: /Alle offenen im Quartal/i })); const summary = container.querySelector(".billing-summary"); expect(summary?.textContent ?? "").toMatch(/2 Nutzungen ausgewählt/); expect(summary?.textContent ?? "").toMatch(/75,00 EUR/); }); test("renders repeat downloads in a separate section", () => { render( , ); expect(screen.getByText(/Kostenlose Wiederabrufe/)).toBeTruthy(); expect(screen.getByText("Zweite Nutzung")).toBeTruthy(); }); test("shows empty state when no billable positions exist", () => { render( , ); expect(screen.getByText(/Keine abrechenbaren Nutzungen/)).toBeTruthy(); }); test("offers only quarterly periods", () => { render(); expect(screen.queryByRole("link", { name: /Monatsansicht/i })).toBeNull(); expect(screen.queryByText("Monat")).toBeNull(); expect(screen.getByText("Quartal")).toBeTruthy(); expect(screen.getByText(/Zeitraum: Q3 2026/)).toBeTruthy(); });