@@ -5,7 +5,6 @@ describe('BatchUploader', () => {
55 let batchUploader : BatchUploader ;
66 let mockMPInstance : IMParticleWebSDKInstance ;
77 let originalFetch : typeof global . fetch ;
8- let mockSendBatchToForwarders : jest . Mock ;
98
109 beforeEach ( ( ) => {
1110 const now = Date . now ( ) ;
@@ -17,7 +16,6 @@ describe('BatchUploader', () => {
1716 global . fetch = jest . fn ( ) . mockResolvedValue ( { ok : true , status : 200 } ) ;
1817
1918 // Create a mock mParticle instance with mocked methods for instantiating a BatchUploader
20- mockSendBatchToForwarders = jest . fn ( ) ;
2119 mockMPInstance = {
2220 _Store : {
2321 SDKConfig : {
@@ -30,9 +28,6 @@ describe('BatchUploader', () => {
3028 createServiceUrl : jest . fn ( ) . mockReturnValue ( 'https://mock-url.com' ) ,
3129 generateUniqueId : jest . fn ( ) . mockReturnValue ( 'mock-uuid' ) ,
3230 } ,
33- _Forwarders : {
34- sendBatchToForwarders : mockSendBatchToForwarders ,
35- } ,
3631 Identity : {
3732 getCurrentUser : jest . fn ( ) . mockReturnValue ( {
3833 getMPID : ( ) => 'test-mpid' ,
@@ -58,227 +53,6 @@ describe('BatchUploader', () => {
5853 global . fetch = originalFetch ;
5954 } ) ;
6055
61- describe ( 'batch forwarding to kits' , ( ) => {
62- it ( 'should call sendBatchToForwarders for each new batch in createNewBatches' , ( ) => {
63- const mockEvents = [
64- {
65- EventName : 'Test Event' ,
66- EventDataType : 4 ,
67- MPID : 'test-mpid' ,
68- SessionId : 'session-1' ,
69- EventCategory : 1 ,
70- Timestamp : Date . now ( ) ,
71- } ,
72- ] ;
73-
74- const mockUser = {
75- getMPID : ( ) => 'test-mpid' ,
76- getConsentState : ( ) => null ,
77- getUserIdentities : ( ) => ( { userIdentities : { } } ) ,
78- getAllUserAttributes : ( ) => ( { } ) ,
79- } ;
80-
81- BatchUploader [ 'createNewBatches' ] (
82- mockEvents as any ,
83- mockUser as any ,
84- mockMPInstance
85- ) ;
86-
87- expect ( mockSendBatchToForwarders ) . toHaveBeenCalledTimes ( 1 ) ;
88- expect ( mockSendBatchToForwarders ) . toHaveBeenCalledWith (
89- expect . objectContaining ( {
90- mpid : 'test-mpid' ,
91- } )
92- ) ;
93- } ) ;
94-
95- it ( 'should call sendBatchToForwarders before onCreateBatch' , ( ) => {
96- const callOrder : string [ ] = [ ] ;
97-
98- mockSendBatchToForwarders . mockImplementation ( ( ) => {
99- callOrder . push ( 'sendBatchToForwarders' ) ;
100- } ) ;
101-
102- mockMPInstance . _Store . SDKConfig . onCreateBatch = ( batch ) => {
103- callOrder . push ( 'onCreateBatch' ) ;
104- return batch ;
105- } ;
106-
107- const mockEvents = [
108- {
109- EventName : 'Test Event' ,
110- EventDataType : 4 ,
111- MPID : 'test-mpid' ,
112- SessionId : 'session-1' ,
113- EventCategory : 1 ,
114- Timestamp : Date . now ( ) ,
115- } ,
116- ] ;
117-
118- const mockUser = {
119- getMPID : ( ) => 'test-mpid' ,
120- getConsentState : ( ) => null ,
121- getUserIdentities : ( ) => ( { userIdentities : { } } ) ,
122- getAllUserAttributes : ( ) => ( { } ) ,
123- } ;
124-
125- BatchUploader [ 'createNewBatches' ] (
126- mockEvents as any ,
127- mockUser as any ,
128- mockMPInstance
129- ) ;
130-
131- expect ( callOrder ) . toEqual ( [
132- 'sendBatchToForwarders' ,
133- 'onCreateBatch' ,
134- ] ) ;
135- } ) ;
136-
137- it ( 'should forward batch before onCreateBatch can modify it' , ( ) => {
138- let forwardedEventCount = 0 ;
139-
140- mockSendBatchToForwarders . mockImplementation ( ( batch ) => {
141- forwardedEventCount = batch . events . length ;
142- } ) ;
143-
144- mockMPInstance . _Store . SDKConfig . onCreateBatch = ( batch ) => {
145- batch . modified = true ;
146- batch . events = [ ] ;
147- return batch ;
148- } ;
149-
150- const mockEvents = [
151- {
152- EventName : 'Test Event' ,
153- EventDataType : 4 ,
154- MPID : 'test-mpid' ,
155- SessionId : 'session-1' ,
156- EventCategory : 1 ,
157- Timestamp : Date . now ( ) ,
158- } ,
159- ] ;
160-
161- const mockUser = {
162- getMPID : ( ) => 'test-mpid' ,
163- getConsentState : ( ) => null ,
164- getUserIdentities : ( ) => ( { userIdentities : { } } ) ,
165- getAllUserAttributes : ( ) => ( { } ) ,
166- } ;
167-
168- BatchUploader [ 'createNewBatches' ] (
169- mockEvents as any ,
170- mockUser as any ,
171- mockMPInstance
172- ) ;
173-
174- expect ( forwardedEventCount ) . toBe ( 1 ) ;
175- } ) ;
176-
177- it ( 'should forward batch even when onCreateBatch drops it' , ( ) => {
178- mockMPInstance . _Store . SDKConfig . onCreateBatch = ( ) => {
179- return null ;
180- } ;
181-
182- ( mockMPInstance . Logger as any ) . warning = jest . fn ( ) ;
183-
184- const mockEvents = [
185- {
186- EventName : 'Test Event' ,
187- EventDataType : 4 ,
188- MPID : 'test-mpid' ,
189- SessionId : 'session-1' ,
190- EventCategory : 1 ,
191- Timestamp : Date . now ( ) ,
192- } ,
193- ] ;
194-
195- const mockUser = {
196- getMPID : ( ) => 'test-mpid' ,
197- getConsentState : ( ) => null ,
198- getUserIdentities : ( ) => ( { userIdentities : { } } ) ,
199- getAllUserAttributes : ( ) => ( { } ) ,
200- } ;
201-
202- BatchUploader [ 'createNewBatches' ] (
203- mockEvents as any ,
204- mockUser as any ,
205- mockMPInstance
206- ) ;
207-
208- expect ( mockSendBatchToForwarders ) . toHaveBeenCalledTimes ( 1 ) ;
209- } ) ;
210-
211- it ( 'should not throw if sendBatchToForwarders errors' , ( ) => {
212- mockSendBatchToForwarders . mockImplementation ( ( ) => {
213- throw new Error ( 'Kit failure' ) ;
214- } ) ;
215-
216- const mockEvents = [
217- {
218- EventName : 'Test Event' ,
219- EventDataType : 4 ,
220- MPID : 'test-mpid' ,
221- SessionId : 'session-1' ,
222- EventCategory : 1 ,
223- Timestamp : Date . now ( ) ,
224- } ,
225- ] ;
226-
227- const mockUser = {
228- getMPID : ( ) => 'test-mpid' ,
229- getConsentState : ( ) => null ,
230- getUserIdentities : ( ) => ( { userIdentities : { } } ) ,
231- getAllUserAttributes : ( ) => ( { } ) ,
232- } ;
233-
234- expect ( ( ) => {
235- BatchUploader [ 'createNewBatches' ] (
236- mockEvents as any ,
237- mockUser as any ,
238- mockMPInstance
239- ) ;
240- } ) . not . toThrow ( ) ;
241-
242- expect ( mockMPInstance . Logger . error ) . toHaveBeenCalled ( ) ;
243- } ) ;
244-
245- it ( 'should create separate batches per MPID and forward each' , ( ) => {
246- const mockEvents = [
247- {
248- EventName : 'Event User A' ,
249- EventDataType : 4 ,
250- MPID : 'mpid-a' ,
251- SessionId : 'session-1' ,
252- EventCategory : 1 ,
253- Timestamp : Date . now ( ) ,
254- } ,
255- {
256- EventName : 'Event User B' ,
257- EventDataType : 4 ,
258- MPID : 'mpid-b' ,
259- SessionId : 'session-1' ,
260- EventCategory : 1 ,
261- Timestamp : Date . now ( ) ,
262- } ,
263- ] ;
264-
265- const mockUser = {
266- getMPID : ( ) => 'mpid-a' ,
267- getConsentState : ( ) => null ,
268- getUserIdentities : ( ) => ( { userIdentities : { } } ) ,
269- getAllUserAttributes : ( ) => ( { } ) ,
270- } ;
271-
272- BatchUploader [ 'createNewBatches' ] (
273- mockEvents as any ,
274- mockUser as any ,
275- mockMPInstance
276- ) ;
277-
278- expect ( mockSendBatchToForwarders ) . toHaveBeenCalledTimes ( 2 ) ;
279- } ) ;
280- } ) ;
281-
28256 describe ( 'shouldDebounceAST' , ( ) => {
28357 it ( 'should return false for first call' , ( ) => {
28458 // First call should not be debounced
0 commit comments