Skip to content

Commit 8727c9b

Browse files
committed
factory test
1 parent 5b04476 commit 8727c9b

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

lib/event_processor/event_processor_factory.browser.spec.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,15 @@ vi.mock('./forwarding_event_processor', () => {
2424
return { getForwardingEventProcessor };
2525
});
2626

27-
import { createForwardingEventProcessor } from './event_processor_factory.browser';
27+
vi.mock('./event_processor_factory', () => {
28+
const getBatchEventProcessor = vi.fn().mockReturnValue({});
29+
return { getBatchEventProcessor };
30+
});
31+
32+
import { createForwardingEventProcessor, createBatchEventProcessor } from './event_processor_factory.browser';
2833
import { getForwardingEventProcessor } from './forwarding_event_processor';
2934
import browserDefaultEventDispatcher from './default_dispatcher.browser';
35+
import { getBatchEventProcessor } from './event_processor_factory';
3036

3137
describe('createForwardingEventProcessor', () => {
3238
const mockGetForwardingEventProcessor = vi.mocked(getForwardingEventProcessor);
@@ -53,3 +59,27 @@ describe('createForwardingEventProcessor', () => {
5359
expect(mockGetForwardingEventProcessor).toHaveBeenNthCalledWith(1, browserDefaultEventDispatcher);
5460
});
5561
});
62+
63+
64+
describe('createBatchEventProcessor', () => {
65+
const mockGetBatchEventProcessor = vi.mocked(getBatchEventProcessor);
66+
67+
beforeEach(() => {
68+
mockGetBatchEventProcessor.mockClear();
69+
});
70+
71+
it('uses localStorageCache and SyncPrefixCache to create eventStore', () => {
72+
const options = {
73+
eventDispatcher: {
74+
dispatchEvent: vi.fn(),
75+
},
76+
flushInterval: 1000,
77+
batchSize: 10,
78+
};
79+
80+
const processor = createBatchEventProcessor(options);
81+
expect(Object.is(processor, mockGetBatchEventProcessor.mock.results[0].value)).toBe(true);
82+
const eventStore = mockGetBatchEventProcessor.mock.calls[0][0].eventStore;
83+
expect
84+
});
85+
});

lib/event_processor/event_processor_factory.browser.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
import { getForwardingEventProcessor } from './forwarding_event_processor';
1818
import { EventDispatcher } from './eventDispatcher';
1919
import { EventProcessor } from './eventProcessor';
20-
import { BatchEventProcessor, BatchEventProcessorConfig, EventWithId } from './batch_event_processor';
21-
import { getBatchEventProcessor, QueueingEventProcessorOptions } from './event_processor_factory';
20+
import { EventWithId } from './batch_event_processor';
21+
import { getBatchEventProcessor, BatchEventProcessorOptions } from './event_processor_factory';
2222
import defaultEventDispatcher from './default_dispatcher.browser';
2323
import sendBeaconEventDispatcher from '../plugins/event_dispatcher/send_beacon_dispatcher';
2424
import { LocalStorageCache } from '../utils/cache/local_storage_cache.browser';
25-
import { SyncPrefixCache } from '../utils/cache/cache';
25+
import { SyncPrefixCache, AsyncPrefixCache, Cache } from '../utils/cache/cache';
2626

2727
export const FAILED_EVENT_RETRY_INTERVAL = 20 * 1000;
2828
export const EVENT_STORE_PREFIX = 'fs_optly_pending_events';
@@ -33,14 +33,16 @@ export const createForwardingEventProcessor = (
3333
return getForwardingEventProcessor(eventDispatcher);
3434
};
3535

36-
export const createQueueingEventProcessor = (
37-
options: QueueingEventProcessorOptions
36+
const identity = <T>(v: T): T => v;
37+
38+
export const createBatchEventProcessor = (
39+
options: BatchEventProcessorOptions
3840
): EventProcessor => {
39-
const localStorageCache = new LocalStorageCache<string>();
40-
const eventStore = new SyncPrefixCache<string, EventWithId>(
41+
const localStorageCache = new LocalStorageCache<EventWithId>();
42+
const eventStore = new SyncPrefixCache<EventWithId, EventWithId>(
4143
localStorageCache, EVENT_STORE_PREFIX,
42-
JSON.parse,
43-
JSON.stringify
44+
identity,
45+
identity,
4446
);
4547

4648
return getBatchEventProcessor({

lib/event_processor/event_processor_factory.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ export const DEFAULT_EVENT_MAX_QUEUE_SIZE = 10000;
1212
export const DEFAULT_MIN_BACKOFF = 1000;
1313
export const DEFAULT_MAX_BACKOFF = 32000;
1414

15-
export type QueueingEventProcessorOptions = {
15+
export type BatchEventProcessorOptions = {
1616
eventDispatcher?: EventDispatcher;
1717
closingEventDispatcher?: EventDispatcher;
1818
flushInterval?: number;
1919
batchSize?: number;
20+
eventStore?: Cache<string>;
2021
};
2122

22-
export type QueueingEventProcessorFactoryOptions = Omit<QueueingEventProcessorOptions, 'eventDispatcher'> & {
23+
export type BatchEventProcessorFactoryOptions = Omit<BatchEventProcessorOptions, 'eventDispatcher' | 'eventStore'> & {
2324
eventDispatcher: EventDispatcher;
2425
failedEventRetryInterval?: number;
2526
eventStore?: Cache<EventWithId>;
@@ -31,7 +32,7 @@ export type QueueingEventProcessorFactoryOptions = Omit<QueueingEventProcessorOp
3132
}
3233

3334
export const getBatchEventProcessor = (
34-
options: QueueingEventProcessorFactoryOptions,
35+
options: BatchEventProcessorFactoryOptions,
3536
EventProcessorConstructor: typeof BatchEventProcessor = BatchEventProcessor
3637
): EventProcessor => {
3738
const { eventDispatcher, closingEventDispatcher, retryOptions, eventStore } = options;

vitest.config.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default defineConfig({
2020
test: {
2121
onConsoleLog: () => true,
2222
environment: 'happy-dom',
23-
include: ['**/event_processor_factory.spec.ts'],
23+
include: ['**/event_processor_factory.browser.spec.ts'],
2424
typecheck: {
2525
tsconfig: 'tsconfig.spec.json',
2626
},

0 commit comments

Comments
 (0)