From 81c157b1578c35fc7d82b067f7482ee06e973f50 Mon Sep 17 00:00:00 2001 From: Blake McIntyre Date: Tue, 21 Mar 2017 11:03:19 -0400 Subject: [PATCH 1/2] Fix Empty adSlot Object Caused by Ad Blocker Issue When using an ad blocker adSlot can become an empty object which causes the call to adSlot.getServices to produce the error: "getServices is not a function". Solution By adding the check adSlot.hasOwnProperty("getServices") we can ensure that the adSlot object has the correct function before attempting to call it. --- src/Bling.js | 2 +- src/createManager.js | 9 ++++++--- test/Bling.spec.js | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/Bling.js b/src/Bling.js index de8feff..968fdcd 100644 --- a/src/Bling.js +++ b/src/Bling.js @@ -668,7 +668,7 @@ class Bling extends Component { clear() { const adSlot = this._adSlot; - if (adSlot) { + if (adSlot && adSlot.hasOwnProperty("getServices")) { // googletag.ContentService doesn't clear content const services = adSlot.getServices(); if (this._divId && services.some(s => !!s.setContent)) { diff --git a/src/createManager.js b/src/createManager.js index e3eeda9..e97abca 100644 --- a/src/createManager.js +++ b/src/createManager.js @@ -321,9 +321,12 @@ export class AdManager extends EventEmitter { if (!instance.notInViewport()) { instance.defineSlot(); const adSlot = instance.adSlot; - const services = adSlot.getServices(); - if (!hasPubAdsService) { - hasPubAdsService = services.filter(service => !!service.enableAsyncRendering).length > 0; + + if (adSlot && adSlot.hasOwnProperty("getServices")) { + const services = adSlot.getServices(); + if (!hasPubAdsService) { + hasPubAdsService = services.filter(service => !!service.enableAsyncRendering).length > 0; + } } } }); diff --git a/test/Bling.spec.js b/test/Bling.spec.js index 2c56f16..329b06b 100644 --- a/test/Bling.spec.js +++ b/test/Bling.spec.js @@ -216,6 +216,20 @@ describe("Bling", () => { clear.restore(); }); + it("handles empty adSlot on clear", () => { + const instance = new Bling(); + instance._adSlot = {}; + instance.clear(); + }); + + it("calls getServices on adSlot on clear", () => { + const instance = new Bling(); + const adSlot = sinon.mock({getServices: () => {}}); + adSlot.expects("getServices").once(); + instance._adSlot = adSlot; + instance.clear(); + }); + it("updates correlator", () => { const updateCorrelator = sinon.stub(Bling._adManager, "updateCorrelator"); From b640665953f58f9c41f0bfc58cebbe65bd699c12 Mon Sep 17 00:00:00 2001 From: Blake McIntyre Date: Wed, 22 Mar 2017 16:55:01 -0400 Subject: [PATCH 2/2] Updating test to verify an exception is not thrown. --- test/Bling.spec.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/Bling.spec.js b/test/Bling.spec.js index 329b06b..8fd61fa 100644 --- a/test/Bling.spec.js +++ b/test/Bling.spec.js @@ -219,7 +219,10 @@ describe("Bling", () => { it("handles empty adSlot on clear", () => { const instance = new Bling(); instance._adSlot = {}; - instance.clear(); + + expect(() => { + instance.clear(); + }).to.not.throw("adSlot.getServices is not a function"); }); it("calls getServices on adSlot on clear", () => {