Skip to content

Commit

Permalink
Test coverage improved
Browse files Browse the repository at this point in the history
  • Loading branch information
ym-dlabuzov committed Oct 18, 2021
1 parent 8d73b0f commit fd1cec9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion modules/yieldmoSyntheticInventoryModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function init(config) {
document.addEventListener('DOMContentLoaded', () => googletagCmd(config, containerName, googletag));
}
});
};
}

export function validateConfig(config) {
if (!('placementId' in config)) {
Expand Down
49 changes: 49 additions & 0 deletions test/spec/modules/yieldmoSyntheticInventoryModule_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,32 @@ const mockedYmConfig = {
adUnitPath: '/6355419/ad_unit_name_used_in_gam'
};

const setGoogletag = () => {
window.top.googletag = {
cmd: [],
defineSlot: sinon.stub(),
addService: sinon.stub(),
pubads: sinon.stub(),
setTargeting: sinon.stub(),
enableServices: sinon.stub(),
display: sinon.stub(),
};
window.top.googletag.defineSlot.returns(window.top.googletag);
window.top.googletag.addService.returns(window.top.googletag);
return window.top.googletag;
}

describe('Yieldmo Synthetic Inventory Module', function() {
let config = Object.assign({}, mockedYmConfig);

beforeEach(function () {
delete window.top.googletag;
});

afterEach(function () {
delete window.top.googletag;
});

it('should be enabled with valid required params', function() {
expect(function () {
init(mockedYmConfig);
Expand All @@ -34,4 +57,30 @@ describe('Yieldmo Synthetic Inventory Module', function() {
validateConfig(config);
}).throw(`${MODULE_NAME}: adUnitPath required`)
});

it('should add correct googletag.cmd', function() {
const containerName = 'ym_sim_container_' + mockedYmConfig.placementId;
const gtag = setGoogletag();

init(mockedYmConfig);

expect(gtag.cmd.length).to.equal(1);

gtag.cmd[0]();

expect(gtag.addService.getCall(0)).to.not.be.null;
expect(gtag.setTargeting.getCall(0)).to.not.be.null;
expect(gtag.setTargeting.getCall(0).args[0]).to.exist.and.to.equal('ym_sim_p_id');
expect(gtag.setTargeting.getCall(0).args[1]).to.exist.and.to.equal(mockedYmConfig.placementId);
expect(gtag.defineSlot.getCall(0)).to.not.be.null;
expect(gtag.enableServices.getCall(0)).to.not.be.null;
expect(gtag.display.getCall(0)).to.not.be.null;
expect(gtag.display.getCall(0).args[0]).to.exist.and.to.equal(containerName);
expect(gtag.pubads.getCall(0)).to.not.be.null;

const gamContainerEl = window.top.document.getElementById(containerName);
expect(gamContainerEl).to.not.be.null;

gamContainerEl.parentNode.removeChild(gamContainerEl);
});
});

0 comments on commit fd1cec9

Please sign in to comment.