Skip to content

Commit 95cd8a0

Browse files
authored
Fix test stubbings of global XHR object (#489)
Summary: Fixes cross-test pollution of global XHR object. The global XHR object was being stubbed in some tests but not others. But the tests that didn't stub weren't failing because the tests that _were_ stubbing weren't being cleaned up! So some test suites were depending (implicitly) on side effects from other test files. Issues: partially fixes #488
1 parent 7b3750a commit 95cd8a0

File tree

4 files changed

+13
-42
lines changed

4 files changed

+13
-42
lines changed

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ var LocalStoragePendingEventsDispatcher = eventProcessor.LocalStoragePendingEven
2929

3030
describe('javascript-sdk', function() {
3131
describe('APIs', function() {
32-
var xhr;
33-
var requests;
34-
3532
it('should expose logger, errorHandler, eventDispatcher and enums', function() {
3633
assert.isDefined(optimizelyFactory.logging);
3734
assert.isDefined(optimizelyFactory.logging.createLogger);
@@ -54,12 +51,7 @@ describe('javascript-sdk', function() {
5451
sinon.spy(console, 'error');
5552
sinon.stub(configValidator, 'validate');
5653

57-
xhr = sinon.useFakeXMLHttpRequest();
58-
global.XMLHttpRequest = xhr;
59-
requests = [];
60-
xhr.onCreate = function(req) {
61-
requests.push(req);
62-
};
54+
global.XMLHttpRequest = sinon.useFakeXMLHttpRequest();
6355

6456
sinon.stub(LocalStoragePendingEventsDispatcher.prototype, 'sendPendingEvents');
6557
});
@@ -69,7 +61,7 @@ describe('javascript-sdk', function() {
6961
optimizelyFactory.__internalResetRetryState();
7062
console.error.restore();
7163
configValidator.validate.restore();
72-
xhr.restore();
64+
delete global.XMLHttpRequest
7365
});
7466

7567
describe('when an eventDispatcher is not passed in', function() {

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import eventDispatcher from './plugins/event_dispatcher/index.browser';
2626

2727
describe('javascript-sdk', function() {
2828
describe('APIs', function() {
29-
var xhr;
30-
var requests;
3129
describe('createInstance', function() {
3230
var fakeErrorHandler = { handleError: function() {} };
3331
var fakeEventDispatcher = { dispatchEvent: function() {} };
@@ -41,12 +39,7 @@ describe('javascript-sdk', function() {
4139
sinon.stub(configValidator, 'validate');
4240
sinon.stub(Optimizely.prototype, 'close');
4341

44-
xhr = sinon.useFakeXMLHttpRequest();
45-
global.XMLHttpRequest = xhr;
46-
requests = [];
47-
xhr.onCreate = function(req) {
48-
requests.push(req);
49-
};
42+
global.XMLHttpRequest = sinon.useFakeXMLHttpRequest();
5043

5144
sinon.spy(console, 'log');
5245
sinon.spy(console, 'info');
@@ -64,7 +57,7 @@ describe('javascript-sdk', function() {
6457
window.addEventListener.restore();
6558
configValidator.validate.restore();
6659
Optimizely.prototype.close.restore();
67-
xhr.restore();
60+
delete global.XMLHttpRequest
6861
});
6962

7063
// this test has to come first due to local state of the logLevel

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ import eventProcessorConfigValidator from './utils/event_processor_config_valida
2828

2929
describe('javascript-sdk/react-native', function() {
3030
describe('APIs', function() {
31-
var xhr;
32-
var requests;
33-
3431
it('should expose logger, errorHandler, eventDispatcher and enums', function() {
3532
assert.isDefined(optimizelyFactory.logging);
3633
assert.isDefined(optimizelyFactory.logging.createLogger);
@@ -52,18 +49,11 @@ describe('javascript-sdk/react-native', function() {
5249
});
5350
sinon.spy(console, 'error');
5451
sinon.stub(configValidator, 'validate');
55-
56-
xhr = sinon.useFakeXMLHttpRequest();
57-
requests = [];
58-
xhr.onCreate = function(req) {
59-
requests.push(req);
60-
};
6152
});
6253

6354
afterEach(function() {
6455
console.error.restore();
6556
configValidator.validate.restore();
66-
xhr.restore();
6757
});
6858

6959
it('should not throw if the provided config is not valid', function() {

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,14 @@ import { dispatchEvent } from './index.browser';
2121
describe('lib/plugins/event_dispatcher/browser', function() {
2222
describe('APIs', function() {
2323
describe('dispatchEvent', function() {
24-
var xhr;
25-
var requests;
2624
beforeEach(function() {
27-
xhr = sinon.useFakeXMLHttpRequest();
28-
requests = [];
29-
xhr.onCreate = function(req) {
30-
requests.push(req);
31-
};
25+
this.requests = [];
26+
global.XMLHttpRequest = sinon.useFakeXMLHttpRequest();
27+
global.XMLHttpRequest.onCreate = (req) => { this.requests.push(req); };
3228
});
3329

3430
afterEach(function() {
35-
xhr.restore();
31+
delete global.XMLHttpRequest
3632
});
3733

3834
it('should send a POST request with the specified params', function(done) {
@@ -48,9 +44,9 @@ describe('lib/plugins/event_dispatcher/browser', function() {
4844

4945
var callback = sinon.spy();
5046
dispatchEvent(eventObj, callback);
51-
assert.strictEqual(1, requests.length);
52-
assert.strictEqual(requests[0].method, 'POST');
53-
assert.strictEqual(requests[0].requestBody, JSON.stringify(eventParams));
47+
assert.strictEqual(1, this.requests.length);
48+
assert.strictEqual(this.requests[0].method, 'POST');
49+
assert.strictEqual(this.requests[0].requestBody, JSON.stringify(eventParams));
5450
done();
5551
});
5652

@@ -67,7 +63,7 @@ describe('lib/plugins/event_dispatcher/browser', function() {
6763

6864
var callback = sinon.spy();
6965
dispatchEvent(eventObj, callback);
70-
requests[0].respond([
66+
this.requests[0].respond([
7167
200,
7268
{},
7369
'{"url":"https://cdn.com/event","body":{"id":123},"httpVerb":"POST","params":{"testParam":"testParamValue"}}',
@@ -84,7 +80,7 @@ describe('lib/plugins/event_dispatcher/browser', function() {
8480

8581
var callback = sinon.spy();
8682
dispatchEvent(eventObj, callback);
87-
requests[0].respond([200, {}, '{"url":"https://cdn.com/event","httpVerb":"GET"']);
83+
this.requests[0].respond([200, {}, '{"url":"https://cdn.com/event","httpVerb":"GET"']);
8884
sinon.assert.calledOnce(callback);
8985
done();
9086
});

0 commit comments

Comments
 (0)