diff --git a/packages/optimizely-sdk/lib/index.browser.js b/packages/optimizely-sdk/lib/index.browser.js index cde323a96..458ef532b 100644 --- a/packages/optimizely-sdk/lib/index.browser.js +++ b/packages/optimizely-sdk/lib/index.browser.js @@ -85,18 +85,26 @@ module.exports = { config.skipJSONValidation = true; } - var wrappedEventDispatcher = new eventProcessor.LocalStoragePendingEventsDispatcher({ - eventDispatcher: config.eventDispatcher || defaultEventDispatcher, - }); - if (!hasRetriedEvents) { - wrappedEventDispatcher.sendPendingEvents(); - hasRetriedEvents = true; + var eventDispatcher; + // prettier-ignore + if (config.eventDispatcher == null) { // eslint-disable-line eqeqeq + // only wrap the event dispatcher with pending events retry if the user didnt override + eventDispatcher = new eventProcessor.LocalStoragePendingEventsDispatcher({ + eventDispatcher: defaultEventDispatcher, + }); + + if (!hasRetriedEvents) { + eventDispatcher.sendPendingEvents(); + hasRetriedEvents = true; + } + } else { + eventDispatcher = config.eventDispatcher; } config = fns.assignIn({ clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE, }, config, { - eventDispatcher: wrappedEventDispatcher, + eventDispatcher: eventDispatcher, // always get the OptimizelyLogger facade from logging logger: logger, errorHandler: logging.getErrorHandler(), diff --git a/packages/optimizely-sdk/lib/index.browser.tests.js b/packages/optimizely-sdk/lib/index.browser.tests.js index f35b8225c..24fefbacb 100644 --- a/packages/optimizely-sdk/lib/index.browser.tests.js +++ b/packages/optimizely-sdk/lib/index.browser.tests.js @@ -62,7 +62,7 @@ describe('javascript-sdk', function() { requests.push(req); }; - sinon.spy(LocalStoragePendingEventsDispatcher.prototype, 'sendPendingEvents'); + sinon.stub(LocalStoragePendingEventsDispatcher.prototype, 'sendPendingEvents'); }); afterEach(function() { @@ -73,11 +73,39 @@ describe('javascript-sdk', function() { xhr.restore(); }); + describe('when an eventDispatcher is not passed in', function() { + it('should wrap the default eventDispatcher and invoke sendPendingEvents', function() { + var optlyInstance = optimizelyFactory.createInstance({ + datafile: {}, + errorHandler: fakeErrorHandler, + logger: silentLogger, + }); + // Invalid datafile causes onReady Promise rejection - catch this error + optlyInstance.onReady().catch(function() {}); + + sinon.assert.calledOnce(LocalStoragePendingEventsDispatcher.prototype.sendPendingEvents); + }); + }); + + describe('when an eventDispatcher is passed in', function() { + it('should NOT wrap the default eventDispatcher and invoke sendPendingEvents', function() { + var optlyInstance = optimizelyFactory.createInstance({ + datafile: {}, + errorHandler: fakeErrorHandler, + eventDispatcher: fakeEventDispatcher, + logger: silentLogger, + }); + // Invalid datafile causes onReady Promise rejection - catch this error + optlyInstance.onReady().catch(function() {}); + + sinon.assert.notCalled(LocalStoragePendingEventsDispatcher.prototype.sendPendingEvents); + }); + }); + it('should invoke resendPendingEvents at most once', function() { var optlyInstance = optimizelyFactory.createInstance({ datafile: {}, errorHandler: fakeErrorHandler, - eventDispatcher: fakeEventDispatcher, logger: silentLogger, }); // Invalid datafile causes onReady Promise rejection - catch this error @@ -88,7 +116,6 @@ describe('javascript-sdk', function() { optlyInstance = optimizelyFactory.createInstance({ datafile: {}, errorHandler: fakeErrorHandler, - eventDispatcher: fakeEventDispatcher, logger: silentLogger, }); optlyInstance.onReady().catch(function() {});