Skip to content

Commit

Permalink
update all pixels to use utils.triggerPixel and stub tests
Browse files Browse the repository at this point in the history
  • Loading branch information
snapwich committed Jan 6, 2020
1 parent d944d14 commit 0c69fed
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 21 deletions.
4 changes: 2 additions & 2 deletions modules/adponeBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {BANNER} from '../src/mediaTypes';
import {registerBidder} from '../src/adapters/bidderFactory';
import {triggerPixel} from '../src/utils';

const ADPONE_CODE = 'adpone';
const ADPONE_ENDPOINT = 'https://rtb.adpone.com/bid-request';
Expand Down Expand Up @@ -87,8 +88,7 @@ export const spec = {
onBidWon: bid => {
const bidString = JSON.stringify(bid);
const encodedBuf = window.btoa(bidString);
const img = new Image(1, 1);
img.src = `https://rtb.adpone.com/prebid/analytics?q=${encodedBuf}`;
triggerPixel(`https://rtb.adpone.com/prebid/analytics?q=${encodedBuf}`);
},

};
Expand Down
8 changes: 2 additions & 6 deletions modules/mgidBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export const spec = {
/\${AUCTION_PRICE}/,
cpm
);
pixel(bid.nurl);
utils.triggerPixel(bid.nurl);
}
if (bid.isBurl) {
if (bid.mediaType === BANNER) {
Expand All @@ -245,7 +245,7 @@ export const spec = {
/\${AUCTION_PRICE}/,
cpm
);
pixel(bid.burl);
utils.triggerPixel(bid.burl);
}
}
utils.logInfo(LOG_INFO_PREFIX + `onBidWon`);
Expand Down Expand Up @@ -331,10 +331,6 @@ function extractDomainFromHost(pageHost) {
return domain;
}

function pixel(url) {
(document.createElement('IMG')).src = url;
}

function getLanguage() {
const language = navigator.language ? 'language' : 'userLanguage';
const lang2 = navigator[language].split('-')[0];
Expand Down
5 changes: 1 addition & 4 deletions modules/videoNowBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,7 @@ function getUserSyncs(syncOptions, serverResponses) {
function onBidWon(bid) {
const { nurl } = bid || {}
if (nurl) {
const img = document.createElement('img')
img.src = utils.replaceAuctionPrice(nurl, bid.cpm)
img.style.cssText = 'display:none !important;'
document.body.appendChild(img)
utils.triggerPixel(utils.replaceAuctionPrice(nurl, bid.cpm));
}
}

Expand Down
10 changes: 9 additions & 1 deletion test/spec/modules/adponeBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from 'chai';
import { spec } from 'modules/adponeBidAdapter';
import {newBidder} from '../../../src/adapters/bidderFactory';
import {newBidder} from 'src/adapters/bidderFactory';
import * as utils from 'src/utils';

const EMPTY_ARRAY = [];
describe('adponeBidAdapter', function () {
Expand Down Expand Up @@ -213,11 +214,18 @@ describe('getUserSyncs', function () {
});

describe('test onBidWon function', function () {
beforeEach(function() {
sinon.stub(utils, 'triggerPixel');
});
afterEach(function() {
utils.triggerPixel.restore();
});
it('exists and is a function', () => {
expect(spec.onBidWon).to.exist.and.to.be.a('function');
});
it('should return nothing', function () {
var response = spec.onBidWon({});
expect(response).to.be.an('undefined')
expect(utils.triggerPixel.called).to.equal(true);
});
});
6 changes: 3 additions & 3 deletions test/spec/modules/emoteevBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ describe('emoteevBidAdapter', function () {
});

describe('side effects', function () {
let triggerPixelSpy;
let triggerPixelStub;
let getCookieSpy;
let getConfigSpy;
let getParameterByNameSpy;
Expand All @@ -748,13 +748,13 @@ describe('emoteevBidAdapter', function () {
config.resetConfig();
});
beforeEach(function () {
triggerPixelSpy = sinon.spy(utils, 'triggerPixel');
triggerPixelStub = sinon.stub(utils, 'triggerPixel');
getCookieSpy = sinon.spy(utils, 'getCookie');
getConfigSpy = sinon.spy(config, 'getConfig');
getParameterByNameSpy = sinon.spy(utils, 'getParameterByName');
});
afterEach(function () {
triggerPixelSpy.restore();
triggerPixelStub.restore();
getCookieSpy.restore();
getConfigSpy.restore();
getParameterByNameSpy.restore();
Expand Down
6 changes: 6 additions & 0 deletions test/spec/modules/mgidBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,12 @@ describe('Mgid bid adapter', function () {
});
});
describe('on bidWon', function () {
beforeEach(function() {
sinon.stub(utils, 'triggerPixel');
});
afterEach(function() {
utils.triggerPixel.restore();
});
it('should replace nurl and burl for native', function () {
const burl = 'burl&s=${' + 'AUCTION_PRICE}';
const nurl = 'nurl&s=${' + 'AUCTION_PRICE}';
Expand Down
10 changes: 10 additions & 0 deletions test/spec/modules/telariaBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {expect} from 'chai';
import {newBidder} from 'src/adapters/bidderFactory';
import {spec, getTimeoutUrl} from 'modules/telariaBidAdapter';
import * as utils from 'src/utils';

const ENDPOINT = '.ads.tremorhub.com/ad/tag';
const AD_CODE = 'ssp-!demo!-lufip';
Expand Down Expand Up @@ -269,13 +270,22 @@ describe('TelariaAdapter', () => {
}]
}];

beforeEach(function() {
sinon.stub(utils, 'triggerPixel');
});

afterEach(function() {
utils.triggerPixel.restore();
});

it('should return a pixel url', () => {
let url = getTimeoutUrl(timeoutData);
assert(url);
});

it('should fire a pixel', () => {
expect(spec.onTimeout(timeoutData)).to.be.undefined;
expect(utils.triggerPixel.called).to.equal(true);
});
});
});
18 changes: 13 additions & 5 deletions test/spec/modules/videoNowBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from 'chai'
import { spec } from 'modules/videoNowBidAdapter'
import { replaceAuctionPrice } from '../../../src/utils'
import { replaceAuctionPrice } from 'src/utils'
import * as utils from 'src/utils';

// childNode.remove polyfill for ie11
// suggested by: https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove
Expand Down Expand Up @@ -260,21 +261,28 @@ describe('videonowAdapterTests', function() {
const cpm = 10
const nurl = 'https://fakedomain.nld?price=${AUCTION_PRICE}'
const imgSrc = replaceAuctionPrice(nurl, cpm)
const foundPixels = () => window.document.body.querySelectorAll(`img[src="${imgSrc}"]`)

beforeEach(function() {
sinon.stub(utils, 'triggerPixel')
})

afterEach(function() {
utils.triggerPixel.restore()
})

it('Should not create nurl pixel if bid is undefined', function() {
spec.onBidWon()
expect(foundPixels().length).to.equal(0)
expect(utils.triggerPixel.called).to.equal(false);
})

it('Should not create nurl pixel if bid does not contains nurl', function() {
spec.onBidWon({})
expect(foundPixels().length).to.equal(0)
expect(utils.triggerPixel.called).to.equal(false);
})

it('Should create nurl pixel if bid nurl', function() {
spec.onBidWon({ nurl, cpm })
expect(foundPixels().length).to.equal(1)
expect(utils.triggerPixel.calledWith(imgSrc)).to.equal(true);
})
})

Expand Down

0 comments on commit 0c69fed

Please sign in to comment.