Skip to content

Commit 6c32dc2

Browse files
committed
test: Add unit tests for EmailLinkAuthScreen and OAuthScreen
1 parent b6d732b commit 6c32dc2

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
2+
import { render } from "@testing-library/react";
3+
import { EmailLinkAuthScreen } from "~/auth/screens/email-link-auth-screen";
4+
import * as hooks from "~/hooks";
5+
6+
// Mock the hooks
7+
vi.mock("~/hooks", () => ({
8+
useConfig: vi.fn(),
9+
useTranslations: vi.fn(),
10+
}));
11+
12+
// Mock dependencies
13+
vi.mock("@firebase-ui/core", () => ({
14+
getTranslation: vi.fn((category, key) => {
15+
if (category === "labels" && key === "signIn") return "Sign In";
16+
if (category === "prompts" && key === "signInToAccount")
17+
return "Sign in to your account";
18+
if (category === "messages" && key === "dividerOr") return "or";
19+
return key;
20+
}),
21+
}));
22+
23+
// Mock the EmailLinkForm component
24+
vi.mock("~/auth/forms/email-link-form", () => ({
25+
EmailLinkForm: () => <div data-testid="email-link-form">Email Link Form</div>,
26+
}));
27+
28+
describe("EmailLinkAuthScreen", () => {
29+
beforeEach(() => {
30+
// Setup default mock values
31+
vi.mocked(hooks.useConfig).mockReturnValue({
32+
language: "en",
33+
} as any);
34+
35+
vi.mocked(hooks.useTranslations).mockReturnValue({} as any);
36+
});
37+
38+
afterEach(() => {
39+
vi.clearAllMocks();
40+
});
41+
42+
it("renders with correct title and subtitle", () => {
43+
const { getByText } = render(<EmailLinkAuthScreen />);
44+
45+
expect(getByText("Sign In")).toBeInTheDocument();
46+
expect(getByText("Sign in to your account")).toBeInTheDocument();
47+
});
48+
49+
it("calls useConfig to get the language", () => {
50+
render(<EmailLinkAuthScreen />);
51+
52+
expect(hooks.useConfig).toHaveBeenCalled();
53+
});
54+
55+
it("includes the EmailLinkForm component", () => {
56+
const { getByTestId } = render(<EmailLinkAuthScreen />);
57+
58+
expect(getByTestId("email-link-form")).toBeInTheDocument();
59+
});
60+
61+
it("does not render divider and children when no children are provided", () => {
62+
const { queryByText } = render(<EmailLinkAuthScreen />);
63+
64+
expect(queryByText("or")).not.toBeInTheDocument();
65+
});
66+
67+
it("renders divider and children when children are provided", () => {
68+
const { getByText } = render(
69+
<EmailLinkAuthScreen>
70+
<div>Test Child</div>
71+
</EmailLinkAuthScreen>
72+
);
73+
74+
expect(getByText("or")).toBeInTheDocument();
75+
expect(getByText("Test Child")).toBeInTheDocument();
76+
});
77+
});
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { describe, it, expect, vi } from "vitest";
2+
import { render } from "@testing-library/react";
3+
import { OAuthScreen } from "~/auth/screens/oauth-screen";
4+
5+
// Mock hooks
6+
vi.mock("~/hooks", () => ({
7+
useConfig: () => ({ language: "en" }),
8+
useTranslations: () => ({}),
9+
}));
10+
11+
// Mock getTranslation
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+
return key;
18+
}),
19+
}));
20+
21+
// Mock TermsAndPrivacy component
22+
vi.mock("../../../../src/components/terms-and-privacy", () => ({
23+
TermsAndPrivacy: () => (
24+
<div
25+
className="text-text-muted text-xs text-start"
26+
data-testid="terms-and-privacy"
27+
>
28+
Terms and Privacy
29+
</div>
30+
),
31+
}));
32+
33+
describe("OAuthScreen", () => {
34+
it("renders with correct title and subtitle", () => {
35+
const { getByText } = render(<OAuthScreen>OAuth Provider</OAuthScreen>);
36+
37+
expect(getByText("Sign In")).toBeInTheDocument();
38+
expect(getByText("Sign in to your account")).toBeInTheDocument();
39+
});
40+
41+
it("calls useConfig to get the language", () => {
42+
render(<OAuthScreen>OAuth Provider</OAuthScreen>);
43+
44+
// This test implicitly tests that useConfig is called through the mock
45+
// If it hadn't been called, the title and subtitle wouldn't render correctly
46+
});
47+
48+
it("renders children", () => {
49+
const { getByText } = render(<OAuthScreen>OAuth Provider</OAuthScreen>);
50+
51+
expect(getByText("OAuth Provider")).toBeInTheDocument();
52+
});
53+
54+
it("renders multiple children when provided", () => {
55+
const { getByText } = render(
56+
<OAuthScreen>
57+
<div>Provider 1</div>
58+
<div>Provider 2</div>
59+
</OAuthScreen>
60+
);
61+
62+
expect(getByText("Provider 1")).toBeInTheDocument();
63+
expect(getByText("Provider 2")).toBeInTheDocument();
64+
});
65+
66+
it("includes the TermsAndPrivacy component", () => {
67+
const { getByTestId } = render(<OAuthScreen>OAuth Provider</OAuthScreen>);
68+
69+
expect(getByTestId("terms-and-privacy")).toBeInTheDocument();
70+
});
71+
});

0 commit comments

Comments
 (0)