@@ -25,7 +25,7 @@ vi.mock('./forwarding_event_processor', () => {
25
25
return { getForwardingEventProcessor } ;
26
26
} ) ;
27
27
28
- vi . mock ( './event_processor_factory' , async ( importOriginal ) => {
28
+ vi . mock ( './event_processor_factory' , async importOriginal => {
29
29
const getBatchEventProcessor = vi . fn ( ) . mockImplementation ( ( ) => {
30
30
return { } ;
31
31
} ) ;
@@ -46,13 +46,14 @@ vi.mock('@react-native-community/netinfo', () => {
46
46
} ) ;
47
47
48
48
let isNetInfoAvailable = false ;
49
+ let isAsyncStorageAvailable = true ;
49
50
50
51
await vi . hoisted ( async ( ) => {
51
52
await mockRequireNetInfo ( ) ;
52
53
} ) ;
53
54
54
55
async function mockRequireNetInfo ( ) {
55
- const { Module} = await import ( 'module' ) ;
56
+ const { Module } = await import ( 'module' ) ;
56
57
const M : any = Module ;
57
58
58
59
M . _load_original = M . _load ;
@@ -61,14 +62,19 @@ async function mockRequireNetInfo() {
61
62
if ( isNetInfoAvailable ) return { } ;
62
63
throw new Error ( 'Module not found: @react-native-community/netinfo' ) ;
63
64
}
65
+ if ( uri === '@react-native-async-storage/async-storage' ) {
66
+ if ( isAsyncStorageAvailable ) return { } ;
67
+ throw new Error ( 'Module not found: @react-native-async-storage/async-storage' ) ;
68
+ }
69
+
64
70
return M . _load_original ( uri , parent ) ;
65
71
} ;
66
72
}
67
73
68
74
import { createForwardingEventProcessor , createBatchEventProcessor } from './event_processor_factory.react_native' ;
69
75
import { getForwardingEventProcessor } from './forwarding_event_processor' ;
70
76
import defaultEventDispatcher from './event_dispatcher/default_dispatcher.browser' ;
71
- import { EVENT_STORE_PREFIX , FAILED_EVENT_RETRY_INTERVAL } from './event_processor_factory' ;
77
+ import { EVENT_STORE_PREFIX , FAILED_EVENT_RETRY_INTERVAL , getPrefixEventStore } from './event_processor_factory' ;
72
78
import { getBatchEventProcessor } from './event_processor_factory' ;
73
79
import { AsyncCache , AsyncPrefixCache , SyncCache , SyncPrefixCache } from '../utils/cache/cache' ;
74
80
import { AsyncStorageCache } from '../utils/cache/async_storage_cache.react_native' ;
@@ -96,7 +102,7 @@ describe('createForwardingEventProcessor', () => {
96
102
97
103
it ( 'uses the browser default event dispatcher if none is provided' , ( ) => {
98
104
const processor = createForwardingEventProcessor ( ) ;
99
-
105
+
100
106
expect ( Object . is ( processor , mockGetForwardingEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
101
107
expect ( mockGetForwardingEventProcessor ) . toHaveBeenNthCalledWith ( 1 , defaultEventDispatcher ) ;
102
108
} ) ;
@@ -146,14 +152,86 @@ describe('createBatchEventProcessor', () => {
146
152
expect ( transformSet ( 'value' ) ) . toBe ( 'value' ) ;
147
153
} ) ;
148
154
155
+ it ( 'should throw error if @react-native-async-storage/async-storage is not available' , async ( ) => {
156
+ isAsyncStorageAvailable = false ;
157
+ const { getBatchEventProcessor } = await vi . importActual < typeof import ( './event_processor_factory' ) > (
158
+ './event_processor_factory'
159
+ ) ;
160
+ const { AsyncStorageCache } = await vi . importActual <
161
+ typeof import ( '../utils/cache/async_storage_cache.react_native' )
162
+ > ( '../utils/cache/async_storage_cache.react_native' ) ;
163
+
164
+ mockGetBatchEventProcessor . mockImplementationOnce ( ( ) => {
165
+ return getBatchEventProcessor (
166
+ {
167
+ eventDispatcher : defaultEventDispatcher ,
168
+ flushInterval : 1000 ,
169
+ batchSize : 10 ,
170
+ retryOptions : {
171
+ maxRetries : 5 ,
172
+ } ,
173
+ failedEventRetryInterval : FAILED_EVENT_RETRY_INTERVAL ,
174
+ } ,
175
+ BatchEventProcessor
176
+ ) ;
177
+ } ) ;
178
+
179
+ MockAsyncStorageCache . mockImplementationOnce ( ( ) => {
180
+ return new AsyncStorageCache ( ) ;
181
+ } ) ;
182
+
183
+ expect ( ( ) => createBatchEventProcessor ( { } ) ) . toThrowError (
184
+ 'Module not found: @react-native-async-storage/async-storage'
185
+ ) ;
186
+
187
+ isAsyncStorageAvailable = true ;
188
+ } ) ;
189
+
190
+ it ( 'should not throw error if eventStore is provided and @react-native-async-storage/async-storage is not available' , async ( ) => {
191
+ isAsyncStorageAvailable = false ;
192
+ const eventStore = {
193
+ operation : 'sync' ,
194
+ } as SyncCache < string > ;
195
+ const { getBatchEventProcessor } = await vi . importActual < typeof import ( './event_processor_factory' ) > (
196
+ './event_processor_factory'
197
+ ) ;
198
+ const { AsyncStorageCache } = await vi . importActual <
199
+ typeof import ( '../utils/cache/async_storage_cache.react_native' )
200
+ > ( '../utils/cache/async_storage_cache.react_native' ) ;
201
+
202
+ mockGetBatchEventProcessor . mockImplementationOnce ( ( ) => {
203
+ return getBatchEventProcessor (
204
+ {
205
+ eventDispatcher : defaultEventDispatcher ,
206
+ flushInterval : 1000 ,
207
+ batchSize : 10 ,
208
+ eventStore : getPrefixEventStore ( eventStore ) ,
209
+ retryOptions : {
210
+ maxRetries : 5 ,
211
+ } ,
212
+ failedEventRetryInterval : FAILED_EVENT_RETRY_INTERVAL ,
213
+ } ,
214
+ BatchEventProcessor
215
+ ) ;
216
+ } ) ;
217
+
218
+ MockAsyncStorageCache . mockImplementationOnce ( ( ) => {
219
+ return new AsyncStorageCache ( ) ;
220
+ } ) ;
221
+
222
+ expect ( ( ) => createBatchEventProcessor ( { eventStore } ) ) . not . toThrow ( ) ;
223
+
224
+ isAsyncStorageAvailable = true ;
225
+ } ) ;
226
+
149
227
it ( 'wraps the provided eventStore in a SyncPrefixCache if a SyncCache is provided as eventStore' , ( ) => {
150
228
const eventStore = {
151
229
operation : 'sync' ,
152
230
} as SyncCache < string > ;
153
231
154
232
const processor = createBatchEventProcessor ( { eventStore } ) ;
155
233
expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
156
-
234
+
157
235
expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . eventStore ) . toBe ( MockSyncPrefixCache . mock . results [ 0 ] . value ) ;
158
236
const [ cache , prefix , transformGet , transformSet ] = MockSyncPrefixCache . mock . calls [ 0 ] ;
159
237
@@ -172,7 +250,7 @@ describe('createBatchEventProcessor', () => {
172
250
173
251
const processor = createBatchEventProcessor ( { eventStore } ) ;
174
252
expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
175
-
253
+
176
254
expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . eventStore ) . toBe ( MockAsyncPrefixCache . mock . results [ 0 ] . value ) ;
177
255
const [ cache , prefix , transformGet , transformSet ] = MockAsyncPrefixCache . mock . calls [ 0 ] ;
178
256
@@ -184,7 +262,6 @@ describe('createBatchEventProcessor', () => {
184
262
expect ( transformSet ( { value : 1 } ) ) . toBe ( '{"value":1}' ) ;
185
263
} ) ;
186
264
187
-
188
265
it ( 'uses the provided eventDispatcher' , ( ) => {
189
266
const eventDispatcher = {
190
267
dispatchEvent : vi . fn ( ) ,
@@ -196,7 +273,7 @@ describe('createBatchEventProcessor', () => {
196
273
} ) ;
197
274
198
275
it ( 'uses the default browser event dispatcher if none is provided' , ( ) => {
199
- const processor = createBatchEventProcessor ( { } ) ;
276
+ const processor = createBatchEventProcessor ( { } ) ;
200
277
expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
201
278
expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . eventDispatcher ) . toBe ( defaultEventDispatcher ) ;
202
279
} ) ;
@@ -210,7 +287,7 @@ describe('createBatchEventProcessor', () => {
210
287
expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
211
288
expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . closingEventDispatcher ) . toBe ( closingEventDispatcher ) ;
212
289
213
- const processor2 = createBatchEventProcessor ( { } ) ;
290
+ const processor2 = createBatchEventProcessor ( { } ) ;
214
291
expect ( Object . is ( processor2 , mockGetBatchEventProcessor . mock . results [ 1 ] . value ) ) . toBe ( true ) ;
215
292
expect ( mockGetBatchEventProcessor . mock . calls [ 1 ] [ 0 ] . closingEventDispatcher ) . toBe ( undefined ) ;
216
293
} ) ;
@@ -220,7 +297,7 @@ describe('createBatchEventProcessor', () => {
220
297
expect ( Object . is ( processor1 , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
221
298
expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . flushInterval ) . toBe ( 2000 ) ;
222
299
223
- const processor2 = createBatchEventProcessor ( { } ) ;
300
+ const processor2 = createBatchEventProcessor ( { } ) ;
224
301
expect ( Object . is ( processor2 , mockGetBatchEventProcessor . mock . results [ 1 ] . value ) ) . toBe ( true ) ;
225
302
expect ( mockGetBatchEventProcessor . mock . calls [ 1 ] [ 0 ] . flushInterval ) . toBe ( undefined ) ;
226
303
} ) ;
@@ -230,19 +307,19 @@ describe('createBatchEventProcessor', () => {
230
307
expect ( Object . is ( processor1 , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
231
308
expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . batchSize ) . toBe ( 20 ) ;
232
309
233
- const processor2 = createBatchEventProcessor ( { } ) ;
310
+ const processor2 = createBatchEventProcessor ( { } ) ;
234
311
expect ( Object . is ( processor2 , mockGetBatchEventProcessor . mock . results [ 1 ] . value ) ) . toBe ( true ) ;
235
312
expect ( mockGetBatchEventProcessor . mock . calls [ 1 ] [ 0 ] . batchSize ) . toBe ( undefined ) ;
236
313
} ) ;
237
314
238
315
it ( 'uses maxRetries value of 5' , ( ) => {
239
- const processor = createBatchEventProcessor ( { } ) ;
316
+ const processor = createBatchEventProcessor ( { } ) ;
240
317
expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
241
318
expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . retryOptions ?. maxRetries ) . toBe ( 5 ) ;
242
319
} ) ;
243
320
244
321
it ( 'uses the default failedEventRetryInterval' , ( ) => {
245
- const processor = createBatchEventProcessor ( { } ) ;
322
+ const processor = createBatchEventProcessor ( { } ) ;
246
323
expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
247
324
expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . failedEventRetryInterval ) . toBe ( FAILED_EVENT_RETRY_INTERVAL ) ;
248
325
} ) ;
0 commit comments