|
1 | 1 | import { describe, it, expect, vi, beforeEach } from "vitest";
|
2 | 2 | import { createMockUI } from "~/tests/utils";
|
3 |
| -import { autoAnonymousLogin, autoUpgradeAnonymousUsers, getBehavior, hasBehavior } from "./behaviors"; |
4 |
| -import { Auth, signInAnonymously, User, UserCredential, linkWithCredential, linkWithRedirect, AuthCredential, AuthProvider } from "firebase/auth"; |
| 3 | +import { autoAnonymousLogin, autoUpgradeAnonymousUsers, getBehavior, hasBehavior, recaptchaVerification } from "./behaviors"; |
| 4 | +import { Auth, signInAnonymously, User, UserCredential, linkWithCredential, linkWithRedirect, AuthCredential, AuthProvider, RecaptchaVerifier } from "firebase/auth"; |
5 | 5 |
|
6 | 6 | vi.mock("firebase/auth", () => ({
|
7 | 7 | signInAnonymously: vi.fn(),
|
8 | 8 | linkWithCredential: vi.fn(),
|
9 | 9 | linkWithRedirect: vi.fn(),
|
| 10 | + RecaptchaVerifier: vi.fn(), |
10 | 11 | }));
|
11 | 12 |
|
12 | 13 | describe("hasBehavior", () => {
|
@@ -218,3 +219,104 @@ describe("autoUpgradeAnonymousUsers", () => {
|
218 | 219 | });
|
219 | 220 | });
|
220 | 221 | });
|
| 222 | + |
| 223 | +describe("recaptchaVerification", () => { |
| 224 | + beforeEach(() => { |
| 225 | + vi.clearAllMocks(); |
| 226 | + }); |
| 227 | + |
| 228 | + it("should create a RecaptchaVerifier with default options", () => { |
| 229 | + const mockRecaptchaVerifier = { render: vi.fn() }; |
| 230 | + vi.mocked(RecaptchaVerifier).mockImplementation(() => mockRecaptchaVerifier as any); |
| 231 | + |
| 232 | + const mockElement = document.createElement("div"); |
| 233 | + const mockUI = createMockUI(); |
| 234 | + |
| 235 | + const behavior = recaptchaVerification(); |
| 236 | + const result = behavior.recaptchaVerification(mockUI, mockElement); |
| 237 | + |
| 238 | + expect(RecaptchaVerifier).toHaveBeenCalledWith(mockUI.auth, mockElement, { |
| 239 | + size: "invisible", |
| 240 | + theme: "light", |
| 241 | + tabindex: 0, |
| 242 | + }); |
| 243 | + expect(result).toBe(mockRecaptchaVerifier); |
| 244 | + }); |
| 245 | + |
| 246 | + it("should create a RecaptchaVerifier with custom options", () => { |
| 247 | + const mockRecaptchaVerifier = { render: vi.fn() }; |
| 248 | + vi.mocked(RecaptchaVerifier).mockImplementation(() => mockRecaptchaVerifier as any); |
| 249 | + |
| 250 | + const mockElement = document.createElement("div"); |
| 251 | + const mockUI = createMockUI(); |
| 252 | + const customOptions = { |
| 253 | + size: "normal" as const, |
| 254 | + theme: "dark" as const, |
| 255 | + tabindex: 5, |
| 256 | + }; |
| 257 | + |
| 258 | + const behavior = recaptchaVerification(customOptions); |
| 259 | + const result = behavior.recaptchaVerification(mockUI, mockElement); |
| 260 | + |
| 261 | + expect(RecaptchaVerifier).toHaveBeenCalledWith(mockUI.auth, mockElement, { |
| 262 | + size: "normal", |
| 263 | + theme: "dark", |
| 264 | + tabindex: 5, |
| 265 | + }); |
| 266 | + expect(result).toBe(mockRecaptchaVerifier); |
| 267 | + }); |
| 268 | + |
| 269 | + it("should create a RecaptchaVerifier with partial custom options", () => { |
| 270 | + const mockRecaptchaVerifier = { render: vi.fn() }; |
| 271 | + vi.mocked(RecaptchaVerifier).mockImplementation(() => mockRecaptchaVerifier as any); |
| 272 | + |
| 273 | + const mockElement = document.createElement("div"); |
| 274 | + const mockUI = createMockUI(); |
| 275 | + const partialOptions = { |
| 276 | + size: "compact" as const, |
| 277 | + }; |
| 278 | + |
| 279 | + const behavior = recaptchaVerification(partialOptions); |
| 280 | + const result = behavior.recaptchaVerification(mockUI, mockElement); |
| 281 | + |
| 282 | + expect(RecaptchaVerifier).toHaveBeenCalledWith(mockUI.auth, mockElement, { |
| 283 | + size: "compact", |
| 284 | + theme: "light", |
| 285 | + tabindex: 0, |
| 286 | + }); |
| 287 | + expect(result).toBe(mockRecaptchaVerifier); |
| 288 | + }); |
| 289 | + |
| 290 | + it("should work with hasBehavior and getBehavior", () => { |
| 291 | + const mockRecaptchaVerifier = { render: vi.fn() }; |
| 292 | + vi.mocked(RecaptchaVerifier).mockImplementation(() => mockRecaptchaVerifier as any); |
| 293 | + |
| 294 | + const mockElement = document.createElement("div"); |
| 295 | + const mockUI = createMockUI({ |
| 296 | + behaviors: { |
| 297 | + recaptchaVerification: recaptchaVerification().recaptchaVerification, |
| 298 | + }, |
| 299 | + }); |
| 300 | + |
| 301 | + expect(hasBehavior(mockUI, "recaptchaVerification")).toBe(true); |
| 302 | + |
| 303 | + const behavior = getBehavior(mockUI, "recaptchaVerification"); |
| 304 | + const result = behavior(mockUI, mockElement); |
| 305 | + |
| 306 | + expect(RecaptchaVerifier).toHaveBeenCalledWith(mockUI.auth, mockElement, { |
| 307 | + size: "invisible", |
| 308 | + theme: "light", |
| 309 | + tabindex: 0, |
| 310 | + }); |
| 311 | + expect(result).toBe(mockRecaptchaVerifier); |
| 312 | + }); |
| 313 | + |
| 314 | + it("should throw error when trying to get non-existent recaptchaVerification behavior", () => { |
| 315 | + const mockUI = createMockUI(); |
| 316 | + |
| 317 | + expect(hasBehavior(mockUI, "recaptchaVerification")).toBe(false); |
| 318 | + expect(() => getBehavior(mockUI, "recaptchaVerification")).toThrow("Behavior recaptchaVerification not found"); |
| 319 | + }); |
| 320 | +}); |
| 321 | + |
| 322 | + |
0 commit comments