Skip to content

Commit c201a23

Browse files
[FSSDK-10616] logOnlyEventDispatcher improvement
1 parent 3ef3a01 commit c201a23

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

src/Experiment.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ describe('<OptimizelyExperiment>', () => {
143143
// while it's waiting for onReady()
144144
expect(container.innerHTML).toBe('');
145145

146-
// Simulate client becoming ready; oReady resolving, firing config update notification
146+
// Simulate client becoming ready; onReady resolving, firing config update notification
147147
resolver.resolve({ success: true });
148148

149149
await waitFor(() => expect(optimizelyMock.activate).toHaveBeenCalledWith('experiment1', undefined, undefined));

src/logOnlyEventDispatcher.spec.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,32 @@ import { getLogger } from '@optimizely/optimizely-sdk';
2424
const logger = getLogger('ReactSDK');
2525

2626
describe('logOnlyEventDispatcher', () => {
27+
beforeEach(() => {
28+
jest.clearAllMocks();
29+
});
30+
2731
it('logs a message', () => {
2832
const callback = jest.fn();
29-
logOnlyEventDispatcher.dispatchEvent({ url: 'https://localhost:8080', httpVerb: 'POST', params: {} }, callback);
33+
const mockEvent = { url: 'https://localhost:8080', httpVerb: 'POST' as const, params: {} };
34+
logOnlyEventDispatcher.dispatchEvent(mockEvent, callback);
35+
const secondArgFunction = (logger.debug as jest.Mock).mock.calls[0][1];
36+
const result = secondArgFunction();
37+
3038
expect(callback).toHaveBeenCalled();
3139
expect(logger.debug).toHaveBeenCalled();
40+
expect(result).toBe(JSON.stringify(mockEvent));
41+
});
42+
43+
it('debugger log print error stringifying event', () => {
44+
const callback = jest.fn();
45+
// circular reference to force JSON.stringify to throw an error
46+
const circularReference: any = {};
47+
circularReference.self = circularReference;
48+
logOnlyEventDispatcher.dispatchEvent(circularReference, callback);
49+
const secondArgFunction = (logger.debug as jest.Mock).mock.calls[0][1];
50+
const result = secondArgFunction();
51+
52+
expect(typeof secondArgFunction).toBe('function');
53+
expect(result).toBe('error stringifying event');
3254
});
3355
});

src/logOnlyEventDispatcher.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2019, 2023 Optimizely
2+
* Copyright 2019, 2023-2024 Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,10 +15,8 @@
1515
*/
1616

1717
import * as optimizely from '@optimizely/optimizely-sdk';
18-
import { getLogger } from '@optimizely/optimizely-sdk';
19-
20-
const logger = getLogger('ReactSDK');
2118

19+
const logger = optimizely.getLogger('ReactSDK');
2220
/**
2321
* logOnlyEventDispatcher only logs a message at the debug level, and does not
2422
* send any requests to the Optimizely results backend. Use this to disable

0 commit comments

Comments
 (0)