44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
66 * You may obtain a copy of the License at
7- *
87 * http://www.apache.org/licenses/LICENSE-2.0
98 *
109 * Unless required by applicable law or agreed to in writing, software
1413 * limitations under the License.
1514 */
1615
17- import { describe , expect , it , vi } from "vitest" ;
18- import { render , screen } from "@testing-library/react" ;
19- import { GoogleIcon , GoogleSignInButton } from "~/auth/oauth/google-sign-in-button" ;
20-
21- // Mock hooks
22- vi . mock ( "~/hooks" , ( ) => ( {
23- useUI : ( ) => ( {
24- locale : "en-US" ,
25- translations : {
26- "en-US" : { labels : { signInWithGoogle : "foo bar" } } ,
27- } ,
28- } ) ,
29- } ) ) ;
16+ import { describe , it , expect , vi , afterEach , beforeEach } from "vitest" ;
17+ import { render , screen , cleanup } from "@testing-library/react" ;
18+ import { GoogleIcon , GoogleSignInButton } from "./google-sign-in-button" ;
19+ import { CreateFirebaseUIProvider , createMockUI } from "~/tests/utils" ;
20+ import { registerLocale } from "@firebase-ui/translations" ;
21+ import { ComponentProps } from "react" ;
3022
3123// Mock the OAuthButton component
32- vi . mock ( "~/auth/oauth /oauth-button" , ( ) => ( {
33- OAuthButton : ( { children, provider } : { children : React . ReactNode ; provider : any } ) => (
34- < div data-testid = "oauth-button" data-provider = { provider . constructor . name } >
24+ vi . mock ( ". /oauth-button" , ( ) => ( {
25+ OAuthButton : ( { children, provider } : ComponentProps < "div" > & { provider : any } ) => (
26+ < div data-testid = "oauth-button" data-provider = { provider ? .constructor ? .name || "GoogleAuthProvider" } >
3527 { children }
3628 </ div >
3729 ) ,
@@ -46,30 +38,165 @@ vi.mock("firebase/auth", () => ({
4638 } ,
4739} ) ) ;
4840
49- describe ( "GoogleSignInButton" , ( ) => {
41+ afterEach ( ( ) => {
42+ cleanup ( ) ;
43+ } ) ;
44+
45+ describe ( "<GoogleSignInButton />" , ( ) => {
46+ beforeEach ( ( ) => {
47+ vi . clearAllMocks ( ) ;
48+ } ) ;
49+
5050 it ( "renders with the correct provider" , ( ) => {
51- render ( < GoogleSignInButton /> ) ;
52- expect ( screen . getByTestId ( "oauth-button" ) ) . toHaveAttribute ( "data-provider" , "GoogleAuthProvider" ) ;
51+ const ui = createMockUI ( {
52+ locale : registerLocale ( "test" , {
53+ labels : {
54+ signInWithGoogle : "Sign in with Google" ,
55+ } ,
56+ } ) ,
57+ } ) ;
58+
59+ render (
60+ < CreateFirebaseUIProvider ui = { ui } >
61+ < GoogleSignInButton />
62+ </ CreateFirebaseUIProvider >
63+ ) ;
64+
65+ const oauthButton = screen . getByTestId ( "oauth-button" ) ;
66+ expect ( oauthButton ) . toBeDefined ( ) ;
67+ expect ( oauthButton . getAttribute ( "data-provider" ) ) . toBe ( "GoogleAuthProvider" ) ;
5368 } ) ;
5469
55- it ( "renders with the Google icon SVG" , ( ) => {
56- render ( < GoogleSignInButton /> ) ;
70+ it ( "renders with custom provider when provided" , ( ) => {
71+ const ui = createMockUI ( {
72+ locale : registerLocale ( "test" , {
73+ labels : {
74+ signInWithGoogle : "Sign in with Google" ,
75+ } ,
76+ } ) ,
77+ } ) ;
78+
79+ const customProvider = new ( class CustomGoogleProvider {
80+ constructor ( ) {
81+ // Empty constructor
82+ }
83+ } ) ( ) ;
84+
85+ render (
86+ < CreateFirebaseUIProvider ui = { ui } >
87+ < GoogleSignInButton provider = { customProvider as any } />
88+ </ CreateFirebaseUIProvider >
89+ ) ;
90+
91+ const oauthButton = screen . getByTestId ( "oauth-button" ) ;
92+ expect ( oauthButton ) . toBeDefined ( ) ;
93+ expect ( oauthButton . getAttribute ( "data-provider" ) ) . toBe ( "CustomGoogleProvider" ) ;
94+ } ) ;
95+
96+ it ( "renders with the Google icon" , ( ) => {
97+ const ui = createMockUI ( {
98+ locale : registerLocale ( "test" , {
99+ labels : {
100+ signInWithGoogle : "Sign in with Google" ,
101+ } ,
102+ } ) ,
103+ } ) ;
104+
105+ render (
106+ < CreateFirebaseUIProvider ui = { ui } >
107+ < GoogleSignInButton />
108+ </ CreateFirebaseUIProvider >
109+ ) ;
110+
57111 const svg = document . querySelector ( ".fui-provider__icon" ) ;
58- expect ( svg ) . toBeInTheDocument ( ) ;
112+ expect ( svg ) . toBeDefined ( ) ;
59113 expect ( svg ) . toHaveClass ( "fui-provider__icon" ) ;
114+ expect ( svg ?. tagName . toLowerCase ( ) ) . toBe ( "svg" ) ;
60115 } ) ;
61116
62- it ( "renders with the correct text" , ( ) => {
63- render ( < GoogleSignInButton /> ) ;
64- expect ( screen . getByText ( "foo bar" ) ) . toBeInTheDocument ( ) ;
117+ it ( "renders with the correct translated text" , ( ) => {
118+ const ui = createMockUI ( {
119+ locale : registerLocale ( "test" , {
120+ labels : {
121+ signInWithGoogle : "Sign in with Google" ,
122+ } ,
123+ } ) ,
124+ } ) ;
125+
126+ render (
127+ < CreateFirebaseUIProvider ui = { ui } >
128+ < GoogleSignInButton />
129+ </ CreateFirebaseUIProvider >
130+ ) ;
131+
132+ expect ( screen . getByText ( "Sign in with Google" ) ) . toBeDefined ( ) ;
65133 } ) ;
66- } ) ;
67134
68- it ( "exports a valid GoogleIcon component which is an svg" , ( ) => {
69- const { container } = render ( < GoogleIcon /> ) ;
70- const svg = container . querySelector ( "svg" ) ;
71- expect ( svg ) . toBeInTheDocument ( ) ;
72- expect ( svg ?. tagName . toLowerCase ( ) ) . toBe ( "svg" ) ;
73- expect ( svg ) . toHaveClass ( "fui-provider__icon" ) ;
135+ it ( "renders with different translated text for different locales" , ( ) => {
136+ const ui = createMockUI ( {
137+ locale : registerLocale ( "test" , {
138+ labels : {
139+ signInWithGoogle : "Iniciar sesión con Google" ,
140+ } ,
141+ } ) ,
142+ } ) ;
143+
144+ render (
145+ < CreateFirebaseUIProvider ui = { ui } >
146+ < GoogleSignInButton />
147+ </ CreateFirebaseUIProvider >
148+ ) ;
149+
150+ expect ( screen . getByText ( "Iniciar sesión con Google" ) ) . toBeDefined ( ) ;
151+ } ) ;
152+
153+ it ( "passes children to OAuthButton" , ( ) => {
154+ const ui = createMockUI ( {
155+ locale : registerLocale ( "test" , {
156+ labels : {
157+ signInWithGoogle : "Sign in with Google" ,
158+ } ,
159+ } ) ,
160+ } ) ;
161+
162+ render (
163+ < CreateFirebaseUIProvider ui = { ui } >
164+ < GoogleSignInButton />
165+ </ CreateFirebaseUIProvider >
166+ ) ;
167+
168+ const oauthButton = screen . getByTestId ( "oauth-button" ) ;
169+ expect ( oauthButton ) . toBeDefined ( ) ;
170+
171+ const svg = oauthButton . querySelector ( ".fui-provider__icon" ) ;
172+ const text = oauthButton . querySelector ( "span" ) ;
173+
174+ expect ( svg ) . toBeDefined ( ) ;
175+ expect ( text ) . toBeDefined ( ) ;
176+ expect ( text ?. textContent ) . toBe ( "Sign in with Google" ) ;
177+ } ) ;
74178} ) ;
75179
180+ describe ( "<GoogleIcon />" , ( ) => {
181+ it ( "renders as an SVG element" , ( ) => {
182+ const { container } = render ( < GoogleIcon /> ) ;
183+ const svg = container . querySelector ( "svg" ) ;
184+
185+ expect ( svg ) . toBeDefined ( ) ;
186+ expect ( svg ?. tagName . toLowerCase ( ) ) . toBe ( "svg" ) ;
187+ } ) ;
188+
189+ it ( "has the correct CSS class" , ( ) => {
190+ const { container } = render ( < GoogleIcon /> ) ;
191+ const svg = container . querySelector ( "svg" ) ;
192+
193+ expect ( svg ) . toHaveClass ( "fui-provider__icon" ) ;
194+ } ) ;
195+
196+ it ( "has the correct viewBox attribute" , ( ) => {
197+ const { container } = render ( < GoogleIcon /> ) ;
198+ const svg = container . querySelector ( "svg" ) ;
199+
200+ expect ( svg ?. getAttribute ( "viewBox" ) ) . toBe ( "0 0 48 48" ) ;
201+ } ) ;
202+ } ) ;
0 commit comments