Skip to content

Commit

Permalink
[FSSDK-10616] logOnlyEventDispatcher improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
junaed-optimizely committed Sep 5, 2024
1 parent 3ef3a01 commit c201a23
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Experiment.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ describe('<OptimizelyExperiment>', () => {
// while it's waiting for onReady()
expect(container.innerHTML).toBe('');

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

await waitFor(() => expect(optimizelyMock.activate).toHaveBeenCalledWith('experiment1', undefined, undefined));
Expand Down
24 changes: 23 additions & 1 deletion src/logOnlyEventDispatcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,32 @@ import { getLogger } from '@optimizely/optimizely-sdk';
const logger = getLogger('ReactSDK');

describe('logOnlyEventDispatcher', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('logs a message', () => {
const callback = jest.fn();
logOnlyEventDispatcher.dispatchEvent({ url: 'https://localhost:8080', httpVerb: 'POST', params: {} }, callback);
const mockEvent = { url: 'https://localhost:8080', httpVerb: 'POST' as const, params: {} };
logOnlyEventDispatcher.dispatchEvent(mockEvent, callback);
const secondArgFunction = (logger.debug as jest.Mock).mock.calls[0][1];
const result = secondArgFunction();

expect(callback).toHaveBeenCalled();
expect(logger.debug).toHaveBeenCalled();
expect(result).toBe(JSON.stringify(mockEvent));
});

it('debugger log print error stringifying event', () => {
const callback = jest.fn();
// circular reference to force JSON.stringify to throw an error
const circularReference: any = {};
circularReference.self = circularReference;
logOnlyEventDispatcher.dispatchEvent(circularReference, callback);
const secondArgFunction = (logger.debug as jest.Mock).mock.calls[0][1];
const result = secondArgFunction();

expect(typeof secondArgFunction).toBe('function');
expect(result).toBe('error stringifying event');
});
});
6 changes: 2 additions & 4 deletions src/logOnlyEventDispatcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2019, 2023 Optimizely
* Copyright 2019, 2023-2024 Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,10 +15,8 @@
*/

import * as optimizely from '@optimizely/optimizely-sdk';
import { getLogger } from '@optimizely/optimizely-sdk';

const logger = getLogger('ReactSDK');

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

0 comments on commit c201a23

Please sign in to comment.