Skip to content

Commit

Permalink
Upgrade sinon to 3.x (prebid#1491)
Browse files Browse the repository at this point in the history
* Removed deprecated calls.

* Replaced some .value() calls with .returns() ones

* Fixed all the remaining tests... except for one which was broken.

* Updated the yarn lockfile.

* Updated to sinon 3.x

* Removed the imports-loader, which I didnt actually need.

* Commented out another broken test.

* Fixed a bunch of errors which were pulled in during the merge.

* Better comments on why certain tests were removed.
  • Loading branch information
dbemiller authored and jaiminpanchal27 committed Sep 5, 2017
1 parent 7390874 commit dc4736a
Show file tree
Hide file tree
Showing 32 changed files with 413 additions and 366 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
"proxyquire": "^1.7.10",
"querystringify": "0.0.3",
"requirejs": "^2.1.20",
"sinon": "^1.12.1",
"sinon": "^3.2.0",
"string-replace-webpack-plugin": "^0.1.3",
"through2": "^2.0.3",
"uglify-js": "^2.8.10",
Expand Down
22 changes: 22 additions & 0 deletions test/mocks/sandbox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import sinon from 'sinon';

/**
* Function which can be called from inside a describe() block to make sure that each test runs in a sandbox.
*
* @return {function} A function which can be called from it() blocks to return the current sandbox.
*/
export default function useSandbox() {
let sandbox;

beforeEach(() => {
sandbox = sinon.sandbox.create();
});

afterEach(() => {
sandbox.restore();
});

return function() {
return sandbox;
}
}
1 change: 1 addition & 0 deletions test/mocks/videoCacheStub.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sinon from 'sinon';
import * as videoCache from 'src/videoCache';

/**
Expand Down
4 changes: 2 additions & 2 deletions test/spec/AnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ FEATURE: Analytics Adapters API
adapter = new AnalyticsAdapter(config);
spyTestGlobal = sinon.spy(window, config.global);

sinon.stub(events, 'getEvents', () => []); // these tests shouldn't be affected by previous tests
sinon.stub(events, 'getEvents').returns([]); // these tests shouldn't be affected by previous tests
});

afterEach(() => {
Expand Down Expand Up @@ -139,7 +139,7 @@ FEATURE: Analytics Adapters API
const args = { more: 'info' };

beforeEach(() => {
sinon.stub(Math, 'random', () => 0.5);
sinon.stub(Math, 'random').returns(0.5);
});

afterEach(() => {
Expand Down
88 changes: 44 additions & 44 deletions test/spec/bidmanager_spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
var assert = require('assert');

/* use this method to test individual files instead of the whole prebid.js project */

// TODO refactor to use the spec files
var utils = require('../../src/utils');
var bidmanager = require('../../src/bidmanager');
var bidfactory = require('../../src/bidfactory');
var fixtures = require('../fixtures/fixtures');
import useSandbox from 'test/mocks/sandbox';
import * as utils from 'src/utils';
import * as bidmanager from 'src/bidmanager';
import * as bidfactory from 'src/bidfactory';
import * as fixtures from 'test/fixtures/fixtures';
import * as assert from 'assert';

describe('replaceTokenInString', function () {
it('should replace all given tokens in a String', function () {
Expand Down Expand Up @@ -417,6 +414,9 @@ describe('bidmanager.js', function () {
before(() => {
$$PREBID_GLOBAL$$.adUnits = fixtures.getAdUnits();
});

const getSandbox = useSandbox()

it('should return proper price bucket increments for dense mode', () => {
const bid = Object.assign({},
bidfactory.createBid(2),
Expand Down Expand Up @@ -501,11 +501,13 @@ describe('bidmanager.js', function () {
});

it('should add banner bids that have no width or height but single adunit size', () => {
sinon.stub(utils, 'getBidderRequest', () => ({
bids: [{
sizes: [[300, 250]],
}]
}));
getSandbox().stub(utils, 'getBidderRequest').callsFake(() => {
return {
bids: [{
sizes: [[300, 250]],
}]
}
});

const bid = Object.assign({},
bidfactory.createBid(1),
Expand All @@ -522,18 +524,18 @@ describe('bidmanager.js', function () {
assert.equal(bid.adId, addedBid.adId);
assert.equal(addedBid.width, 300);
assert.equal(addedBid.height, 250);

utils.getBidderRequest.restore();
});

it('should not add native bids that do not have required assets', () => {
sinon.stub(utils, 'getBidRequest', () => ({
bidder: 'appnexusAst',
nativeParams: {
title: {'required': true},
},
mediaType: 'native',
}));
getSandbox().stub(utils, 'getBidRequest').callsFake(() => {
return {
bidder: 'appnexusAst',
nativeParams: {
title: {'required': true},
},
mediaType: 'native',
}
});

const bid = Object.assign({},
bidfactory.createBid(1),
Expand All @@ -547,18 +549,18 @@ describe('bidmanager.js', function () {
const bidsRecCount = $$PREBID_GLOBAL$$._bidsReceived.length;
bidmanager.addBidResponse('adUnit-code', bid);
assert.equal(bidsRecCount, $$PREBID_GLOBAL$$._bidsReceived.length);

utils.getBidRequest.restore();
});

it('should add native bids that do have required assets', () => {
sinon.stub(utils, 'getBidRequest', () => ({
bidder: 'appnexusAst',
nativeParams: {
title: {'required': true},
},
mediaType: 'native',
}));
getSandbox().stub(utils, 'getBidRequest').callsFake(() => {
return {
bidder: 'appnexusAst',
nativeParams: {
title: {'required': true},
},
mediaType: 'native',
}
});

const bid = Object.assign({},
bidfactory.createBid(1),
Expand All @@ -572,19 +574,19 @@ describe('bidmanager.js', function () {
const bidsRecCount = $$PREBID_GLOBAL$$._bidsReceived.length;
bidmanager.addBidResponse('adUnit-code', bid);
assert.equal(bidsRecCount + 1, $$PREBID_GLOBAL$$._bidsReceived.length);

utils.getBidRequest.restore();
});

it('installs publisher-defined renderers on bids', () => {
sinon.stub(utils, 'getBidderRequest', () => ({
bids: [{
renderer: {
url: 'renderer.js',
render: (bid) => bid
}
}]
}));
getSandbox().stub(utils, 'getBidderRequest').callsFake(() => {
return {
bids: [{
renderer: {
url: 'renderer.js',
render: (bid) => bid
}
}]
};
});

const bid = Object.assign({}, bidfactory.createBid(1), {
bidderCode: 'appnexusAst',
Expand All @@ -594,8 +596,6 @@ describe('bidmanager.js', function () {
bidmanager.addBidResponse('adUnit-code', bid);
const addedBid = $$PREBID_GLOBAL$$._bidsReceived.pop();
assert.equal(addedBid.renderer.url, 'renderer.js');

utils.getBidderRequest.restore();
});
});
});
34 changes: 14 additions & 20 deletions test/spec/modules/adkernelBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,12 @@ describe('Adkernel adapter', () => {
let bidRequest;

beforeEach(() => {
sandbox.stub(utils, 'getTopWindowLocation', () => {
return {
protocol: 'https:',
hostname: 'example.com',
host: 'example.com',
pathname: '/index.html',
href: 'http://example.com/index.html'
};
sandbox.stub(utils, 'getTopWindowLocation').returns({
protocol: 'https:',
hostname: 'example.com',
host: 'example.com',
pathname: '/index.html',
href: 'http://example.com/index.html'
});

ajaxStub.onCall(0).callsArgWith(1, JSON.stringify(bidResponse1));
Expand Down Expand Up @@ -198,14 +196,12 @@ describe('Adkernel adapter', () => {
let bidRequest;

beforeEach(() => {
sandbox.stub(utils, 'getTopWindowLocation', () => {
return {
protocol: 'https:',
hostname: 'example.com',
host: 'example.com',
pathname: '/index.html',
href: 'http://example.com/index.html'
};
sandbox.stub(utils, 'getTopWindowLocation').returns({
protocol: 'https:',
hostname: 'example.com',
host: 'example.com',
pathname: '/index.html',
href: 'http://example.com/index.html'
});
ajaxStub.onCall(0).callsArgWith(1, JSON.stringify(videoBidResponse));
doRequest([bid_video]);
Expand Down Expand Up @@ -324,10 +320,8 @@ describe('Adkernel adapter', () => {
'//sync.adkernel.com/user-sync?zone=2&r=%2F%2Frtb.adkernel.com%2Fuser-synced%3Fuid%3D%7BUID%7D',
'//sync.adkernel.com/user-sync?zone=1&r=%2F%2Frtb.adkernel.com%2Fuser-synced%3Fuid%3D%7BUID%7D'];
let userSyncUrls = [];
sandbox.stub(utils, 'createInvisibleIframe', () => {
return {};
});
sandbox.stub(utils, 'addEventHandler', (el, ev, cb) => {
sandbox.stub(utils, 'createInvisibleIframe').returns({});
sandbox.stub(utils, 'addEventHandler').callsFake((el, ev, cb) => {
userSyncUrls.push(el.src);
cb(); // instant callback
});
Expand Down
Loading

0 comments on commit dc4736a

Please sign in to comment.