Skip to content

Commit 6b2c20d

Browse files
authored
🏗 ✅ Isolate tests by eliminating the use of the global `sinon… (ampproject#25587)
1 parent 153dcba commit 6b2c20d

File tree

348 files changed

+8720
-8771
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

348 files changed

+8720
-8771
lines changed

‎.eslintrc‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,6 @@
210210
"before": false,
211211
"after": false,
212212
"assert": false,
213-
"sinon": true,
214-
"sandbox": true,
215213
"describes": true,
216214
"allowConsoleError": false,
217215
"expectAsyncConsoleError": false,

‎ads/google/a4a/test/test-line-delimited-response-handler.js‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
describe('#line-delimited-response-handler', () => {
2323
let chunkHandlerStub;
2424
let slotData;
25-
let sandbox;
2625
let win;
2726
let response;
2827

@@ -93,12 +92,7 @@ describe('#line-delimited-response-handler', () => {
9392
}
9493

9594
beforeEach(() => {
96-
sandbox = sinon.sandbox;
97-
chunkHandlerStub = sandbox.stub();
98-
});
99-
100-
afterEach(() => {
101-
sandbox.restore();
95+
chunkHandlerStub = window.sandbox.stub();
10296
});
10397

10498
describe('stream not supported', () => {
@@ -143,7 +137,6 @@ describe('#line-delimited-response-handler', () => {
143137

144138
describe('streaming', () => {
145139
let readStub;
146-
let sandbox;
147140

148141
function setup() {
149142
const responseString = generateResponseFormat();
@@ -161,8 +154,7 @@ describe('#line-delimited-response-handler', () => {
161154
}
162155

163156
beforeEach(() => {
164-
sandbox = sinon.sandbox;
165-
readStub = sandbox.stub();
157+
readStub = window.sandbox.stub();
166158
response = {
167159
text: () => Promise.resolve(),
168160
body: {
@@ -178,10 +170,6 @@ describe('#line-delimited-response-handler', () => {
178170
};
179171
});
180172

181-
afterEach(() => {
182-
sandbox.restore();
183-
});
184-
185173
// TODO(lannka, #15748): Fails on Safari 11.1.0.
186174
it.configure().skipSafari(
187175
'should handle empty streamed response properly',

‎ads/google/a4a/test/test-utils.js‎

Lines changed: 31 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,6 @@ describe('Google A4A utils', () => {
112112
});
113113

114114
describe('#ActiveView AmpAnalytics integration', () => {
115-
let sandbox;
116-
117-
beforeEach(() => {
118-
sandbox = sinon.sandbox;
119-
});
120-
121-
afterEach(() => {
122-
sandbox.restore();
123-
});
124-
125115
const builtConfig = {
126116
transport: {beacon: false, xhrpost: false},
127117
requests: {
@@ -194,7 +184,9 @@ describe('Google A4A utils', () => {
194184
});
195185

196186
it('should add the correct CSI signals', () => {
197-
sandbox.stub(Services, 'documentInfoForDoc').returns({pageViewId: 777});
187+
window.sandbox
188+
.stub(Services, 'documentInfoForDoc')
189+
.returns({pageViewId: 777});
198190
const mockElement = {
199191
getAttribute: function(name) {
200192
switch (name) {
@@ -315,16 +307,6 @@ describe('Google A4A utils', () => {
315307
});
316308

317309
describe('#googleAdUrl', () => {
318-
let sandbox;
319-
320-
beforeEach(() => {
321-
sandbox = sinon.sandbox;
322-
});
323-
324-
afterEach(() => {
325-
sandbox.restore();
326-
});
327-
328310
it('should set ad position', function() {
329311
// When ran locally, this test tends to exceed 2000ms timeout.
330312
this.timeout(5000);
@@ -338,7 +320,7 @@ describe('Google A4A utils', () => {
338320
'height': '50',
339321
});
340322
const impl = new MockA4AImpl(elem);
341-
noopMethods(impl, fixture.ampdoc, sandbox);
323+
noopMethods(impl, fixture.ampdoc, window.sandbox);
342324
return fixture.addElement(elem).then(() => {
343325
return googleAdUrl(impl, '', 0, [], []).then(url1 => {
344326
expect(url1).to.match(/ady=11/);
@@ -359,7 +341,7 @@ describe('Google A4A utils', () => {
359341
'height': '50',
360342
});
361343
const impl = new MockA4AImpl(elem);
362-
noopMethods(impl, fixture.ampdoc, sandbox);
344+
noopMethods(impl, fixture.ampdoc, window.sandbox);
363345
const getRect = () => {
364346
return {'width': 100, 'height': 200};
365347
};
@@ -368,7 +350,7 @@ describe('Google A4A utils', () => {
368350
};
369351
const getScrollLeft = () => 12;
370352
const getScrollTop = () => 34;
371-
const viewportStub = sandbox.stub(Services, 'viewportForDoc');
353+
const viewportStub = window.sandbox.stub(Services, 'viewportForDoc');
372354
viewportStub.returns({getRect, getSize, getScrollTop, getScrollLeft});
373355
return fixture.addElement(elem).then(() => {
374356
return googleAdUrl(impl, '', 0, {}, []).then(url1 => {
@@ -392,7 +374,7 @@ describe('Google A4A utils', () => {
392374
'data-experiment-id': '123,456',
393375
});
394376
const impl = new MockA4AImpl(elem);
395-
noopMethods(impl, fixture.ampdoc, sandbox);
377+
noopMethods(impl, fixture.ampdoc, window.sandbox);
396378
return fixture.addElement(elem).then(() => {
397379
return googleAdUrl(impl, '', 0, {}, ['789', '098']).then(url1 => {
398380
expect(url1).to.match(/eid=123%2C456%2C789%2C098/);
@@ -412,7 +394,7 @@ describe('Google A4A utils', () => {
412394
'height': '50',
413395
});
414396
const impl = new MockA4AImpl(elem);
415-
noopMethods(impl, fixture.ampdoc, sandbox);
397+
noopMethods(impl, fixture.ampdoc, window.sandbox);
416398
impl.win.AMP_CONFIG = {type: 'production'};
417399
impl.win.location.hash = 'foo,deid=123456,654321,bar';
418400
return fixture.addElement(elem).then(() => {
@@ -434,7 +416,7 @@ describe('Google A4A utils', () => {
434416
'height': '50',
435417
});
436418
const impl = new MockA4AImpl(elem);
437-
noopMethods(impl, fixture.ampdoc, sandbox);
419+
noopMethods(impl, fixture.ampdoc, window.sandbox);
438420
impl.win.gaGlobal = {cid: 'foo', hid: 'bar'};
439421
return fixture.addElement(elem).then(() => {
440422
return googleAdUrl(impl, '', 0, [], []).then(url => {
@@ -456,8 +438,8 @@ describe('Google A4A utils', () => {
456438
'height': '50',
457439
});
458440
const impl = new MockA4AImpl(elem);
459-
noopMethods(impl, fixture.ampdoc, sandbox);
460-
const createElementStub = sandbox.stub(
441+
noopMethods(impl, fixture.ampdoc, window.sandbox);
442+
const createElementStub = window.sandbox.stub(
461443
impl.win.document,
462444
'createElement'
463445
);
@@ -485,8 +467,8 @@ describe('Google A4A utils', () => {
485467
'height': '50',
486468
});
487469
const impl = new MockA4AImpl(elem);
488-
noopMethods(impl, fixture.ampdoc, sandbox);
489-
const createElementStub = sandbox.stub(
470+
noopMethods(impl, fixture.ampdoc, window.sandbox);
471+
const createElementStub = window.sandbox.stub(
490472
impl.win.document,
491473
'createElement'
492474
);
@@ -512,9 +494,9 @@ describe('Google A4A utils', () => {
512494
'height': '50',
513495
});
514496
const impl = new MockA4AImpl(elem);
515-
noopMethods(impl, fixture.ampdoc, sandbox);
497+
noopMethods(impl, fixture.ampdoc, window.sandbox);
516498
impl.win.SVGElement = undefined;
517-
const createElementStub = sandbox.stub(
499+
const createElementStub = window.sandbox.stub(
518500
impl.win.document,
519501
'createElement'
520502
);
@@ -542,11 +524,11 @@ describe('Google A4A utils', () => {
542524
'height': '50',
543525
});
544526
const impl = new MockA4AImpl(elem);
545-
noopMethods(impl, fixture.ampdoc, sandbox);
546-
sandbox
527+
noopMethods(impl, fixture.ampdoc, window.sandbox);
528+
window.sandbox
547529
.stub(Services.viewerForDoc(impl.getAmpDoc()), 'getReferrerUrl')
548530
.returns(new Promise(() => {}));
549-
const createElementStub = sandbox.stub(
531+
const createElementStub = window.sandbox.stub(
550532
impl.win.document,
551533
'createElement'
552534
);
@@ -571,7 +553,7 @@ describe('Google A4A utils', () => {
571553
doc.win = fixture.win;
572554
const elem = createElementWithAttributes(doc, 'amp-a4a', {});
573555
const impl = new MockA4AImpl(elem);
574-
noopMethods(impl, fixture.ampdoc, sandbox);
556+
noopMethods(impl, fixture.ampdoc, window.sandbox);
575557
return fixture.addElement(elem).then(() => {
576558
return googleAdUrl(impl, '', Date.now(), [], []).then(url => {
577559
expect(url).to.match(/[&?]bdt=[1-9][0-9]*[&$]/);
@@ -663,11 +645,11 @@ describe('Google A4A utils', () => {
663645
});
664646
});
665647

666-
describes.fakeWin('#getIdentityTokenRequestUrl', {}, () => {
648+
describes.fakeWin('#getIdentityTokenRequestUrl', {}, env => {
667649
let doc;
668650
let fakeWin;
669651
beforeEach(() => {
670-
const documentInfoStub = sandbox.stub(Services, 'documentInfoForDoc');
652+
const documentInfoStub = env.sandbox.stub(Services, 'documentInfoForDoc');
671653
doc = {};
672654
fakeWin = {location: {}};
673655
documentInfoStub
@@ -719,7 +701,7 @@ describe('Google A4A utils', () => {
719701
describes.fakeWin('#getIdentityToken', {amp: true, mockFetch: true}, env => {
720702
beforeEach(() => {
721703
installXhrService(env.win);
722-
const documentInfoStub = sandbox.stub(Services, 'documentInfoForDoc');
704+
const documentInfoStub = env.sandbox.stub(Services, 'documentInfoForDoc');
723705
documentInfoStub
724706
.withArgs(env.ampdoc)
725707
.returns({canonicalUrl: 'http://f.blah.com?some_site'});
@@ -818,7 +800,7 @@ describe('Google A4A utils', () => {
818800
});
819801

820802
it('should handle fetch error', () => {
821-
sandbox
803+
env.sandbox
822804
.stub(Services, 'xhrFor')
823805
.returns({fetchJson: () => Promise.reject('some network failure')});
824806
return getIdentityToken(env.win, env.ampdoc).then(result =>
@@ -837,7 +819,7 @@ describe('Google A4A utils', () => {
837819
validLifetimeSecs: '5678',
838820
})
839821
);
840-
sandbox.stub(Services, 'consentPolicyServiceForDocOrNull').returns(
822+
env.sandbox.stub(Services, 'consentPolicyServiceForDocOrNull').returns(
841823
Promise.resolve({
842824
whenPolicyResolved: () => CONSENT_POLICY_STATE.SUFFICIENT,
843825
})
@@ -850,7 +832,7 @@ describe('Google A4A utils', () => {
850832
it.configure()
851833
.skipFirefox()
852834
.run('should not fetch if INSUFFICIENT consent', () => {
853-
sandbox.stub(Services, 'consentPolicyServiceForDocOrNull').returns(
835+
env.sandbox.stub(Services, 'consentPolicyServiceForDocOrNull').returns(
854836
Promise.resolve({
855837
whenPolicyResolved: () => CONSENT_POLICY_STATE.INSUFFICIENT,
856838
})
@@ -863,7 +845,7 @@ describe('Google A4A utils', () => {
863845
it.configure()
864846
.skipFirefox()
865847
.run('should not fetch if UNKNOWN consent', () => {
866-
sandbox.stub(Services, 'consentPolicyServiceForDocOrNull').returns(
848+
env.sandbox.stub(Services, 'consentPolicyServiceForDocOrNull').returns(
867849
Promise.resolve({
868850
whenPolicyResolved: () => CONSENT_POLICY_STATE.UNKNOWN,
869851
})
@@ -876,11 +858,9 @@ describe('Google A4A utils', () => {
876858

877859
describe('variables for amp-analytics', () => {
878860
let a4a;
879-
let sandbox;
880861
let ampdoc;
881862

882863
beforeEach(() => {
883-
sandbox = sinon.sandbox;
884864
return createIframePromise().then(fixture => {
885865
setupForAdTesting(fixture);
886866
const element = createElementWithAttributes(fixture.doc, 'amp-a4a', {
@@ -895,10 +875,6 @@ describe('Google A4A utils', () => {
895875
});
896876
});
897877

898-
afterEach(() => {
899-
sandbox.restore();
900-
});
901-
902878
it('should include the correlator', () => {
903879
const vars = getCsiAmpAnalyticsVariables('trigger', a4a, null);
904880
expect(vars['correlator']).not.to.be.undefined;
@@ -934,7 +910,7 @@ describe('Google A4A utils', () => {
934910
});
935911

936912
it('should include viewer lastVisibleTime', () => {
937-
sandbox.stub(ampdoc, 'getLastVisibleTime').returns(300);
913+
window.sandbox.stub(ampdoc, 'getLastVisibleTime').returns(300);
938914

939915
const vars = getCsiAmpAnalyticsVariables('trigger', a4a, null);
940916
expect(vars['viewerLastVisibleTime']).to.be.a('number');
@@ -983,7 +959,7 @@ describe('Google A4A utils', () => {
983959

984960
it('should calculate correlator from PVID and CID if possible', () => {
985961
const pageViewId = '818181';
986-
sandbox.stub(Services, 'documentInfoForDoc').callsFake(() => {
962+
env.sandbox.stub(Services, 'documentInfoForDoc').callsFake(() => {
987963
return {pageViewId};
988964
});
989965
const cid = '12345678910';
@@ -1021,7 +997,7 @@ describes.realWin('#groupAmpAdsByType', {amp: true}, env => {
1021997
createResource({type: 'blah'}),
1022998
createResource({}, 'amp-foo'),
1023999
];
1024-
sandbox
1000+
env.sandbox
10251001
.stub(IniLoad, 'getMeasuredResources')
10261002
.callsFake((doc, win, fn) => Promise.resolve(resources.filter(fn)));
10271003
return groupAmpAdsByType(ampdoc, 'doubleclick', () => 'foo').then(
@@ -1051,7 +1027,7 @@ describes.realWin('#groupAmpAdsByType', {amp: true}, env => {
10511027
stickyResource.element
10521028
);
10531029
ampAdResource.element.createdCallback = true;
1054-
sandbox
1030+
env.sandbox
10551031
.stub(IniLoad, 'getMeasuredResources')
10561032
.callsFake((doc, win, fn) => Promise.resolve(resources.filter(fn)));
10571033
return groupAmpAdsByType(win, 'doubleclick', () => 'foo').then(result => {
@@ -1082,7 +1058,7 @@ describes.realWin('#groupAmpAdsByType', {amp: true}, env => {
10821058
stickyResource.element
10831059
);
10841060
ampAdResource.element.createdCallback = true;
1085-
sandbox
1061+
env.sandbox
10861062
.stub(IniLoad, 'getMeasuredResources')
10871063
.callsFake((doc, win, fn) => Promise.resolve(resources.filter(fn)));
10881064
return groupAmpAdsByType(win, 'doubleclick', element =>

‎extensions/amp-3d-gltf/0.1/test/test-amp-3d-gltf.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describes.realWin(
6060
await amp3dGltfEl.build();
6161

6262
const amp3dGltf = amp3dGltfEl.implementation_;
63-
sandbox
63+
env.sandbox
6464
.stub(amp3dGltf, 'iframe_')
6565
.get(() => iframe)
6666
.set(() => {});
@@ -84,7 +84,7 @@ describes.realWin(
8484
it.skip('sends toggleAmpViewport(false) when exiting viewport', async () => {
8585
const amp3dGltf = await createElement();
8686

87-
const postMessageSpy = sandbox.spy(amp3dGltf, 'postMessage_');
87+
const postMessageSpy = env.sandbox.spy(amp3dGltf, 'postMessage_');
8888
await amp3dGltf.viewportCallback(false);
8989
expect(postMessageSpy.calledOnce).to.be.true;
9090
expect(postMessageSpy.firstCall.args[0]).to.equal('action');
@@ -97,7 +97,7 @@ describes.realWin(
9797
// TODO (#16080): this test times out on Travis. Re-enable when fixed.
9898
it.skip('sends toggleAmpViewport(true) when entering viewport', async () => {
9999
const amp3dGltf = await createElement();
100-
const postMessageSpy = sandbox.spy(amp3dGltf, 'postMessage_');
100+
const postMessageSpy = env.sandbox.spy(amp3dGltf, 'postMessage_');
101101
await amp3dGltf.viewportCallback(true);
102102
expect(postMessageSpy.calledOnce).to.be.true;
103103
expect(postMessageSpy.firstCall.args[0]).to.equal('action');

0 commit comments

Comments
 (0)