From d746d0114dfc843a24fc93493fcdea04efbe25d3 Mon Sep 17 00:00:00 2001 From: binoy Date: Fri, 25 May 2018 17:16:46 +0530 Subject: [PATCH 1/4] Media.net adapter: passing slot visibility to server --- modules/medianetBidAdapter.js | 76 +++++++ test/spec/modules/medianetBidAdapter_spec.js | 206 ++++++++++++++++++- 2 files changed, 274 insertions(+), 8 deletions(-) diff --git a/modules/medianetBidAdapter.js b/modules/medianetBidAdapter.js index 08232231417..2317c79685d 100644 --- a/modules/medianetBidAdapter.js +++ b/modules/medianetBidAdapter.js @@ -4,6 +4,11 @@ import { config } from 'src/config'; const BIDDER_CODE = 'medianet'; const BID_URL = '//prebid.media.net/rtb/prebid'; +const SLOT_VISIBILITY = { + NOT_DETERMINED: 0, + ABOVE_THE_FOLD: 1, + BELOW_THE_FOLD: 2 +}; $$PREBID_GLOBAL$$.medianetGlobals = {}; @@ -71,6 +76,31 @@ function getSize(size) { } } +function getWindowSize() { + return { + w: window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || -1, + h: window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || -1 + } +} + +function getCoordinates(id) { + const element = document.getElementById(id); + if (element && element.getBoundingClientRect) { + const rect = element.getBoundingClientRect(); + let coordinates = {}; + coordinates.top_left = { + y: rect.top, + x: rect.left, + }; + coordinates.bottom_right = { + y: rect.bottom, + x: rect.right, + }; + return coordinates + } + return null; +} + function extParams(params, gdpr) { let ext = { customer_id: params.cid, @@ -80,6 +110,10 @@ function extParams(params, gdpr) { if (ext.gdpr_applies) { ext.gdpr_consent_string = gdpr.consentString || ''; } + let windowSize = getWindowSize(); + if (windowSize.w !== -1 && windowSize.h !== -1) { + ext.screen = windowSize; + } return ext; } @@ -102,9 +136,51 @@ function slotParams(bidRequest) { if (bidFloor) { params.bidfloor = bidFloor; } + const coordinates = getCoordinates(bidRequest.adUnitCode); + if (coordinates) { + params.ext.coordinates = coordinates; + let viewability = getSlotVisibility(coordinates.top_left, getMinSize(params.banner)); + params.ext.viewability = viewability; + if (viewability > 0.5) { + params.ext.visibility = SLOT_VISIBILITY.ABOVE_THE_FOLD; + } else { + params.ext.visibility = SLOT_VISIBILITY.BELOW_THE_FOLD; + } + } else { + params.ext.visibility = SLOT_VISIBILITY.NOT_DETERMINED; + } + return params; } +function getMinSize(sizes) { + return sizes.reduce((min, size) => size.h * size.w < min.h * min.w ? size : min); +} + +function getSlotVisibility(topLeft, size) { + let maxArea = size.w * size.h; + let windowSize = getWindowSize(); + let bottomRight = { + x: topLeft.x + size.w, + y: topLeft.y + size.h + }; + if (maxArea === 0 || windowSize.w === -1 || windowSize.h === -1) { + return 0; + } + + return getOverlapArea(topLeft, bottomRight, {x: 0, y: 0}, {x: windowSize.w, y: windowSize.h}) / maxArea; +} + +// find the overlapping area between two rectangles +function getOverlapArea(topLeft1, bottomRight1, topLeft2, bottomRight2) { + // If no overlap, return 0 + if ((topLeft1.x > bottomRight2.x || bottomRight1.x < topLeft2.x) || (topLeft1.y > bottomRight2.y || bottomRight1.y < topLeft2.y)) { + return 0; + } + // return overlapping area : [ min of rightmost/bottommost co-ordinates ] - [ max of leftmost/topmost co-ordinates ] + return ((Math.min(bottomRight1.x, bottomRight2.x) - Math.max(topLeft1.x, topLeft2.x)) * (Math.min(bottomRight1.y, bottomRight2.y) - Math.max(topLeft1.y, topLeft2.y))); +} + function generatePayload(bidRequests, bidderRequests) { return { site: siteDetails(bidRequests[0].params.site), diff --git a/test/spec/modules/medianetBidAdapter_spec.js b/test/spec/modules/medianetBidAdapter_spec.js index a10dcb2624d..2f57e2a4c6f 100644 --- a/test/spec/modules/medianetBidAdapter_spec.js +++ b/test/spec/modules/medianetBidAdapter_spec.js @@ -82,12 +82,28 @@ let VALID_BID_REQUEST = [{ 'customer_id': 'customer_id', 'prebid_version': $$PREBID_GLOBAL$$.version, 'gdpr_applies': false, + 'screen': { + 'w': 1000, + 'h': 1000 + } }, 'id': 'aafabfd0-28c0-4ac0-aa09-99689e88b81d', 'imp': [{ 'id': '28f8f8130a583e', 'ext': { - 'dfp_id': 'div-gpt-ad-1460505748561-0' + 'dfp_id': 'div-gpt-ad-1460505748561-0', + 'visibility': 1, + 'viewability': 1, + 'coordinates': { + 'top_left': { + x: 50, + y: 50 + }, + 'bottom_right': { + x: 100, + y: 100 + } + } }, 'banner': [{ 'w': 300, @@ -105,7 +121,19 @@ let VALID_BID_REQUEST = [{ }, { 'id': '3f97ca71b1e5c2', 'ext': { - 'dfp_id': 'div-gpt-ad-1460505748561-123' + 'dfp_id': 'div-gpt-ad-1460505748561-123', + 'visibility': 1, + 'viewability': 1, + 'coordinates': { + 'top_left': { + x: 50, + y: 50 + }, + 'bottom_right': { + x: 100, + y: 100 + } + } }, 'banner': [{ 'w': 300, @@ -131,13 +159,29 @@ let VALID_BID_REQUEST = [{ 'ext': { 'customer_id': 'customer_id', 'prebid_version': $$PREBID_GLOBAL$$.version, - 'gdpr_applies': false + 'gdpr_applies': false, + 'screen': { + 'w': 1000, + 'h': 1000 + } }, 'id': 'aafabfd0-28c0-4ac0-aa09-99689e88b81d', 'imp': [{ 'id': '28f8f8130a583e', 'ext': { - 'dfp_id': 'div-gpt-ad-1460505748561-0' + 'dfp_id': 'div-gpt-ad-1460505748561-0', + 'visibility': 1, + 'viewability': 1, + 'coordinates': { + 'top_left': { + x: 50, + y: 50 + }, + 'bottom_right': { + x: 100, + y: 100 + } + } }, 'banner': [{ 'w': 300, @@ -154,7 +198,19 @@ let VALID_BID_REQUEST = [{ }, { 'id': '3f97ca71b1e5c2', 'ext': { - 'dfp_id': 'div-gpt-ad-1460505748561-123' + 'dfp_id': 'div-gpt-ad-1460505748561-123', + 'visibility': 1, + 'viewability': 1, + 'coordinates': { + 'top_left': { + x: 50, + y: 50 + }, + 'bottom_right': { + x: 100, + y: 100 + } + } }, 'banner': [{ 'w': 300, @@ -339,7 +395,7 @@ let VALID_BID_REQUEST = [{ 'consentString': 'consentString', 'gdprApplies': true, }, - 'timeout': 3000, + 'timeout': 3000 }, VALID_PAYLOAD_FOR_GDPR = { 'site': { @@ -352,12 +408,28 @@ let VALID_BID_REQUEST = [{ 'prebid_version': $$PREBID_GLOBAL$$.version, 'gdpr_consent_string': 'consentString', 'gdpr_applies': true, + 'screen': { + 'w': 1000, + 'h': 1000 + } }, 'id': 'aafabfd0-28c0-4ac0-aa09-99689e88b81d', 'imp': [{ 'id': '28f8f8130a583e', 'ext': { - 'dfp_id': 'div-gpt-ad-1460505748561-0' + 'dfp_id': 'div-gpt-ad-1460505748561-0', + 'visibility': 1, + 'viewability': 1, + 'coordinates': { + 'top_left': { + x: 50, + y: 50 + }, + 'bottom_right': { + x: 100, + y: 100 + } + } }, 'banner': [{ 'w': 300, @@ -374,7 +446,19 @@ let VALID_BID_REQUEST = [{ }, { 'id': '3f97ca71b1e5c2', 'ext': { - 'dfp_id': 'div-gpt-ad-1460505748561-123' + 'dfp_id': 'div-gpt-ad-1460505748561-123', + 'visibility': 1, + 'viewability': 1, + 'coordinates': { + 'top_left': { + x: 50, + y: 50 + }, + 'bottom_right': { + x: 100, + y: 100 + } + } }, 'banner': [{ 'w': 300, @@ -416,6 +500,31 @@ describe('Media.net bid adapter', () => { }); describe('buildRequests', () => { + let sandbox; + let mock; + before(() => { + sandbox = sinon.sandbox.create(); + mock = sinon.mock(window); + window.innerWidth = 1000; + window.innerHeight = 1000; + let documentStub = sandbox.stub(document, 'getElementById'); + let boundingRect = { + top: 50, + left: 50, + bottom: 100, + right: 100 + }; + documentStub.withArgs('div-gpt-ad-1460505748561-123').returns({ + getBoundingClientRect: () => boundingRect + }); + documentStub.withArgs('div-gpt-ad-1460505748561-0').returns({ + getBoundingClientRect: () => boundingRect + }); + }); + after(() => { + sandbox.restore(); + mock.restore(); + }); it('should build valid payload on bid', () => { let requestObj = spec.buildRequests(VALID_BID_REQUEST, VALID_AUCTIONDATA); expect(JSON.parse(requestObj.data)).to.deep.equal(VALID_PAYLOAD); @@ -456,6 +565,87 @@ describe('Media.net bid adapter', () => { }); }); + describe('slot visibility', () => { + let mock; + before(() => { + mock = sinon.mock(window); + window.innerWidth = 1000; + window.innerHeight = 1000; + }); + after(() => { + mock.restore(); + }); + it('slot visibility should be 2 and ratio 0 when ad unit is BTF', () => { + let sandbox = sinon.sandbox.create(); + let documentStub = sandbox.stub(document, 'getElementById'); + let boundingRect = { + top: 1010, + left: 1010, + bottom: 1050, + right: 1050 + }; + documentStub.withArgs('div-gpt-ad-1460505748561-123').returns({ + getBoundingClientRect: () => boundingRect + }); + documentStub.withArgs('div-gpt-ad-1460505748561-0').returns({ + getBoundingClientRect: () => boundingRect + }); + let bidReq = spec.buildRequests(VALID_BID_REQUEST, VALID_AUCTIONDATA); + let data = JSON.parse(bidReq.data); + expect(data.imp[0].ext.visibility).to.equal(2); + expect(data.imp[0].ext.viewability).to.equal(0); + sandbox.restore(); + }); + it('slot visibility should be 2 and ratio < 0.5 when ad unit is partially inside viewport', () => { + let sandbox = sinon.sandbox.create(); + let documentStub = sandbox.stub(document, 'getElementById'); + let boundingRect = { + top: 990, + left: 990, + bottom: 1050, + right: 1050 + }; + documentStub.withArgs('div-gpt-ad-1460505748561-123').returns({ + getBoundingClientRect: () => boundingRect + }); + documentStub.withArgs('div-gpt-ad-1460505748561-0').returns({ + getBoundingClientRect: () => boundingRect + }); + let bidReq = spec.buildRequests(VALID_BID_REQUEST, VALID_AUCTIONDATA); + let data = JSON.parse(bidReq.data); + expect(data.imp[0].ext.visibility).to.equal(2); + expect(data.imp[0].ext.viewability).to.equal(100 / 75000); + sandbox.restore(); + }); + it('slot visibility should be 1 and ratio > 0.5 when ad unit mostly in viewport', () => { + let sandbox = sinon.sandbox.create(); + let documentStub = sandbox.stub(document, 'getElementById'); + let boundingRect = { + top: 800, + left: 800, + bottom: 1050, + right: 1050 + }; + documentStub.withArgs('div-gpt-ad-1460505748561-123').returns({ + getBoundingClientRect: () => boundingRect + }); + documentStub.withArgs('div-gpt-ad-1460505748561-0').returns({ + getBoundingClientRect: () => boundingRect + }); + let bidReq = spec.buildRequests(VALID_BID_REQUEST, VALID_AUCTIONDATA); + let data = JSON.parse(bidReq.data); + expect(data.imp[0].ext.visibility).to.equal(1); + expect(data.imp[0].ext.viewability).to.equal(40000 / 75000); + sandbox.restore(); + }); + it('co-ordinates should not be sent and slot visibility should be 0 when ad unit is not present', () => { + let bidReq = spec.buildRequests(VALID_BID_REQUEST, VALID_AUCTIONDATA); + let data = JSON.parse(bidReq.data); + expect(data.imp[1].ext).to.not.have.ownPropertyDescriptor('viewability'); + expect(data.imp[1].ext.visibility).to.equal(0); + }); + }); + describe('getUserSyncs', () => { it('should exclude iframe syncs if iframe is disabled', () => { let userSyncs = spec.getUserSyncs(SYNC_OPTIONS_PIXEL_ENABLED, SERVER_CSYNC_RESPONSE); From 06a82aa7809d4f48505beba18338be6274c30a60 Mon Sep 17 00:00:00 2001 From: binoy Date: Mon, 4 Jun 2018 19:08:06 +0530 Subject: [PATCH 2/4] Media.net adapter: changed slot visibility logic and removed mocks on window properties --- modules/medianetBidAdapter.js | 33 +++++++++++++----- test/spec/modules/medianetBidAdapter_spec.js | 36 +++++++++----------- 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/modules/medianetBidAdapter.js b/modules/medianetBidAdapter.js index 2317c79685d..4ff7bfd000e 100644 --- a/modules/medianetBidAdapter.js +++ b/modules/medianetBidAdapter.js @@ -90,11 +90,11 @@ function getCoordinates(id) { let coordinates = {}; coordinates.top_left = { y: rect.top, - x: rect.left, + x: rect.left }; coordinates.bottom_right = { y: rect.bottom, - x: rect.right, + x: rect.right }; return coordinates } @@ -110,7 +110,7 @@ function extParams(params, gdpr) { if (ext.gdpr_applies) { ext.gdpr_consent_string = gdpr.consentString || ''; } - let windowSize = getWindowSize(); + let windowSize = spec.getWindowSize(); if (windowSize.w !== -1 && windowSize.h !== -1) { ext.screen = windowSize; } @@ -138,10 +138,10 @@ function slotParams(bidRequest) { } const coordinates = getCoordinates(bidRequest.adUnitCode); if (coordinates) { - params.ext.coordinates = coordinates; - let viewability = getSlotVisibility(coordinates.top_left, getMinSize(params.banner)); - params.ext.viewability = viewability; - if (viewability > 0.5) { + let normCoordinates = normalizeCoordinates(coordinates); + params.ext.coordinates = normCoordinates; + params.ext.viewability = getSlotVisibility(coordinates.top_left, getMinSize(params.banner)); + if (getSlotVisibility(normCoordinates.top_left, getMinSize(params.banner)) > 0.5) { params.ext.visibility = SLOT_VISIBILITY.ABOVE_THE_FOLD; } else { params.ext.visibility = SLOT_VISIBILITY.BELOW_THE_FOLD; @@ -159,7 +159,7 @@ function getMinSize(sizes) { function getSlotVisibility(topLeft, size) { let maxArea = size.w * size.h; - let windowSize = getWindowSize(); + let windowSize = spec.getWindowSize(); let bottomRight = { x: topLeft.x + size.w, y: topLeft.y + size.h @@ -181,6 +181,19 @@ function getOverlapArea(topLeft1, bottomRight1, topLeft2, bottomRight2) { return ((Math.min(bottomRight1.x, bottomRight2.x) - Math.max(topLeft1.x, topLeft2.x)) * (Math.min(bottomRight1.y, bottomRight2.y) - Math.max(topLeft1.y, topLeft2.y))); } +function normalizeCoordinates(coordinates) { + return { + top_left: { + x: coordinates.top_left.x + window.pageXOffset, + y: coordinates.top_left.y + window.pageYOffset, + }, + bottom_right: { + x: coordinates.bottom_right.x + window.pageXOffset, + y: coordinates.bottom_right.y + window.pageYOffset, + } + } +} + function generatePayload(bidRequests, bidderRequests) { return { site: siteDetails(bidRequests[0].params.site), @@ -280,6 +293,8 @@ export const spec = { if (syncOptions.pixelEnabled) { return filterUrlsByType(cookieSyncUrls, 'image'); } - } + }, + + getWindowSize, }; registerBidder(spec); diff --git a/test/spec/modules/medianetBidAdapter_spec.js b/test/spec/modules/medianetBidAdapter_spec.js index 2f57e2a4c6f..303e639018b 100644 --- a/test/spec/modules/medianetBidAdapter_spec.js +++ b/test/spec/modules/medianetBidAdapter_spec.js @@ -501,12 +501,8 @@ describe('Media.net bid adapter', () => { describe('buildRequests', () => { let sandbox; - let mock; before(() => { sandbox = sinon.sandbox.create(); - mock = sinon.mock(window); - window.innerWidth = 1000; - window.innerHeight = 1000; let documentStub = sandbox.stub(document, 'getElementById'); let boundingRect = { top: 50, @@ -520,10 +516,14 @@ describe('Media.net bid adapter', () => { documentStub.withArgs('div-gpt-ad-1460505748561-0').returns({ getBoundingClientRect: () => boundingRect }); + let windowSizeStub = sandbox.stub(spec, 'getWindowSize'); + windowSizeStub.returns({ + w: 1000, + h: 1000 + }); }); after(() => { sandbox.restore(); - mock.restore(); }); it('should build valid payload on bid', () => { let requestObj = spec.buildRequests(VALID_BID_REQUEST, VALID_AUCTIONDATA); @@ -566,17 +566,16 @@ describe('Media.net bid adapter', () => { }); describe('slot visibility', () => { - let mock; + let sandbox = sinon.sandbox.create(); before(() => { - mock = sinon.mock(window); - window.innerWidth = 1000; - window.innerHeight = 1000; - }); - after(() => { - mock.restore(); + let windowSizeStub = sandbox.stub(spec, 'getWindowSize'); + windowSizeStub.returns({ + w: 1000, + h: 1000 + }); }); + after(() => sandbox.restore()); it('slot visibility should be 2 and ratio 0 when ad unit is BTF', () => { - let sandbox = sinon.sandbox.create(); let documentStub = sandbox.stub(document, 'getElementById'); let boundingRect = { top: 1010, @@ -590,14 +589,14 @@ describe('Media.net bid adapter', () => { documentStub.withArgs('div-gpt-ad-1460505748561-0').returns({ getBoundingClientRect: () => boundingRect }); + let bidReq = spec.buildRequests(VALID_BID_REQUEST, VALID_AUCTIONDATA); let data = JSON.parse(bidReq.data); expect(data.imp[0].ext.visibility).to.equal(2); expect(data.imp[0].ext.viewability).to.equal(0); - sandbox.restore(); + documentStub.restore(); }); it('slot visibility should be 2 and ratio < 0.5 when ad unit is partially inside viewport', () => { - let sandbox = sinon.sandbox.create(); let documentStub = sandbox.stub(document, 'getElementById'); let boundingRect = { top: 990, @@ -615,10 +614,9 @@ describe('Media.net bid adapter', () => { let data = JSON.parse(bidReq.data); expect(data.imp[0].ext.visibility).to.equal(2); expect(data.imp[0].ext.viewability).to.equal(100 / 75000); - sandbox.restore(); + documentStub.restore(); }); it('slot visibility should be 1 and ratio > 0.5 when ad unit mostly in viewport', () => { - let sandbox = sinon.sandbox.create(); let documentStub = sandbox.stub(document, 'getElementById'); let boundingRect = { top: 800, @@ -636,7 +634,7 @@ describe('Media.net bid adapter', () => { let data = JSON.parse(bidReq.data); expect(data.imp[0].ext.visibility).to.equal(1); expect(data.imp[0].ext.viewability).to.equal(40000 / 75000); - sandbox.restore(); + documentStub.restore(); }); it('co-ordinates should not be sent and slot visibility should be 0 when ad unit is not present', () => { let bidReq = spec.buildRequests(VALID_BID_REQUEST, VALID_AUCTIONDATA); @@ -679,7 +677,7 @@ describe('Media.net bid adapter', () => { it('should not push response if no-bid', () => { let validBids = []; let bids = spec.interpretResponse(SERVER_RESPONSE_NOBID, []); - expect(bids).to.deep.equal(validBids) + expect(bids).to.deep.equal(validBids); }); }); }); From 4e7a59a7e9e8096de81406351b4031f23d1feae7 Mon Sep 17 00:00:00 2001 From: binoy Date: Fri, 8 Jun 2018 12:40:31 +0530 Subject: [PATCH 3/4] Media.net adapter: refactored slot-visibility tests to use beforeEach and afterEach hooks --- test/spec/modules/medianetBidAdapter_spec.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/test/spec/modules/medianetBidAdapter_spec.js b/test/spec/modules/medianetBidAdapter_spec.js index 303e639018b..263371b4024 100644 --- a/test/spec/modules/medianetBidAdapter_spec.js +++ b/test/spec/modules/medianetBidAdapter_spec.js @@ -566,15 +566,16 @@ describe('Media.net bid adapter', () => { }); describe('slot visibility', () => { - let sandbox = sinon.sandbox.create(); - before(() => { + let sandbox; + beforeEach(() => { + sandbox = sinon.sandbox.create(); let windowSizeStub = sandbox.stub(spec, 'getWindowSize'); windowSizeStub.returns({ w: 1000, h: 1000 }); }); - after(() => sandbox.restore()); + afterEach(() => sandbox.restore()); it('slot visibility should be 2 and ratio 0 when ad unit is BTF', () => { let documentStub = sandbox.stub(document, 'getElementById'); let boundingRect = { @@ -594,7 +595,6 @@ describe('Media.net bid adapter', () => { let data = JSON.parse(bidReq.data); expect(data.imp[0].ext.visibility).to.equal(2); expect(data.imp[0].ext.viewability).to.equal(0); - documentStub.restore(); }); it('slot visibility should be 2 and ratio < 0.5 when ad unit is partially inside viewport', () => { let documentStub = sandbox.stub(document, 'getElementById'); @@ -614,7 +614,6 @@ describe('Media.net bid adapter', () => { let data = JSON.parse(bidReq.data); expect(data.imp[0].ext.visibility).to.equal(2); expect(data.imp[0].ext.viewability).to.equal(100 / 75000); - documentStub.restore(); }); it('slot visibility should be 1 and ratio > 0.5 when ad unit mostly in viewport', () => { let documentStub = sandbox.stub(document, 'getElementById'); @@ -634,7 +633,6 @@ describe('Media.net bid adapter', () => { let data = JSON.parse(bidReq.data); expect(data.imp[0].ext.visibility).to.equal(1); expect(data.imp[0].ext.viewability).to.equal(40000 / 75000); - documentStub.restore(); }); it('co-ordinates should not be sent and slot visibility should be 0 when ad unit is not present', () => { let bidReq = spec.buildRequests(VALID_BID_REQUEST, VALID_AUCTIONDATA); From 79560e8a9a2ac87055a3aff8b5987d7650e3515a Mon Sep 17 00:00:00 2001 From: Rich Snapp Date: Fri, 8 Jun 2018 10:01:10 -0600 Subject: [PATCH 4/4] create only one sandbox and restore for medianet --- test/spec/modules/medianetBidAdapter_spec.js | 27 ++++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/test/spec/modules/medianetBidAdapter_spec.js b/test/spec/modules/medianetBidAdapter_spec.js index 263371b4024..d94cb64c145 100644 --- a/test/spec/modules/medianetBidAdapter_spec.js +++ b/test/spec/modules/medianetBidAdapter_spec.js @@ -477,6 +477,15 @@ let VALID_BID_REQUEST = [{ }; describe('Media.net bid adapter', () => { + let sandbox; + beforeEach(() => { + sandbox = sinon.sandbox.create(); + }); + + afterEach(() => { + sandbox.restore(); + }); + describe('isBidRequestValid', () => { it('should accept valid bid params', () => { let isValid = spec.isBidRequestValid(VALID_PARAMS); @@ -500,9 +509,7 @@ describe('Media.net bid adapter', () => { }); describe('buildRequests', () => { - let sandbox; - before(() => { - sandbox = sinon.sandbox.create(); + beforeEach(() => { let documentStub = sandbox.stub(document, 'getElementById'); let boundingRect = { top: 50, @@ -522,9 +529,7 @@ describe('Media.net bid adapter', () => { h: 1000 }); }); - after(() => { - sandbox.restore(); - }); + it('should build valid payload on bid', () => { let requestObj = spec.buildRequests(VALID_BID_REQUEST, VALID_AUCTIONDATA); expect(JSON.parse(requestObj.data)).to.deep.equal(VALID_PAYLOAD); @@ -547,7 +552,6 @@ describe('Media.net bid adapter', () => { describe('build requests: when page meta-data is available', () => { it('should pass canonical, twitter and fb paramters if available', () => { - let sandbox = sinon.sandbox.create(); let documentStub = sandbox.stub(window.top.document, 'querySelector'); documentStub.withArgs('link[rel="canonical"]').returns({ href: 'http://localhost:9999/canonical-test' @@ -560,24 +564,21 @@ describe('Media.net bid adapter', () => { }); let bidReq = spec.buildRequests(VALID_BID_REQUEST, VALID_AUCTIONDATA); expect(JSON.parse(bidReq.data)).to.deep.equal(VALID_PAYLOAD_PAGE_META); - sandbox.restore(); }); }); }); describe('slot visibility', () => { - let sandbox; + let documentStub; beforeEach(() => { - sandbox = sinon.sandbox.create(); let windowSizeStub = sandbox.stub(spec, 'getWindowSize'); windowSizeStub.returns({ w: 1000, h: 1000 }); + documentStub = sandbox.stub(document, 'getElementById'); }); - afterEach(() => sandbox.restore()); it('slot visibility should be 2 and ratio 0 when ad unit is BTF', () => { - let documentStub = sandbox.stub(document, 'getElementById'); let boundingRect = { top: 1010, left: 1010, @@ -597,7 +598,6 @@ describe('Media.net bid adapter', () => { expect(data.imp[0].ext.viewability).to.equal(0); }); it('slot visibility should be 2 and ratio < 0.5 when ad unit is partially inside viewport', () => { - let documentStub = sandbox.stub(document, 'getElementById'); let boundingRect = { top: 990, left: 990, @@ -616,7 +616,6 @@ describe('Media.net bid adapter', () => { expect(data.imp[0].ext.viewability).to.equal(100 / 75000); }); it('slot visibility should be 1 and ratio > 0.5 when ad unit mostly in viewport', () => { - let documentStub = sandbox.stub(document, 'getElementById'); let boundingRect = { top: 800, left: 800,