4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
6
6
* You may obtain a copy of the License at
7
- *
8
7
* http://www.apache.org/licenses/LICENSE-2.0
9
8
*
10
9
* Unless required by applicable law or agreed to in writing, software
14
13
* limitations under the License.
15
14
*/
16
15
17
- import { describe , it , expect , vi } from "vitest" ;
18
- import { render , fireEvent } from "@testing-library/react" ;
16
+ import { describe , it , expect , vi , afterEach , beforeEach } from "vitest" ;
17
+ import { render , screen , fireEvent , cleanup } from "@testing-library/react" ;
19
18
import { SignInAuthScreen } from "~/auth/screens/sign-in-auth-screen" ;
19
+ import { CreateFirebaseUIProvider , createMockUI } from "~/tests/utils" ;
20
+ import { registerLocale } from "@firebase-ui/translations" ;
20
21
21
- // Mock the hooks
22
- vi . mock ( "~/hooks" , ( ) => ( {
23
- useUI : ( ) => ( {
24
- locale : "en-US" ,
25
- translations : {
26
- "en-US" : {
27
- labels : {
28
- signIn : "Sign in" ,
29
- dividerOr : "or" ,
30
- } ,
31
- prompts : {
32
- signInToAccount : "Sign in to your account" ,
33
- } ,
34
- } ,
35
- } ,
36
- } ) ,
37
- } ) ) ;
38
-
39
- // Mock the EmailPasswordForm component
40
- vi . mock ( "~/auth/forms/email-password-form" , ( ) => ( {
41
- EmailPasswordForm : ( {
22
+ vi . mock ( "~/auth/forms/sign-in-auth-form" , ( ) => ( {
23
+ SignInAuthForm : ( {
42
24
onForgotPasswordClick,
43
25
onRegisterClick,
44
26
} : {
45
27
onForgotPasswordClick ?: ( ) => void ;
46
28
onRegisterClick ?: ( ) => void ;
47
29
} ) => (
48
- < div data-testid = "email-password -form" >
30
+ < div data-testid = "sign-in-auth -form" >
49
31
< button data-testid = "forgot-password-button" onClick = { onForgotPasswordClick } >
50
32
Forgot Password
51
33
</ button >
@@ -56,60 +38,151 @@ vi.mock("~/auth/forms/email-password-form", () => ({
56
38
) ,
57
39
} ) ) ;
58
40
59
- describe ( "SignInAuthScreen" , ( ) => {
60
- it ( "displays the correct title and subtitle" , ( ) => {
61
- const { getByText } = render ( < SignInAuthScreen /> ) ;
41
+ vi . mock ( "~/components/divider" , async ( originalModule ) => {
42
+ const module = await originalModule ( ) ;
43
+ return {
44
+ ...( module as object ) ,
45
+ Divider : ( { children } : { children : React . ReactNode } ) => (
46
+ < div data-testid = "divider" > { children } </ div >
47
+ ) ,
48
+ } ;
49
+ } ) ;
50
+
51
+ describe ( "<SignInAuthScreen />" , ( ) => {
52
+ beforeEach ( ( ) => {
53
+ vi . clearAllMocks ( ) ;
54
+ } ) ;
62
55
63
- expect ( getByText ( "Sign in" ) ) . toBeInTheDocument ( ) ;
64
- expect ( getByText ( "Sign in to your account" ) ) . toBeInTheDocument ( ) ;
56
+ afterEach ( ( ) => {
57
+ cleanup ( ) ;
65
58
} ) ;
66
59
67
- it ( "calls useConfig to retrieve the language" , ( ) => {
68
- const { getByText } = render ( < SignInAuthScreen /> ) ;
60
+ it ( "renders with correct title and subtitle" , ( ) => {
61
+ const ui = createMockUI ( {
62
+ locale : registerLocale ( "test" , {
63
+ labels : {
64
+ signIn : "signIn" ,
65
+ } ,
66
+ prompts : {
67
+ signInToAccount : "signInToAccount" ,
68
+ } ,
69
+ } ) ,
70
+ } ) ;
71
+
72
+ render (
73
+ < CreateFirebaseUIProvider ui = { ui } >
74
+ < SignInAuthScreen />
75
+ </ CreateFirebaseUIProvider >
76
+ ) ;
77
+
78
+ const title = screen . getByText ( "signIn" ) ;
79
+ expect ( title ) . toBeDefined ( ) ;
80
+ expect ( title . className ) . toContain ( "fui-card__title" ) ;
69
81
70
- expect ( getByText ( "Sign in" ) ) . toBeInTheDocument ( ) ;
82
+ const subtitle = screen . getByText ( "signInToAccount" ) ;
83
+ expect ( subtitle ) . toBeDefined ( ) ;
84
+ expect ( subtitle . className ) . toContain ( "fui-card__subtitle" ) ;
71
85
} ) ;
72
86
73
- it ( "includes the EmailPasswordForm component" , ( ) => {
74
- const { getByTestId } = render ( < SignInAuthScreen /> ) ;
87
+ it ( "renders the <SignInAuthForm /> component" , ( ) => {
88
+ const ui = createMockUI ( ) ;
89
+
90
+ render (
91
+ < CreateFirebaseUIProvider ui = { ui } >
92
+ < SignInAuthScreen />
93
+ </ CreateFirebaseUIProvider >
94
+ ) ;
75
95
76
- expect ( getByTestId ( "email-password-form" ) ) . toBeInTheDocument ( ) ;
96
+ // Mocked so only has as test id
97
+ expect ( screen . getByTestId ( "sign-in-auth-form" ) ) . toBeDefined ( ) ;
77
98
} ) ;
78
99
79
- it ( "passes onForgotPasswordClick to EmailPasswordForm " , ( ) => {
100
+ it ( "passes onForgotPasswordClick to SignInAuthForm " , ( ) => {
80
101
const mockOnForgotPasswordClick = vi . fn ( ) ;
81
- const { getByTestId } = render ( < SignInAuthScreen onForgotPasswordClick = { mockOnForgotPasswordClick } /> ) ;
102
+ const ui = createMockUI ( ) ;
103
+
104
+ render (
105
+ < CreateFirebaseUIProvider ui = { ui } >
106
+ < SignInAuthScreen onForgotPasswordClick = { mockOnForgotPasswordClick } />
107
+ </ CreateFirebaseUIProvider >
108
+ ) ;
82
109
83
- const forgotPasswordButton = getByTestId ( "forgot-password-button" ) ;
110
+ const forgotPasswordButton = screen . getByTestId ( "forgot-password-button" ) ;
84
111
fireEvent . click ( forgotPasswordButton ) ;
85
112
86
113
expect ( mockOnForgotPasswordClick ) . toHaveBeenCalledTimes ( 1 ) ;
87
114
} ) ;
88
115
89
- it ( "passes onRegisterClick to EmailPasswordForm " , ( ) => {
116
+ it ( "passes onRegisterClick to SignInAuthForm " , ( ) => {
90
117
const mockOnRegisterClick = vi . fn ( ) ;
91
- const { getByTestId } = render ( < SignInAuthScreen onRegisterClick = { mockOnRegisterClick } /> ) ;
118
+ const ui = createMockUI ( ) ;
119
+
120
+ render (
121
+ < CreateFirebaseUIProvider ui = { ui } >
122
+ < SignInAuthScreen onRegisterClick = { mockOnRegisterClick } />
123
+ </ CreateFirebaseUIProvider >
124
+ ) ;
92
125
93
- const registerButton = getByTestId ( "register-button" ) ;
126
+ const registerButton = screen . getByTestId ( "register-button" ) ;
94
127
fireEvent . click ( registerButton ) ;
95
128
96
129
expect ( mockOnRegisterClick ) . toHaveBeenCalledTimes ( 1 ) ;
97
130
} ) ;
98
131
99
- it ( "renders children when provided" , ( ) => {
100
- const { getByText, getByTestId } = render (
101
- < SignInAuthScreen >
102
- < button data-testid = "test-button" > Test Button</ button >
103
- </ SignInAuthScreen >
132
+ it ( "renders a divider with children when present" , ( ) => {
133
+ const ui = createMockUI ( {
134
+ locale : registerLocale ( "test" , {
135
+ messages : {
136
+ dividerOr : "dividerOr" ,
137
+ } ,
138
+ } ) ,
139
+ } ) ;
140
+
141
+ render (
142
+ < CreateFirebaseUIProvider ui = { ui } >
143
+ < SignInAuthScreen >
144
+ < div data-testid = "test-child" > Test Child</ div >
145
+ </ SignInAuthScreen >
146
+ </ CreateFirebaseUIProvider >
147
+ ) ;
148
+
149
+ expect ( screen . getByTestId ( "divider" ) ) . toBeDefined ( ) ;
150
+ expect ( screen . getByText ( "dividerOr" ) ) . toBeDefined ( ) ;
151
+ expect ( screen . getByTestId ( "test-child" ) ) . toBeDefined ( ) ;
152
+ } ) ;
153
+
154
+ it ( "does not render divider and children when no children are provided" , ( ) => {
155
+ const ui = createMockUI ( ) ;
156
+
157
+ render (
158
+ < CreateFirebaseUIProvider ui = { ui } >
159
+ < SignInAuthScreen />
160
+ </ CreateFirebaseUIProvider >
104
161
) ;
105
162
106
- expect ( getByTestId ( "test-button" ) ) . toBeInTheDocument ( ) ;
107
- expect ( getByText ( "or" ) ) . toBeInTheDocument ( ) ;
163
+ expect ( screen . queryByTestId ( "divider" ) ) . toBeNull ( ) ;
108
164
} ) ;
109
165
110
- it ( "does not render children or divider when not provided" , ( ) => {
111
- const { queryByText } = render ( < SignInAuthScreen /> ) ;
166
+ it ( "renders multiple children when provided" , ( ) => {
167
+ const ui = createMockUI ( {
168
+ locale : registerLocale ( "test" , {
169
+ messages : {
170
+ dividerOr : "dividerOr" ,
171
+ } ,
172
+ } ) ,
173
+ } ) ;
174
+
175
+ render (
176
+ < CreateFirebaseUIProvider ui = { ui } >
177
+ < SignInAuthScreen >
178
+ < div data-testid = "child-1" > Child 1</ div >
179
+ < div data-testid = "child-2" > Child 2</ div >
180
+ </ SignInAuthScreen >
181
+ </ CreateFirebaseUIProvider >
182
+ ) ;
112
183
113
- expect ( queryByText ( "or" ) ) . not . toBeInTheDocument ( ) ;
184
+ expect ( screen . getByTestId ( "divider" ) ) . toBeDefined ( ) ;
185
+ expect ( screen . getByTestId ( "child-1" ) ) . toBeDefined ( ) ;
186
+ expect ( screen . getByTestId ( "child-2" ) ) . toBeDefined ( ) ;
114
187
} ) ;
115
- } ) ;
188
+ } ) ;
0 commit comments