Skip to content

Commit

Permalink
Rubicon project fix remove ad unit iteration (#535)
Browse files Browse the repository at this point in the history
* iterate over adUnits backwards to prevent errors with in-place array mutation

* add unit test for removeAdUnit
  • Loading branch information
Matt Kendall authored and protonate committed Aug 20, 2016
1 parent e4e4f67 commit 1285731
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ $$PREBID_GLOBAL$$.renderAd = function (doc, id) {
$$PREBID_GLOBAL$$.removeAdUnit = function (adUnitCode) {
utils.logInfo('Invoking $$PREBID_GLOBAL$$.removeAdUnit', arguments);
if (adUnitCode) {
for (var i = 0; i < $$PREBID_GLOBAL$$.adUnits.length; i++) {
for (var i = $$PREBID_GLOBAL$$.adUnits.length - 1; i >= 0; i--) {
if ($$PREBID_GLOBAL$$.adUnits[i].code === adUnitCode) {
$$PREBID_GLOBAL$$.adUnits.splice(i, 1);
}
Expand Down
30 changes: 30 additions & 0 deletions test/spec/unit/pbjs_api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -834,4 +834,34 @@ describe('Unit: Prebid Module', function () {
$$PREBID_GLOBAL$$._winningBids = [];
});
});

describe('removeAdUnit', () => {
it('should remove given adUnit in adUnits array', () => {
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('foobar');
assert.deepEqual($$PREBID_GLOBAL$$.adUnits, adUnits);
$$PREBID_GLOBAL$$.removeAdUnit('adUnit1');
assert.deepEqual($$PREBID_GLOBAL$$.adUnits, [adUnit2]);

});
});
});

0 comments on commit 1285731

Please sign in to comment.