Skip to content

Commit

Permalink
Merge pull request #75 from prebid/unitTests
Browse files Browse the repository at this point in the history
Unit tests
  • Loading branch information
mkendall07 committed Oct 22, 2015
2 parents 30c6925 + 1fc8e6f commit 021238f
Show file tree
Hide file tree
Showing 11 changed files with 10,478 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,8 @@ Change `{id}` values appropriately
$ gulp serve

Navigate to http://localhost:9999/integrationExamples/gpt/pbjs_example_gpt.html to run the example file

### Unit Test In the Browser ###

Navigate to http://localhost:9999/test/spec/runner.html to run the test file.

2 changes: 1 addition & 1 deletion src/adapters/appnexus.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ var AppNexusAdapter = function AppNexusAdapter() {
adloader.trackPixel(trackingUrl);
}

utils.logMessage('latency for placmeent code : ' + placementCode + ' : ' + timeDiff + ' ms.' + ' Tracking URL Fired : ' + trackingUrl);
utils.logMessage('latency for placement code : ' + placementCode + ' : ' + timeDiff + ' ms.' + ' Tracking URL Fired : ' + trackingUrl);
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,3 +718,11 @@ pbjs.addBidResponse = function(adUnitCode, bid){
};

processQue();


//only for test
pbjs_testonly = {};

