Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/components/screens/hr/HrMyDocumentsPage.test.tsx
T
2026-07-08 16:15:37 +02:00

52 lines
2.9 KiB
TypeScript

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<typeof buildHrDocumentListVM>[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(<HrMyDocumentsPage vm={vm(false)} submitting={false} upload={IDLE} actions={{ onArchive: vi.fn(), onUpload: vi.fn(), onResetUpload: vi.fn() }} />);
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(<HrMyDocumentsPage vm={vm(true)} submitting={false} upload={IDLE} actions={{ onArchive: vi.fn(), onUpload, onResetUpload: vi.fn() }} />);
// 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(
<HrMyDocumentsPage vm={vm(true)} submitting={false} upload={{ status: "error", errorKey: "hr.documents.upload.error.generic" }} actions={{ onArchive: vi.fn(), onUpload: vi.fn(), onResetUpload: vi.fn() }} />,
);
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();
});
});