import { vi, describe, it, expect } from "vitest"; import "@testing-library/jest-dom"; import { render, fireEvent } from "@testing-library/react"; import { buildHrDocumentListVM } from "@hivegrid/view-models"; import { HrMyDocumentsPage, type HrDocumentUploadState } from "./HrMyDocumentsPage"; vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (str: string) => str, i18n: { language: "de" } }), })); const IDLE: HrDocumentUploadState = { status: "idle", errorKey: null }; function vm(canUpload: boolean, rows: Parameters[0]["rows"] = []) { return buildHrDocumentListVM({ canUpload, uploadHref: "/acme/hr/my-documents", rows }); } describe("HrMyDocumentsPage (interim-UI, upload wiring)", () => { it("hides the upload affordance when the VM says the viewer cannot upload", () => { const { container } = render(); expect(container.querySelector('[data-ui-id="ui.hr.documents.upload"]')).not.toBeInTheDocument(); }); it("opens the upload dialog and hands the picked file + metadata to onUpload", () => { const onUpload = vi.fn(); const { container } = render(); // The trigger lives on the page; the dialog content is portalled to document.body. fireEvent.click(container.querySelector('[data-ui-id="ui.hr.documents.upload"]') as HTMLElement); const titleInput = document.querySelector('[data-ui-id="ui.hr.documents.upload.title"]') as HTMLInputElement; fireEvent.change(titleInput, { target: { value: "Arbeitsvertrag" } }); const fileInput = document.querySelector('[data-ui-id="ui.hr.documents.upload.file"]') as HTMLInputElement; const file = new File(["hello"], "vertrag.pdf", { type: "application/pdf" }); fireEvent.change(fileInput, { target: { files: [file] } }); fireEvent.click(document.querySelector('[data-ui-id="ui.hr.documents.upload.submit"]') as HTMLElement); expect(onUpload).toHaveBeenCalledTimes(1); const arg = onUpload.mock.calls[0][0]; expect(arg.title).toBe("Arbeitsvertrag"); expect(arg.file).toBe(file); expect(arg.documentKind).toBe("other"); }); it("surfaces an upload error via the Alert", () => { const { container } = render( , ); fireEvent.click(container.querySelector('[data-ui-id="ui.hr.documents.upload"]') as HTMLElement); expect(document.querySelector('[data-ui-id="ui.hr.documents.upload.progress"][data-status="error"]')).toBeInTheDocument(); }); });