import "@testing-library/jest-dom"; import { fireEvent, render, screen } from "@testing-library/react"; import React from "react"; import { describe, expect, it, vi } from "vitest"; import LoginScreen from "./LoginScreen"; import type { LoginActionData } from "./LoginForm"; const navigate = vi.hoisted(() => vi.fn()); vi.mock("react-router", async (importOriginal) => { const actual = await importOriginal(); return { ...actual, Form: ({ children, ...props }: React.FormHTMLAttributes) =>
{children}
, Link: ({ children, to, ...props }: React.AnchorHTMLAttributes & { to: string }) => ( {children} ), useNavigate: () => navigate, useNavigation: () => ({ state: "idle" }), useSearchParams: () => [new URLSearchParams()], }; }); const translations: Record = { "account.login.button": "Log in", "account.login.divider": "or", "account.login.forgot": "Forgot your password?", "account.login.headline": "Welcome back.", "account.login.invalidCredentialsDescription": "Please check your details and try again.", "account.login.invalidCredentialsTitle": "Email or password is incorrect.", "account.login.magicLink": "Sign in with magic link", "account.login.noAccount": "No account yet?", "account.login.passwordPlaceholder": "Enter your password", "account.login.register": "Sign up", "account.login.title": "Log in", "account.shared.email": "Email", "account.shared.emailPlaceholder": "name@company.com", "account.shared.password": "Password", "api.errors.invalidPassword": "Email or password is incorrect.", }; vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (key: string) => translations[key] ?? key }), })); vi.mock("~/utils/data/useRootData", () => ({ useRootData: () => ({ csrf: "csrf-token", appConfiguration: { auth: { captcha: { enabled: false, provider: "mcaptcha", instanceUrl: undefined, loginSiteKey: undefined, }, }, }, }), })); vi.mock("~/components/captcha/Captcha", () => ({ default: () => null })); describe("LoginScreen", () => { it("shows a clear password placeholder instead of mask characters", () => { render(); const passwordInput = screen.getByLabelText("Password"); expect(passwordInput).toHaveValue(""); expect(passwordInput).toHaveAttribute("placeholder", "Enter your password"); expect(passwordInput.getAttribute("placeholder")).not.toMatch(/^[*•]+$/u); }); it("focuses and describes the password field after a server validation error without restoring the submitted secret", () => { const { rerender } = render(); const passwordInput = screen.getByLabelText("Password"); fireEvent.change(passwordInput, { target: { value: "user-submitted-secret" } }); expect(passwordInput).toHaveValue("user-submitted-secret"); const responseWithLegacySecret = { fieldErrors: { email: undefined, password: "Invalid password" }, fields: { email: "user@example.com", password: "must-not-be-restored" }, } as unknown as LoginActionData; rerender(); expect(passwordInput).toHaveFocus(); expect(passwordInput).toHaveValue(""); expect(passwordInput).toHaveAttribute("aria-invalid", "true"); expect(passwordInput).toHaveAttribute("aria-describedby", "login-password-error"); expect(passwordInput).toHaveAccessibleDescription("Invalid password"); expect(screen.getByRole("alert")).toHaveTextContent("Invalid password"); }); it("describes both fields after server validation errors", () => { render( ); const emailInput = screen.getByLabelText("Email"); const passwordInput = screen.getByLabelText("Password"); expect(emailInput).toHaveAttribute("aria-invalid", "true"); expect(emailInput).toHaveAttribute("aria-describedby", "login-email-error"); expect(emailInput).toHaveAccessibleDescription("Invalid email"); expect(passwordInput).toHaveAttribute("aria-invalid", "true"); expect(passwordInput).toHaveAccessibleDescription("Invalid password"); expect(screen.getAllByRole("alert")).toHaveLength(2); }); it("focuses and describes the password field after generic invalid credentials", () => { const { rerender } = render(); const passwordInput = screen.getByLabelText("Password"); fireEvent.change(passwordInput, { target: { value: "first-wrong-secret" } }); rerender( ); expect(passwordInput).toHaveFocus(); expect(passwordInput).toHaveValue(""); expect(passwordInput).toHaveAttribute("aria-invalid", "true"); expect(passwordInput).toHaveAccessibleDescription("Email or password is incorrect."); fireEvent.change(passwordInput, { target: { value: "second-wrong-secret" } }); rerender( ); expect(passwordInput).toHaveFocus(); expect(passwordInput).toHaveValue(""); }); });