import "@testing-library/jest-dom"; import { render, screen } from "@testing-library/react"; import { describe, expect, it, vi } from "vitest"; import ImpersonationBanner from "./components/impersonation-banner"; vi.mock("react-router", () => ({ Form: ({ children, ...props }: React.ComponentProps<"form">) =>
{children}
, })); vi.mock("react-i18next", () => ({ useTranslation: () => ({ i18n: { language: "en" }, t: (key: string) => ({ "impersonation.active": "Impersonation active", "impersonation.actingAs": "Acting as {to} from {from}", "impersonation.exit": "Exit impersonation", "impersonation.ariaLabel": "Active impersonation session", })[key] ?? key, }), })); type RootImpersonationBannerProps = { impersonatingSession: { fromUser: { email: string }; toUser: { email: string }; } | null; }; function RootImpersonationBanner({ impersonatingSession }: RootImpersonationBannerProps) { if (!impersonatingSession) { return null; } return ; } describe("root impersonation banner", () => { it("renders actor and target emails with a single exit action", () => { render( ); expect(screen.getByTestId("impersonation-banner")).toHaveAttribute("role", "status"); expect(screen.getByTestId("impersonation-banner")).toHaveTextContent("admin@example.com"); expect(screen.getByTestId("impersonation-banner")).toHaveTextContent("member@example.com"); expect(screen.getByRole("button", { name: "Exit impersonation" })).toHaveAttribute("type", "submit"); expect(screen.getByRole("button", { name: "Exit impersonation" }).closest("form")).toHaveAttribute("action", "/logout"); expect(screen.getByDisplayValue("csrf-token")).toHaveAttribute("name", "csrf"); }); it("renders no banner marker when impersonation data is null", () => { render(); expect(screen.queryByTestId("impersonation-banner")).not.toBeInTheDocument(); }); });