|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2024 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 19 | +import { render, screen, renderHook, cleanup } from "@testing-library/react"; |
| 20 | +import { |
| 21 | + TotpMultiFactorAssertionForm, |
| 22 | + useTotpMultiFactorAssertionFormAction, |
| 23 | +} from "./totp-multi-factor-assertion-form"; |
| 24 | +import { act } from "react"; |
| 25 | +import { signInWithMultiFactorAssertion } from "@firebase-ui/core"; |
| 26 | +import { createFirebaseUIProvider, createMockUI } from "~/tests/utils"; |
| 27 | +import { registerLocale } from "@firebase-ui/translations"; |
| 28 | +import { TotpMultiFactorGenerator } from "firebase/auth"; |
| 29 | + |
| 30 | +vi.mock("@firebase-ui/core", async (importOriginal) => { |
| 31 | + const mod = await importOriginal<typeof import("@firebase-ui/core")>(); |
| 32 | + return { |
| 33 | + ...mod, |
| 34 | + signInWithMultiFactorAssertion: vi.fn(), |
| 35 | + }; |
| 36 | +}); |
| 37 | + |
| 38 | +vi.mock("firebase/auth", async (importOriginal) => { |
| 39 | + const mod = await importOriginal<typeof import("firebase/auth")>(); |
| 40 | + return { |
| 41 | + ...mod, |
| 42 | + TotpMultiFactorGenerator: { |
| 43 | + assertionForSignIn: vi.fn(), |
| 44 | + }, |
| 45 | + }; |
| 46 | +}); |
| 47 | + |
| 48 | +describe("useTotpMultiFactorAssertionFormAction", () => { |
| 49 | + beforeEach(() => { |
| 50 | + vi.clearAllMocks(); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should return a function", () => { |
| 54 | + const mockUI = createMockUI(); |
| 55 | + const { result } = renderHook(() => useTotpMultiFactorAssertionFormAction(), { |
| 56 | + wrapper: ({ children }) => createFirebaseUIProvider({ children, ui: mockUI }), |
| 57 | + }); |
| 58 | + |
| 59 | + expect(typeof result.current).toBe("function"); |
| 60 | + }); |
| 61 | + |
| 62 | + it("should call TotpMultiFactorGenerator.assertionForSignIn and signInWithMultiFactorAssertion", async () => { |
| 63 | + const mockUI = createMockUI(); |
| 64 | + const mockAssertion = { assertion: true }; |
| 65 | + const signInWithMultiFactorAssertionMock = vi.mocked(signInWithMultiFactorAssertion); |
| 66 | + const mockHint = { |
| 67 | + factorId: "totp" as const, |
| 68 | + uid: "test-uid", |
| 69 | + enrollmentTime: "2023-01-01T00:00:00Z", |
| 70 | + }; |
| 71 | + |
| 72 | + vi.mocked(TotpMultiFactorGenerator.assertionForSignIn).mockReturnValue(mockAssertion as any); |
| 73 | + |
| 74 | + const { result } = renderHook(() => useTotpMultiFactorAssertionFormAction(), { |
| 75 | + wrapper: ({ children }) => createFirebaseUIProvider({ children, ui: mockUI }), |
| 76 | + }); |
| 77 | + |
| 78 | + await act(async () => { |
| 79 | + await result.current({ verificationCode: "123456", hint: mockHint }); |
| 80 | + }); |
| 81 | + |
| 82 | + expect(TotpMultiFactorGenerator.assertionForSignIn).toHaveBeenCalledWith("test-uid", "123456"); |
| 83 | + expect(signInWithMultiFactorAssertionMock).toHaveBeenCalledWith(expect.any(Object), mockAssertion); |
| 84 | + }); |
| 85 | +}); |
| 86 | + |
| 87 | +describe("<TotpMultiFactorAssertionForm />", () => { |
| 88 | + beforeEach(() => { |
| 89 | + vi.clearAllMocks(); |
| 90 | + }); |
| 91 | + |
| 92 | + afterEach(() => { |
| 93 | + cleanup(); |
| 94 | + }); |
| 95 | + |
| 96 | + it("should render the form correctly", () => { |
| 97 | + const mockUI = createMockUI({ |
| 98 | + locale: registerLocale("test", { |
| 99 | + labels: { |
| 100 | + verificationCode: "verificationCode", |
| 101 | + verifyCode: "verifyCode", |
| 102 | + }, |
| 103 | + }), |
| 104 | + }); |
| 105 | + |
| 106 | + const mockHint = { |
| 107 | + factorId: "totp" as const, |
| 108 | + uid: "test-uid", |
| 109 | + enrollmentTime: "2023-01-01T00:00:00Z", |
| 110 | + }; |
| 111 | + |
| 112 | + const { container } = render( |
| 113 | + createFirebaseUIProvider({ |
| 114 | + children: <TotpMultiFactorAssertionForm hint={mockHint} />, |
| 115 | + ui: mockUI, |
| 116 | + }) |
| 117 | + ); |
| 118 | + |
| 119 | + const form = container.querySelectorAll("form.fui-form"); |
| 120 | + expect(form.length).toBe(1); |
| 121 | + |
| 122 | + expect(screen.getByRole("textbox", { name: /verificationCode/i })).toBeInTheDocument(); |
| 123 | + |
| 124 | + const verifyCodeButton = screen.getByRole("button", { name: "verifyCode" }); |
| 125 | + expect(verifyCodeButton).toBeInTheDocument(); |
| 126 | + expect(verifyCodeButton).toHaveAttribute("type", "submit"); |
| 127 | + }); |
| 128 | + |
| 129 | + it("should accept onSuccess callback prop", () => { |
| 130 | + const mockUI = createMockUI({ |
| 131 | + locale: registerLocale("test", { |
| 132 | + labels: { |
| 133 | + verificationCode: "verificationCode", |
| 134 | + }, |
| 135 | + }), |
| 136 | + }); |
| 137 | + |
| 138 | + const mockHint = { |
| 139 | + factorId: "totp" as const, |
| 140 | + uid: "test-uid", |
| 141 | + enrollmentTime: "2023-01-01T00:00:00Z", |
| 142 | + }; |
| 143 | + const onSuccessMock = vi.fn(); |
| 144 | + |
| 145 | + expect(() => { |
| 146 | + render( |
| 147 | + createFirebaseUIProvider({ |
| 148 | + children: <TotpMultiFactorAssertionForm hint={mockHint} onSuccess={onSuccessMock} />, |
| 149 | + ui: mockUI, |
| 150 | + }) |
| 151 | + ); |
| 152 | + }).not.toThrow(); |
| 153 | + }); |
| 154 | + |
| 155 | + it("should render form elements correctly", () => { |
| 156 | + const mockUI = createMockUI({ |
| 157 | + locale: registerLocale("test", { |
| 158 | + labels: { |
| 159 | + verificationCode: "verificationCode", |
| 160 | + verifyCode: "verifyCode", |
| 161 | + }, |
| 162 | + }), |
| 163 | + }); |
| 164 | + |
| 165 | + const mockHint = { |
| 166 | + factorId: "totp" as const, |
| 167 | + uid: "test-uid", |
| 168 | + enrollmentTime: "2023-01-01T00:00:00Z", |
| 169 | + }; |
| 170 | + |
| 171 | + render( |
| 172 | + createFirebaseUIProvider({ |
| 173 | + children: <TotpMultiFactorAssertionForm hint={mockHint} />, |
| 174 | + ui: mockUI, |
| 175 | + }) |
| 176 | + ); |
| 177 | + |
| 178 | + expect(screen.getByRole("textbox", { name: /verificationCode/i })).toBeInTheDocument(); |
| 179 | + expect(screen.getByRole("button", { name: "verifyCode" })).toBeInTheDocument(); |
| 180 | + }); |
| 181 | + |
| 182 | + it("should render input field for TOTP code", () => { |
| 183 | + const mockUI = createMockUI({ |
| 184 | + locale: registerLocale("test", { |
| 185 | + labels: { |
| 186 | + verificationCode: "verificationCode", |
| 187 | + }, |
| 188 | + }), |
| 189 | + }); |
| 190 | + |
| 191 | + const mockHint = { |
| 192 | + factorId: "totp" as const, |
| 193 | + uid: "test-uid", |
| 194 | + enrollmentTime: "2023-01-01T00:00:00Z", |
| 195 | + }; |
| 196 | + |
| 197 | + render( |
| 198 | + createFirebaseUIProvider({ |
| 199 | + children: <TotpMultiFactorAssertionForm hint={mockHint} />, |
| 200 | + ui: mockUI, |
| 201 | + }) |
| 202 | + ); |
| 203 | + |
| 204 | + const input = screen.getByRole("textbox", { name: /verificationCode/i }); |
| 205 | + expect(input).toBeInTheDocument(); |
| 206 | + }); |
| 207 | +}); |
0 commit comments