@@ -37,8 +37,17 @@ vi.mock('../utils/cache/async_storage_cache.react_native', () => {
3737 return { AsyncStorageCache : vi . fn ( ) } ;
3838} ) ;
3939
40- vi . mock ( '../utils/cache/store' , ( ) => {
41- return { SyncPrefixStore : vi . fn ( ) , AsyncPrefixStore : vi . fn ( ) } ;
40+ vi . mock ( './event_store' , async ( importOriginal ) => {
41+ const actual : any = await importOriginal ( )
42+ return {
43+ ...actual ,
44+ EventStore : vi . fn ( ) ,
45+ }
46+ } ) ;
47+
48+ vi . mock ( '../utils/cache/store' , async ( importOriginal ) => {
49+ const actual : any = await importOriginal ( )
50+ return { ...actual , SyncPrefixStore : vi . fn ( ) , AsyncPrefixStore : vi . fn ( ) } ;
4251} ) ;
4352
4453vi . mock ( '@react-native-community/netinfo' , ( ) => {
@@ -66,10 +75,11 @@ async function mockRequireNetInfo() {
6675
6776import { createForwardingEventProcessor , createBatchEventProcessor } from './event_processor_factory.react_native' ;
6877import defaultEventDispatcher from './event_dispatcher/default_dispatcher.browser' ;
69- import { EVENT_STORE_PREFIX , extractEventProcessor , getForwardingEventProcessor , FAILED_EVENT_RETRY_INTERVAL } from './event_processor_factory' ;
78+ import { extractEventProcessor , getForwardingEventProcessor , FAILED_EVENT_RETRY_INTERVAL } from './event_processor_factory' ;
7079import { getOpaqueBatchEventProcessor } from './event_processor_factory' ;
7180import { AsyncStore , AsyncPrefixStore , SyncStore , SyncPrefixStore } from '../utils/cache/store' ;
7281import { AsyncStorageCache } from '../utils/cache/async_storage_cache.react_native' ;
82+ import { EVENT_STORE_PREFIX , EventStore } from './event_store' ;
7383import { MODULE_NOT_FOUND_REACT_NATIVE_ASYNC_STORAGE } from '../utils/import.react_native/@react-native-async-storage/async-storage' ;
7484
7585describe ( 'createForwardingEventProcessor' , ( ) => {
@@ -103,30 +113,45 @@ describe('createBatchEventProcessor', () => {
103113 const MockAsyncStorageCache = vi . mocked ( AsyncStorageCache ) ;
104114 const MockSyncPrefixStore = vi . mocked ( SyncPrefixStore ) ;
105115 const MockAsyncPrefixStore = vi . mocked ( AsyncPrefixStore ) ;
116+ const MockEventStore = vi . mocked ( EventStore ) ;
106117
107118 beforeEach ( ( ) => {
108119 mockGetOpaqueBatchEventProcessor . mockClear ( ) ;
109120 MockAsyncStorageCache . mockClear ( ) ;
110121 MockSyncPrefixStore . mockClear ( ) ;
111122 MockAsyncPrefixStore . mockClear ( ) ;
123+ MockEventStore . mockClear ( ) ;
112124 } ) ;
113125
114- it ( 'uses an EventStore instance with correct options if no eventStore is provided' , ( ) => {
126+ it ( 'uses an EventStore instance with AsyncStorageCache and correct options if no eventStore is provided' , ( ) => {
115127 const processor = createBatchEventProcessor ( {
116128 storeTtl : 60_000 ,
117129 } ) ;
118130
119131 expect ( Object . is ( processor , mockGetOpaqueBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
120132 const eventStore = mockGetOpaqueBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . eventStore ;
121- expect ( Object . is ( eventStore , MockAsyncPrefixStore . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
133+ expect ( Object . is ( eventStore , MockEventStore . mock . instances [ 0 ] ) ) . toBe ( true ) ;
122134
123- const [ cache , prefix , transformGet , transformSet ] = MockAsyncPrefixStore . mock . calls [ 0 ] ;
124- expect ( Object . is ( cache , MockAsyncStorageCache . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
125- expect ( prefix ) . toBe ( EVENT_STORE_PREFIX ) ;
135+ let { store, ttl, maxSize } = MockEventStore . mock . calls [ 0 ] [ 0 ] ;
136+ expect ( Object . is ( store , MockAsyncStorageCache . mock . instances [ 0 ] ) ) . toBe ( true ) ;
137+
138+ expect ( ttl ) . toBe ( 60_000 ) ;
139+ expect ( maxSize ) . toBe ( 500 ) ; // the default max size * 2 < 500
140+
141+ const processor2 = createBatchEventProcessor ( {
142+ storeTtl : 10_000 ,
143+ batchSize : 260 ,
144+ } ) ;
145+
146+ expect ( Object . is ( processor2 , mockGetOpaqueBatchEventProcessor . mock . results [ 1 ] . value ) ) . toBe ( true ) ;
147+ const eventStore2 = mockGetOpaqueBatchEventProcessor . mock . calls [ 1 ] [ 0 ] . eventStore ;
148+ expect ( Object . is ( eventStore2 , MockEventStore . mock . instances [ 1 ] ) ) . toBe ( true ) ;
149+
150+ ( { store, ttl, maxSize } = MockEventStore . mock . calls [ 1 ] [ 0 ] ) ;
151+ expect ( Object . is ( store , MockAsyncStorageCache . mock . instances [ 1 ] ) ) . toBe ( true ) ;
126152
127- // transformGet and transformSet should be identity functions
128- expect ( transformGet ( 'value' ) ) . toBe ( 'value' ) ;
129- expect ( transformSet ( 'value' ) ) . toBe ( 'value' ) ;
153+ expect ( ttl ) . toBe ( 10_000 ) ;
154+ expect ( maxSize ) . toBe ( 520 ) ; // the provided batch size * 2 > 500
130155 } ) ;
131156
132157 it ( 'should throw error if @react-native-async-storage/async-storage is not available' , async ( ) => {
0 commit comments