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' , ( ) => {
@@ -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' , ( ) => {
@@ -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' , ( ) => {
@@ -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' , ( ) => {
@@ -1019,9 +1020,13 @@ describe('SDK bootstraps itself if init has already been completed', () => {
10191020 sdkWindow . __uid2 = new UID2 ( ) ;
10201021 } ) ;
10211022
1022- test . skip ( 'should bootstrap therefore public functions should return the correct values without calling init again' , async ( ) => {
1023+ test ( 'should bootstrap therefore public functions should return the correct values without calling init again' , async ( ) => {
10231024 const identity = { ...makeIdentity ( ) , refresh_from : Date . now ( ) + 100 } ;
10241025
1026+ jest
1027+ . spyOn ( ApiClient . prototype , 'callCstgApi' )
1028+ . mockResolvedValue ( { status : 'success' , identity } ) ;
1029+
10251030 uid2 . init ( { identity } ) ;
10261031
10271032 // mimicking closing the page, but not re-calling the UID2 constructor
@@ -1031,36 +1036,32 @@ describe('SDK bootstraps itself if init has already been completed', () => {
10311036
10321037 expect ( uid2 . getAdvertisingToken ( ) ) . toBe ( identity . advertising_token ) ;
10331038 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 ( ) ;
1039+ await expect (
1040+ uid2 . setIdentityFromEmail ( email , mocks . makeUid2CstgOption ( ) ) ,
1041+ ) . resolves . toBeUndefined ( ) ;
1042+ await expect (
1043+ uid2 . setIdentityFromEmailHash ( emailHash , mocks . makeUid2CstgOption ( ) ) ,
1044+ ) . resolves . toBeUndefined ( ) ;
1045+ await expect (
1046+ uid2 . setIdentityFromPhone ( phone , mocks . makeUid2CstgOption ( ) ) ,
1047+ ) . resolves . toBeUndefined ( ) ;
1048+ await expect (
1049+ uid2 . setIdentityFromPhoneHash ( phoneHash , mocks . makeUid2CstgOption ( ) ) ,
1050+ ) . resolves . toBeUndefined ( ) ;
10461051 } ) ;
10471052
10481053 test ( 'should not bootstrap therefore public functions error or return undefined/null' , async ( ) => {
10491054 __uid2InternalHandleScriptLoad ( ) ;
10501055 expect ( uid2 . getAdvertisingToken ( ) ) . toBe ( undefined ) ;
10511056 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 ( ) ;
1057+ await expect ( uid2 . setIdentityFromEmail ( email , mocks . makeUid2CstgOption ( ) ) ) . rejects . toThrow ( ) ;
1058+ await expect (
1059+ uid2 . setIdentityFromEmailHash ( emailHash , mocks . makeUid2CstgOption ( ) ) ,
1060+ ) . rejects . toThrow ( ) ;
1061+ await expect ( uid2 . setIdentityFromPhone ( phone , mocks . makeUid2CstgOption ( ) ) ) . rejects . toThrow ( ) ;
1062+ await expect (
1063+ uid2 . setIdentityFromPhoneHash ( phoneHash , mocks . makeUid2CstgOption ( ) ) ,
1064+ ) . rejects . toThrow ( ) ;
10641065 } ) ;
10651066} ) ;
10661067
@@ -1084,24 +1085,21 @@ describe('Token retrieval and related public functions working without init', ()
10841085 } ,
10851086 ] ;
10861087
1087- identityStorageFunctions . forEach ( ( functions ) => {
1088+ for ( const functions of identityStorageFunctions ) {
10881089 functions . setIdentity ( ) ;
10891090 expect ( uid2 . getAdvertisingToken ( ) ) . toBe ( identity . advertising_token ) ;
10901091 expect ( uid2 . getIdentity ( ) ) . toStrictEqual ( identity ) ;
1092+ // Without init, getAdvertisingTokenAsync() returns a deferred promise that only resolves after init; do not await to avoid timeout.
10911093 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 ( ) ;
1094+ await expect ( uid2 . setIdentityFromEmail ( email , mocks . makeUid2CstgOption ( ) ) ) . rejects . toThrow ( ) ;
1095+ await expect (
1096+ uid2 . setIdentityFromEmailHash ( emailHash , mocks . makeUid2CstgOption ( ) ) ,
1097+ ) . rejects . toThrow ( ) ;
1098+ await expect ( uid2 . setIdentityFromPhone ( phone , mocks . makeUid2CstgOption ( ) ) ) . rejects . toThrow ( ) ;
1099+ await expect (
1100+ uid2 . setIdentityFromPhoneHash ( phoneHash , mocks . makeUid2CstgOption ( ) ) ,
1101+ ) . rejects . toThrow ( ) ;
11041102 functions . removeIdentity ( ) ;
1105- } ) ;
1103+ }
11061104 } ) ;
11071105} ) ;
0 commit comments