@@ -59,12 +59,13 @@ mockPageStylesCss();
5959
6060describe ( 'OneSignal' , ( ) => {
6161 beforeAll ( ( ) => {
62+ setConsentRequired ( true ) ;
63+ } ) ;
64+
65+ beforeEach ( ( ) => {
6266 TestEnvironment . initialize ( {
6367 initUserAndPushSubscription : true ,
6468 } ) ;
65- } ) ;
66-
67- beforeEach ( async ( ) => {
6869 OneSignal . coreDirector . subscriptionModelStore . replaceAll (
6970 [ ] ,
7071 ModelChangeTags . NO_PROPAGATE ,
@@ -368,15 +369,6 @@ describe('OneSignal', () => {
368369 await expect ( OneSignal . login ( '' , 1 ) ) . rejects . toThrowError (
369370 '"jwtToken" is the wrong type' ,
370371 ) ;
371-
372- // TODO: add consent required test
373- // if needing consent required
374- setConsentRequired ( true ) ;
375- await OneSignal . login ( externalId ) ;
376- await vi . waitUntil ( ( ) => errorSpy . mock . calls . length === 1 ) ;
377-
378- const error = errorSpy . mock . calls [ 0 ] [ 1 ] as Error ;
379- expect ( error . message ) . toBe ( 'Consent required but not given' ) ;
380372 } ) ;
381373
382374 test ( 'can login with a new external id' , async ( ) => {
@@ -683,16 +675,6 @@ describe('OneSignal', () => {
683675
684676 test ( 'login then accept web push permissions - it should make two user calls' , async ( ) => {
685677 setGetUserResponse ( ) ;
686- OneSignal . coreDirector . subscriptionModelStore . replaceAll (
687- [ ] ,
688- ModelChangeTags . NO_PROPAGATE ,
689- ) ;
690- setPushToken ( '' ) ;
691- subscribeFcmFromPageSpy . mockImplementation (
692- // @ts -expect-error - subscribeFcmFromPage is a private method of SubscriptionManagerPage
693- async ( ) => rawPushSubscription ,
694- ) ;
695-
696678 setCreateUserResponse ( {
697679 onesignalId : ONESIGNAL_ID ,
698680 externalId,
@@ -703,6 +685,16 @@ describe('OneSignal', () => {
703685 ] ,
704686 } ) ;
705687
688+ OneSignal . coreDirector . subscriptionModelStore . replaceAll (
689+ [ ] ,
690+ ModelChangeTags . NO_PROPAGATE ,
691+ ) ;
692+ setPushToken ( '' ) ;
693+ subscribeFcmFromPageSpy . mockImplementation (
694+ // @ts -expect-error - subscribeFcmFromPage is a private method of SubscriptionManagerPage
695+ async ( ) => rawPushSubscription ,
696+ ) ;
697+
706698 // new/empty user
707699 setupIdentityModel ( IDManager . createLocalId ( ) ) ;
708700
@@ -1105,6 +1097,48 @@ describe('OneSignal', () => {
11051097 } ) ;
11061098 } ) ;
11071099
1100+ describe ( 'Consent' , ( ) => {
1101+ beforeEach ( ( ) => {
1102+ setConsentRequired ( true ) ;
1103+ } ) ;
1104+
1105+ test ( 'should not execute operations if consent is not given' , async ( ) => {
1106+ setUpdateUserResponse ( ) ;
1107+
1108+ OneSignal . User . addTag ( 'some-tag' , 'some-value' ) ;
1109+ await OneSignal . login ( 'login-user' ) ;
1110+
1111+ // should not execute login op
1112+ await vi . waitUntil ( ( ) => {
1113+ const calls = debugSpy . mock . calls . map ( ( call ) => call [ 0 ] ) ;
1114+ return calls . includes ( 'Consent not given; not executing operations' ) ;
1115+ } ) ;
1116+
1117+ // should not execute any other ops
1118+ expect ( addAliasFn ) . not . toHaveBeenCalled ( ) ;
1119+ expect ( OneSignal . coreDirector . operationRepo . queue ) . toMatchObject ( [
1120+ {
1121+ operation : {
1122+ name : 'set-property' ,
1123+ } ,
1124+ } ,
1125+ ] ) ;
1126+
1127+ // can resume operations if consent is given
1128+ OneSignal . setConsentGiven ( true ) ;
1129+ expect ( OneSignal . _consentGiven ) . toBe ( true ) ;
1130+ await vi . waitUntil ( ( ) => updateUserFn . mock . calls . length === 1 ) ;
1131+ expect ( updateUserFn ) . toHaveBeenCalledWith ( {
1132+ properties : {
1133+ tags : {
1134+ 'some-tag' : 'some-value' ,
1135+ } ,
1136+ } ,
1137+ refresh_device_metadata : false ,
1138+ } ) ;
1139+ } ) ;
1140+ } ) ;
1141+
11081142 test ( 'should preserve operations order without needing await' , async ( ) => {
11091143 await setupSubModelStore ( {
11101144 id : SUB_ID ,
@@ -1173,7 +1207,7 @@ Object.defineProperty(global.navigator, 'serviceWorker', {
11731207 writable : true ,
11741208} ) ;
11751209
1176- const errorSpy = vi . spyOn ( Log , 'error' ) . mockImplementation ( ( ) => '' ) ;
1210+ vi . spyOn ( Log , 'error' ) . mockImplementation ( ( ) => '' ) ;
11771211const debugSpy = vi . spyOn ( Log , 'debug' ) ;
11781212
11791213const rawPushSubscription = new RawPushSubscription ( ) ;
0 commit comments