diff --git a/modules/geoedgeRtdProvider.js b/modules/geoedgeRtdProvider.js index 9bdf74c6e3c..37db5860001 100644 --- a/modules/geoedgeRtdProvider.js +++ b/modules/geoedgeRtdProvider.js @@ -17,11 +17,12 @@ import { submodule } from '../src/hook.js'; import { ajax } from '../src/ajax.js'; -import { generateUUID, insertElement, isEmpty, logError } from '../src/utils.js'; +import { generateUUID, createInvisibleIframe, insertElement, isEmpty, logError } from '../src/utils.js'; import * as events from '../src/events.js'; import CONSTANTS from '../src/constants.json'; import { loadExternalScript } from '../src/adloader.js'; import { auctionManager } from '../src/auctionManager.js'; +import { getRefererInfo } from '../src/refererDetection.js'; /** @type {string} */ const SUBMODULE_NAME = 'geoedge'; @@ -69,17 +70,38 @@ export function setWrapper(responseText) { wrapper = responseText; } +export function getInitialParams(key) { + let refererInfo = getRefererInfo(); + let params = { + wver: 'pbjs', + wtype: 'pbjs-module', + key, + meta: { + topUrl: refererInfo.page + }, + site: refererInfo.domain, + pimp: PV_ID, + fsRan: true, + frameApi: true + }; + return params; +} + +export function markAsLoaded() { + preloaded = true; +} + /** * preloads the client * @param {string} key */ export function preloadClient(key) { - let link = document.createElement('link'); - link.rel = 'preload'; - link.as = 'script'; - link.href = getClientUrl(key); - link.onload = () => { preloaded = true }; - insertElement(link); + let iframe = createInvisibleIframe(); + iframe.id = 'grumiFrame'; + insertElement(iframe); + iframe.contentWindow.grumi = getInitialParams(key); + let url = getClientUrl(key); + loadExternalScript(url, SUBMODULE_NAME, markAsLoaded, iframe.contentDocument); } /** diff --git a/test/spec/modules/geoedgeRtdProvider_spec.js b/test/spec/modules/geoedgeRtdProvider_spec.js index b0307a60a3c..211a3efa3c6 100644 --- a/test/spec/modules/geoedgeRtdProvider_spec.js +++ b/test/spec/modules/geoedgeRtdProvider_spec.js @@ -1,18 +1,21 @@ import * as utils from '../../../src/utils.js'; import {loadExternalScript} from '../../../src/adloader.js'; -import { +import * as geoedgeRtdModule from '../../../modules/geoedgeRtdProvider.js'; +import {server} from '../../../test/mocks/xhr.js'; +import * as events from '../../../src/events.js'; +import CONSTANTS from '../../../src/constants.json'; + +let { geoedgeSubmodule, getClientUrl, getInPageUrl, htmlPlaceholder, setWrapper, getMacros, - wrapper, - WRAPPER_URL -} from '../../../modules/geoedgeRtdProvider.js'; -import {server} from '../../../test/mocks/xhr.js'; -import * as events from '../../../src/events.js'; -import CONSTANTS from '../../../src/constants.json'; + WRAPPER_URL, + preloadClient, + markAsLoaded +} = geoedgeRtdModule; let key = '123123123'; function makeConfig(gpt) { @@ -65,13 +68,11 @@ describe('Geoedge RTD module', function () { }); }); describe('init', function () { - let insertElementStub; - before(function () { - insertElementStub = sinon.stub(utils, 'insertElement'); + sinon.spy(geoedgeRtdModule, 'preloadClient'); }); after(function () { - utils.insertElement.restore(); + geoedgeRtdModule.preloadClient.restore(); }); it('should return false when missing params or key', function () { let missingParams = geoedgeSubmodule.init({}); @@ -87,14 +88,13 @@ describe('Geoedge RTD module', function () { let isWrapperRequest = request && request.url && request.url && request.url === WRAPPER_URL; expect(isWrapperRequest).to.equal(true); }); - it('should preload the client', function () { - let isLinkPreloadAsScript = arg => arg.tagName === 'LINK' && arg.rel === 'preload' && arg.as === 'script' && arg.href === getClientUrl(key); - expect(insertElementStub.calledWith(sinon.match(isLinkPreloadAsScript))).to.equal(true); + it('should call preloadClient', function () { + expect(preloadClient.called); }); it('should emit billable events with applicable winning bids', function (done) { let counter = 0; events.on(CONSTANTS.EVENTS.BILLABLE_EVENT, function (event) { - if (event.vendor === 'geoedge' && event.type === 'impression') { + if (event.vendor === geoedgeSubmodule.name && event.type === 'impression') { counter += 1; } expect(counter).to.equal(1); @@ -104,7 +104,7 @@ describe('Geoedge RTD module', function () { }); it('should load the in page code when gpt params is true', function () { geoedgeSubmodule.init(makeConfig(true)); - let isInPageUrl = arg => arg == getInPageUrl(key); + let isInPageUrl = arg => arg === getInPageUrl(key); expect(loadExternalScript.calledWith(sinon.match(isInPageUrl))).to.equal(true); }); it('should set the window.grumi config object when gpt params is true', function () { @@ -112,10 +112,27 @@ describe('Geoedge RTD module', function () { expect(hasGrumiObj && window.grumi.key === key && window.grumi.fromPrebid).to.equal(true); }); }); + describe('preloadClient', function () { + let iframe; + preloadClient(key); + let loadExternalScriptCall = loadExternalScript.getCall(0); + it('should create an invisible iframe and insert it to the DOM', function () { + iframe = document.getElementById('grumiFrame'); + expect(iframe && iframe.style.display === 'none'); + }); + it('should assign params object to the iframe\'s window', function () { + let grumi = iframe.contentWindow.grumi; + expect(grumi.key).to.equal(key); + }); + it('should preload the client into the iframe', function () { + let isClientUrl = arg => arg === getClientUrl(key); + expect(loadExternalScriptCall.calledWithMatch(isClientUrl)).to.equal(true); + }); + }); describe('setWrapper', function () { it('should set the wrapper', function () { setWrapper(mockWrapper); - expect(wrapper).to.equal(mockWrapper); + expect(geoedgeRtdModule.wrapper).to.equal(mockWrapper); }); }); describe('getMacros', function () {