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

Geodge RTD module: update preload mechanism #10911

Merged
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
36 changes: 29 additions & 7 deletions modules/geoedgeRtdProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}

/**
Expand Down
51 changes: 34 additions & 17 deletions test/spec/modules/geoedgeRtdProvider_spec.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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({});
Expand All @@ -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);
Expand All @@ -104,18 +104,35 @@ 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 () {
let hasGrumiObj = typeof window.grumi === 'object';
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 () {
Expand Down