@@ -20,16 +20,33 @@ vi.mock('./default_dispatcher.browser', () => {
20
20
} ) ;
21
21
22
22
vi . mock ( './forwarding_event_processor' , ( ) => {
23
- const getForwardingEventProcessor = vi . fn ( ) . mockReturnValue ( { } ) ;
23
+ const getForwardingEventProcessor = vi . fn ( ) . mockImplementation ( ( ) => {
24
+ return { } ;
25
+ } ) ;
24
26
return { getForwardingEventProcessor } ;
25
27
} ) ;
26
28
27
29
vi . mock ( './event_processor_factory' , ( ) => {
28
- const getBatchEventProcessor = vi . fn ( ) . mockReturnValue ( { } ) ;
30
+ const getBatchEventProcessor = vi . fn ( ) . mockImplementation ( ( ) => {
31
+ return { } ;
32
+ } ) ;
29
33
return { getBatchEventProcessor } ;
30
34
} ) ;
31
35
32
- import { createForwardingEventProcessor , createBatchEventProcessor } from './event_processor_factory.browser' ;
36
+ vi . mock ( '../utils/cache/local_storage_cache.browser' , ( ) => {
37
+ return { LocalStorageCache : vi . fn ( ) } ;
38
+ } ) ;
39
+
40
+ vi . mock ( '../utils/cache/cache' , ( ) => {
41
+ return { SyncPrefixCache : vi . fn ( ) } ;
42
+ } ) ;
43
+
44
+
45
+ import defaultEventDispatcher from './default_dispatcher.browser' ;
46
+ import { LocalStorageCache } from '../utils/cache/local_storage_cache.browser' ;
47
+ import { SyncPrefixCache } from '../utils/cache/cache' ;
48
+ import { createForwardingEventProcessor , createBatchEventProcessor , EVENT_STORE_PREFIX , FAILED_EVENT_RETRY_INTERVAL } from './event_processor_factory.browser' ;
49
+ import sendBeaconEventDispatcher from '../plugins/event_dispatcher/send_beacon_dispatcher' ;
33
50
import { getForwardingEventProcessor } from './forwarding_event_processor' ;
34
51
import browserDefaultEventDispatcher from './default_dispatcher.browser' ;
35
52
import { getBatchEventProcessor } from './event_processor_factory' ;
@@ -60,12 +77,15 @@ describe('createForwardingEventProcessor', () => {
60
77
} ) ;
61
78
} ) ;
62
79
63
-
64
80
describe ( 'createBatchEventProcessor' , ( ) => {
65
81
const mockGetBatchEventProcessor = vi . mocked ( getBatchEventProcessor ) ;
82
+ const MockLocalStorageCache = vi . mocked ( LocalStorageCache ) ;
83
+ const MockSyncPrefixCache = vi . mocked ( SyncPrefixCache ) ;
66
84
67
85
beforeEach ( ( ) => {
68
86
mockGetBatchEventProcessor . mockClear ( ) ;
87
+ MockLocalStorageCache . mockClear ( ) ;
88
+ MockSyncPrefixCache . mockClear ( ) ;
69
89
} ) ;
70
90
71
91
it ( 'uses localStorageCache and SyncPrefixCache to create eventStore' , ( ) => {
@@ -80,6 +100,88 @@ describe('createBatchEventProcessor', () => {
80
100
const processor = createBatchEventProcessor ( options ) ;
81
101
expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
82
102
const eventStore = mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . eventStore ;
83
- expect
103
+ expect ( Object . is ( eventStore , MockSyncPrefixCache . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
104
+
105
+ const [ cache , prefix , transformGet , transformSet ] = MockSyncPrefixCache . mock . calls [ 0 ] ;
106
+ expect ( Object . is ( cache , MockLocalStorageCache . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
107
+ expect ( prefix ) . toBe ( EVENT_STORE_PREFIX ) ;
108
+
109
+ // transformGet and transformSet should be identity functions
110
+ expect ( transformGet ( 'value' ) ) . toBe ( 'value' ) ;
111
+ expect ( transformSet ( 'value' ) ) . toBe ( 'value' ) ;
112
+ } ) ;
113
+
114
+ it ( 'uses the provided eventDispatcher' , ( ) => {
115
+ const eventDispatcher = {
116
+ dispatchEvent : vi . fn ( ) ,
117
+ } ;
118
+
119
+ const processor = createBatchEventProcessor ( { eventDispatcher } ) ;
120
+ expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
121
+ expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . eventDispatcher ) . toBe ( eventDispatcher ) ;
122
+ } ) ;
123
+
124
+ it ( 'uses the default broser event dispatcher if none is provided' , ( ) => {
125
+ const processor = createBatchEventProcessor ( { } ) ;
126
+ expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
127
+ expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . eventDispatcher ) . toBe ( defaultEventDispatcher ) ;
128
+ } ) ;
129
+
130
+ it ( 'uses the provided closingEventDispatcher' , ( ) => {
131
+ const closingEventDispatcher = {
132
+ dispatchEvent : vi . fn ( ) ,
133
+ } ;
134
+
135
+ const processor = createBatchEventProcessor ( { closingEventDispatcher } ) ;
136
+ expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
137
+ expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . closingEventDispatcher ) . toBe ( closingEventDispatcher ) ;
138
+ } ) ;
139
+
140
+ it ( 'does not use any closingEventDispatcher if eventDispatcher is provided but closingEventDispatcher is not' , ( ) => {
141
+ const eventDispatcher = {
142
+ dispatchEvent : vi . fn ( ) ,
143
+ } ;
144
+
145
+ const processor = createBatchEventProcessor ( { eventDispatcher } ) ;
146
+ expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
147
+ expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . closingEventDispatcher ) . toBe ( undefined ) ;
148
+ } ) ;
149
+
150
+ it ( 'uses the default sendBeacon event dispatcher if neither eventDispatcher nor closingEventDispatcher is provided' , ( ) => {
151
+ const processor = createBatchEventProcessor ( { } ) ;
152
+ expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
153
+ expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . closingEventDispatcher ) . toBe ( sendBeaconEventDispatcher ) ;
154
+ } ) ;
155
+
156
+ it ( 'uses the provided flushInterval' , ( ) => {
157
+ const processor1 = createBatchEventProcessor ( { flushInterval : 2000 } ) ;
158
+ expect ( Object . is ( processor1 , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
159
+ expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . flushInterval ) . toBe ( 2000 ) ;
160
+
161
+ const processor2 = createBatchEventProcessor ( { } ) ;
162
+ expect ( Object . is ( processor2 , mockGetBatchEventProcessor . mock . results [ 1 ] . value ) ) . toBe ( true ) ;
163
+ expect ( mockGetBatchEventProcessor . mock . calls [ 1 ] [ 0 ] . flushInterval ) . toBe ( undefined ) ;
164
+ } ) ;
165
+
166
+ it ( 'uses the provided batchSize' , ( ) => {
167
+ const processor1 = createBatchEventProcessor ( { batchSize : 20 } ) ;
168
+ expect ( Object . is ( processor1 , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
169
+ expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . batchSize ) . toBe ( 20 ) ;
170
+
171
+ const processor2 = createBatchEventProcessor ( { } ) ;
172
+ expect ( Object . is ( processor2 , mockGetBatchEventProcessor . mock . results [ 1 ] . value ) ) . toBe ( true ) ;
173
+ expect ( mockGetBatchEventProcessor . mock . calls [ 1 ] [ 0 ] . batchSize ) . toBe ( undefined ) ;
174
+ } ) ;
175
+
176
+ it ( 'uses maxRetries value of 5' , ( ) => {
177
+ const processor = createBatchEventProcessor ( { } ) ;
178
+ expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
179
+ expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . retryOptions ?. maxRetries ) . toBe ( 5 ) ;
180
+ } ) ;
181
+
182
+ it ( 'uses the default failedEventRetryInterval' , ( ) => {
183
+ const processor = createBatchEventProcessor ( { } ) ;
184
+ expect ( Object . is ( processor , mockGetBatchEventProcessor . mock . results [ 0 ] . value ) ) . toBe ( true ) ;
185
+ expect ( mockGetBatchEventProcessor . mock . calls [ 0 ] [ 0 ] . failedEventRetryInterval ) . toBe ( FAILED_EVENT_RETRY_INTERVAL ) ;
84
186
} ) ;
85
187
} ) ;
0 commit comments