11import { afterEach , beforeEach , describe , expect , jest , test } from '@jest/globals' ;
22
3+ import { ApiClient } from '../apiClient' ;
34import * as mocks from '../mocks' ;
45import { __uid2InternalHandleScriptLoad , sdkWindow , UID2 } from '../uid2Sdk' ;
56import { CallbackHandler , EventType } from '../callbackManager' ;
@@ -127,7 +128,7 @@ testCookieAndLocalStorage(() => {
127128 expect ( ( ) =>
128129 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
129130 // @ts -ignore
130- uid2 . init ( { callback : ( ) => { } , refreshRetryPeriod : 'abc' } )
131+ uid2 . init ( { callback : ( ) => { } , refreshRetryPeriod : 'abc' } ) ,
131132 ) . toThrow ( TypeError ) ;
132133 } ) ;
133134 test ( 'should fail on refreshRetryPeriod being less than 1 second' , ( ) => {
@@ -153,7 +154,7 @@ testCookieAndLocalStorage(() => {
153154 advertisingToken : undefined ,
154155 advertising_token : undefined ,
155156 status : UID2 . IdentityStatus . NO_IDENTITY ,
156- } )
157+ } ) ,
157158 ) ;
158159 } ) ;
159160 test ( 'should not set value' , ( ) => {
@@ -181,7 +182,7 @@ testCookieAndLocalStorage(() => {
181182 advertisingToken : undefined ,
182183 advertising_token : undefined ,
183184 status : UID2 . IdentityStatus . NO_IDENTITY ,
184- } )
185+ } ) ,
185186 ) ;
186187 } ) ;
187188 test ( 'should clear value' , ( ) => {
@@ -211,7 +212,7 @@ testCookieAndLocalStorage(() => {
211212 advertisingToken : identity . advertising_token ,
212213 advertising_token : identity . advertising_token ,
213214 status : UID2 . IdentityStatus . ESTABLISHED ,
214- } )
215+ } ) ,
215216 ) ;
216217 } ) ;
217218 test ( 'should set value' , ( ) => {
@@ -243,7 +244,7 @@ testCookieAndLocalStorage(() => {
243244 advertisingToken : undefined ,
244245 advertising_token : undefined ,
245246 status : UID2 . IdentityStatus . REFRESH_EXPIRED ,
246- } )
247+ } ) ,
247248 ) ;
248249 } ) ;
249250 test ( 'should clear value' , ( ) => {
@@ -305,7 +306,7 @@ testCookieAndLocalStorage(() => {
305306 expect ( xhrMock . send ) . toHaveBeenCalledTimes ( 1 ) ;
306307 expect ( xhrMock . open ) . toHaveBeenLastCalledWith ( 'POST' , uid2RefreshUrl , true ) ;
307308 expect ( xhrMock . send ) . toHaveBeenLastCalledWith ( identity . refresh_token ) ;
308- xhrMock . onreadystatechange ( ) ;
309+ xhrMock . onreadystatechange ( new Event ( '' ) ) ;
309310 expect ( cryptoMock . subtle . importKey ) . toHaveBeenCalled ( ) ;
310311 } ) ;
311312
@@ -333,7 +334,7 @@ testCookieAndLocalStorage(() => {
333334 expect ( xhrMock . send ) . toHaveBeenCalledTimes ( 1 ) ;
334335 expect ( xhrMock . open ) . toHaveBeenLastCalledWith ( 'POST' , uid2RefreshUrl , true ) ;
335336 expect ( xhrMock . send ) . toHaveBeenLastCalledWith ( identity . refresh_token ) ;
336- xhrMock . onreadystatechange ( ) ;
337+ xhrMock . onreadystatechange ( new Event ( '' ) ) ;
337338 expect ( cryptoMock . subtle . importKey ) . toHaveBeenCalledTimes ( 0 ) ;
338339 mocks . resetCrypto ( sdkWindow ) ;
339340 } ) ;
@@ -375,7 +376,7 @@ testCookieAndLocalStorage(() => {
375376 advertisingToken : undefined ,
376377 advertising_token : undefined ,
377378 status : UID2 . IdentityStatus . INVALID ,
378- } )
379+ } ) ,
379380 ) ;
380381 } ) ;
381382 test ( 'should clear value' , ( ) => {
@@ -408,7 +409,7 @@ testCookieAndLocalStorage(() => {
408409 advertisingToken : identity . advertising_token ,
409410 advertising_token : identity . advertising_token ,
410411 status : UID2 . IdentityStatus . ESTABLISHED ,
411- } )
412+ } ) ,
412413 ) ;
413414 } ) ;
414415 test ( 'should set value' , ( ) => {
@@ -447,7 +448,7 @@ testCookieAndLocalStorage(() => {
447448 advertisingToken : initIdentity . advertising_token ,
448449 advertising_token : initIdentity . advertising_token ,
449450 status : UID2 . IdentityStatus . ESTABLISHED ,
450- } )
451+ } ) ,
451452 ) ;
452453 } ) ;
453454 test ( 'should set value' , ( ) => {
@@ -478,7 +479,7 @@ testCookieAndLocalStorage(() => {
478479 identity : originalIdentity ,
479480 useCookie : useCookie ,
480481 } ) ;
481- let cryptoMock = new mocks . CryptoMock ( sdkWindow ) ;
482+ new mocks . CryptoMock ( sdkWindow ) ;
482483 } ) ;
483484 afterEach ( ( ) => {
484485 mocks . resetCrypto ( sdkWindow ) ;
@@ -496,7 +497,7 @@ testCookieAndLocalStorage(() => {
496497 advertisingToken : updatedIdentity . advertising_token ,
497498 advertising_token : updatedIdentity . advertising_token ,
498499 status : UID2 . IdentityStatus . REFRESHED ,
499- } )
500+ } ) ,
500501 ) ;
501502 } ) ;
502503 test ( 'should set value' , ( ) => {
@@ -524,7 +525,7 @@ testCookieAndLocalStorage(() => {
524525 advertisingToken : originalIdentity . advertising_token ,
525526 advertising_token : originalIdentity . advertising_token ,
526527 status : UID2 . IdentityStatus . ESTABLISHED ,
527- } )
528+ } ) ,
528529 ) ;
529530 } ) ;
530531 test ( 'should set value' , ( ) => {
@@ -551,7 +552,7 @@ testCookieAndLocalStorage(() => {
551552 advertisingToken : undefined ,
552553 advertising_token : undefined ,
553554 status : UID2 . IdentityStatus . OPTOUT ,
554- } )
555+ } ) ,
555556 ) ;
556557 } ) ;
557558 test ( 'should set cookie to optout' , ( ) => {
@@ -579,7 +580,7 @@ testCookieAndLocalStorage(() => {
579580 advertisingToken : undefined ,
580581 advertising_token : undefined ,
581582 status : UID2 . IdentityStatus . REFRESH_EXPIRED ,
582- } )
583+ } ) ,
583584 ) ;
584585 } ) ;
585586 test ( 'should not set cookie' , ( ) => {
@@ -610,7 +611,7 @@ testCookieAndLocalStorage(() => {
610611 advertisingToken : originalIdentity . advertising_token ,
611612 advertising_token : originalIdentity . advertising_token ,
612613 status : UID2 . IdentityStatus . ESTABLISHED ,
613- } )
614+ } ) ,
614615 ) ;
615616 } ) ;
616617 test ( 'should set value' , ( ) => {
@@ -638,7 +639,7 @@ testCookieAndLocalStorage(() => {
638639 advertisingToken : originalIdentity . advertising_token ,
639640 advertising_token : originalIdentity . advertising_token ,
640641 status : UID2 . IdentityStatus . ESTABLISHED ,
641- } )
642+ } ) ,
642643 ) ;
643644 } ) ;
644645 test ( 'should set value' , ( ) => {
@@ -666,7 +667,7 @@ testCookieAndLocalStorage(() => {
666667 advertisingToken : originalIdentity . advertising_token ,
667668 advertising_token : originalIdentity . advertising_token ,
668669 status : UID2 . IdentityStatus . ESTABLISHED ,
669- } )
670+ } ) ,
670671 ) ;
671672 } ) ;
672673 test ( 'should set value' , ( ) => {
@@ -694,7 +695,7 @@ testCookieAndLocalStorage(() => {
694695 advertisingToken : originalIdentity . advertising_token ,
695696 advertising_token : originalIdentity . advertising_token ,
696697 status : UID2 . IdentityStatus . ESTABLISHED ,
697- } )
698+ } ) ,
698699 ) ;
699700 } ) ;
700701 test ( 'should set value' , ( ) => {
@@ -722,7 +723,7 @@ testCookieAndLocalStorage(() => {
722723 advertisingToken : undefined ,
723724 advertising_token : undefined ,
724725 status : UID2 . IdentityStatus . REFRESH_EXPIRED ,
725- } )
726+ } ) ,
726727 ) ;
727728 } ) ;
728729 test ( 'should not set cookie' , ( ) => {
@@ -754,7 +755,7 @@ testCookieAndLocalStorage(() => {
754755 identity : originalIdentity ,
755756 useCookie : useCookie ,
756757 } ) ;
757- let cryptoMock = new mocks . CryptoMock ( sdkWindow ) ;
758+ new mocks . CryptoMock ( sdkWindow ) ;
758759 } ) ;
759760 afterEach ( ( ) => {
760761 mocks . resetCrypto ( sdkWindow ) ;
@@ -773,7 +774,7 @@ testCookieAndLocalStorage(() => {
773774 advertisingToken : updatedIdentity . advertising_token ,
774775 advertising_token : updatedIdentity . advertising_token ,
775776 status : UID2 . IdentityStatus . REFRESHED ,
776- } )
777+ } ) ,
777778 ) ;
778779 } ) ;
779780 test ( 'should set value' , ( ) => {
@@ -796,7 +797,7 @@ testCookieAndLocalStorage(() => {
796797 xhrMock . responseText = btoa (
797798 JSON . stringify ( {
798799 status : 'optout' ,
799- } )
800+ } ) ,
800801 ) ;
801802 xhrMock . onreadystatechange ( new Event ( '' ) ) ;
802803 } ) ;
@@ -807,7 +808,7 @@ testCookieAndLocalStorage(() => {
807808 advertisingToken : undefined ,
808809 advertising_token : undefined ,
809810 status : UID2 . IdentityStatus . OPTOUT ,
810- } )
811+ } ) ,
811812 ) ;
812813 } ) ;
813814 test ( 'should invoke the callback with no identity' , ( ) => {
@@ -843,7 +844,7 @@ testCookieAndLocalStorage(() => {
843844 advertisingToken : undefined ,
844845 advertising_token : undefined ,
845846 status : UID2 . IdentityStatus . REFRESH_EXPIRED ,
846- } )
847+ } ) ,
847848 ) ;
848849 } ) ;
849850 test ( 'should not set cookie' , ( ) => {
@@ -874,7 +875,7 @@ testCookieAndLocalStorage(() => {
874875 advertisingToken : undefined ,
875876 advertising_token : undefined ,
876877 status : UID2 . IdentityStatus . EXPIRED ,
877- } )
878+ } ) ,
878879 ) ;
879880 } ) ;
880881 test ( 'should set value' , ( ) => {
@@ -903,7 +904,7 @@ testCookieAndLocalStorage(() => {
903904 advertisingToken : undefined ,
904905 advertising_token : undefined ,
905906 status : UID2 . IdentityStatus . REFRESH_EXPIRED ,
906- } )
907+ } ) ,
907908 ) ;
908909 } ) ;
909910 test ( 'should not set cookie' , ( ) => {
@@ -1015,13 +1016,23 @@ describe('SDK bootstraps itself if init has already been completed', () => {
10151016 const phone = '+12345678901' ;
10161017 const phoneHash = 'EObwtHBUqDNZR33LNSMdtt5cafsYFuGmuY4ZLenlue4=' ;
10171018
1019+ let callCstgApiSpy : jest . SpiedFunction < ApiClient [ 'callCstgApi' ] > ;
1020+
10181021 beforeEach ( ( ) => {
10191022 sdkWindow . __uid2 = new UID2 ( ) ;
10201023 } ) ;
10211024
1022- test . skip ( 'should bootstrap therefore public functions should return the correct values without calling init again' , async ( ) => {
1025+ afterEach ( ( ) => {
1026+ callCstgApiSpy ?. mockRestore ( ) ;
1027+ } ) ;
1028+
1029+ test ( 'should bootstrap therefore public functions should return the correct values without calling init again' , async ( ) => {
10231030 const identity = { ...makeIdentity ( ) , refresh_from : Date . now ( ) + 100 } ;
10241031
1032+ callCstgApiSpy = jest
1033+ . spyOn ( ApiClient . prototype , 'callCstgApi' )
1034+ . mockResolvedValue ( { status : 'success' , identity } ) ;
1035+
10251036 uid2 . init ( { identity } ) ;
10261037
10271038 // mimicking closing the page, but not re-calling the UID2 constructor
@@ -1031,36 +1042,32 @@ describe('SDK bootstraps itself if init has already been completed', () => {
10311042
10321043 expect ( uid2 . getAdvertisingToken ( ) ) . toBe ( identity . advertising_token ) ;
10331044 expect ( uid2 . getIdentity ( ) ) . toStrictEqual ( identity ) ;
1034- expect ( async ( ) => {
1035- await uid2 . setIdentityFromEmail ( email , mocks . makeUid2CstgOption ( ) ) ;
1036- } ) . not . toThrow ( ) ;
1037- expect ( async ( ) => {
1038- await uid2 . setIdentityFromEmailHash ( emailHash , mocks . makeUid2CstgOption ( ) ) ;
1039- } ) . not . toThrow ( ) ;
1040- expect ( async ( ) => {
1041- await uid2 . setIdentityFromPhone ( phone , mocks . makeUid2CstgOption ( ) ) ;
1042- } ) . not . toThrow ( ) ;
1043- expect ( async ( ) => {
1044- await uid2 . setIdentityFromPhoneHash ( phoneHash , mocks . makeUid2CstgOption ( ) ) ;
1045- } ) . not . toThrow ( ) ;
1045+ await expect (
1046+ uid2 . setIdentityFromEmail ( email , mocks . makeUid2CstgOption ( ) ) ,
1047+ ) . resolves . toBeUndefined ( ) ;
1048+ await expect (
1049+ uid2 . setIdentityFromEmailHash ( emailHash , mocks . makeUid2CstgOption ( ) ) ,
1050+ ) . resolves . toBeUndefined ( ) ;
1051+ await expect (
1052+ uid2 . setIdentityFromPhone ( phone , mocks . makeUid2CstgOption ( ) ) ,
1053+ ) . resolves . toBeUndefined ( ) ;
1054+ await expect (
1055+ uid2 . setIdentityFromPhoneHash ( phoneHash , mocks . makeUid2CstgOption ( ) ) ,
1056+ ) . resolves . toBeUndefined ( ) ;
10461057 } ) ;
10471058
10481059 test ( 'should not bootstrap therefore public functions error or return undefined/null' , async ( ) => {
10491060 __uid2InternalHandleScriptLoad ( ) ;
10501061 expect ( uid2 . getAdvertisingToken ( ) ) . toBe ( undefined ) ;
10511062 expect ( uid2 . getIdentity ( ) ) . toStrictEqual ( null ) ;
1052- expect ( async ( ) => {
1053- await uid2 . setIdentityFromEmail ( email , mocks . makeUid2CstgOption ( ) ) ;
1054- } ) . rejects . toThrow ( ) ;
1055- expect ( async ( ) => {
1056- await uid2 . setIdentityFromEmailHash ( emailHash , mocks . makeUid2CstgOption ( ) ) ;
1057- } ) . rejects . toThrow ( ) ;
1058- expect ( async ( ) => {
1059- await uid2 . setIdentityFromPhone ( phone , mocks . makeUid2CstgOption ( ) ) ;
1060- } ) . rejects . toThrow ( ) ;
1061- expect ( async ( ) => {
1062- await uid2 . setIdentityFromPhoneHash ( phoneHash , mocks . makeUid2CstgOption ( ) ) ;
1063- } ) . rejects . toThrow ( ) ;
1063+ await expect ( uid2 . setIdentityFromEmail ( email , mocks . makeUid2CstgOption ( ) ) ) . rejects . toThrow ( ) ;
1064+ await expect (
1065+ uid2 . setIdentityFromEmailHash ( emailHash , mocks . makeUid2CstgOption ( ) ) ,
1066+ ) . rejects . toThrow ( ) ;
1067+ await expect ( uid2 . setIdentityFromPhone ( phone , mocks . makeUid2CstgOption ( ) ) ) . rejects . toThrow ( ) ;
1068+ await expect (
1069+ uid2 . setIdentityFromPhoneHash ( phoneHash , mocks . makeUid2CstgOption ( ) ) ,
1070+ ) . rejects . toThrow ( ) ;
10641071 } ) ;
10651072} ) ;
10661073
@@ -1084,24 +1091,21 @@ describe('Token retrieval and related public functions working without init', ()
10841091 } ,
10851092 ] ;
10861093
1087- identityStorageFunctions . forEach ( ( functions ) => {
1094+ for ( const functions of identityStorageFunctions ) {
10881095 functions . setIdentity ( ) ;
10891096 expect ( uid2 . getAdvertisingToken ( ) ) . toBe ( identity . advertising_token ) ;
10901097 expect ( uid2 . getIdentity ( ) ) . toStrictEqual ( identity ) ;
1098+ // Without init, getAdvertisingTokenAsync() returns a deferred promise that only resolves after init; do not await to avoid timeout.
10911099 expect ( uid2 . getAdvertisingTokenAsync ( ) ) . resolves . toBe ( identity . advertising_token ) ;
1092- expect ( async ( ) => {
1093- await uid2 . setIdentityFromEmail ( email , mocks . makeUid2CstgOption ( ) ) ;
1094- } ) . rejects . toThrow ( ) ;
1095- expect ( async ( ) => {
1096- await uid2 . setIdentityFromEmailHash ( emailHash , mocks . makeUid2CstgOption ( ) ) ;
1097- } ) . rejects . toThrow ( ) ;
1098- expect ( async ( ) => {
1099- await uid2 . setIdentityFromPhone ( phone , mocks . makeUid2CstgOption ( ) ) ;
1100- } ) . rejects . toThrow ( ) ;
1101- expect ( async ( ) => {
1102- await uid2 . setIdentityFromPhoneHash ( phoneHash , mocks . makeUid2CstgOption ( ) ) ;
1103- } ) . rejects . toThrow ( ) ;
1100+ await expect ( uid2 . setIdentityFromEmail ( email , mocks . makeUid2CstgOption ( ) ) ) . rejects . toThrow ( ) ;
1101+ await expect (
1102+ uid2 . setIdentityFromEmailHash ( emailHash , mocks . makeUid2CstgOption ( ) ) ,
1103+ ) . rejects . toThrow ( ) ;
1104+ await expect ( uid2 . setIdentityFromPhone ( phone , mocks . makeUid2CstgOption ( ) ) ) . rejects . toThrow ( ) ;
1105+ await expect (
1106+ uid2 . setIdentityFromPhoneHash ( phoneHash , mocks . makeUid2CstgOption ( ) ) ,
1107+ ) . rejects . toThrow ( ) ;
11041108 functions . removeIdentity ( ) ;
1105- } ) ;
1109+ }
11061110 } ) ;
11071111} ) ;
0 commit comments