Skip to content

Commit def52f5

Browse files
author
Michael Ng
authored
fix: Merge some bug fixes from master (#349)
1 parent ec96fb8 commit def52f5

File tree

4 files changed

+79
-23
lines changed

4 files changed

+79
-23
lines changed

packages/optimizely-sdk/lib/index.browser.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,26 @@ module.exports = {
8585
config.skipJSONValidation = true;
8686
}
8787

88-
var wrappedEventDispatcher = new eventProcessor.LocalStoragePendingEventsDispatcher({
89-
eventDispatcher: config.eventDispatcher || defaultEventDispatcher,
90-
});
91-
if (!hasRetriedEvents) {
92-
wrappedEventDispatcher.sendPendingEvents();
93-
hasRetriedEvents = true;
88+
var eventDispatcher;
89+
// prettier-ignore
90+
if (config.eventDispatcher == null) { // eslint-disable-line eqeqeq
91+
// only wrap the event dispatcher with pending events retry if the user didnt override
92+
eventDispatcher = new eventProcessor.LocalStoragePendingEventsDispatcher({
93+
eventDispatcher: defaultEventDispatcher,
94+
});
95+
96+
if (!hasRetriedEvents) {
97+
eventDispatcher.sendPendingEvents();
98+
hasRetriedEvents = true;
99+
}
100+
} else {
101+
eventDispatcher = config.eventDispatcher;
94102
}
95103

96104
config = fns.assignIn({
97105
clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE,
98106
}, config, {
99-
eventDispatcher: wrappedEventDispatcher,
107+
eventDispatcher: eventDispatcher,
100108
// always get the OptimizelyLogger facade from logging
101109
logger: logger,
102110
errorHandler: logging.getErrorHandler(),

packages/optimizely-sdk/lib/index.browser.tests.js

+30-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe('javascript-sdk', function() {
6262
requests.push(req);
6363
};
6464

65-
sinon.spy(LocalStoragePendingEventsDispatcher.prototype, 'sendPendingEvents');
65+
sinon.stub(LocalStoragePendingEventsDispatcher.prototype, 'sendPendingEvents');
6666
});
6767

6868
afterEach(function() {
@@ -73,11 +73,39 @@ describe('javascript-sdk', function() {
7373
xhr.restore();
7474
});
7575

76+
describe('when an eventDispatcher is not passed in', function() {
77+
it('should wrap the default eventDispatcher and invoke sendPendingEvents', function() {
78+
var optlyInstance = optimizelyFactory.createInstance({
79+
datafile: {},
80+
errorHandler: fakeErrorHandler,
81+
logger: silentLogger,
82+
});
83+
// Invalid datafile causes onReady Promise rejection - catch this error
84+
optlyInstance.onReady().catch(function() {});
85+
86+
sinon.assert.calledOnce(LocalStoragePendingEventsDispatcher.prototype.sendPendingEvents);
87+
});
88+
});
89+
90+
describe('when an eventDispatcher is passed in', function() {
91+
it('should NOT wrap the default eventDispatcher and invoke sendPendingEvents', function() {
92+
var optlyInstance = optimizelyFactory.createInstance({
93+
datafile: {},
94+
errorHandler: fakeErrorHandler,
95+
eventDispatcher: fakeEventDispatcher,
96+
logger: silentLogger,
97+
});
98+
// Invalid datafile causes onReady Promise rejection - catch this error
99+
optlyInstance.onReady().catch(function() {});
100+
101+
sinon.assert.notCalled(LocalStoragePendingEventsDispatcher.prototype.sendPendingEvents);
102+
});
103+
});
104+
76105
it('should invoke resendPendingEvents at most once', function() {
77106
var optlyInstance = optimizelyFactory.createInstance({
78107
datafile: {},
79108
errorHandler: fakeErrorHandler,
80-
eventDispatcher: fakeEventDispatcher,
81109
logger: silentLogger,
82110
});
83111
// Invalid datafile causes onReady Promise rejection - catch this error
@@ -88,7 +116,6 @@ describe('javascript-sdk', function() {
88116
optlyInstance = optimizelyFactory.createInstance({
89117
datafile: {},
90118
errorHandler: fakeErrorHandler,
91-
eventDispatcher: fakeEventDispatcher,
92119
logger: silentLogger,
93120
});
94121
optlyInstance.onReady().catch(function() {});

packages/optimizely-sdk/package-lock.json

+33-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/optimizely-sdk/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"@optimizely/js-sdk-logging": "^0.1.0",
3838
"@optimizely/js-sdk-utils": "^0.1.0",
3939
"json-schema": "^0.2.3",
40-
"lodash": "^4.0.0",
40+
"lodash": "^4.17.11",
4141
"murmurhash": "0.0.2",
4242
"promise-polyfill": "8.1.0",
4343
"uuid": "^3.3.2"

0 commit comments

Comments
 (0)