pbjs_testonly.getAdUnits = function() {
return pbjs.adUnits;
};
76 changes: 76 additions & 0 deletions test/spec/adUnits_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
describe("AdUnits", function() {
var assert = chai.assert;

describe('addAdUnits', function() {

it('adUnits test', function() {
var adUnits = [{
code: "/1996833/slot-1",
sizes: [[300, 250], [728, 90]],
bids: [
{
bidder: "openx",
params: {
pgid: "2342353",
unit: "234234",
jstag_url: "http://"
}
},{
bidder: "appnexus",
params: {
placementId: "234235"
}
}
]
},{
code: "/1996833/slot-2",
sizes: [[468, 60]],
bids: [
{
bidder: "rubicon",
params: {
rp_account: "4934",
rp_site: "13945",
rp_zonesize: "23948-15"
}
},{
bidder: "appnexus",
params: {
placementId: "827326"
}
}
]
}];

pbjs.addAdUnits(adUnits);

var adUnits = pbjs_testonly.getAdUnits();
var adUnit1 = adUnits[0];
var bids1 = adUnit1.bids;
var adUnit2 = adUnits[1];
var bids2 = adUnit2.bids;

assert.strictEqual(adUnit1.code,'/1996833/slot-1','adUnit1 code');
assert.deepEqual(adUnit1.sizes,[[300, 250], [728, 90]],'adUnit1 sizes');
assert.strictEqual(bids1[0].bidder,'openx','adUnit1 bids1 bidder');
assert.strictEqual(bids1[0].params.pgid,'2342353','adUnit1 bids1 params.pgid');
assert.strictEqual(bids1[0].params.unit,'234234','adUnit1 bids1 params.unit');
assert.strictEqual(bids1[0].params.jstag_url,'http://','adUnit1 bids1 params.jstag_url');

assert.strictEqual(bids1[1].bidder,'appnexus','adUnit1 bids2 bidder');
assert.strictEqual(bids1[1].params.placementId,'234235','adUnit1 bids2 params.placementId');

assert.strictEqual(adUnit2.code,'/1996833/slot-2','adUnit2 code');
assert.deepEqual(adUnit2.sizes,[[468, 60]],'adUnit2 sizes');
assert.strictEqual(bids2[0].bidder,'rubicon','adUnit2 bids1 bidder');
assert.strictEqual(bids2[0].params.rp_account,'4934','adUnit2 bids1 params.rp_account');
assert.strictEqual(bids2[0].params.rp_zonesize,'23948-15','adUnit2 bids1 params.rp_zonesize');
assert.strictEqual(bids2[0].params.rp_site,'13945','adUnit2 bids1 params.rp_site');


assert.strictEqual(bids2[1].bidder,'appnexus','adUnit2 bids2 bidder');
assert.strictEqual(bids2[1].params.placementId,'827326','adUnit2 bids2 params.placementId');

});
});
});
27 changes: 27 additions & 0 deletions test/spec/api_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
describe("API", function() {
var assert = chai.assert;

describe("has api", function() {

it('has api of command queue', function() {
assert.isObject(pbjs);
assert.isFunction(pbjs.que.push);
});

it('has function',function(){
assert.isFunction(pbjs.getAdserverTargeting);
assert.isFunction(pbjs.getAdserverTargetingForAdUnitCode);
assert.isFunction(pbjs.getBidResponses);
assert.isFunction(pbjs.getBidResponsesForAdUnitCode);
assert.isFunction(pbjs.setTargetingForGPTAsync);
assert.isFunction(pbjs.allBidsAvailable);
assert.isFunction(pbjs.renderAd);
assert.isFunction(pbjs.removeAdUnit);
assert.isFunction(pbjs.requestBids);
assert.isFunction(pbjs.addAdUnits);
assert.isFunction(pbjs.addCallback);
assert.isFunction(pbjs.removeCallback);
});
});

});
193 changes: 193 additions & 0 deletions test/spec/bids_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
describe("Bids", function() {
var assert = chai.assert,
expect = chai.expect,
should = chai.should();

var rightSlotCode = '/19968336/header-bid-tag-0';
var rightSlotSizes = [[300, 250], [300, 600]];

var topSlotCode = '/19968336/header-bid-tag1';
var topSlotSizes = [[728, 90], [970, 90]];


beforeEach(function(){

var adUnit1 = {
code: rightSlotCode,
sizes: rightSlotSizes,
bids: [{
bidder: 'appnexus',
params: {
placementId: '2251610'
}
},
{
bidder: 'rubicon',
params: {
rp_account : '9707',
rp_site: '17955',
rp_zonesize : '50983-15',
rp_tracking : 'affiliate-1701207318',
rp_inventory : '{ deals : "mobv3_excl,atf,demo1849,csm1834,znexcl1,exunisite,exmars,extargt,ldacomp,ent19116,rn14858,ukent,g03070,qc12170,qc2690,qc2695,qc1988,asov1,qc12172,qc12169,qc27434,rn24858,ent29116,lngen,cntq,cntauto,anthea,smg_blklist,amnetctr,ntflxblk,amtblk,zentend,nortb,deschoeff,js,excltop," }',
rp_floor : '0.1'
}
},
{
bidder: 'openx',
params: {
unit: 537245128,
pageURL : 'http://drudgereport.com',
refererURL : 'http://drudgereport.com',
jstag_url : 'http://ox-d.intermarkets.net/w/1.0/jstag'
}
},
{
bidder: 'pubmatic',
params: {
publisherId: 39741,
adSlot: '39620189@300x250'
}
},
{
bidder: 'criteo',
params: {
nid: '2612',
cookiename: 'cto_topix',
varname : 'crtg_content'
}
},
{
bidder: 'casale',
params: {
slotId: 2,
casaleUrl: 'http://js.indexww.com/ht/elitedaily.js'
}
},
{
bidder: 'casale',
params: {
slotId: 3
}
},
{
bidder: 'yieldbot',
params: {
pub: 'id',
name: 'name'
}
},
{
bidder: 'amazon',
params: {
aId : 3080
}
}
]
}; //adUnit1 end

var adUnit2 = {
code: topSlotCode,
sizes: topSlotSizes,
bids: [{
bidder: 'appnexus',
params: {
placementId : '5215561'
}
}]
};

var arr = [adUnit2, adUnit1];
pbjs.addAdUnits([adUnit2, adUnit1]);

var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];

// pbjs.setTargetingForGPTAsync([topSlotCode, rightSlotCode]);
pbjs.setTargetingForAdUnitsGPTAsync([topSlotCode, rightSlotCode]);

googletag.cmd.push(function() {
var rightSlot = googletag.defineSlot(rightSlotCode, rightSlotSizes, 'div-gpt-ad-1438287399331-0').addService(googletag.pubads());
var topSlot = googletag.defineSlot(topSlotCode, topSlotSizes, 'div-gpt-ad-1438287399331-1').addService(googletag.pubads());

googletag.pubads().enableSingleRequest();
googletag.enableServices();
});
});

describe('Functions', function() {

it('check type of return value',function(){
assert.typeOf(pbjs.allBidsAvailable(),'boolean');
});
});

describe('Requests', function() {

it('bidsBackHandler callBack', function() {

pbjs.requestBids({
timeout: 500,
bidsBackHandler : function(a){
assertTargeting();
assertArguments(a);
}
});

var assertTargeting = function(){

var top = pbjs.getAdserverTargetingForAdUnitCode(topSlotCode),
right = pbjs.getAdserverTargetingForAdUnitCode(rightSlotCode),
all = pbjs.getAdserverTargeting();

console.log(top);

assert.typeOf(top,'array');
assert.typeOf(top[0],'object');
assert.typeOf(top[0].hb_adid,'string');
assert.typeOf(top[0].hb_bidder,'string');
assert.typeOf(top[0].hb_pb,'string');
assert.typeOf(top[0].hb_size,'string');


console.log(right);

assert.typeOf(right,'array');
assert.typeOf(right[0],'object');
assert.typeOf(right[0].hb_adid,'string');
assert.typeOf(right[0].hb_bidder,'string');
assert.typeOf(right[0].hb_pb,'string');
assert.typeOf(right[0].hb_size,'string');

console.log(all);
assert.typeOf(all,'object');
should.exist(all[rightSlotCode]);
should.exist(all[topSlotCode]);

assert.deepEqual(top,all[topSlotCode],'top slot targeting');
assert.deepEqual(right,all[rightSlotCode],'right slot targeting');
};

var assertArguments = function(arg){
assert.typeOf(arg,'object');
should.exist(arg[rightSlotCode]);
should.exist(arg[topSlotCode]);

console.log(arg);
var responses = pbjs.getBidResponses();
console.log(responses);
assert.typeOf(responses,'object');
should.exist(responses[rightSlotCode]);
should.exist(responses[topSlotCode]);
assert.deepEqual(arg,responses,'reponse object');

var topResponse = pbjs.getBidResponsesForAdUnitCode(topSlotCode);
var rightResponse = pbjs.getBidResponsesForAdUnitCode(rightSlotCode);

assert.deepEqual(topResponse,responses[topSlotCode],'top slot response');
assert.deepEqual(rightResponse,responses[rightSlotCode],'right slot response');

};

});
});
});
4 changes: 4 additions & 0 deletions test/spec/lib/browser/jquery.js

Large diffs are not rendered by default.

Loading

0 comments on commit 021238f

Please sign in to comment.