|
| 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 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +import { describe, it, expect, vi, afterEach, beforeEach } from "vitest"; |
| 17 | +import { render, screen, cleanup } from "@testing-library/react"; |
| 18 | +import { AppleLogo, AppleSignInButton } from "./apple-sign-in-button"; |
| 19 | +import { CreateFirebaseUIProvider, createMockUI } from "~/tests/utils"; |
| 20 | +import { registerLocale } from "@firebase-ui/translations"; |
| 21 | +import { OAuthProvider } from "firebase/auth"; |
| 22 | + |
| 23 | +vi.mock("firebase/auth", () => ({ |
| 24 | + OAuthProvider: class OAuthProvider { |
| 25 | + constructor(providerId: string) { |
| 26 | + this.providerId = providerId; |
| 27 | + } |
| 28 | + providerId: string; |
| 29 | + }, |
| 30 | +})); |
| 31 | + |
| 32 | +afterEach(() => { |
| 33 | + cleanup(); |
| 34 | +}); |
| 35 | + |
| 36 | +describe("<AppleSignInButton />", () => { |
| 37 | + beforeEach(() => { |
| 38 | + vi.clearAllMocks(); |
| 39 | + }); |
| 40 | + |
| 41 | + it("renders with the correct provider", () => { |
| 42 | + const ui = createMockUI({ |
| 43 | + locale: registerLocale("test", { |
| 44 | + labels: { |
| 45 | + signInWithApple: "Sign in with Apple", |
| 46 | + }, |
| 47 | + }), |
| 48 | + }); |
| 49 | + |
| 50 | + render( |
| 51 | + <CreateFirebaseUIProvider ui={ui}> |
| 52 | + <AppleSignInButton /> |
| 53 | + </CreateFirebaseUIProvider> |
| 54 | + ); |
| 55 | + |
| 56 | + const button = screen.getByRole("button"); |
| 57 | + expect(button).toBeDefined(); |
| 58 | + expect(button.getAttribute("data-provider")).toBe("apple.com"); |
| 59 | + }); |
| 60 | + |
| 61 | + it("renders with custom provider when provided", () => { |
| 62 | + const ui = createMockUI({ |
| 63 | + locale: registerLocale("test", { |
| 64 | + labels: { |
| 65 | + signInWithApple: "Sign in with Apple", |
| 66 | + }, |
| 67 | + }), |
| 68 | + }); |
| 69 | + |
| 70 | + const customProvider = new OAuthProvider("custom.apple.com"); |
| 71 | + |
| 72 | + render( |
| 73 | + <CreateFirebaseUIProvider ui={ui}> |
| 74 | + <AppleSignInButton provider={customProvider} /> |
| 75 | + </CreateFirebaseUIProvider> |
| 76 | + ); |
| 77 | + |
| 78 | + const button = screen.getByRole("button"); |
| 79 | + expect(button).toBeDefined(); |
| 80 | + expect(button.getAttribute("data-provider")).toBe("custom.apple.com"); |
| 81 | + }); |
| 82 | + |
| 83 | + it("renders with the Apple icon", () => { |
| 84 | + const ui = createMockUI({ |
| 85 | + locale: registerLocale("test", { |
| 86 | + labels: { |
| 87 | + signInWithApple: "Sign in with Apple", |
| 88 | + }, |
| 89 | + }), |
| 90 | + }); |
| 91 | + |
| 92 | + render( |
| 93 | + <CreateFirebaseUIProvider ui={ui}> |
| 94 | + <AppleSignInButton /> |
| 95 | + </CreateFirebaseUIProvider> |
| 96 | + ); |
| 97 | + |
| 98 | + const svg = document.querySelector(".fui-provider__icon"); |
| 99 | + expect(svg).toBeDefined(); |
| 100 | + expect(svg).toHaveClass("fui-provider__icon"); |
| 101 | + expect(svg?.tagName.toLowerCase()).toBe("svg"); |
| 102 | + }); |
| 103 | + |
| 104 | + it("renders with the correct translated text", () => { |
| 105 | + const ui = createMockUI({ |
| 106 | + locale: registerLocale("test", { |
| 107 | + labels: { |
| 108 | + signInWithApple: "Sign in with Apple", |
| 109 | + }, |
| 110 | + }), |
| 111 | + }); |
| 112 | + |
| 113 | + render( |
| 114 | + <CreateFirebaseUIProvider ui={ui}> |
| 115 | + <AppleSignInButton /> |
| 116 | + </CreateFirebaseUIProvider> |
| 117 | + ); |
| 118 | + |
| 119 | + expect(screen.getByText("Sign in with Apple")).toBeDefined(); |
| 120 | + }); |
| 121 | + |
| 122 | + it("renders with different translated text for different locales", () => { |
| 123 | + const ui = createMockUI({ |
| 124 | + locale: registerLocale("test", { |
| 125 | + labels: { |
| 126 | + signInWithApple: "Iniciar sesión con Apple", |
| 127 | + }, |
| 128 | + }), |
| 129 | + }); |
| 130 | + |
| 131 | + render( |
| 132 | + <CreateFirebaseUIProvider ui={ui}> |
| 133 | + <AppleSignInButton /> |
| 134 | + </CreateFirebaseUIProvider> |
| 135 | + ); |
| 136 | + |
| 137 | + expect(screen.getByText("Iniciar sesión con Apple")).toBeDefined(); |
| 138 | + }); |
| 139 | + |
| 140 | + it("renders as a button with correct classes", () => { |
| 141 | + const ui = createMockUI({ |
| 142 | + locale: registerLocale("test", { |
| 143 | + labels: { |
| 144 | + signInWithApple: "Sign in with Apple", |
| 145 | + }, |
| 146 | + }), |
| 147 | + }); |
| 148 | + |
| 149 | + render( |
| 150 | + <CreateFirebaseUIProvider ui={ui}> |
| 151 | + <AppleSignInButton /> |
| 152 | + </CreateFirebaseUIProvider> |
| 153 | + ); |
| 154 | + |
| 155 | + const button = screen.getByRole("button"); |
| 156 | + expect(button).toHaveClass("fui-provider__button"); |
| 157 | + expect(button.getAttribute("type")).toBe("button"); |
| 158 | + }); |
| 159 | +}); |
| 160 | + |
| 161 | +describe("<AppleLogo />", () => { |
| 162 | + it("renders as an SVG element", () => { |
| 163 | + const { container } = render(<AppleLogo />); |
| 164 | + const svg = container.querySelector("svg"); |
| 165 | + |
| 166 | + expect(svg).toBeDefined(); |
| 167 | + expect(svg?.tagName.toLowerCase()).toBe("svg"); |
| 168 | + }); |
| 169 | + |
| 170 | + it("has the correct CSS class", () => { |
| 171 | + const { container } = render(<AppleLogo />); |
| 172 | + const svg = container.querySelector("svg"); |
| 173 | + |
| 174 | + expect(svg).toHaveClass("fui-provider__icon"); |
| 175 | + }); |
| 176 | + |
| 177 | + it("forwards custom SVG props", () => { |
| 178 | + const { container } = render(<AppleLogo data-testid="custom-svg" className="foo" width={32} />); |
| 179 | + const svg = container.querySelector('svg[data-testid="custom-svg"]'); |
| 180 | + |
| 181 | + expect(svg).toBeDefined(); |
| 182 | + expect(svg!.getAttribute("width")).toBe("32"); |
| 183 | + expect(svg).toHaveClass("fui-provider__icon"); |
| 184 | + expect(svg).toHaveClass("foo"); |
| 185 | + }); |
| 186 | +}); |
0 commit comments