Skip to content

fix: Merge some bug fixes from master #349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions packages/optimizely-sdk/lib/index.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
33 changes: 30 additions & 3 deletions packages/optimizely-sdk/lib/index.browser.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('javascript-sdk', function() {
requests.push(req);
};

sinon.spy(LocalStoragePendingEventsDispatcher.prototype, 'sendPendingEvents');
sinon.stub(LocalStoragePendingEventsDispatcher.prototype, 'sendPendingEvents');
});

afterEach(function() {
Expand All @@ -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
Expand All @@ -88,7 +116,6 @@ describe('javascript-sdk', function() {
optlyInstance = optimizelyFactory.createInstance({
datafile: {},
errorHandler: fakeErrorHandler,
eventDispatcher: fakeEventDispatcher,
logger: silentLogger,
});
optlyInstance.onReady().catch(function() {});
Expand Down
45 changes: 33 additions & 12 deletions packages/optimizely-sdk/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/optimizely-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"@optimizely/js-sdk-logging": "^0.1.0",
"@optimizely/js-sdk-utils": "^0.1.0",
"json-schema": "^0.2.3",
"lodash": "^4.0.0",
"lodash": "^4.17.11",
"murmurhash": "0.0.2",
"promise-polyfill": "8.1.0",
"uuid": "^3.3.2"
Expand Down