@@ -148,6 +148,84 @@ describe('Client Singleton', () => {
148148 } ) ;
149149 } ) ;
150150
151+ it ( 'preserves sign up and sign in identity when fromJSON receives matching ids' , ( ) => {
152+ const user = createUser ( { first_name : 'John' , last_name : 'Doe' , id : 'user_1' } ) ;
153+ const session = createSession ( { id : 'session_1' } , user ) ;
154+ const initialClientJSON : ClientJSON = {
155+ object : 'client' ,
156+ id : 'test_id' ,
157+ status : 'active' ,
158+ last_active_session_id : 'test_session_id' ,
159+ sign_in : createSignIn ( { id : 'test_sign_in_id' , status : 'needs_first_factor' } , user ) ,
160+ sign_up : createSignUp ( { id : 'test_sign_up_id' , status : 'missing_requirements' } ) ,
161+ sessions : [ session ] ,
162+ created_at : Date . now ( ) - 1000 ,
163+ updated_at : Date . now ( ) ,
164+ } as any ;
165+
166+ // @ts -expect-error We cannot mess with the singleton when tests are running in parallel
167+ const client = new Client ( initialClientJSON ) ;
168+ const initialSignUp = client . signUp ;
169+ const initialSignIn = client . signIn ;
170+
171+ client . fromJSON ( {
172+ ...initialClientJSON ,
173+ sign_in : createSignIn (
174+ {
175+ id : 'test_sign_in_id' ,
176+ status : 'needs_second_factor' ,
177+ identifier : 'updated@example.com' ,
178+ } ,
179+ user ,
180+ ) ,
181+ sign_up : createSignUp ( {
182+ id : 'test_sign_up_id' ,
183+ status : 'missing_requirements' ,
184+ email_address : 'updated@example.com' ,
185+ } ) ,
186+ updated_at : Date . now ( ) + 1000 ,
187+ } ) ;
188+
189+ expect ( client . signUp ) . toBe ( initialSignUp ) ;
190+ expect ( client . signIn ) . toBe ( initialSignIn ) ;
191+ expect ( client . signUp . emailAddress ) . toBe ( 'updated@example.com' ) ;
192+ expect ( client . signIn . identifier ) . toBe ( 'updated@example.com' ) ;
193+ expect ( client . signIn . status ) . toBe ( 'needs_second_factor' ) ;
194+ } ) ;
195+
196+ it ( 'replaces sign up and sign in identity when fromJSON receives new ids' , ( ) => {
197+ const user = createUser ( { first_name : 'John' , last_name : 'Doe' , id : 'user_1' } ) ;
198+ const session = createSession ( { id : 'session_1' } , user ) ;
199+ const initialClientJSON : ClientJSON = {
200+ object : 'client' ,
201+ id : 'test_id' ,
202+ status : 'active' ,
203+ last_active_session_id : 'test_session_id' ,
204+ sign_in : createSignIn ( { id : 'test_sign_in_id' , status : 'needs_first_factor' } , user ) ,
205+ sign_up : createSignUp ( { id : 'test_sign_up_id' , status : 'missing_requirements' } ) ,
206+ sessions : [ session ] ,
207+ created_at : Date . now ( ) - 1000 ,
208+ updated_at : Date . now ( ) ,
209+ } as any ;
210+
211+ // @ts -expect-error We cannot mess with the singleton when tests are running in parallel
212+ const client = new Client ( initialClientJSON ) ;
213+ const initialSignUp = client . signUp ;
214+ const initialSignIn = client . signIn ;
215+
216+ client . fromJSON ( {
217+ ...initialClientJSON ,
218+ sign_in : createSignIn ( { id : 'test_sign_in_id_v2' , status : 'needs_first_factor' } , user ) ,
219+ sign_up : createSignUp ( { id : 'test_sign_up_id_v2' , status : 'missing_requirements' } ) ,
220+ updated_at : Date . now ( ) + 1000 ,
221+ } ) ;
222+
223+ expect ( client . signUp ) . not . toBe ( initialSignUp ) ;
224+ expect ( client . signIn ) . not . toBe ( initialSignIn ) ;
225+ expect ( client . signUp . id ) . toBe ( 'test_sign_up_id_v2' ) ;
226+ expect ( client . signIn . id ) . toBe ( 'test_sign_in_id_v2' ) ;
227+ } ) ;
228+
151229 it ( 'has the same initial properties' , ( ) => {
152230 const clientJSON = {
153231 object : 'client' ,
0 commit comments