|
| 1 | +/** |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 18 | +import { render, screen, fireEvent, cleanup } from "@testing-library/react"; |
| 19 | +import { Policies } from "./policies"; |
| 20 | +import { createMockUI } from "../../tests/utils"; |
| 21 | +import { registerLocale } from "@firebase-ui/translations"; |
| 22 | +import { FirebaseUIProvider } from "@firebase-ui/react"; |
| 23 | + |
| 24 | +vi.mock("@firebase-ui/core", async (importOriginal) => { |
| 25 | + const mod = await importOriginal<typeof import("@firebase-ui/core")>(); |
| 26 | + return { |
| 27 | + ...mod, |
| 28 | + signInWithEmailAndPassword: vi.fn(), |
| 29 | + }; |
| 30 | +}); |
| 31 | + |
| 32 | +vi.mock("@firebase-ui/react", async (importOriginal) => { |
| 33 | + const mod = await importOriginal<typeof import("@firebase-ui/react")>(); |
| 34 | + return { |
| 35 | + ...mod, |
| 36 | + useSignInAuthFormAction: vi.fn(), |
| 37 | + }; |
| 38 | +}); |
| 39 | + |
| 40 | +describe("<Policies />", () => { |
| 41 | + beforeEach(() => { |
| 42 | + vi.clearAllMocks(); |
| 43 | + }); |
| 44 | + |
| 45 | + afterEach(() => { |
| 46 | + cleanup(); |
| 47 | + }); |
| 48 | + |
| 49 | + it("should return null when no policies are provided", () => { |
| 50 | + const mockUI = createMockUI(); |
| 51 | + |
| 52 | + const { container } = render( |
| 53 | + <FirebaseUIProvider ui={mockUI}> |
| 54 | + <Policies /> |
| 55 | + </FirebaseUIProvider> |
| 56 | + ); |
| 57 | + |
| 58 | + expect(container.firstChild).toBeNull(); |
| 59 | + }); |
| 60 | + |
| 61 | + it("should render policies with navigation callback", () => { |
| 62 | + const onNavigateMock = vi.fn(); |
| 63 | + const mockUI = createMockUI({ |
| 64 | + locale: registerLocale("test", { |
| 65 | + messages: { |
| 66 | + termsAndPrivacy: "{tos} and {privacy}", |
| 67 | + }, |
| 68 | + labels: { |
| 69 | + termsOfService: "tos", |
| 70 | + privacyPolicy: "pp", |
| 71 | + }, |
| 72 | + }), |
| 73 | + }); |
| 74 | + |
| 75 | + const mockPolicies = { |
| 76 | + termsOfServiceUrl: "https://example.com/terms", |
| 77 | + privacyPolicyUrl: "https://example.com/privacy", |
| 78 | + onNavigate: onNavigateMock, |
| 79 | + }; |
| 80 | + |
| 81 | + const { container } = render( |
| 82 | + <FirebaseUIProvider ui={mockUI} policies={mockPolicies}> |
| 83 | + <Policies /> |
| 84 | + </FirebaseUIProvider> |
| 85 | + ); |
| 86 | + |
| 87 | + const buttons = container.querySelectorAll("button"); |
| 88 | + expect(buttons).toHaveLength(2); |
| 89 | + |
| 90 | + const termsButton = screen.getByText("tos"); |
| 91 | + const privacyButton = screen.getByText("pp"); |
| 92 | + |
| 93 | + expect(termsButton).toBeInTheDocument(); |
| 94 | + expect(privacyButton).toBeInTheDocument(); |
| 95 | + |
| 96 | + fireEvent.click(termsButton); |
| 97 | + expect(onNavigateMock).toHaveBeenCalledWith("https://example.com/terms"); |
| 98 | + |
| 99 | + fireEvent.click(privacyButton); |
| 100 | + expect(onNavigateMock).toHaveBeenCalledWith("https://example.com/privacy"); |
| 101 | + }); |
| 102 | + |
| 103 | + it("should render policies with external links when no navigation callback", () => { |
| 104 | + const mockUI = createMockUI({ |
| 105 | + locale: registerLocale("test", { |
| 106 | + messages: { |
| 107 | + termsAndPrivacy: "{tos} and {privacy}", |
| 108 | + }, |
| 109 | + labels: { |
| 110 | + termsOfService: "tos", |
| 111 | + privacyPolicy: "pp", |
| 112 | + }, |
| 113 | + }), |
| 114 | + }); |
| 115 | + |
| 116 | + const mockPolicies = { |
| 117 | + termsOfServiceUrl: "https://example.com/terms", |
| 118 | + privacyPolicyUrl: "https://example.com/privacy", |
| 119 | + onNavigate: undefined, |
| 120 | + }; |
| 121 | + |
| 122 | + const { container } = render( |
| 123 | + <FirebaseUIProvider ui={mockUI} policies={mockPolicies}> |
| 124 | + <Policies /> |
| 125 | + </FirebaseUIProvider> |
| 126 | + ); |
| 127 | + |
| 128 | + const links = container.querySelectorAll("a"); |
| 129 | + expect(links).toHaveLength(2); |
| 130 | + |
| 131 | + const termsLink = screen.getByText("tos"); |
| 132 | + const privacyLink = screen.getByText("pp"); |
| 133 | + |
| 134 | + expect(termsLink).toHaveAttribute("href", "https://example.com/terms"); |
| 135 | + expect(termsLink).toHaveAttribute("target", "_blank"); |
| 136 | + expect(termsLink).toHaveAttribute("rel", "noopener noreferrer"); |
| 137 | + |
| 138 | + expect(privacyLink).toHaveAttribute("href", "https://example.com/privacy"); |
| 139 | + expect(privacyLink).toHaveAttribute("target", "_blank"); |
| 140 | + expect(privacyLink).toHaveAttribute("rel", "noopener noreferrer"); |
| 141 | + }); |
| 142 | +}); |
0 commit comments