Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rad 2751/specify ad units set targeting for ast #3805

Merged
merged 7 commits into from
May 10, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/targeting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
42 changes: 42 additions & 0 deletions test/spec/unit/core/targeting_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,4 +326,46 @@ 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();
});

function mockGetAllTargeting(adUnitCodes) {
adUnitCodes = Array.isArray(adUnitCodes) ? adUnitCodes : Array(adUnitCodes);
sandbox.stub(targetingInstance, 'getAllTargeting').callsFake(function() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest here to use stub.returns. Changing the implementation of getAllTargeting with this fake function is not a good idea. So you can do something like below in each test case

targetingStub.returns({
   'adUnitCode': {hb_bidder:'appnexus'}
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

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;
});
}

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);
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']
mockGetAllTargeting(adUnitCodes);
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To differentiate between this test and the one with single adUnitCode, you should test here that window.apntag.setKeywords executed twice. Also another assertion can be called with args. Check this sinonjs/sinon#953 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated the tests.

});
});
});