63 lines
2.3 KiB
TypeScript
63 lines
2.3 KiB
TypeScript
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">) => <form {...props}>{children}</form>,
|
|
}));
|
|
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 <ImpersonationBanner fromEmail={impersonatingSession.fromUser.email} toEmail={impersonatingSession.toUser.email} csrf="csrf-token" />;
|
|
}
|
|
|
|
describe("root impersonation banner", () => {
|
|
it("renders actor and target emails with a single exit action", () => {
|
|
render(
|
|
<RootImpersonationBanner
|
|
impersonatingSession={{
|
|
fromUser: { email: "admin@example.com" },
|
|
toUser: { email: "member@example.com" },
|
|
}}
|
|
/>
|
|
);
|
|
|
|
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(<RootImpersonationBanner impersonatingSession={null} />);
|
|
|
|
expect(screen.queryByTestId("impersonation-banner")).not.toBeInTheDocument();
|
|
});
|
|
});
|