Skip to content

Commit

Permalink
WIP unit tests for instream tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
monisq committed Aug 4, 2020
1 parent 33aac6b commit d049922
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
5 changes: 5 additions & 0 deletions karma.conf.maker.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ module.exports = function(codeCoverage, browserstack, watchMode, file) {

plugins: plugins
}

if (file) {
config.preprocessors[file] = ['webpack', 'sourcemap'];
}

setReporters(config, codeCoverage, browserstack);
setBrowsers(config, browserstack);
return config;
Expand Down
8 changes: 6 additions & 2 deletions src/instreamTracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,20 @@ const whitelistedResources = /video|fetch|xmlhttprequest|other/;
*
* @param {Array<AdUnit>} adUnits
* @param {Array<Bid>} bidsReceived
*
* @return {boolean} returns TRUE if tracking started
*/
export function trackInstreamDeliveredImpressions(adUnits, bidsReceived) {
const instreamTrackingConfig = config.getConfig('instreamTracking') || {};
// check if instreamTracking is enabled and performance api is available
if (!instreamTrackingConfig.enabled || !window.performance || !window.performance.getEntriesByType) {
return;
return false;
}

// filter for video bids
const instreamBids = bidsReceived.filter(bid => bid.mediaType === VIDEO && bid.context !== OUTSTREAM && bid.videoCacheKey);
if (!instreamBids.length) {
return;
return false;
}

// find unique instream ad units
Expand Down Expand Up @@ -99,4 +101,6 @@ export function trackInstreamDeliveredImpressions(adUnits, bidsReceived) {

// start polling for network entries
setTimeout(poll, pollingFreq);

return true;
}
80 changes: 80 additions & 0 deletions test/spec/instreamTracking_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { expect } from 'chai';
import { trackInstreamDeliveredImpressions } from 'src/instreamTracking.js';
import { config } from '../../src/config.js';

const BIDDER_CODE = 'sampleBidder';
let sandbox;

function mockPerformanceApi() {
window.performance = window.performance || {};
sandbox.stub(window, 'performance').callsFake(() => {
return {
'getEntriesByType': () => undefined
}
});
}

function enableInstreamTracking() {
sandbox.stub(config, 'getConfig').callsFake(() => {
return {
'instreamTracking': {
enabled: true,
maxWindow: 1000 * 60,
pollingFreq: 500
}
}
});
}

describe('instream tracking', function () {
beforeEach(function () {
sandbox = sinon.sandbox.create();
})

afterEach(function () {
sandbox.restore();
});

describe('gaurd checks', function () {
it('skip if tracking not enable', function () {
expect(trackInstreamDeliveredImpressions([], [])).to.be.equal(false);
});

it('run only if instream ad units are present', function () {
enableInstreamTracking();
mockPerformanceApi();
expect(trackInstreamDeliveredImpressions([], [])).to.be.equal(false);
});

it('skip if instream bids not present', function () {
enableInstreamTracking();
mockPerformanceApi();

const adUnits = [{
code: 'banner',
bids: [{
bidder: BIDDER_CODE,
params: {placementId: 'id'}
}]
}];
const bidRecieved = [{
'bidderCode': BIDDER_CODE,
'width': 300,
'height': 250,
'statusMessage': 'Bid available',
'adId': '20372a557635064',
'requestId': '16ee6b19daeb431',
'mediaType': 'banner',
'source': 'client',
'no_bid': false,
'cpm': 1.1495,
'ad': "<div id=''></div>",
'ttl': 180,
'creativeId': '7C19U58N2DL-120340442-30-11',
'netRevenue': true,
'currency': 'USD',
}]
expect(trackInstreamDeliveredImpressions(adUnits, bidRecieved)).to.be.equal(false);
});
});
});

0 comments on commit d049922

Please sign in to comment.