|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 2 | +import { render, screen, fireEvent, cleanup } from "@testing-library/react"; |
| 3 | +import { MultiFactorAuthAssertionForm } from "./multi-factor-auth-assertion-form"; |
| 4 | +import { createFirebaseUIProvider, createMockUI } from "~/tests/utils"; |
| 5 | +import { registerLocale } from "@firebase-ui/translations"; |
| 6 | +import { FactorId, MultiFactorResolver, PhoneMultiFactorGenerator, TotpMultiFactorGenerator } from "firebase/auth"; |
| 7 | + |
| 8 | +vi.mock("@/registry/sms-multi-factor-assertion-form", () => ({ |
| 9 | + SmsMultiFactorAssertionForm: ({ hint }: { hint: any }) => ( |
| 10 | + <div data-testid="sms-assertion-form"> |
| 11 | + <div data-testid="sms-hint-factor-id">{hint.factorId}</div> |
| 12 | + </div> |
| 13 | + ), |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock("@/registry/totp-multi-factor-assertion-form", () => ({ |
| 17 | + TotpMultiFactorAssertionForm: ({ hint }: { hint: any }) => ( |
| 18 | + <div data-testid="totp-assertion-form"> |
| 19 | + <div data-testid="totp-hint-factor-id">{hint.factorId}</div> |
| 20 | + </div> |
| 21 | + ), |
| 22 | +})); |
| 23 | + |
| 24 | +describe("<MultiFactorAuthAssertionForm />", () => { |
| 25 | + beforeEach(() => { |
| 26 | + vi.clearAllMocks(); |
| 27 | + }); |
| 28 | + |
| 29 | + afterEach(() => { |
| 30 | + cleanup(); |
| 31 | + }); |
| 32 | + |
| 33 | + it("throws error when no multiFactorResolver is present", () => { |
| 34 | + const ui = createMockUI(); |
| 35 | + |
| 36 | + expect(() => { |
| 37 | + render( |
| 38 | + createFirebaseUIProvider({ |
| 39 | + children: <MultiFactorAuthAssertionForm />, |
| 40 | + ui: ui, |
| 41 | + }) |
| 42 | + ); |
| 43 | + }).toThrow("MultiFactorAuthAssertionForm requires a multi-factor resolver"); |
| 44 | + }); |
| 45 | + |
| 46 | + it("auto-selects single hint and renders corresponding form", () => { |
| 47 | + const mockResolver: MultiFactorResolver = { |
| 48 | + hints: [ |
| 49 | + { |
| 50 | + uid: "test-uid", |
| 51 | + factorId: PhoneMultiFactorGenerator.FACTOR_ID, |
| 52 | + displayName: "Test Phone", |
| 53 | + }, |
| 54 | + ], |
| 55 | + } as MultiFactorResolver; |
| 56 | + |
| 57 | + const ui = createMockUI({ |
| 58 | + multiFactorResolver: mockResolver, |
| 59 | + }); |
| 60 | + |
| 61 | + render( |
| 62 | + createFirebaseUIProvider({ |
| 63 | + children: <MultiFactorAuthAssertionForm />, |
| 64 | + ui: ui, |
| 65 | + }) |
| 66 | + ); |
| 67 | + |
| 68 | + expect(screen.getByTestId("sms-assertion-form")).toBeInTheDocument(); |
| 69 | + expect(screen.getByTestId("sms-hint-factor-id")).toHaveTextContent(PhoneMultiFactorGenerator.FACTOR_ID); |
| 70 | + }); |
| 71 | + |
| 72 | + it("shows buttons for multiple hints and allows selection", () => { |
| 73 | + const mockResolver: MultiFactorResolver = { |
| 74 | + hints: [ |
| 75 | + { |
| 76 | + uid: "test-uid-1", |
| 77 | + factorId: PhoneMultiFactorGenerator.FACTOR_ID, |
| 78 | + displayName: "Test Phone", |
| 79 | + }, |
| 80 | + { |
| 81 | + uid: "test-uid-2", |
| 82 | + factorId: TotpMultiFactorGenerator.FACTOR_ID, |
| 83 | + displayName: "Test TOTP", |
| 84 | + }, |
| 85 | + ], |
| 86 | + } as MultiFactorResolver; |
| 87 | + |
| 88 | + const ui = createMockUI({ |
| 89 | + multiFactorResolver: mockResolver, |
| 90 | + locale: registerLocale("test", { |
| 91 | + labels: { |
| 92 | + mfaTotpVerification: "Set up TOTP", |
| 93 | + mfaSmsVerification: "Set up SMS", |
| 94 | + }, |
| 95 | + }), |
| 96 | + }); |
| 97 | + |
| 98 | + render( |
| 99 | + createFirebaseUIProvider({ |
| 100 | + children: <MultiFactorAuthAssertionForm />, |
| 101 | + ui: ui, |
| 102 | + }) |
| 103 | + ); |
| 104 | + |
| 105 | + expect(screen.getByRole("button", { name: "Set up SMS" })).toBeInTheDocument(); |
| 106 | + expect(screen.getByRole("button", { name: "Set up TOTP" })).toBeInTheDocument(); |
| 107 | + |
| 108 | + fireEvent.click(screen.getByRole("button", { name: "Set up TOTP" })); |
| 109 | + |
| 110 | + expect(screen.getByTestId("totp-assertion-form")).toBeInTheDocument(); |
| 111 | + expect(screen.getByTestId("totp-hint-factor-id")).toHaveTextContent(TotpMultiFactorGenerator.FACTOR_ID); |
| 112 | + expect(screen.queryByTestId("sms-assertion-form")).not.toBeInTheDocument(); |
| 113 | + }); |
| 114 | + |
| 115 | + it("renders SMS form when SMS hint is selected", () => { |
| 116 | + const mockResolver: MultiFactorResolver = { |
| 117 | + hints: [ |
| 118 | + { |
| 119 | + uid: "test-uid-1", |
| 120 | + factorId: PhoneMultiFactorGenerator.FACTOR_ID, |
| 121 | + displayName: "Test Phone", |
| 122 | + }, |
| 123 | + { |
| 124 | + uid: "test-uid-2", |
| 125 | + factorId: TotpMultiFactorGenerator.FACTOR_ID, |
| 126 | + displayName: "Test TOTP", |
| 127 | + }, |
| 128 | + ], |
| 129 | + } as MultiFactorResolver; |
| 130 | + |
| 131 | + const ui = createMockUI({ |
| 132 | + multiFactorResolver: mockResolver, |
| 133 | + locale: registerLocale("test", { |
| 134 | + labels: { |
| 135 | + mfaTotpVerification: "Set up TOTP", |
| 136 | + mfaSmsVerification: "Set up SMS", |
| 137 | + }, |
| 138 | + }), |
| 139 | + }); |
| 140 | + |
| 141 | + render( |
| 142 | + createFirebaseUIProvider({ |
| 143 | + children: <MultiFactorAuthAssertionForm />, |
| 144 | + ui: ui, |
| 145 | + }) |
| 146 | + ); |
| 147 | + |
| 148 | + fireEvent.click(screen.getByRole("button", { name: "Set up SMS" })); |
| 149 | + |
| 150 | + expect(screen.getByTestId("sms-assertion-form")).toBeInTheDocument(); |
| 151 | + expect(screen.getByTestId("sms-hint-factor-id")).toHaveTextContent(PhoneMultiFactorGenerator.FACTOR_ID); |
| 152 | + expect(screen.queryByTestId("totp-assertion-form")).not.toBeInTheDocument(); |
| 153 | + }); |
| 154 | + |
| 155 | + it("shows selection message when multiple hints are available", () => { |
| 156 | + const mockResolver: MultiFactorResolver = { |
| 157 | + hints: [ |
| 158 | + { |
| 159 | + uid: "test-uid-1", |
| 160 | + factorId: PhoneMultiFactorGenerator.FACTOR_ID, |
| 161 | + displayName: "Test Phone", |
| 162 | + }, |
| 163 | + { |
| 164 | + uid: "test-uid-2", |
| 165 | + factorId: TotpMultiFactorGenerator.FACTOR_ID, |
| 166 | + displayName: "Test TOTP", |
| 167 | + }, |
| 168 | + ], |
| 169 | + } as MultiFactorResolver; |
| 170 | + |
| 171 | + const ui = createMockUI({ |
| 172 | + multiFactorResolver: mockResolver, |
| 173 | + }); |
| 174 | + |
| 175 | + render( |
| 176 | + createFirebaseUIProvider({ |
| 177 | + children: <MultiFactorAuthAssertionForm />, |
| 178 | + ui: ui, |
| 179 | + }) |
| 180 | + ); |
| 181 | + |
| 182 | + expect(screen.getByText("Select a multi-factor authentication method")).toBeInTheDocument(); |
| 183 | + }); |
| 184 | +}); |
0 commit comments