11import { BatchUploader } from '../../src/batchUploader' ;
22import { IMParticleWebSDKInstance } from '../../src/mp-instance' ;
3+ import { IStore } from '../../src/store' ;
4+ import { StoragePrivacyMap , StorageTypes } from '../../src/constants' ;
5+ import { SDKEvent } from '../../src/sdkRuntimeModels' ;
36
47describe ( 'BatchUploader' , ( ) => {
58 let batchUploader : BatchUploader ;
69 let mockMPInstance : IMParticleWebSDKInstance ;
10+ let originalFetch : typeof global . fetch ;
711
812 beforeEach ( ( ) => {
913 const now = Date . now ( ) ;
1014 jest . useFakeTimers ( {
1115 now : now ,
1216 advanceTimers : true // This improves the performance of nested timers, equivalent to Sinon's shouldAdvanceTime
1317 } ) ;
14-
18+ originalFetch = global . fetch ;
19+ global . fetch = jest . fn ( ) . mockResolvedValue ( { ok : true , status : 200 } ) ;
20+
1521 // Create a mock mParticle instance with mocked methods for instantiating a BatchUploader
1622 mockMPInstance = {
1723 _Store : {
24+ storageName : 'mprtcl-v4_abcdef' ,
25+ getNoFunctional : function ( this : IStore ) { return this . noFunctional ; } ,
26+ getNoTargeting : function ( this : IStore ) { return this . noTargeting ; } ,
27+ getPrivacyFlag : function ( this : IStore , storageType : StorageTypes ) {
28+ const privacyControl = StoragePrivacyMap [ storageType ] ;
29+ if ( privacyControl === 'functional' ) {
30+ return this . getNoFunctional ( ) ;
31+ }
32+ if ( privacyControl === 'targeting' ) {
33+ return this . getNoTargeting ( ) ;
34+ }
35+ return false ;
36+ } ,
37+ deviceId : 'device-1' ,
1838 SDKConfig : {
1939 flags : { }
2040 }
2141 } ,
2242 _Helpers : {
23- getFeatureFlag : jest . fn ( ) . mockReturnValue ( false ) ,
24- createServiceUrl : jest . fn ( ) . mockReturnValue ( 'https://mock-url.com' )
43+ getFeatureFlag : jest . fn ( ) . mockReturnValue ( '100' ) ,
44+ createServiceUrl : jest . fn ( ) . mockReturnValue ( 'https://mock-url.com' ) ,
45+ generateUniqueId : jest . fn ( ) . mockReturnValue ( 'req-1' ) ,
2546 } ,
2647 Identity : {
2748 getCurrentUser : jest . fn ( ) . mockReturnValue ( {
28- getMPID : ( ) => 'test-mpid'
49+ getMPID : ( ) => 'test-mpid' ,
50+ getConsentState : jest . fn ( ) . mockReturnValue ( null ) ,
2951 } )
52+ } ,
53+ Logger : {
54+ verbose : jest . fn ( ) ,
55+ error : jest . fn ( ) ,
56+ warning : jest . fn ( ) ,
3057 }
3158 } as unknown as IMParticleWebSDKInstance ;
3259
@@ -35,6 +62,7 @@ describe('BatchUploader', () => {
3562
3663 afterEach ( ( ) => {
3764 jest . useRealTimers ( ) ;
65+ global . fetch = originalFetch ;
3866 } ) ;
3967
4068 describe ( 'shouldDebounceAST' , ( ) => {
@@ -104,4 +132,50 @@ describe('BatchUploader', () => {
104132 expect ( secondCallTime ) . toBe ( firstCallTime ) ;
105133 } ) ;
106134 } ) ;
135+
136+ describe ( 'noFunctional' , ( ) => {
137+ beforeEach ( ( ) => {
138+ localStorage . clear ( ) ;
139+ sessionStorage . clear ( ) ;
140+ } ) ;
141+
142+ it ( 'should disable offline storage when noFunctional is true' , ( ) => {
143+ mockMPInstance . _Store . noFunctional = true ;
144+
145+ const uploader = new BatchUploader ( mockMPInstance , 1000 ) ;
146+ expect ( uploader [ 'offlineStorageEnabled' ] ) . toBe ( false ) ;
147+
148+ uploader . queueEvent ( { EventDataType : 4 } as SDKEvent ) ;
149+ expect ( sessionStorage . getItem ( 'mprtcl-v4_abcdef-events' ) ) . toBeNull ( ) ;
150+ expect ( localStorage . getItem ( 'mprtcl-v4_abcdef-batches' ) ) . toBeNull ( ) ;
151+ } ) ;
152+
153+ it ( 'should enable offline storage when noFunctional is false by default' , async ( ) => {
154+ const uploader = new BatchUploader ( mockMPInstance , 1000 ) ;
155+
156+ expect ( uploader [ 'offlineStorageEnabled' ] ) . toBe ( true ) ;
157+
158+ uploader . queueEvent ( { EventDataType : 4 } as SDKEvent ) ;
159+ expect ( sessionStorage . getItem ( 'mprtcl-v4_abcdef-events' ) ) . not . toBeNull ( ) ;
160+
161+ jest . advanceTimersByTime ( 1000 ) ;
162+ await Promise . resolve ( ) ;
163+
164+ expect ( localStorage . getItem ( 'mprtcl-v4_abcdef-batches' ) ) . not . toBeNull ( ) ;
165+ } ) ;
166+
167+ it ( 'should enable offline storage when noFunctional is false' , async ( ) => {
168+ mockMPInstance . _Store . noFunctional = false ;
169+
170+ const uploader = new BatchUploader ( mockMPInstance , 1000 ) ;
171+
172+ uploader . queueEvent ( { EventDataType : 4 } as SDKEvent ) ;
173+ expect ( sessionStorage . getItem ( 'mprtcl-v4_abcdef-events' ) ) . not . toBeNull ( ) ;
174+
175+ jest . advanceTimersByTime ( 1000 ) ;
176+ await Promise . resolve ( ) ;
177+
178+ expect ( localStorage . getItem ( 'mprtcl-v4_abcdef-batches' ) ) . not . toBeNull ( ) ;
179+ } ) ;
180+ } ) ;
107181} ) ;
0 commit comments