// Guards the contract the create route relies on after "Neuer API-Schluessel" // moved from the boilerplate OpenModal onto the shared FormDialog: the dialog // reports the trimmed alias, the expiry and the active flag, and it refuses to // submit without an alias (the action rejects an empty one server-side). import { fireEvent, render, screen } from "@testing-library/react"; import "@testing-library/jest-dom/vitest"; import { describe, expect, it, vi } from "vitest"; import { ApiKeyNewDialog, type ApiKeyNewInput } from "./ApiKeyNewDialog"; vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (s: string) => s, i18n: { language: "de" } }), })); vi.mock("~/hooks/use-copy-toast", () => ({ useCopyToast: () => async () => true })); function renderDialog() { const onCreate = vi.fn<(input: ApiKeyNewInput) => void>(); render( undefined} submitting={false} errorMessage={null} onCreate={onCreate} />); return { onCreate }; } function submitButton(): HTMLButtonElement { return document.querySelector("[data-ui-id='ui.settings.apiKeys.newDialog.submit']") as HTMLButtonElement; } describe("ApiKeyNewDialog", () => { it("reports the trimmed alias, the expiry and the active flag", () => { const { onCreate } = renderDialog(); fireEvent.change(screen.getByLabelText("models.apiKey.alias"), { target: { value: " Website Lead Intake " } }); fireEvent.change(screen.getByLabelText("models.apiKey.expires"), { target: { value: "2027-01-31" } }); fireEvent.click(submitButton()); expect(onCreate).toHaveBeenCalledWith({ alias: "Website Lead Intake", expires: "2027-01-31", active: true }); }); it("keeps the submit disabled until an alias is set", () => { renderDialog(); expect(submitButton()).toBeDisabled(); fireEvent.change(screen.getByLabelText("models.apiKey.alias"), { target: { value: "Intake" } }); expect(submitButton()).not.toBeDisabled(); }); it("shows the server-side rejection", () => { render( undefined} submitting={false} errorMessage="models.apiKey.errors.aliasExists" onCreate={vi.fn()} /> ); expect(screen.getByText("models.apiKey.errors.aliasExists")).toBeInTheDocument(); }); });