Skip to content

Commit

Permalink
Restructure data for global window variable. (#8152)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcallari committed Mar 7, 2022
1 parent 9875c33 commit f67b786
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 29 deletions.
28 changes: 17 additions & 11 deletions modules/optimeraRtdProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export function scoreFileRequest() {
export function returnTargetingData(adUnits, config) {
const targeting = {};
try {
adUnits.forEach(function(adUnit) {
adUnits.forEach((adUnit) => {
if (optimeraTargeting[adUnit]) {
targeting[adUnit] = {};
targeting[adUnit][optimeraKeyName] = [optimeraTargeting[adUnit]];
Expand Down Expand Up @@ -141,12 +141,11 @@ export function init(moduleConfig) {
setScoresURL();
scoreFileRequest();
return true;
} else {
if (!_moduleParams.clientID) {
logError('Optimera clientID is missing in the Optimera RTD configuration.');
}
return false;
}
if (!_moduleParams.clientID) {
logError('Optimera clientID is missing in the Optimera RTD configuration.');
}
return false;
}

/**
Expand All @@ -163,7 +162,7 @@ export function init(moduleConfig) {
export function setScoresURL() {
const optimeraHost = window.location.host;
const optimeraPathName = window.location.pathname;
let newScoresURL = `${scoresBaseURL}${clientID}/${optimeraHost}${optimeraPathName}.js`;
const newScoresURL = `${scoresBaseURL}${clientID}/${optimeraHost}${optimeraPathName}.js`;
if (scoresURL !== newScoresURL) {
scoresURL = newScoresURL;
fetchScoreFile = true;
Expand All @@ -174,7 +173,7 @@ export function setScoresURL() {

/**
* Set the scores for the device if given.
* Add any any insights to the winddow.optimeraInsights object.
* Add data and insights to the winddow.optimera object.
*
* @param {*} result
* @returns {string} JSON string of Optimera Scores.
Expand All @@ -187,12 +186,19 @@ export function setScores(result) {
scores = scores.device[device];
}
logInfo(scores);
window.optimera = window.optimera || {};
window.optimera.data = window.optimera.data || {};
window.optimera.insights = window.optimera.insights || {};
Object.keys(scores).map((key) => {
if (key !== 'insights') {
window.optimera.data[key] = scores[key];
}
});
if (scores.insights) {
window.optimeraInsights = window.optimeraInsights || {};
window.optimeraInsights.data = scores.insights;
window.optimera.insights = scores.insights;
}
} catch (e) {
logError('Optimera score file could not be parsed.');
logError('Optimera score file could not be parsed.', e);
}
optimeraTargeting = scores;
}
Expand Down
70 changes: 52 additions & 18 deletions test/spec/modules/optimeraRtdProvider_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as optimeraRTD from '../../../modules/optimeraRtdProvider.js';
let utils = require('src/utils.js');

const utils = require('src/utils.js');

describe('Optimera RTD sub module', () => {
it('should init, return true, and set the params', () => {
Expand Down Expand Up @@ -31,53 +32,86 @@ describe('Optimera RTD score file properly sets targeting values', () => {
const scores = {
'div-0': ['A1', 'A2'],
'div-1': ['A3', 'A4'],
'device': {
'de': {
device: {
de: {
'div-0': ['A5', 'A6'],
'div-1': ['A7', 'A8'],
'insights': {
'ilv': ['div-0'],
'miv': ['div-4'],
insights: {
ilv: ['div-0'],
miv: ['div-4'],
}
},
'mo': {
mo: {
'div-0': ['A9', 'B0'],
'div-1': ['B1', 'B2'],
'insights': {
'ilv': ['div-1'],
'miv': ['div-2'],
insights: {
ilv: ['div-1'],
miv: ['div-2'],
}
}
},
'insights': {
'ilv': ['div-5'],
'miv': ['div-6'],
insights: {
ilv: ['div-5'],
miv: ['div-6'],
}
};
it('Properly set the score file url and scores', () => {
optimeraRTD.setScores(JSON.stringify(scores));
expect(optimeraRTD.optimeraTargeting['div-0']).to.include.ordered.members(['A5', 'A6']);
expect(optimeraRTD.optimeraTargeting['div-1']).to.include.ordered.members(['A7', 'A8']);
expect(window.optimeraInsights.data['ilv']).to.include.ordered.members(['div-0']);
});
});

describe('Optimera RTD propery sets the window.optimera object', () => {
const scores = {
'div-0': ['A1', 'A2'],
'div-1': ['A3', 'A4'],
device: {
de: {
'div-0': ['A5', 'A6'],
'div-1': ['A7', 'A8'],
insights: {
ilv: ['div-0'],
miv: ['div-4'],
}
},
mo: {
'div-0': ['A9', 'B0'],
'div-1': ['B1', 'B2'],
insights: {
ilv: ['div-1'],
miv: ['div-2'],
}
}
},
insights: {
ilv: ['div-5'],
miv: ['div-6'],
}
};
it('Properly set the score file url and scores', () => {
optimeraRTD.setScores(JSON.stringify(scores));
expect(window.optimera.data['div-1']).to.include.ordered.members(['A7', 'A8']);
expect(window.optimera.insights.ilv).to.include.ordered.members(['div-0']);
});
});

describe('Optimera RTD targeting object is properly formed', () => {
const adDivs = ['div-0', 'div-1'];
it('applyTargeting properly created the targeting object', () => {
const targeting = optimeraRTD.returnTargetingData(adDivs);
expect(targeting).to.deep.include({'div-0': {'optimera': [['A5', 'A6']]}});
expect(targeting).to.deep.include({'div-1': {'optimera': [['A7', 'A8']]}});
expect(targeting).to.deep.include({ 'div-0': { optimera: [['A5', 'A6']] } });
expect(targeting).to.deep.include({ 'div-1': { optimera: [['A7', 'A8']] } });
});
});

describe('Optimera RTD error logging', () => {
let utilsLogErrorStub;

beforeEach(function () {
beforeEach(() => {
utilsLogErrorStub = sinon.stub(utils, 'logError');
});
afterEach(function () {
afterEach(() => {
utilsLogErrorStub.restore();
});

Expand Down

0 comments on commit f67b786

Please sign in to comment.