@@ -41,21 +41,48 @@ vi.mock('../utils/cache/cache', () => {
41
41
return { SyncPrefixCache : vi . fn ( ) , AsyncPrefixCache : vi . fn ( ) } ;
42
42
} ) ;
43
43
44
+ vi . mock ( '@react-native-community/netinfo' , ( ) => {
45
+ return { NetInfoState : { } , addEventListener : vi . fn ( ) } ;
46
+ } ) ;
47
+
48
+ let isNetInfoAvailable = false ;
49
+
50
+ await vi . hoisted ( async ( ) => {
51
+ await mockRequireNetInfo ( ) ;
52
+ } ) ;
53
+
54
+ async function mockRequireNetInfo ( ) {
55
+ const { Module} = await import ( 'module' ) ;
56
+ const M : any = Module ;
57
+
58
+ M . _load_original = M . _load ;
59
+ M . _load = ( uri : string , parent : string ) => {
60
+ if ( uri === '@react-native-community/netinfo' ) {
61
+ if ( isNetInfoAvailable ) return { } ;
62
+ throw new Error ( 'Module not found: @react-native-community/netinfo' ) ;
63
+ }
64
+ return M . _load_original ( uri , parent ) ;
65
+ } ;
66
+ }
67
+
44
68
import { createForwardingEventProcessor , createBatchEventProcessor } from './event_processor_factory.react_native' ;
45
69
import { getForwardingEventProcessor } from './forwarding_event_processor' ;
46
70
import defaultEventDispatcher from './default_dispatcher.browser' ;
47
71
import { EVENT_STORE_PREFIX , FAILED_EVENT_RETRY_INTERVAL } from './event_processor_factory' ;
48
72
import { getBatchEventProcessor } from './event_processor_factory' ;
49
73
import { AsyncCache , AsyncPrefixCache , SyncCache , SyncPrefixCache } from '../utils/cache/cache' ;
50
74
import { AsyncStorageCache } from '../utils/cache/async_storage_cache.react_native' ;
75
+ import { ReactNativeNetInfoEventProcessor } from './batch_event_processor.react_native' ;
76
+ import { BatchEventProcessor } from './batch_event_processor' ;
51
77
52
78
describe ( 'createForwardingEventProcessor' , ( ) => {
53
79
const mockGetForwardingEventProcessor = vi . mocked ( getForwardingEventProcessor ) ;
54
80
55
81
beforeEach ( ( ) => {
56
82
mockGetForwardingEventProcessor . mockClear ( ) ;
83
+ isNetInfoAvailable = false ;
57
84
} ) ;
58
-
85
+
59
86
it ( 'returns forwarding event processor by calling getForwardingEventProcessor with the provided dispatcher' , ( ) => {
60
87
const eventDispatcher = {
61
88
dispatchEvent : vi . fn ( ) ,
@@ -82,12 +109,26 @@ describe('createBatchEventProcessor', () => {
82
109
const MockAsyncPrefixCache = vi . mocked ( AsyncPrefixCache ) ;
83
110
84
111
beforeEach ( ( ) => {
112
+ isNetInfoAvailable = false ;
85
113
mockGetBatchEventProcessor . mockClear ( ) ;
86
114
MockAsyncStorageCache . mockClear ( ) ;
87
115
MockSyncPrefixCache . mockClear ( ) ;
88
116
MockAsyncPrefixCache . mockClear ( ) ;
89
117
} ) ;
90
118
119
+ it ( 'returns an instance of ReacNativeNetInfoEventProcessor if netinfo can be required' , async ( ) => {
120
+ isNetInfoAvailable = true ;
121
+ const processor = createBatchEventProcessor ( { } ) ;
122
+ expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
123
+ expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 1 ] ) . toBe ( ReactNativeNetInfoEventProcessor ) ;
124
+ } ) ;
125
+
126
+ it ( 'returns an instance of BatchEventProcessor if netinfo cannot be required' , async ( ) => {
127
+ isNetInfoAvailable = false ;
128
+ const processor = createBatchEventProcessor ( { } ) ; ;
129
+ expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 1 ] ) . toBe ( BatchEventProcessor ) ;
130
+ } ) ;
131
+
91
132
it ( 'uses AsyncStorageCache and AsyncPrefixCache to create eventStore if no eventStore is provided' , ( ) => {
92
133
const processor = createBatchEventProcessor ( { } ) ;
93
134
0 commit comments