14
14
* limitations under the License.
15
15
*/
16
16
17
- import { CommonModule } from ' @angular/common' ;
18
- import { Component , Input } from ' @angular/core' ;
17
+ import { CommonModule } from " @angular/common" ;
18
+ import { Component , Input } from " @angular/core" ;
19
19
import {
20
20
ComponentFixture ,
21
21
TestBed ,
22
22
fakeAsync ,
23
23
tick ,
24
- } from ' @angular/core/testing' ;
25
- import { By } from ' @angular/platform-browser' ;
26
- import { Router , provideRouter } from ' @angular/router' ;
27
- import { TanStackField } from ' @tanstack/angular-form' ;
28
- import { getFirebaseUITestProviders } from ' ../../../testing/test-helpers' ;
29
- import { EmailPasswordFormComponent } from ' ./email-password-form.component' ;
24
+ } from " @angular/core/testing" ;
25
+ import { By } from " @angular/platform-browser" ;
26
+ import { Router , provideRouter } from " @angular/router" ;
27
+ import { TanStackField } from " @tanstack/angular-form" ;
28
+ import { getFirebaseUITestProviders } from " ../../../testing/test-helpers" ;
29
+ import { EmailPasswordFormComponent } from " ./email-password-form.component" ;
30
30
31
31
// Define window properties for testing
32
32
declare global {
@@ -38,42 +38,42 @@ declare global {
38
38
39
39
// Mock Button component
40
40
@Component ( {
41
- selector : ' fui-button' ,
41
+ selector : " fui-button" ,
42
42
template : `<button (click)="click.emit()" data-testid="submit-button">
43
43
<ng-content></ng-content>
44
44
</button>` ,
45
45
standalone : true ,
46
46
} )
47
47
class MockButtonComponent {
48
- @Input ( ) type : string = ' button' ;
48
+ @Input ( ) type : string = " button" ;
49
49
}
50
50
51
51
// Mock TermsAndPrivacy component
52
52
@Component ( {
53
- selector : ' fui-terms-and-privacy' ,
53
+ selector : " fui-terms-and-privacy" ,
54
54
template : `<div data-testid="terms-and-privacy"></div>` ,
55
55
standalone : true ,
56
56
} )
57
57
class MockTermsAndPrivacyComponent { }
58
58
59
- describe ( ' EmailPasswordFormComponent' , ( ) => {
59
+ describe ( " EmailPasswordFormComponent" , ( ) => {
60
60
let component : EmailPasswordFormComponent ;
61
61
let fixture : ComponentFixture < EmailPasswordFormComponent > ;
62
62
let mockRouter : any ;
63
63
let signInSpy : jasmine . Spy ;
64
64
65
65
// Expected error messages from the actual implementation
66
66
const errorMessages = {
67
- invalidEmail : ' Please enter a valid email address' ,
68
- passwordTooShort : ' Password should be at least 8 characters' ,
69
- unknownError : ' An unknown error occurred' ,
67
+ invalidEmail : " Please enter a valid email address" ,
68
+ passwordTooShort : " Password should be at least 8 characters" ,
69
+ unknownError : " An unknown error occurred" ,
70
70
} ;
71
71
72
72
// Mock schema returned by createEmailFormSchema
73
73
const mockSchema = {
74
74
safeParse : ( data : any ) => {
75
75
// Test email validation
76
- if ( ! data . email . includes ( '@' ) ) {
76
+ if ( ! data . email . includes ( "@" ) ) {
77
77
return {
78
78
success : false ,
79
79
error : {
@@ -101,22 +101,22 @@ describe('EmailPasswordFormComponent', () => {
101
101
beforeEach ( async ( ) => {
102
102
// Mock router
103
103
mockRouter = {
104
- navigateByUrl : jasmine . createSpy ( ' navigateByUrl' ) ,
104
+ navigateByUrl : jasmine . createSpy ( " navigateByUrl" ) ,
105
105
} ;
106
106
107
107
// Create spies for the global functions
108
108
signInSpy = jasmine
109
- . createSpy ( ' signInWithEmailAndPassword' )
109
+ . createSpy ( " signInWithEmailAndPassword" )
110
110
. and . returnValue ( Promise . resolve ( ) ) ;
111
111
112
112
// Define the function on the window object
113
- Object . defineProperty ( window , ' signInWithEmailAndPassword' , {
113
+ Object . defineProperty ( window , " signInWithEmailAndPassword" , {
114
114
value : signInSpy ,
115
115
writable : true ,
116
116
configurable : true ,
117
117
} ) ;
118
118
119
- Object . defineProperty ( window , ' createEmailFormSchema' , {
119
+ Object . defineProperty ( window , " createEmailFormSchema" , {
120
120
value : ( ) => mockSchema ,
121
121
writable : true ,
122
122
configurable : true ,
@@ -141,17 +141,17 @@ describe('EmailPasswordFormComponent', () => {
141
141
component = fixture . componentInstance ;
142
142
143
143
// Set required inputs
144
- component . forgotPasswordRoute = ' /forgot-password' ;
145
- component . registerRoute = ' /register' ;
144
+ component . forgotPasswordRoute = " /forgot-password" ;
145
+ component . registerRoute = " /register" ;
146
146
147
147
// Mock the validateAndSignIn method without any TypeScript errors
148
- component . validateAndSignIn = jasmine . createSpy ( ' validateAndSignIn' ) ;
148
+ component . validateAndSignIn = jasmine . createSpy ( " validateAndSignIn" ) ;
149
149
150
150
fixture . detectChanges ( ) ;
151
151
await fixture . whenStable ( ) ; // Wait for async ngOnInit
152
152
} ) ;
153
153
154
- it ( ' renders the form correctly' , ( ) => {
154
+ it ( " renders the form correctly" , ( ) => {
155
155
expect ( component ) . toBeTruthy ( ) ;
156
156
157
157
// Check essential elements are present
@@ -162,26 +162,26 @@ describe('EmailPasswordFormComponent', () => {
162
162
By . css ( 'input[type="password"]' )
163
163
) ;
164
164
const termsAndPrivacy = fixture . debugElement . query (
165
- By . css ( ' fui-terms-and-privacy' )
165
+ By . css ( " fui-terms-and-privacy" )
166
166
) ;
167
- const submitButton = fixture . debugElement . query ( By . css ( ' fui-button' ) ) ;
167
+ const submitButton = fixture . debugElement . query ( By . css ( " fui-button" ) ) ;
168
168
169
169
expect ( emailInput ) . toBeTruthy ( ) ;
170
170
expect ( passwordInput ) . toBeTruthy ( ) ;
171
171
expect ( termsAndPrivacy ) . toBeTruthy ( ) ;
172
172
expect ( submitButton ) . toBeTruthy ( ) ;
173
173
} ) ;
174
174
175
- it ( ' submits the form when handleSubmit is called' , fakeAsync ( ( ) => {
175
+ it ( " submits the form when handleSubmit is called" , fakeAsync ( ( ) => {
176
176
// Set values directly on the form state
177
- component . form . state . values . email = ' [email protected] ' ;
178
- component . form . state . values . password = ' password123' ;
177
+ component . form . state . values . email = " [email protected] " ;
178
+ component . form . state . values . password = " password123" ;
179
179
180
180
// Create a submit event
181
- const event = new Event ( ' submit' ) ;
181
+ const event = new Event ( " submit" ) ;
182
182
Object . defineProperties ( event , {
183
- preventDefault : { value : jasmine . createSpy ( ' preventDefault' ) } ,
184
- stopPropagation : { value : jasmine . createSpy ( ' stopPropagation' ) } ,
183
+ preventDefault : { value : jasmine . createSpy ( " preventDefault" ) } ,
184
+ stopPropagation : { value : jasmine . createSpy ( " stopPropagation" ) } ,
185
185
} ) ;
186
186
187
187
// Call handleSubmit directly
@@ -190,62 +190,62 @@ describe('EmailPasswordFormComponent', () => {
190
190
191
191
// Check if validateAndSignIn was called with correct values
192
192
expect ( component . validateAndSignIn ) . toHaveBeenCalledWith (
193
-
194
- ' password123'
193
+
194
+ " password123"
195
195
) ;
196
196
} ) ) ;
197
197
198
- it ( ' displays error message when sign in fails' , fakeAsync ( ( ) => {
198
+ it ( " displays error message when sign in fails" , fakeAsync ( ( ) => {
199
199
// Manually set the error
200
- component . formError = ' Invalid credentials' ;
200
+ component . formError = " Invalid credentials" ;
201
201
fixture . detectChanges ( ) ;
202
202
203
203
// Check that the error message is displayed in the DOM
204
- const formErrorEl = fixture . debugElement . query ( By . css ( ' .fui-form__error' ) ) ;
204
+ const formErrorEl = fixture . debugElement . query ( By . css ( " .fui-form__error" ) ) ;
205
205
expect ( formErrorEl ) . toBeTruthy ( ) ;
206
206
expect ( formErrorEl . nativeElement . textContent . trim ( ) ) . toBe (
207
- ' Invalid credentials'
207
+ " Invalid credentials"
208
208
) ;
209
209
} ) ) ;
210
210
211
- it ( ' shows an error message for invalid input' , ( ) => {
211
+ it ( " shows an error message for invalid input" , ( ) => {
212
212
// Manually set error message for testing
213
213
component . formError = errorMessages . invalidEmail ;
214
214
fixture . detectChanges ( ) ;
215
215
216
216
// Check for error display in the DOM
217
- const formErrorEl = fixture . debugElement . query ( By . css ( ' .fui-form__error' ) ) ;
217
+ const formErrorEl = fixture . debugElement . query ( By . css ( " .fui-form__error" ) ) ;
218
218
expect ( formErrorEl ) . toBeTruthy ( ) ;
219
219
expect ( formErrorEl . nativeElement . textContent . trim ( ) ) . toBe (
220
220
errorMessages . invalidEmail
221
221
) ;
222
222
} ) ;
223
223
224
- it ( ' navigates to register route when that button is clicked' , ( ) => {
224
+ it ( " navigates to register route when that button is clicked" , ( ) => {
225
225
// Find the register button (second action button)
226
226
const registerButton = fixture . debugElement . queryAll (
227
- By . css ( ' .fui-form__action' )
227
+ By . css ( " .fui-form__action" )
228
228
) [ 1 ] ;
229
229
expect ( registerButton ) . toBeTruthy ( ) ;
230
230
231
231
// Click the button
232
232
registerButton . nativeElement . click ( ) ;
233
233
234
234
// Check navigation was triggered
235
- expect ( mockRouter . navigateByUrl ) . toHaveBeenCalledWith ( ' /register' ) ;
235
+ expect ( mockRouter . navigateByUrl ) . toHaveBeenCalledWith ( " /register" ) ;
236
236
} ) ;
237
237
238
- it ( ' navigates to forgot password route when that button is clicked' , ( ) => {
238
+ it ( " navigates to forgot password route when that button is clicked" , ( ) => {
239
239
// Find the forgot password button (first action button)
240
240
const forgotPasswordButton = fixture . debugElement . queryAll (
241
- By . css ( ' .fui-form__action' )
241
+ By . css ( " .fui-form__action" )
242
242
) [ 0 ] ;
243
243
expect ( forgotPasswordButton ) . toBeTruthy ( ) ;
244
244
245
245
// Click the button
246
246
forgotPasswordButton . nativeElement . click ( ) ;
247
247
248
248
// Check navigation was triggered
249
- expect ( mockRouter . navigateByUrl ) . toHaveBeenCalledWith ( ' /forgot-password' ) ;
249
+ expect ( mockRouter . navigateByUrl ) . toHaveBeenCalledWith ( " /forgot-password" ) ;
250
250
} ) ;
251
251
} ) ;
0 commit comments