diff --git a/src/Experiment.spec.tsx b/src/Experiment.spec.tsx index 97e6951..3686538 100644 --- a/src/Experiment.spec.tsx +++ b/src/Experiment.spec.tsx @@ -143,7 +143,7 @@ describe('', () => { // 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)); diff --git a/src/logOnlyEventDispatcher.spec.ts b/src/logOnlyEventDispatcher.spec.ts index 7edd53f..c3b1a43 100644 --- a/src/logOnlyEventDispatcher.spec.ts +++ b/src/logOnlyEventDispatcher.spec.ts @@ -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'); }); }); diff --git a/src/logOnlyEventDispatcher.ts b/src/logOnlyEventDispatcher.ts index 933edf8..89c8e90 100644 --- a/src/logOnlyEventDispatcher.ts +++ b/src/logOnlyEventDispatcher.ts @@ -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. @@ -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