Skip to content

Commit c471033

Browse files
committed
test(react): PhoneAuthScreen tests
1 parent 3497ac0 commit c471033

File tree

1 file changed

+120
-47
lines changed

1 file changed

+120
-47
lines changed

packages/react/src/auth/screens/phone-auth-screen.test.tsx

Lines changed: 120 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
87
* http://www.apache.org/licenses/LICENSE-2.0
98
*
109
* Unless required by applicable law or agreed to in writing, software
@@ -14,73 +13,147 @@
1413
* limitations under the License.
1514
*/
1615

17-
import { describe, it, expect, vi } from "vitest";
18-
import { render } from "@testing-library/react";
16+
import { describe, it, expect, vi, afterEach } from "vitest";
17+
import { render, screen, cleanup } from "@testing-library/react";
1918
import { PhoneAuthScreen } from "~/auth/screens/phone-auth-screen";
19+
import { CreateFirebaseUIProvider, createMockUI } from "~/tests/utils";
20+
import { registerLocale } from "@firebase-ui/translations";
21+
22+
vi.mock("~/auth/forms/phone-auth-form", () => ({
23+
PhoneAuthForm: ({ resendDelay }: { resendDelay?: number }) => (
24+
<div data-testid="phone-auth-form" data-resend-delay={resendDelay}>
25+
Phone Auth Form
26+
</div>
27+
),
28+
}));
29+
30+
vi.mock("~/components/divider", async (originalModule) => {
31+
const module = await originalModule();
32+
return {
33+
...(module as object),
34+
Divider: ({ children }: { children: React.ReactNode }) => (
35+
<div data-testid="divider">{children}</div>
36+
),
37+
};
38+
});
39+
40+
afterEach(() => {
41+
cleanup();
42+
});
43+
44+
describe("<PhoneAuthScreen />", () => {
45+
afterEach(() => {
46+
vi.clearAllMocks();
47+
});
2048

21-
// Mock the hooks
22-
vi.mock("~/hooks", () => ({
23-
useUI: () => ({
24-
locale: "en-US",
25-
translations: {
26-
"en-US": {
49+
it("renders with correct title and subtitle", () => {
50+
const ui = createMockUI({
51+
locale: registerLocale("test", {
2752
labels: {
28-
signIn: "Sign in",
29-
dividerOr: "or",
53+
signIn: "signIn",
3054
},
3155
prompts: {
32-
signInToAccount: "Sign in to your account",
56+
signInToAccount: "signInToAccount",
3357
},
34-
},
35-
},
36-
}),
37-
}));
58+
}),
59+
});
3860

39-
// Mock the PhoneForm component
40-
vi.mock("~/auth/forms/phone-form", () => ({
41-
PhoneForm: ({ resendDelay }: { resendDelay?: number }) => (
42-
<div data-testid="phone-form" data-resend-delay={resendDelay}>
43-
Phone Form
44-
</div>
45-
),
46-
}));
61+
render(
62+
<CreateFirebaseUIProvider ui={ui}>
63+
<PhoneAuthScreen />
64+
</CreateFirebaseUIProvider>
65+
);
4766

48-
describe("PhoneAuthScreen", () => {
49-
it("displays the correct title and subtitle", () => {
50-
const { getByText } = render(<PhoneAuthScreen />);
67+
const title = screen.getByText("signIn");
68+
expect(title).toBeDefined();
69+
expect(title.className).toContain("fui-card__title");
5170

52-
expect(getByText("Sign in")).toBeInTheDocument();
53-
expect(getByText("Sign in to your account")).toBeInTheDocument();
71+
const subtitle = screen.getByText("signInToAccount");
72+
expect(subtitle).toBeDefined();
73+
expect(subtitle.className).toContain("fui-card__subtitle");
5474
});
5575

56-
it("calls useConfig to retrieve the language", () => {
57-
const { getByText } = render(<PhoneAuthScreen />);
76+
it("renders the <PhoneAuthForm /> component", () => {
77+
const ui = createMockUI();
5878

59-
expect(getByText("Sign in")).toBeInTheDocument();
79+
render(
80+
<CreateFirebaseUIProvider ui={ui}>
81+
<PhoneAuthScreen />
82+
</CreateFirebaseUIProvider>
83+
);
84+
85+
// Mocked so only has as test id
86+
expect(screen.getByTestId("phone-auth-form")).toBeDefined();
6087
});
6188

62-
it("includes the PhoneForm with the correct resendDelay prop", () => {
63-
const { getByTestId } = render(<PhoneAuthScreen resendDelay={60} />);
89+
it("passes resendDelay prop to PhoneAuthForm", () => {
90+
const ui = createMockUI();
91+
92+
render(
93+
<CreateFirebaseUIProvider ui={ui}>
94+
<PhoneAuthScreen resendDelay={60} />
95+
</CreateFirebaseUIProvider>
96+
);
6497

65-
const phoneForm = getByTestId("phone-form");
66-
expect(phoneForm).toBeInTheDocument();
98+
const phoneForm = screen.getByTestId("phone-auth-form");
99+
expect(phoneForm).toBeDefined();
67100
expect(phoneForm.getAttribute("data-resend-delay")).toBe("60");
68101
});
69102

70-
it("renders children when provided", () => {
71-
const { getByText, getByTestId } = render(
72-
<PhoneAuthScreen>
73-
<button data-testid="test-button">Test Button</button>
74-
</PhoneAuthScreen>
103+
it("renders a divider with children when present", () => {
104+
const ui = createMockUI({
105+
locale: registerLocale("test", {
106+
messages: {
107+
dividerOr: "dividerOr",
108+
},
109+
}),
110+
});
111+
112+
render(
113+
<CreateFirebaseUIProvider ui={ui}>
114+
<PhoneAuthScreen>
115+
<div data-testid="test-child">Test Child</div>
116+
</PhoneAuthScreen>
117+
</CreateFirebaseUIProvider>
75118
);
76119

77-
expect(getByTestId("test-button")).toBeInTheDocument();
78-
expect(getByText("or")).toBeInTheDocument();
120+
expect(screen.getByTestId("divider")).toBeDefined();
121+
expect(screen.getByText("dividerOr")).toBeDefined();
122+
expect(screen.getByTestId("test-child")).toBeDefined();
79123
});
80124

81-
it("does not render children or divider when not provided", () => {
82-
const { queryByText } = render(<PhoneAuthScreen />);
125+
it("does not render divider and children when no children are provided", () => {
126+
const ui = createMockUI();
127+
128+
render(
129+
<CreateFirebaseUIProvider ui={ui}>
130+
<PhoneAuthScreen />
131+
</CreateFirebaseUIProvider>
132+
);
83133

84-
expect(queryByText("or")).not.toBeInTheDocument();
134+
expect(screen.queryByTestId("divider")).toBeNull();
85135
});
86-
});
136+
137+
it("renders multiple children when provided", () => {
138+
const ui = createMockUI({
139+
locale: registerLocale("test", {
140+
messages: {
141+
dividerOr: "dividerOr",
142+
},
143+
}),
144+
});
145+
146+
render(
147+
<CreateFirebaseUIProvider ui={ui}>
148+
<PhoneAuthScreen>
149+
<div data-testid="child-1">Child 1</div>
150+
<div data-testid="child-2">Child 2</div>
151+
</PhoneAuthScreen>
152+
</CreateFirebaseUIProvider>
153+
);
154+
155+
expect(screen.getByTestId("divider")).toBeDefined();
156+
expect(screen.getByTestId("child-1")).toBeDefined();
157+
expect(screen.getByTestId("child-2")).toBeDefined();
158+
});
159+
});

0 commit comments

Comments
 (0)