|
15 | 15 | */ |
16 | 16 |
|
17 | 17 | import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
18 | | -import { render, screen, fireEvent, renderHook, cleanup } from "@testing-library/react"; |
| 18 | +import { render, screen, fireEvent, renderHook, cleanup, waitFor } from "@testing-library/react"; |
19 | 19 | import { |
20 | 20 | PhoneAuthForm, |
21 | 21 | usePhoneNumberFormAction, |
@@ -80,25 +80,39 @@ import { registerLocale } from "@invertase/firebaseui-translations"; |
80 | 80 | import { FirebaseUIProvider } from "~/context"; |
81 | 81 |
|
82 | 82 | vi.mock("~/components/country-selector", () => ({ |
83 | | - CountrySelector: vi.fn().mockImplementation(({ value, onChange }) => ( |
84 | | - <div data-testid="country-selector"> |
85 | | - <select |
86 | | - onChange={(e) => |
87 | | - onChange && |
88 | | - onChange({ |
89 | | - code: e.target.value, |
90 | | - name: e.target.value === "US" ? "United States" : "United Kingdom", |
91 | | - dialCode: e.target.value === "US" ? "+1" : "+44", |
92 | | - emoji: e.target.value === "US" ? "🇺🇸" : "🇬🇧", |
93 | | - }) |
94 | | - } |
95 | | - value={value?.code} |
96 | | - > |
97 | | - <option value="US">United States</option> |
98 | | - <option value="GB">United Kingdom</option> |
99 | | - </select> |
100 | | - </div> |
101 | | - )), |
| 83 | + CountrySelector: vi.fn().mockImplementation(({ value, onChange, ref }: any) => { |
| 84 | + if (ref && typeof ref === "object" && "current" in ref) { |
| 85 | + ref.current = { |
| 86 | + getCountry: () => ({ |
| 87 | + code: "US", |
| 88 | + name: "United States", |
| 89 | + dialCode: "+1", |
| 90 | + emoji: "🇺🇸", |
| 91 | + }), |
| 92 | + setCountry: () => {}, |
| 93 | + }; |
| 94 | + } |
| 95 | + |
| 96 | + return ( |
| 97 | + <div data-testid="country-selector"> |
| 98 | + <select |
| 99 | + onChange={(e) => |
| 100 | + onChange && |
| 101 | + onChange({ |
| 102 | + code: e.target.value, |
| 103 | + name: e.target.value === "US" ? "United States" : "United Kingdom", |
| 104 | + dialCode: e.target.value === "US" ? "+1" : "+44", |
| 105 | + emoji: e.target.value === "US" ? "🇺🇸" : "🇬🇧", |
| 106 | + }) |
| 107 | + } |
| 108 | + value={value?.code} |
| 109 | + > |
| 110 | + <option value="US">United States</option> |
| 111 | + <option value="GB">United Kingdom</option> |
| 112 | + </select> |
| 113 | + </div> |
| 114 | + ); |
| 115 | + }), |
102 | 116 | })); |
103 | 117 |
|
104 | 118 | describe("usePhoneNumberFormAction", () => { |
@@ -545,4 +559,48 @@ describe("<PhoneAuthForm />", () => { |
545 | 559 | expect(screen.getByRole("button", { name: "sendCode" })).toBeInTheDocument(); |
546 | 560 | expect(screen.getByTestId("country-selector")).toBeInTheDocument(); |
547 | 561 | }); |
| 562 | + |
| 563 | + it("should render the verification code form with description after phone number submission", async () => { |
| 564 | + const mockUI = createMockUI({ |
| 565 | + locale: registerLocale("test", { |
| 566 | + labels: { |
| 567 | + phoneNumber: "Phone Number", |
| 568 | + sendCode: "Send Code", |
| 569 | + verificationCode: "verificationCode", |
| 570 | + verifyCode: "verifyCode", |
| 571 | + }, |
| 572 | + prompts: { |
| 573 | + smsVerificationPrompt: "Enter the verification code sent to your phone number", |
| 574 | + }, |
| 575 | + }), |
| 576 | + }); |
| 577 | + |
| 578 | + const mockVerificationId = "test-verification-id"; |
| 579 | + vi.mocked(verifyPhoneNumber).mockResolvedValue(mockVerificationId); |
| 580 | + |
| 581 | + const { container } = render( |
| 582 | + <FirebaseUIProvider ui={mockUI}> |
| 583 | + <PhoneAuthForm /> |
| 584 | + </FirebaseUIProvider> |
| 585 | + ); |
| 586 | + |
| 587 | + const phoneInput = screen.getByRole("textbox", { name: /phone number/i }); |
| 588 | + expect(phoneInput).toBeInTheDocument(); |
| 589 | + |
| 590 | + const sendCodeButton = screen.getByRole("button", { name: /send code/i }); |
| 591 | + |
| 592 | + await act(async () => { |
| 593 | + fireEvent.change(phoneInput, { target: { value: "1234567890" } }); |
| 594 | + fireEvent.click(sendCodeButton); |
| 595 | + }); |
| 596 | + |
| 597 | + const verificationInput = await waitFor(() => { |
| 598 | + return screen.getByRole("textbox", { name: /verificationCode/i }); |
| 599 | + }); |
| 600 | + expect(verificationInput).toBeInTheDocument(); |
| 601 | + |
| 602 | + const description = container.querySelector("[data-input-description]"); |
| 603 | + expect(description).toBeInTheDocument(); |
| 604 | + expect(description).toHaveTextContent("Enter the verification code sent to your phone number"); |
| 605 | + }); |
548 | 606 | }); |
0 commit comments