// @vitest-environment jsdom import { render, screen, fireEvent, cleanup } from "@testing-library/react"; import { afterEach, expect, test } from "vitest"; 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, 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, 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, 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, month: 8 as number | null, quarter: null as number | null, isQuarter: false, periodLabel: "August 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 Monat", () => { render( , ); fireEvent.click(screen.getByRole("button", { name: /Alle offenen im Monat/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 Monat/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("links to quarter view from month view", () => { render( , ); const quarterLink = screen.getByRole("link", { name: /Ganzes Quartal/i }); expect(quarterLink.getAttribute("href") ?? "").toContain("quarter=3"); }); test("links to month view from quarter view", () => { render( , ); const monthLink = screen.getByRole("link", { name: /Monatsansicht/i }); expect(monthLink.getAttribute("href") ?? "").toContain("month="); });