@@ -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,50 @@ 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 { AsyncStorageCache } = await vi . importActual <
158
+ typeof import ( '../utils/cache/async_storage_cache.react_native' )
159
+ > ( '../utils/cache/async_storage_cache.react_native' ) ;
160
+
161
+ MockAsyncStorageCache . mockImplementationOnce ( ( ) => {
162
+ return new AsyncStorageCache ( ) ;
163
+ } ) ;
164
+
165
+ expect ( ( ) => createBatchEventProcessor ( { } ) ) . toThrowError (
166
+ 'Module not found: @react-native-async-storage/async-storage'
167
+ ) ;
168
+
169
+ isAsyncStorageAvailable = true ;
170
+ } ) ;
171
+
172
+ it ( 'should not throw error if eventStore is provided and @react-native-async-storage/async-storage is not available' , async ( ) => {
173
+ isAsyncStorageAvailable = false ;
174
+ const eventStore = {
175
+ operation : 'sync' ,
176
+ } as SyncCache < string > ;
177
+
178
+ const { AsyncStorageCache } = await vi . importActual <
179
+ typeof import ( '../utils/cache/async_storage_cache.react_native' )
180
+ > ( '../utils/cache/async_storage_cache.react_native' ) ;
181
+
182
+ MockAsyncStorageCache . mockImplementationOnce ( ( ) => {
183
+ return new AsyncStorageCache ( ) ;
184
+ } ) ;
185
+
186
+ expect ( ( ) => createBatchEventProcessor ( { eventStore } ) ) . not . toThrow ( ) ;
187
+
188
+ isAsyncStorageAvailable = true ;
189
+ } ) ;
190
+
149
191
it ( 'wraps the provided eventStore in a SyncPrefixCache if a SyncCache is provided as eventStore' , ( ) => {
150
192
const eventStore = {
151
193
operation : 'sync' ,
152
194
} as SyncCache < string > ;
153
195
154
196
const processor = createBatchEventProcessor ( { eventStore } ) ;
155
197
expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
156
-
198
+
157
199
expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . eventStore ) . toBe ( MockSyncPrefixCache . mock . results [ 0 ] . value ) ;
158
200
const [ cache , prefix , transformGet , transformSet ] = MockSyncPrefixCache . mock . calls [ 0 ] ;
159
201
@@ -172,7 +214,7 @@ describe('createBatchEventProcessor', () => {
172
214
173
215
const processor = createBatchEventProcessor ( { eventStore } ) ;
174
216
expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
175
-
217
+
176
218
expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . eventStore ) . toBe ( MockAsyncPrefixCache . mock . results [ 0 ] . value ) ;
177
219
const [ cache , prefix , transformGet , transformSet ] = MockAsyncPrefixCache . mock . calls [ 0 ] ;
178
220
@@ -184,7 +226,6 @@ describe('createBatchEventProcessor', () => {
184
226
expect ( transformSet ( { value : 1 } ) ) . toBe ( '{"value":1}' ) ;
185
227
} ) ;
186
228
187
-
188
229
it ( 'uses the provided eventDispatcher' , ( ) => {
189
230
const eventDispatcher = {
190
231
dispatchEvent : vi . fn ( ) ,
@@ -196,7 +237,7 @@ describe('createBatchEventProcessor', () => {
196
237
} ) ;
197
238
198
239
it ( 'uses the default browser event dispatcher if none is provided' , ( ) => {
199
- const processor = createBatchEventProcessor ( { } ) ;
240
+ const processor = createBatchEventProcessor ( { } ) ;
200
241
expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
201
242
expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . eventDispatcher ) . toBe ( defaultEventDispatcher ) ;
202
243
} ) ;
@@ -210,7 +251,7 @@ describe('createBatchEventProcessor', () => {
210
251
expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
211
252
expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . closingEventDispatcher ) . toBe ( closingEventDispatcher ) ;
212
253
213
- const processor2 = createBatchEventProcessor ( { } ) ;
254
+ const processor2 = createBatchEventProcessor ( { } ) ;
214
255
expect ( Object . is ( processor2 , mockGetBatchEventProcessor . mock . results [ 1 ] . value ) ) . toBe ( true ) ;
215
256
expect ( mockGetBatchEventProcessor . mock . calls [ 1 ] [ 0 ] . closingEventDispatcher ) . toBe ( undefined ) ;
216
257
} ) ;
@@ -220,7 +261,7 @@ describe('createBatchEventProcessor', () => {
220
261
expect ( Object . is ( processor1 , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
221
262
expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . flushInterval ) . toBe ( 2000 ) ;
222
263
223
- const processor2 = createBatchEventProcessor ( { } ) ;
264
+ const processor2 = createBatchEventProcessor ( { } ) ;
224
265
expect ( Object . is ( processor2 , mockGetBatchEventProcessor . mock . results [ 1 ] . value ) ) . toBe ( true ) ;
225
266
expect ( mockGetBatchEventProcessor . mock . calls [ 1 ] [ 0 ] . flushInterval ) . toBe ( undefined ) ;
226
267
} ) ;
@@ -230,19 +271,19 @@ describe('createBatchEventProcessor', () => {
230
271
expect ( Object . is ( processor1 , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
231
272
expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . batchSize ) . toBe ( 20 ) ;
232
273
233
- const processor2 = createBatchEventProcessor ( { } ) ;
274
+ const processor2 = createBatchEventProcessor ( { } ) ;
234
275
expect ( Object . is ( processor2 , mockGetBatchEventProcessor . mock . results [ 1 ] . value ) ) . toBe ( true ) ;
235
276
expect ( mockGetBatchEventProcessor . mock . calls [ 1 ] [ 0 ] . batchSize ) . toBe ( undefined ) ;
236
277
} ) ;
237
278
238
279
it ( 'uses maxRetries value of 5' , ( ) => {
239
- const processor = createBatchEventProcessor ( { } ) ;
280
+ const processor = createBatchEventProcessor ( { } ) ;
240
281
expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
241
282
expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . retryOptions ?. maxRetries ) . toBe ( 5 ) ;
242
283
} ) ;
243
284
244
285
it ( 'uses the default failedEventRetryInterval' , ( ) => {
245
- const processor = createBatchEventProcessor ( { } ) ;
286
+ const processor = createBatchEventProcessor ( { } ) ;
246
287
expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
247
288
expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . failedEventRetryInterval ) . toBe ( FAILED_EVENT_RETRY_INTERVAL ) ;
248
289
} ) ;
0 commit comments