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

Update removeAdUnit #3527

Merged
merged 5 commits into from
Feb 21, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 19 additions & 5 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,19 +360,33 @@ $$PREBID_GLOBAL$$.renderAd = function (doc, id) {
};

/**
* Remove adUnit from the $$PREBID_GLOBAL$$ configuration
* @param {string} adUnitCode the adUnitCode to remove
* Remove adUnit from the $$PREBID_GLOBAL$$ configuration, if there are no addUnitCode(s) it will remove all
* @param {string| Array} adUnitCode the adUnitCode(s) to remove
* @alias module:pbjs.removeAdUnit
*/
$$PREBID_GLOBAL$$.removeAdUnit = function (adUnitCode) {
utils.logInfo('Invoking $$PREBID_GLOBAL$$.removeAdUnit', arguments);
if (adUnitCode) {
for (var i = 0; i < $$PREBID_GLOBAL$$.adUnits.length; i++) {

if (!adUnitCode) {
$$PREBID_GLOBAL$$.adUnits = [];
return;
}

let adUnitCodes;

if (utils.isArray(adUnitCode)) {
adUnitCodes = adUnitCode;
} else {
adUnitCodes = [adUnitCode];
}

adUnitCodes.forEach((adUnitCode) => {
for (let i = 0; i < $$PREBID_GLOBAL$$.adUnits.length; i++) {
if ($$PREBID_GLOBAL$$.adUnits[i].code === adUnitCode) {
$$PREBID_GLOBAL$$.adUnits.splice(i, 1);
}
}
}
})
};

/**
Expand Down
59 changes: 59 additions & 0 deletions test/spec/unit/pbjs_api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2138,6 +2138,65 @@ describe('Unit: Prebid Module', function () {
$$PREBID_GLOBAL$$.removeAdUnit('adUnit1');
assert.deepEqual($$PREBID_GLOBAL$$.adUnits, [adUnit2]);
});
it('should remove all adUnits in adUnits array if no adUnits are given', function () {
const adUnit1 = {
code: 'adUnit1',
bids: [{
bidder: 'appnexus',
params: { placementId: '123' }
}]
};
const adUnit2 = {
code: 'adUnit2',
bids: [{
bidder: 'rubicon',
params: {
accountId: '1234',
siteId: '1234',
zoneId: '1234'
}
}]
};
const adUnits = [adUnit1, adUnit2];
$$PREBID_GLOBAL$$.adUnits = adUnits;
$$PREBID_GLOBAL$$.removeAdUnit();
assert.deepEqual($$PREBID_GLOBAL$$.adUnits, []);
});
it('should remove adUnits which match addUnitCodes in adUnit array argument', function () {
const adUnit1 = {
code: 'adUnit1',
bids: [{
bidder: 'appnexus',
params: { placementId: '123' }
}]
};
const adUnit2 = {
code: 'adUnit2',
bids: [{
bidder: 'rubicon',
params: {
accountId: '1234',
siteId: '1234',
zoneId: '1234'
}
}]
};
const adUnit3 = {
code: 'adUnit3',
bids: [{
bidder: 'rubicon3',
params: {
accountId: '12345',
siteId: '12345',
zoneId: '12345'
}
}]
};
const adUnits = [adUnit1, adUnit2, adUnit3];
$$PREBID_GLOBAL$$.adUnits = adUnits;
$$PREBID_GLOBAL$$.removeAdUnit([adUnit1.code, adUnit2.code]);
assert.deepEqual($$PREBID_GLOBAL$$.adUnits, [adUnit3]);
});
});

describe('getDealTargeting', function () {
Expand Down