@@ -25,7 +25,7 @@ vi.mock('./forwarding_event_processor', () => {
2525 return { getForwardingEventProcessor } ;
2626} ) ;
2727
28- vi . mock ( './event_processor_factory' , async ( importOriginal ) => {
28+ vi . mock ( './event_processor_factory' , async importOriginal => {
2929 const getBatchEventProcessor = vi . fn ( ) . mockImplementation ( ( ) => {
3030 return { } ;
3131 } ) ;
@@ -46,13 +46,14 @@ vi.mock('@react-native-community/netinfo', () => {
4646} ) ;
4747
4848let isNetInfoAvailable = false ;
49+ let isAsyncStorageAvailable = true ;
4950
5051await vi . hoisted ( async ( ) => {
5152 await mockRequireNetInfo ( ) ;
5253} ) ;
5354
5455async function mockRequireNetInfo ( ) {
55- const { Module} = await import ( 'module' ) ;
56+ const { Module } = await import ( 'module' ) ;
5657 const M : any = Module ;
5758
5859 M . _load_original = M . _load ;
@@ -61,14 +62,19 @@ async function mockRequireNetInfo() {
6162 if ( isNetInfoAvailable ) return { } ;
6263 throw new Error ( 'Module not found: @react-native-community/netinfo' ) ;
6364 }
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+
6470 return M . _load_original ( uri , parent ) ;
6571 } ;
6672}
6773
6874import { createForwardingEventProcessor , createBatchEventProcessor } from './event_processor_factory.react_native' ;
6975import { getForwardingEventProcessor } from './forwarding_event_processor' ;
7076import 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' ;
7278import { getBatchEventProcessor } from './event_processor_factory' ;
7379import { AsyncCache , AsyncPrefixCache , SyncCache , SyncPrefixCache } from '../utils/cache/cache' ;
7480import { AsyncStorageCache } from '../utils/cache/async_storage_cache.react_native' ;
@@ -96,7 +102,7 @@ describe('createForwardingEventProcessor', () => {
96102
97103 it ( 'uses the browser default event dispatcher if none is provided' , ( ) => {
98104 const processor = createForwardingEventProcessor ( ) ;
99-
105+
100106 expect ( Object . is ( processor , mockGetForwardingEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
101107 expect ( mockGetForwardingEventProcessor ) . toHaveBeenNthCalledWith ( 1 , defaultEventDispatcher ) ;
102108 } ) ;
@@ -146,14 +152,86 @@ describe('createBatchEventProcessor', () => {
146152 expect ( transformSet ( 'value' ) ) . toBe ( 'value' ) ;
147153 } ) ;
148154
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+
149227 it ( 'wraps the provided eventStore in a SyncPrefixCache if a SyncCache is provided as eventStore' , ( ) => {
150228 const eventStore = {
151229 operation : 'sync' ,
152230 } as SyncCache < string > ;
153231
154232 const processor = createBatchEventProcessor ( { eventStore } ) ;
155233 expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
156-
234+
157235 expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . eventStore ) . toBe ( MockSyncPrefixCache . mock . results [ 0 ] . value ) ;
158236 const [ cache , prefix , transformGet , transformSet ] = MockSyncPrefixCache . mock . calls [ 0 ] ;
159237
@@ -172,7 +250,7 @@ describe('createBatchEventProcessor', () => {
172250
173251 const processor = createBatchEventProcessor ( { eventStore } ) ;
174252 expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
175-
253+
176254 expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . eventStore ) . toBe ( MockAsyncPrefixCache . mock . results [ 0 ] . value ) ;
177255 const [ cache , prefix , transformGet , transformSet ] = MockAsyncPrefixCache . mock . calls [ 0 ] ;
178256
@@ -184,7 +262,6 @@ describe('createBatchEventProcessor', () => {
184262 expect ( transformSet ( { value : 1 } ) ) . toBe ( '{"value":1}' ) ;
185263 } ) ;
186264
187-
188265 it ( 'uses the provided eventDispatcher' , ( ) => {
189266 const eventDispatcher = {
190267 dispatchEvent : vi . fn ( ) ,
@@ -196,7 +273,7 @@ describe('createBatchEventProcessor', () => {
196273 } ) ;
197274
198275 it ( 'uses the default browser event dispatcher if none is provided' , ( ) => {
199- const processor = createBatchEventProcessor ( { } ) ;
276+ const processor = createBatchEventProcessor ( { } ) ;
200277 expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
201278 expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . eventDispatcher ) . toBe ( defaultEventDispatcher ) ;
202279 } ) ;
@@ -210,7 +287,7 @@ describe('createBatchEventProcessor', () => {
210287 expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
211288 expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . closingEventDispatcher ) . toBe ( closingEventDispatcher ) ;
212289
213- const processor2 = createBatchEventProcessor ( { } ) ;
290+ const processor2 = createBatchEventProcessor ( { } ) ;
214291 expect ( Object . is ( processor2 , mockGetBatchEventProcessor . mock . results [ 1 ] . value ) ) . toBe ( true ) ;
215292 expect ( mockGetBatchEventProcessor . mock . calls [ 1 ] [ 0 ] . closingEventDispatcher ) . toBe ( undefined ) ;
216293 } ) ;
@@ -220,7 +297,7 @@ describe('createBatchEventProcessor', () => {
220297 expect ( Object . is ( processor1 , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
221298 expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . flushInterval ) . toBe ( 2000 ) ;
222299
223- const processor2 = createBatchEventProcessor ( { } ) ;
300+ const processor2 = createBatchEventProcessor ( { } ) ;
224301 expect ( Object . is ( processor2 , mockGetBatchEventProcessor . mock . results [ 1 ] . value ) ) . toBe ( true ) ;
225302 expect ( mockGetBatchEventProcessor . mock . calls [ 1 ] [ 0 ] . flushInterval ) . toBe ( undefined ) ;
226303 } ) ;
@@ -230,19 +307,19 @@ describe('createBatchEventProcessor', () => {
230307 expect ( Object . is ( processor1 , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
231308 expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . batchSize ) . toBe ( 20 ) ;
232309
233- const processor2 = createBatchEventProcessor ( { } ) ;
310+ const processor2 = createBatchEventProcessor ( { } ) ;
234311 expect ( Object . is ( processor2 , mockGetBatchEventProcessor . mock . results [ 1 ] . value ) ) . toBe ( true ) ;
235312 expect ( mockGetBatchEventProcessor . mock . calls [ 1 ] [ 0 ] . batchSize ) . toBe ( undefined ) ;
236313 } ) ;
237314
238315 it ( 'uses maxRetries value of 5' , ( ) => {
239- const processor = createBatchEventProcessor ( { } ) ;
316+ const processor = createBatchEventProcessor ( { } ) ;
240317 expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
241318 expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . retryOptions ?. maxRetries ) . toBe ( 5 ) ;
242319 } ) ;
243320
244321 it ( 'uses the default failedEventRetryInterval' , ( ) => {
245- const processor = createBatchEventProcessor ( { } ) ;
322+ const processor = createBatchEventProcessor ( { } ) ;
246323 expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
247324 expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . failedEventRetryInterval ) . toBe ( FAILED_EVENT_RETRY_INTERVAL ) ;
248325 } ) ;
0 commit comments