1
- import { describe , it , expect } from 'vitest' ;
1
+ import { describe , it , expect , beforeEach } from 'vitest' ;
2
2
import { FirebaseUIError , handleFirebaseError } from '../../src/errors' ;
3
3
4
4
describe ( 'FirebaseUIError' , ( ) => {
@@ -64,27 +64,26 @@ describe('FirebaseUIError', () => {
64
64
} ) ;
65
65
66
66
describe ( 'handleFirebaseError' , ( ) => {
67
- it ( 'should throw FirebaseUIError for Firebase errors' , ( ) => {
67
+ beforeEach ( ( ) => {
68
+ window . sessionStorage . clear ( ) ;
69
+ } ) ;
70
+
71
+ it ( 'should throw FirebaseUIError for Firebase errors' , async ( ) => {
68
72
const firebaseError = {
69
73
name : 'FirebaseError' ,
70
74
code : 'auth/user-not-found' ,
71
75
} ;
72
76
73
- expect ( ( ) => handleFirebaseError ( firebaseError ) ) . toThrow ( FirebaseUIError ) ;
77
+ await expect ( handleFirebaseError ( firebaseError ) ) . rejects . toThrow ( FirebaseUIError ) ;
74
78
} ) ;
75
79
76
- it ( 'should throw FirebaseUIError with unknown code for non-Firebase errors' , ( ) => {
80
+ it ( 'should throw FirebaseUIError with unknown code for non-Firebase errors' , async ( ) => {
77
81
const error = new Error ( 'Random error' ) ;
78
82
79
- try {
80
- handleFirebaseError ( error ) ;
81
- } catch ( e ) {
82
- expect ( e ) . toBeInstanceOf ( FirebaseUIError ) ;
83
- expect ( e . code ) . toBe ( 'unknown' ) ;
84
- }
83
+ await expect ( handleFirebaseError ( error ) ) . rejects . toThrow ( FirebaseUIError ) ;
85
84
} ) ;
86
85
87
- it ( 'should pass translations to FirebaseUIError' , ( ) => {
86
+ it ( 'should pass translations and language to FirebaseUIError' , async ( ) => {
88
87
const firebaseError = {
89
88
name : 'FirebaseError' ,
90
89
code : 'auth/user-not-found' ,
@@ -99,29 +98,111 @@ describe('handleFirebaseError', () => {
99
98
} ;
100
99
101
100
try {
102
- handleFirebaseError ( firebaseError , translations , 'es' ) ;
101
+ await handleFirebaseError ( firebaseError , { translations, language : 'es' } ) ;
103
102
} catch ( e ) {
104
103
expect ( e ) . toBeInstanceOf ( FirebaseUIError ) ;
105
104
expect ( e . message ) . toBe ( 'Usuario no encontrado' ) ;
106
105
}
107
106
} ) ;
108
107
109
- it ( 'should handle null/undefined errors' , ( ) => {
110
- expect ( ( ) => handleFirebaseError ( null ) ) . toThrow ( FirebaseUIError ) ;
111
- expect ( ( ) => handleFirebaseError ( undefined ) ) . toThrow ( FirebaseUIError ) ;
108
+ it ( 'should handle null/undefined errors' , async ( ) => {
109
+ await expect ( handleFirebaseError ( null ) ) . rejects . toThrow ( FirebaseUIError ) ;
110
+ await expect ( handleFirebaseError ( undefined ) ) . rejects . toThrow ( FirebaseUIError ) ;
112
111
} ) ;
113
112
114
- it ( 'should preserve the error code in thrown error' , ( ) => {
113
+ it ( 'should preserve the error code in thrown error' , async ( ) => {
115
114
const firebaseError = {
116
115
name : 'FirebaseError' ,
117
116
code : 'auth/wrong-password' ,
118
117
} ;
119
118
120
119
try {
121
- handleFirebaseError ( firebaseError ) ;
120
+ await handleFirebaseError ( firebaseError ) ;
122
121
} catch ( e ) {
123
122
expect ( e ) . toBeInstanceOf ( FirebaseUIError ) ;
124
123
expect ( e . code ) . toBe ( 'auth/wrong-password' ) ;
125
124
}
126
125
} ) ;
126
+
127
+ describe ( 'account exists with different credential handling' , ( ) => {
128
+ beforeEach ( ( ) => {
129
+ window . sessionStorage . clear ( ) ;
130
+ } ) ;
131
+
132
+ it ( 'should store credential and throw error when enableHandleExistingCredential is true' , async ( ) => {
133
+ const existingCredentialError = {
134
+ name : 'FirebaseError' ,
135
+ code : 'auth/account-exists-with-different-credential' ,
136
+ credential : { type : 'google.com' , token : 'mock-token' } ,
137
+ customData : {
138
+
139
+ } ,
140
+ } ;
141
+
142
+ await expect (
143
+ handleFirebaseError ( existingCredentialError , { enableHandleExistingCredential : true } )
144
+ ) . rejects . toThrow ( FirebaseUIError ) ;
145
+ expect ( window . sessionStorage . getItem ( 'pendingCred' ) ) . toBe ( JSON . stringify ( existingCredentialError . credential ) ) ;
146
+ } ) ;
147
+
148
+ it ( 'should not store credential when enableHandleExistingCredential is false' , async ( ) => {
149
+ const existingCredentialError = {
150
+ name : 'FirebaseError' ,
151
+ code : 'auth/account-exists-with-different-credential' ,
152
+ credential : { type : 'google.com' , token : 'mock-token' } ,
153
+ customData : {
154
+
155
+ } ,
156
+ } ;
157
+
158
+ await expect ( handleFirebaseError ( existingCredentialError ) ) . rejects . toThrow ( FirebaseUIError ) ;
159
+ expect ( window . sessionStorage . getItem ( 'pendingCred' ) ) . toBeNull ( ) ;
160
+ } ) ;
161
+
162
+ it ( 'should not store credential when no credential in error' , async ( ) => {
163
+ const existingCredentialError = {
164
+ name : 'FirebaseError' ,
165
+ code : 'auth/account-exists-with-different-credential' ,
166
+ customData : {
167
+
168
+ } ,
169
+ } ;
170
+
171
+ await expect (
172
+ handleFirebaseError ( existingCredentialError , { enableHandleExistingCredential : true } )
173
+ ) . rejects . toThrow ( FirebaseUIError ) ;
174
+ expect ( window . sessionStorage . getItem ( 'pendingCred' ) ) . toBeNull ( ) ;
175
+ } ) ;
176
+
177
+ it ( 'should include email in error and use translations when provided' , async ( ) => {
178
+ const existingCredentialError = {
179
+ name : 'FirebaseError' ,
180
+ code : 'auth/account-exists-with-different-credential' ,
181
+ credential : { type : 'google.com' , token : 'mock-token' } ,
182
+ customData : {
183
+
184
+ } ,
185
+ } ;
186
+
187
+ const translations = {
188
+ es : {
189
+ errors : {
190
+ accountExistsWithDifferentCredential : 'La cuenta ya existe con otras credenciales' ,
191
+ } ,
192
+ } ,
193
+ } ;
194
+
195
+ try {
196
+ await handleFirebaseError ( existingCredentialError , {
197
+ enableHandleExistingCredential : true ,
198
+ translations,
199
+ language : 'es' ,
200
+ } ) ;
201
+ } catch ( e ) {
202
+ expect ( e ) . toBeInstanceOf ( FirebaseUIError ) ;
203
+ expect ( e . code ) . toBe ( 'auth/account-exists-with-different-credential' ) ;
204
+ expect ( e . message ) . toBe ( 'La cuenta ya existe con otras credenciales' ) ;
205
+ }
206
+ } ) ;
207
+ } ) ;
127
208
} ) ;
0 commit comments