|
| 1 | +import { describe, it, expect, vi } from "vitest"; |
| 2 | +import { render, fireEvent } from "@testing-library/react"; |
| 3 | +import { SignInAuthScreen } from "~/auth/screens/sign-in-auth-screen"; |
| 4 | + |
| 5 | +// Mock the hooks |
| 6 | +vi.mock("~/hooks", () => ({ |
| 7 | + useConfig: () => ({ language: "en" }), |
| 8 | + useTranslations: () => ({}), |
| 9 | +})); |
| 10 | + |
| 11 | +// Mock the translation function |
| 12 | +vi.mock("@firebase-ui/core", () => ({ |
| 13 | + getTranslation: vi.fn((category, key) => { |
| 14 | + if (category === "labels" && key === "signIn") return "Sign in"; |
| 15 | + if (category === "prompts" && key === "signInToAccount") |
| 16 | + return "Sign in to your account"; |
| 17 | + if (category === "messages" && key === "dividerOr") return "OR"; |
| 18 | + return key; |
| 19 | + }), |
| 20 | +})); |
| 21 | + |
| 22 | +// Mock the EmailPasswordForm component |
| 23 | +vi.mock("~/auth/forms/email-password-form", () => ({ |
| 24 | + EmailPasswordForm: ({ |
| 25 | + onForgotPasswordClick, |
| 26 | + onRegisterClick, |
| 27 | + }: { |
| 28 | + onForgotPasswordClick?: () => void; |
| 29 | + onRegisterClick?: () => void; |
| 30 | + }) => ( |
| 31 | + <div data-testid="email-password-form"> |
| 32 | + <button |
| 33 | + data-testid="forgot-password-button" |
| 34 | + onClick={onForgotPasswordClick} |
| 35 | + > |
| 36 | + Forgot Password |
| 37 | + </button> |
| 38 | + <button data-testid="register-button" onClick={onRegisterClick}> |
| 39 | + Register |
| 40 | + </button> |
| 41 | + </div> |
| 42 | + ), |
| 43 | +})); |
| 44 | + |
| 45 | +describe("SignInAuthScreen", () => { |
| 46 | + it("displays the correct title and subtitle", () => { |
| 47 | + const { getByText } = render(<SignInAuthScreen />); |
| 48 | + |
| 49 | + expect(getByText("Sign in")).toBeInTheDocument(); |
| 50 | + expect(getByText("Sign in to your account")).toBeInTheDocument(); |
| 51 | + }); |
| 52 | + |
| 53 | + it("calls useConfig to retrieve the language", () => { |
| 54 | + const { getByText } = render(<SignInAuthScreen />); |
| 55 | + |
| 56 | + expect(getByText("Sign in")).toBeInTheDocument(); |
| 57 | + }); |
| 58 | + |
| 59 | + it("includes the EmailPasswordForm component", () => { |
| 60 | + const { getByTestId } = render(<SignInAuthScreen />); |
| 61 | + |
| 62 | + expect(getByTestId("email-password-form")).toBeInTheDocument(); |
| 63 | + }); |
| 64 | + |
| 65 | + it("passes onForgotPasswordClick to EmailPasswordForm", () => { |
| 66 | + const mockOnForgotPasswordClick = vi.fn(); |
| 67 | + const { getByTestId } = render( |
| 68 | + <SignInAuthScreen onForgotPasswordClick={mockOnForgotPasswordClick} /> |
| 69 | + ); |
| 70 | + |
| 71 | + const forgotPasswordButton = getByTestId("forgot-password-button"); |
| 72 | + fireEvent.click(forgotPasswordButton); |
| 73 | + |
| 74 | + expect(mockOnForgotPasswordClick).toHaveBeenCalledTimes(1); |
| 75 | + }); |
| 76 | + |
| 77 | + it("passes onRegisterClick to EmailPasswordForm", () => { |
| 78 | + const mockOnRegisterClick = vi.fn(); |
| 79 | + const { getByTestId } = render( |
| 80 | + <SignInAuthScreen onRegisterClick={mockOnRegisterClick} /> |
| 81 | + ); |
| 82 | + |
| 83 | + const registerButton = getByTestId("register-button"); |
| 84 | + fireEvent.click(registerButton); |
| 85 | + |
| 86 | + expect(mockOnRegisterClick).toHaveBeenCalledTimes(1); |
| 87 | + }); |
| 88 | + |
| 89 | + it("renders children when provided", () => { |
| 90 | + const { getByText, getByTestId } = render( |
| 91 | + <SignInAuthScreen> |
| 92 | + <button data-testid="test-button">Test Button</button> |
| 93 | + </SignInAuthScreen> |
| 94 | + ); |
| 95 | + |
| 96 | + expect(getByTestId("test-button")).toBeInTheDocument(); |
| 97 | + expect(getByText("OR")).toBeInTheDocument(); |
| 98 | + }); |
| 99 | + |
| 100 | + it("does not render children or divider when not provided", () => { |
| 101 | + const { queryByText } = render(<SignInAuthScreen />); |
| 102 | + |
| 103 | + expect(queryByText("OR")).not.toBeInTheDocument(); |
| 104 | + }); |
| 105 | +}); |
0 commit comments