150 lines
5.6 KiB
TypeScript
150 lines
5.6 KiB
TypeScript
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<typeof import("react-router")>();
|
|
return {
|
|
...actual,
|
|
Form: ({ children, ...props }: React.FormHTMLAttributes<HTMLFormElement>) => <form {...props}>{children}</form>,
|
|
Link: ({ children, to, ...props }: React.AnchorHTMLAttributes<HTMLAnchorElement> & { to: string }) => (
|
|
<a href={to} {...props}>
|
|
{children}
|
|
</a>
|
|
),
|
|
useNavigate: () => navigate,
|
|
useNavigation: () => ({ state: "idle" }),
|
|
useSearchParams: () => [new URLSearchParams()],
|
|
};
|
|
});
|
|
|
|
const translations: Record<string, string> = {
|
|
"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(<LoginScreen actionData={undefined} />);
|
|
|
|
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(<LoginScreen actionData={undefined} />);
|
|
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(<LoginScreen actionData={responseWithLegacySecret} />);
|
|
|
|
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(
|
|
<LoginScreen
|
|
actionData={{
|
|
fieldErrors: { email: "Invalid email", password: "Invalid password" },
|
|
fields: { email: "not-an-email" },
|
|
}}
|
|
/>
|
|
);
|
|
|
|
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(<LoginScreen actionData={undefined} />);
|
|
const passwordInput = screen.getByLabelText("Password");
|
|
fireEvent.change(passwordInput, { target: { value: "first-wrong-secret" } });
|
|
|
|
rerender(
|
|
<LoginScreen
|
|
actionData={{
|
|
error: "Email or password is incorrect.",
|
|
fields: { email: "user@example.com" },
|
|
}}
|
|
/>
|
|
);
|
|
|
|
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(
|
|
<LoginScreen
|
|
actionData={{
|
|
error: "Email or password is incorrect.",
|
|
fields: { email: "user@example.com" },
|
|
}}
|
|
/>
|
|
);
|
|
|
|
expect(passwordInput).toHaveFocus();
|
|
expect(passwordInput).toHaveValue("");
|
|
});
|
|
});
|