diff --git a/src/adRendering.js b/src/adRendering.js index 47f335664e5..4b1f2960428 100644 --- a/src/adRendering.js +++ b/src/adRendering.js @@ -137,11 +137,12 @@ export const getRenderingData = hook('sync', function (bidResponse, options) { }; }) -export const doRender = hook('sync', function({renderFn, resizeFn, bidResponse, options}) { - if (FEATURES.VIDEO && bidResponse.mediaType === VIDEO) { +export const doRender = hook('sync', function({renderFn, resizeFn, bidResponse, options, doc, isMainDocument = doc === document && !inIframe()}) { + const videoBid = (FEATURES.VIDEO && bidResponse.mediaType === VIDEO) + if (isMainDocument || videoBid) { emitAdRenderFail({ reason: AD_RENDER_FAILED_REASON.PREVENT_WRITING_ON_MAIN_DOCUMENT, - message: 'Cannot render video ad', + message: videoBid ? 'Cannot render video ad without a renderer' : `renderAd was prevented from writing to the main document.`, bid: bidResponse, id: bidResponse.adId }); @@ -277,14 +278,10 @@ export function renderAdDirect(doc, adId, options) { if (!adId || !doc) { fail(AD_RENDER_FAILED_REASON.MISSING_DOC_OR_ADID, `missing ${adId ? 'doc' : 'adId'}`); } else { - if ((doc === document && !inIframe())) { - fail(AD_RENDER_FAILED_REASON.PREVENT_WRITING_ON_MAIN_DOCUMENT, `renderAd was prevented from writing to the main document.`); - } else { - getBidToRender(adId).then(bidResponse => { - bid = bidResponse; - handleRender({renderFn, resizeFn, adId, options: {clickUrl: options?.clickThrough}, bidResponse, doc}); - }); - } + getBidToRender(adId).then(bidResponse => { + bid = bidResponse; + handleRender({renderFn, resizeFn, adId, options: {clickUrl: options?.clickThrough}, bidResponse, doc}); + }); } } catch (e) { fail(EXCEPTION, e.message); diff --git a/test/spec/unit/adRendering_spec.js b/test/spec/unit/adRendering_spec.js index c3bebf45350..bb9ba9215cf 100644 --- a/test/spec/unit/adRendering_spec.js +++ b/test/spec/unit/adRendering_spec.js @@ -127,6 +127,12 @@ describe('adRendering', () => { sinon.assert.called(bidResponse.renderer.render); }); + it('allows rendering on the main document', () => { + doRender({renderFn, bidResponse, isMainDocument: true}); + sinon.assert.notCalled(renderFn); + sinon.assert.called(bidResponse.renderer.render); + }) + it('emits AD_RENDER_SUCCEDED', () => { doRender({renderFn, bidResponse}); sinon.assert.calledWith(events.emit, EVENTS.AD_RENDER_SUCCEEDED, sinon.match({ @@ -144,6 +150,11 @@ describe('adRendering', () => { }); } + it('should emit AD_RENDER_FAILED when renderer-less bid is being rendered on the main document', () => { + doRender({renderFn, bidResponse, isMainDocument: true}); + expectAdRenderFailedEvent(AD_RENDER_FAILED_REASON.PREVENT_WRITING_ON_MAIN_DOCUMENT); + }); + it('invokes renderFn with rendering data', () => { const data = {ad: 'creative'}; getRenderingDataStub.returns(data);