-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathevent_processor_factory.browser.spec.ts
More file actions
184 lines (151 loc) · 8.19 KB
/
event_processor_factory.browser.spec.ts
File metadata and controls
184 lines (151 loc) · 8.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* Copyright 2024-2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { vi, describe, it, expect, beforeEach } from 'vitest';
vi.mock('./default_dispatcher.browser', () => {
return { default: {} };
});
vi.mock('./forwarding_event_processor', () => {
const getForwardingEventProcessor = vi.fn().mockImplementation(() => {
return {};
});
return { getForwardingEventProcessor };
});
vi.mock('./event_processor_factory', async (importOriginal) => {
const getBatchEventProcessor = vi.fn().mockImplementation(() => {
return {};
});
const getOpaqueBatchEventProcessor = vi.fn().mockImplementation(() => {
return {};
});
const original: any = await importOriginal();
return { ...original, getBatchEventProcessor, getOpaqueBatchEventProcessor };
});
vi.mock('../utils/cache/local_storage_cache.browser', () => {
return { LocalStorageCache: vi.fn() };
});
vi.mock('../utils/cache/store', () => {
return { SyncPrefixStore: vi.fn() };
});
import defaultEventDispatcher from './event_dispatcher/default_dispatcher.browser';
import { LocalStorageCache } from '../utils/cache/local_storage_cache.browser';
import { SyncPrefixStore } from '../utils/cache/store';
import { createForwardingEventProcessor, createBatchEventProcessor } from './event_processor_factory.browser';
import { EVENT_STORE_PREFIX, extractEventProcessor, FAILED_EVENT_RETRY_INTERVAL } from './event_processor_factory';
import sendBeaconEventDispatcher from './event_dispatcher/send_beacon_dispatcher.browser';
import { getForwardingEventProcessor } from './forwarding_event_processor';
import browserDefaultEventDispatcher from './event_dispatcher/default_dispatcher.browser';
import { getOpaqueBatchEventProcessor } from './event_processor_factory';
describe('createForwardingEventProcessor', () => {
const mockGetForwardingEventProcessor = vi.mocked(getForwardingEventProcessor);
beforeEach(() => {
mockGetForwardingEventProcessor.mockClear();
});
it('returns forwarding event processor by calling getForwardingEventProcessor with the provided dispatcher', () => {
const eventDispatcher = {
dispatchEvent: vi.fn(),
};
const processor = extractEventProcessor(createForwardingEventProcessor(eventDispatcher));
expect(Object.is(processor, mockGetForwardingEventProcessor.mock.results[0].value)).toBe(true);
expect(mockGetForwardingEventProcessor).toHaveBeenNthCalledWith(1, eventDispatcher);
});
it('uses the browser default event dispatcher if none is provided', () => {
const processor = extractEventProcessor(createForwardingEventProcessor());
expect(Object.is(processor, mockGetForwardingEventProcessor.mock.results[0].value)).toBe(true);
expect(mockGetForwardingEventProcessor).toHaveBeenNthCalledWith(1, browserDefaultEventDispatcher);
});
});
describe('createBatchEventProcessor', () => {
const mockGetOpaqueBatchEventProcessor = vi.mocked(getOpaqueBatchEventProcessor);
const MockLocalStorageCache = vi.mocked(LocalStorageCache);
const MockSyncPrefixStore = vi.mocked(SyncPrefixStore);
beforeEach(() => {
mockGetOpaqueBatchEventProcessor.mockClear();
MockLocalStorageCache.mockClear();
MockSyncPrefixStore.mockClear();
});
it('uses LocalStorageCache and SyncPrefixStore to create eventStore', () => {
const processor = createBatchEventProcessor({});
expect(Object.is(processor, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true);
const eventStore = mockGetOpaqueBatchEventProcessor.mock.calls[0][0].eventStore;
expect(Object.is(eventStore, MockSyncPrefixStore.mock.results[0].value)).toBe(true);
const [cache, prefix, transformGet, transformSet] = MockSyncPrefixStore.mock.calls[0];
expect(Object.is(cache, MockLocalStorageCache.mock.results[0].value)).toBe(true);
expect(prefix).toBe(EVENT_STORE_PREFIX);
// transformGet and transformSet should be identity functions
expect(transformGet('value')).toBe('value');
expect(transformSet('value')).toBe('value');
});
it('uses the provided eventDispatcher', () => {
const eventDispatcher = {
dispatchEvent: vi.fn(),
};
const processor = createBatchEventProcessor({ eventDispatcher });
expect(Object.is(processor, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true);
expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][0].eventDispatcher).toBe(eventDispatcher);
});
it('uses the default browser event dispatcher if none is provided', () => {
const processor = createBatchEventProcessor({ });
expect(Object.is(processor, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true);
expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][0].eventDispatcher).toBe(defaultEventDispatcher);
});
it('uses the provided closingEventDispatcher', () => {
const closingEventDispatcher = {
dispatchEvent: vi.fn(),
};
const processor = createBatchEventProcessor({ closingEventDispatcher });
expect(Object.is(processor, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true);
expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][0].closingEventDispatcher).toBe(closingEventDispatcher);
});
it('does not use any closingEventDispatcher if eventDispatcher is provided but closingEventDispatcher is not', () => {
const eventDispatcher = {
dispatchEvent: vi.fn(),
};
const processor = createBatchEventProcessor({ eventDispatcher });
expect(Object.is(processor, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true);
expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][0].closingEventDispatcher).toBe(undefined);
});
it('uses the default sendBeacon event dispatcher if neither eventDispatcher nor closingEventDispatcher is provided', () => {
const processor = createBatchEventProcessor({ });
expect(Object.is(processor, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true);
expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][0].closingEventDispatcher).toBe(sendBeaconEventDispatcher);
});
it('uses the provided flushInterval', () => {
const processor1 = createBatchEventProcessor({ flushInterval: 2000 });
expect(Object.is(processor1, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true);
expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][0].flushInterval).toBe(2000);
const processor2 = createBatchEventProcessor({ });
expect(Object.is(processor2, mockGetOpaqueBatchEventProcessor.mock.results[1].value)).toBe(true);
expect(mockGetOpaqueBatchEventProcessor.mock.calls[1][0].flushInterval).toBe(undefined);
});
it('uses the provided batchSize', () => {
const processor1 = createBatchEventProcessor({ batchSize: 20 });
expect(Object.is(processor1, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true);
expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][0].batchSize).toBe(20);
const processor2 = createBatchEventProcessor({ });
expect(Object.is(processor2, mockGetOpaqueBatchEventProcessor.mock.results[1].value)).toBe(true);
expect(mockGetOpaqueBatchEventProcessor.mock.calls[1][0].batchSize).toBe(undefined);
});
it('uses maxRetries value of 5', () => {
const processor = createBatchEventProcessor({ });
expect(Object.is(processor, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true);
expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][0].retryOptions?.maxRetries).toBe(5);
});
it('uses the default failedEventRetryInterval', () => {
const processor = createBatchEventProcessor({ });
expect(Object.is(processor, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true);
expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][0].failedEventRetryInterval).toBe(FAILED_EVENT_RETRY_INTERVAL);
});
});