From 27d32101a51cb4ca8b14cf88c01d226bb67542b2 Mon Sep 17 00:00:00 2001 From: Sumit Sharma Date: Tue, 30 Apr 2019 14:42:08 +0530 Subject: [PATCH 1/5] add adUnitCodes as param for setTargetingForAst() --- src/prebid.js | 5 +++-- src/targeting.js | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/prebid.js b/src/prebid.js index d475a715bbf..1b6a40aff97 100644 --- a/src/prebid.js +++ b/src/prebid.js @@ -260,16 +260,17 @@ $$PREBID_GLOBAL$$.setTargetingForGPTAsync = function (adUnit, customSlotMatching /** * Set query string targeting on all AST (AppNexus Seller Tag) ad units. Note that this function has to be called after all ad units on page are defined. For working example code, see [Using Prebid.js with AppNexus Publisher Ad Server](http://prebid.org/dev-docs/examples/use-prebid-with-appnexus-ad-server.html). + * @param {(string|string[])} adUnitCode adUnitCode or array of adUnitCodes * @alias module:pbjs.setTargetingForAst */ -$$PREBID_GLOBAL$$.setTargetingForAst = function() { +$$PREBID_GLOBAL$$.setTargetingForAst = function(adUnitCodes) { utils.logInfo('Invoking $$PREBID_GLOBAL$$.setTargetingForAn', arguments); if (!targeting.isApntagDefined()) { utils.logError('window.apntag is not defined on the page'); return; } - targeting.setTargetingForAst(); + targeting.setTargetingForAst(adUnitCodes); // emit event events.emit(SET_TARGETING, targeting.getAllTargeting()); diff --git a/src/targeting.js b/src/targeting.js index 90897b8d956..6caa4029c9c 100644 --- a/src/targeting.js +++ b/src/targeting.js @@ -248,10 +248,11 @@ export function newTargeting(auctionManager) { }; /** + * @param {(string|string[])} adUnitCode adUnitCode or array of adUnitCodes * Sets targeting for AST */ - targeting.setTargetingForAst = function() { - let astTargeting = targeting.getAllTargeting(); + targeting.setTargetingForAst = function(adUnitCodes) { + let astTargeting = targeting.getAllTargeting(adUnitCodes); try { targeting.resetPresetTargetingAST(); From fe05bd326d78b5790d47a687fc9530c7cfdc86de Mon Sep 17 00:00:00 2001 From: Sumit Sharma Date: Tue, 7 May 2019 21:44:37 +0530 Subject: [PATCH 2/5] unit tests for setTargetingForAst --- src/targeting.js | 2 +- test/spec/unit/core/targeting_spec.js | 41 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/targeting.js b/src/targeting.js index 6caa4029c9c..4ac993cf94a 100644 --- a/src/targeting.js +++ b/src/targeting.js @@ -255,7 +255,7 @@ export function newTargeting(auctionManager) { let astTargeting = targeting.getAllTargeting(adUnitCodes); try { - targeting.resetPresetTargetingAST(); + targeting.resetPresetTargetingAST(adUnitCodes); } catch (e) { utils.logError('unable to reset targeting for AST' + e) } diff --git a/test/spec/unit/core/targeting_spec.js b/test/spec/unit/core/targeting_spec.js index 13ebfd7dc4e..78041dfeecf 100644 --- a/test/spec/unit/core/targeting_spec.js +++ b/test/spec/unit/core/targeting_spec.js @@ -326,4 +326,45 @@ describe('targeting tests', function () { }); }); }); + + describe('setTargetingForAst', function () { + let sandbox; + beforeEach(function() { + sandbox = sinon.createSandbox(); + sandbox.stub(targetingInstance, 'resetPresetTargetingAST').callsFake(function() {}); + sandbox.stub(window.apntag, 'setKeywords').callsFake(function() {}); + }); + afterEach(function () { + sandbox.restore(); + }); + + it('should set single addUnit code', function() { + let adUnitCode = 'testdiv-abc-ad-123456-0'; + sandbox.stub(targetingInstance, 'getAllTargeting').callsFake(function() { + let mockTargeting = {}; + mockTargeting.adUnitCode = {hb_bidder: 'appnexus'}; + return mockTargeting; + }); + targetingInstance.setTargetingForAst(adUnitCode); + expect(targetingInstance.getAllTargeting.called).to.equal(true); + expect(targetingInstance.resetPresetTargetingAST.called).to.equal(true); + expect(window.apntag.setKeywords.called).to.equal(true); + }); + + it('should set array of addUnit codes', function() { + let adUnitCodes = ['testdiv1-abc-ad-123456-0', 'testdiv2-abc-ad-123456-0'] + sandbox.stub(targetingInstance, 'getAllTargeting').callsFake(function() { + let mockTargeting = {}; + for (let i = 0; i < Object.keys(adUnitCodes).length; i++) { + let key = Object.keys(adUnitCodes)[i]; + mockTargeting[key] = {hb_bidder: 'appnexus'}; + } + return mockTargeting; + }); + targetingInstance.setTargetingForAst(adUnitCodes); + expect(targetingInstance.getAllTargeting.called).to.equal(true); + expect(targetingInstance.resetPresetTargetingAST.called).to.equal(true); + expect(window.apntag.setKeywords.called).to.equal(true); + }); + }); }); From 1a89d024cb561f44cc59fe85d3f0adea11e24381 Mon Sep 17 00:00:00 2001 From: Sumit Sharma Date: Tue, 7 May 2019 22:23:02 +0530 Subject: [PATCH 3/5] refactor --- test/spec/unit/core/targeting_spec.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/test/spec/unit/core/targeting_spec.js b/test/spec/unit/core/targeting_spec.js index 78041dfeecf..f2cd22c8167 100644 --- a/test/spec/unit/core/targeting_spec.js +++ b/test/spec/unit/core/targeting_spec.js @@ -338,13 +338,21 @@ describe('targeting tests', function () { sandbox.restore(); }); - it('should set single addUnit code', function() { - let adUnitCode = 'testdiv-abc-ad-123456-0'; + function mockGetAllTargeting(adUnitCodes) { + adUnitCodes = Array.isArray(adUnitCodes) ? adUnitCodes : Array(adUnitCodes); sandbox.stub(targetingInstance, 'getAllTargeting').callsFake(function() { let mockTargeting = {}; - mockTargeting.adUnitCode = {hb_bidder: 'appnexus'}; + for (let i = 0; i < Object.keys(adUnitCodes).length; i++) { + let key = Object.keys(adUnitCodes)[i]; + mockTargeting[key] = {hb_bidder: 'appnexus'}; + } return mockTargeting; }); + } + + it('should set single addUnit code', function() { + let adUnitCode = 'testdiv-abc-ad-123456-0'; + mockGetAllTargeting(adUnitCode); targetingInstance.setTargetingForAst(adUnitCode); expect(targetingInstance.getAllTargeting.called).to.equal(true); expect(targetingInstance.resetPresetTargetingAST.called).to.equal(true); @@ -353,14 +361,7 @@ describe('targeting tests', function () { it('should set array of addUnit codes', function() { let adUnitCodes = ['testdiv1-abc-ad-123456-0', 'testdiv2-abc-ad-123456-0'] - sandbox.stub(targetingInstance, 'getAllTargeting').callsFake(function() { - let mockTargeting = {}; - for (let i = 0; i < Object.keys(adUnitCodes).length; i++) { - let key = Object.keys(adUnitCodes)[i]; - mockTargeting[key] = {hb_bidder: 'appnexus'}; - } - return mockTargeting; - }); + mockGetAllTargeting(adUnitCodes); targetingInstance.setTargetingForAst(adUnitCodes); expect(targetingInstance.getAllTargeting.called).to.equal(true); expect(targetingInstance.resetPresetTargetingAST.called).to.equal(true); From 6f8e2a86e5617059f1c79c864d59cc05f758edd0 Mon Sep 17 00:00:00 2001 From: Sumit Sharma Date: Fri, 10 May 2019 14:37:45 +0530 Subject: [PATCH 4/5] Revert "refactor" This reverts commit 1a89d024cb561f44cc59fe85d3f0adea11e24381. --- test/spec/unit/core/targeting_spec.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/test/spec/unit/core/targeting_spec.js b/test/spec/unit/core/targeting_spec.js index f2cd22c8167..78041dfeecf 100644 --- a/test/spec/unit/core/targeting_spec.js +++ b/test/spec/unit/core/targeting_spec.js @@ -338,21 +338,13 @@ describe('targeting tests', function () { sandbox.restore(); }); - function mockGetAllTargeting(adUnitCodes) { - adUnitCodes = Array.isArray(adUnitCodes) ? adUnitCodes : Array(adUnitCodes); + it('should set single addUnit code', function() { + let adUnitCode = 'testdiv-abc-ad-123456-0'; sandbox.stub(targetingInstance, 'getAllTargeting').callsFake(function() { let mockTargeting = {}; - for (let i = 0; i < Object.keys(adUnitCodes).length; i++) { - let key = Object.keys(adUnitCodes)[i]; - mockTargeting[key] = {hb_bidder: 'appnexus'}; - } + mockTargeting.adUnitCode = {hb_bidder: 'appnexus'}; return mockTargeting; }); - } - - it('should set single addUnit code', function() { - let adUnitCode = 'testdiv-abc-ad-123456-0'; - mockGetAllTargeting(adUnitCode); targetingInstance.setTargetingForAst(adUnitCode); expect(targetingInstance.getAllTargeting.called).to.equal(true); expect(targetingInstance.resetPresetTargetingAST.called).to.equal(true); @@ -361,7 +353,14 @@ describe('targeting tests', function () { it('should set array of addUnit codes', function() { let adUnitCodes = ['testdiv1-abc-ad-123456-0', 'testdiv2-abc-ad-123456-0'] - mockGetAllTargeting(adUnitCodes); + sandbox.stub(targetingInstance, 'getAllTargeting').callsFake(function() { + let mockTargeting = {}; + for (let i = 0; i < Object.keys(adUnitCodes).length; i++) { + let key = Object.keys(adUnitCodes)[i]; + mockTargeting[key] = {hb_bidder: 'appnexus'}; + } + return mockTargeting; + }); targetingInstance.setTargetingForAst(adUnitCodes); expect(targetingInstance.getAllTargeting.called).to.equal(true); expect(targetingInstance.resetPresetTargetingAST.called).to.equal(true); From 34094f90c1fd65e0eeffd69a21a904be7c362aad Mon Sep 17 00:00:00 2001 From: Sumit Sharma Date: Fri, 10 May 2019 16:02:30 +0530 Subject: [PATCH 5/5] refactor to add more tests --- test/spec/unit/core/targeting_spec.js | 31 +++++++++++++-------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/test/spec/unit/core/targeting_spec.js b/test/spec/unit/core/targeting_spec.js index 78041dfeecf..727c790c991 100644 --- a/test/spec/unit/core/targeting_spec.js +++ b/test/spec/unit/core/targeting_spec.js @@ -328,11 +328,12 @@ describe('targeting tests', function () { }); describe('setTargetingForAst', function () { - let sandbox; + let sandbox, + apnTagStub; beforeEach(function() { sandbox = sinon.createSandbox(); - sandbox.stub(targetingInstance, 'resetPresetTargetingAST').callsFake(function() {}); - sandbox.stub(window.apntag, 'setKeywords').callsFake(function() {}); + sandbox.stub(targetingInstance, 'resetPresetTargetingAST'); + apnTagStub = sandbox.stub(window.apntag, 'setKeywords'); }); afterEach(function () { sandbox.restore(); @@ -340,31 +341,29 @@ describe('targeting tests', function () { it('should set single addUnit code', function() { let adUnitCode = 'testdiv-abc-ad-123456-0'; - sandbox.stub(targetingInstance, 'getAllTargeting').callsFake(function() { - let mockTargeting = {}; - mockTargeting.adUnitCode = {hb_bidder: 'appnexus'}; - return mockTargeting; + sandbox.stub(targetingInstance, 'getAllTargeting').returns({ + 'testdiv1-abc-ad-123456-0': {hb_bidder: 'appnexus'} }); targetingInstance.setTargetingForAst(adUnitCode); expect(targetingInstance.getAllTargeting.called).to.equal(true); expect(targetingInstance.resetPresetTargetingAST.called).to.equal(true); - expect(window.apntag.setKeywords.called).to.equal(true); + expect(apnTagStub.callCount).to.equal(1); + expect(apnTagStub.getCall(0).args[0]).to.deep.equal('testdiv1-abc-ad-123456-0'); + expect(apnTagStub.getCall(0).args[1]).to.deep.equal({HB_BIDDER: 'appnexus'}); }); it('should set array of addUnit codes', function() { let adUnitCodes = ['testdiv1-abc-ad-123456-0', 'testdiv2-abc-ad-123456-0'] - sandbox.stub(targetingInstance, 'getAllTargeting').callsFake(function() { - let mockTargeting = {}; - for (let i = 0; i < Object.keys(adUnitCodes).length; i++) { - let key = Object.keys(adUnitCodes)[i]; - mockTargeting[key] = {hb_bidder: 'appnexus'}; - } - return mockTargeting; + sandbox.stub(targetingInstance, 'getAllTargeting').returns({ + 'testdiv1-abc-ad-123456-0': {hb_bidder: 'appnexus'}, + 'testdiv2-abc-ad-123456-0': {hb_bidder: 'appnexus'} }); targetingInstance.setTargetingForAst(adUnitCodes); expect(targetingInstance.getAllTargeting.called).to.equal(true); expect(targetingInstance.resetPresetTargetingAST.called).to.equal(true); - expect(window.apntag.setKeywords.called).to.equal(true); + expect(apnTagStub.callCount).to.equal(2); + expect(apnTagStub.getCall(1).args[0]).to.deep.equal('testdiv2-abc-ad-123456-0'); + expect(apnTagStub.getCall(1).args[1]).to.deep.equal({HB_BIDDER: 'appnexus'}); }); }); });