@@ -24,9 +24,29 @@ vi.mock('./forwarding_event_processor', () => {
24
24
return { getForwardingEventProcessor } ;
25
25
} ) ;
26
26
27
- import { createForwardingEventProcessor } from './event_processor_factory.node' ;
27
+ vi . mock ( './event_processor_factory' , async ( importOriginal ) => {
28
+ const getBatchEventProcessor = vi . fn ( ) . mockImplementation ( ( ) => {
29
+ return { } ;
30
+ } ) ;
31
+ const original : any = await importOriginal ( ) ;
32
+ return { ...original , getBatchEventProcessor } ;
33
+ } ) ;
34
+
35
+ vi . mock ( '../utils/cache/async_storage_cache.react_native' , ( ) => {
36
+ return { AsyncStorageCache : vi . fn ( ) } ;
37
+ } ) ;
38
+
39
+ vi . mock ( '../utils/cache/cache' , ( ) => {
40
+ return { SyncPrefixCache : vi . fn ( ) , AsyncPrefixCache : vi . fn ( ) } ;
41
+ } ) ;
42
+
43
+ import { createBatchEventProcessor , createForwardingEventProcessor } from './event_processor_factory.node' ;
28
44
import { getForwardingEventProcessor } from './forwarding_event_processor' ;
29
45
import nodeDefaultEventDispatcher from './default_dispatcher.node' ;
46
+ import { EVENT_STORE_PREFIX , FAILED_EVENT_RETRY_INTERVAL } from './event_processor_factory' ;
47
+ import { getBatchEventProcessor } from './event_processor_factory' ;
48
+ import { AsyncCache , AsyncPrefixCache , SyncCache , SyncPrefixCache } from '../utils/cache/cache' ;
49
+ import { AsyncStorageCache } from '../utils/cache/async_storage_cache.react_native' ;
30
50
31
51
describe ( 'createForwardingEventProcessor' , ( ) => {
32
52
const mockGetForwardingEventProcessor = vi . mocked ( getForwardingEventProcessor ) ;
@@ -53,3 +73,126 @@ describe('createForwardingEventProcessor', () => {
53
73
expect ( mockGetForwardingEventProcessor ) . toHaveBeenNthCalledWith ( 1 , nodeDefaultEventDispatcher ) ;
54
74
} ) ;
55
75
} ) ;
76
+
77
+ describe ( 'createBatchEventProcessor' , ( ) => {
78
+ const mockGetBatchEventProcessor = vi . mocked ( getBatchEventProcessor ) ;
79
+ const MockAsyncStorageCache = vi . mocked ( AsyncStorageCache ) ;
80
+ const MockSyncPrefixCache = vi . mocked ( SyncPrefixCache ) ;
81
+ const MockAsyncPrefixCache = vi . mocked ( AsyncPrefixCache ) ;
82
+
83
+ beforeEach ( ( ) => {
84
+ mockGetBatchEventProcessor . mockClear ( ) ;
85
+ MockAsyncStorageCache . mockClear ( ) ;
86
+ MockSyncPrefixCache . mockClear ( ) ;
87
+ MockAsyncPrefixCache . mockClear ( ) ;
88
+ } ) ;
89
+
90
+ it ( 'uses no default event store if no eventStore is provided' , ( ) => {
91
+ const processor = createBatchEventProcessor ( { } ) ;
92
+
93
+ expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
94
+ const eventStore = mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . eventStore ;
95
+ expect ( eventStore ) . toBe ( undefined ) ;
96
+ } ) ;
97
+
98
+ it ( 'wraps the provided eventStore in a SyncPrefixCache if a SyncCache is provided as eventStore' , ( ) => {
99
+ const eventStore = {
100
+ operation : 'sync' ,
101
+ } as SyncCache < string > ;
102
+
103
+ const processor = createBatchEventProcessor ( { eventStore } ) ;
104
+ expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
105
+
106
+ expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . eventStore ) . toBe ( MockSyncPrefixCache . mock . results [ 0 ] . value ) ;
107
+ const [ cache , prefix , transformGet , transformSet ] = MockSyncPrefixCache . mock . calls [ 0 ] ;
108
+
109
+ expect ( cache ) . toBe ( eventStore ) ;
110
+ expect ( prefix ) . toBe ( EVENT_STORE_PREFIX ) ;
111
+
112
+ // transformGet and transformSet should be JSON.parse and JSON.stringify
113
+ expect ( transformGet ( '{"value": 1}' ) ) . toEqual ( { value : 1 } ) ;
114
+ expect ( transformSet ( { value : 1 } ) ) . toBe ( '{"value":1}' ) ;
115
+ } ) ;
116
+
117
+ it ( 'wraps the provided eventStore in a AsyncPrefixCache if a AsyncCache is provided as eventStore' , ( ) => {
118
+ const eventStore = {
119
+ operation : 'async' ,
120
+ } as AsyncCache < string > ;
121
+
122
+ const processor = createBatchEventProcessor ( { eventStore } ) ;
123
+ expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
124
+
125
+ expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . eventStore ) . toBe ( MockAsyncPrefixCache . mock . results [ 0 ] . value ) ;
126
+ const [ cache , prefix , transformGet , transformSet ] = MockAsyncPrefixCache . mock . calls [ 0 ] ;
127
+
128
+ expect ( cache ) . toBe ( eventStore ) ;
129
+ expect ( prefix ) . toBe ( EVENT_STORE_PREFIX ) ;
130
+
131
+ // transformGet and transformSet should be JSON.parse and JSON.stringify
132
+ expect ( transformGet ( '{"value": 1}' ) ) . toEqual ( { value : 1 } ) ;
133
+ expect ( transformSet ( { value : 1 } ) ) . toBe ( '{"value":1}' ) ;
134
+ } ) ;
135
+
136
+
137
+ it ( 'uses the provided eventDispatcher' , ( ) => {
138
+ const eventDispatcher = {
139
+ dispatchEvent : vi . fn ( ) ,
140
+ } ;
141
+
142
+ const processor = createBatchEventProcessor ( { eventDispatcher } ) ;
143
+ expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
144
+ expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . eventDispatcher ) . toBe ( eventDispatcher ) ;
145
+ } ) ;
146
+
147
+ it ( 'uses the default node event dispatcher if none is provided' , ( ) => {
148
+ const processor = createBatchEventProcessor ( { } ) ;
149
+ expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
150
+ expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . eventDispatcher ) . toBe ( nodeDefaultEventDispatcher ) ;
151
+ } ) ;
152
+
153
+ it ( 'uses the provided closingEventDispatcher' , ( ) => {
154
+ const closingEventDispatcher = {
155
+ dispatchEvent : vi . fn ( ) ,
156
+ } ;
157
+
158
+ const processor = createBatchEventProcessor ( { closingEventDispatcher } ) ;
159
+ expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
160
+ expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . closingEventDispatcher ) . toBe ( closingEventDispatcher ) ;
161
+
162
+ const processor2 = createBatchEventProcessor ( { } ) ;
163
+ expect ( Object . is ( processor2 , mockGetBatchEventProcessor . mock . results [ 1 ] . value ) ) . toBe ( true ) ;
164
+ expect ( mockGetBatchEventProcessor . mock . calls [ 1 ] [ 0 ] . closingEventDispatcher ) . toBe ( undefined ) ;
165
+ } ) ;
166
+
167
+ it ( 'uses the provided flushInterval' , ( ) => {
168
+ const processor1 = createBatchEventProcessor ( { flushInterval : 2000 } ) ;
169
+ expect ( Object . is ( processor1 , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
170
+ expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . flushInterval ) . toBe ( 2000 ) ;
171
+
172
+ const processor2 = createBatchEventProcessor ( { } ) ;
173
+ expect ( Object . is ( processor2 , mockGetBatchEventProcessor . mock . results [ 1 ] . value ) ) . toBe ( true ) ;
174
+ expect ( mockGetBatchEventProcessor . mock . calls [ 1 ] [ 0 ] . flushInterval ) . toBe ( undefined ) ;
175
+ } ) ;
176
+
177
+ it ( 'uses the provided batchSize' , ( ) => {
178
+ const processor1 = createBatchEventProcessor ( { batchSize : 20 } ) ;
179
+ expect ( Object . is ( processor1 , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
180
+ expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . batchSize ) . toBe ( 20 ) ;
181
+
182
+ const processor2 = createBatchEventProcessor ( { } ) ;
183
+ expect ( Object . is ( processor2 , mockGetBatchEventProcessor . mock . results [ 1 ] . value ) ) . toBe ( true ) ;
184
+ expect ( mockGetBatchEventProcessor . mock . calls [ 1 ] [ 0 ] . batchSize ) . toBe ( undefined ) ;
185
+ } ) ;
186
+
187
+ it ( 'uses maxRetries value of 10' , ( ) => {
188
+ const processor = createBatchEventProcessor ( { } ) ;
189
+ expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
190
+ expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . retryOptions ?. maxRetries ) . toBe ( 10 ) ;
191
+ } ) ;
192
+
193
+ it ( 'uses the default failedEventRetryInterval' , ( ) => {
194
+ const processor = createBatchEventProcessor ( { } ) ;
195
+ expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
196
+ expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . failedEventRetryInterval ) . toBe ( FAILED_EVENT_RETRY_INTERVAL ) ;
197
+ } ) ;
198
+ } ) ;
0 commit comments