Skip to content
This repository has been archived by the owner on Apr 1, 2020. It is now read-only.

Ads - specifying ad loading viewport margins for different screensizes/page types #1094

Merged
merged 7 commits into from
Sep 27, 2017
30 changes: 15 additions & 15 deletions components/n-ui/ads/js/oAdsConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,21 @@ module.exports = function (flags, appName, adOptions) {
return zone.join('/');
}

function setViewportMarginBy (variant) {
switch (variant) {
case '5':
return'5%';
break;
case '10':
return '10%';
break;
case '15':
return '15%';
break;
default:
return '0%';
function setViewportMarginBy () {
let pt = appName;
pt = pt.toLowerCase().substr(0, 3);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to substring it here? Might be more readable later on to refer to 'front-page' (or whatever the full name is)

let scrnSize = utils.getScreenSize();
if (pt === 'fro') {
if (scrnSize < 760) {return '15%';}
else {
if (pt === 'fro' && scrnSize < 980) {return '5%';}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checking 'fro' again here shouldn't be necessary

}
}
if (pt === 'str'){
if (scrnSize < 760) {return '5%';}
else if (scrnSize < 980) {return '15%';}
}
return '0%';
}

function isTargetSize () {
Expand All @@ -78,8 +79,7 @@ module.exports = function (flags, appName, adOptions) {
function getViewportMargin () {
let viewportMargin = '0%';
if (flags.get('adOptimizeLazyLoad') && isTargetSize() ) {
const variant = flags.get('adOptimizeLazyLoad');
viewportMargin = setViewportMarginBy(variant);
viewportMargin = setViewportMarginBy();
}
return viewportMargin;
}
Expand Down
94 changes: 69 additions & 25 deletions components/n-ui/ads/test/oAdsConfig.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,36 +141,80 @@ describe('Config', () => {
expect(config.lazyLoad.viewportMargin).to.equal('0%');
});

context('when screen size is less than 980px and adOptimizeLazyLoad flag is defined', () => {

beforeEach(() => {
sandbox.stub(utils, 'getScreenSize', () => { return 979; });
});

afterEach(() => {
sandbox.restore();
});
it('Should pass 15% when screen size is narrower than 760px and adOptimizeLazyLoad flag is defined and appName is fro', () => {
sandbox.stub(utils, 'getScreenSize', () => { return 750; });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably remove the flag from the tests...it's a little confusing otherwise

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was planning to keep the flag but update it so that its no longer an MVT - this would allow us to toggle the viewport margin. However, i guess it doesnt give us much benefit so, as you say it might be less confusing to remove it.

const flags = { get: (flagName) => {
switch (flagName) {
case 'adOptimizeLazyLoad':
return '50';
break;
default:
return true;
}
}};
const config = oAdsConfig(flags, 'front');
expect(config.lazyLoad.viewportMargin).to.equal('15%');
});

['5', '10', '15'].forEach( margin => {

it(`Should pass ${margin}% when the flag\'s value is ${margin}`, () => {
const flags = { get: (flagName) => {
switch (flagName) {
case 'adOptimizeLazyLoad':
return margin;
break;
default:
return true;
}
}};
const config = oAdsConfig(flags, 'article');
expect(config.lazyLoad.viewportMargin).to.equal(`${margin}%`);
});
it('Should pass 5% when screen size is narrower than 980px and wider than 759px and adOptimizeLazyLoad flag is defined and appName is fro', () => {
sandbox.stub(utils, 'getScreenSize', () => { return 970; });
const flags = { get: (flagName) => {
switch (flagName) {
case 'adOptimizeLazyLoad':
return '50';
break;
default:
return true;
}
}};
const config = oAdsConfig(flags, 'front');
expect(config.lazyLoad.viewportMargin).to.equal('5%');
});

});
it('Should pass 5% when screen size is narrower than 760px and adOptimizeLazyLoad flag is defined and appName is stream', () => {
sandbox.stub(utils, 'getScreenSize', () => { return 750; });
const flags = { get: (flagName) => {
switch (flagName) {
case 'adOptimizeLazyLoad':
return '50';
break;
default:
return true;
}
}};
const config = oAdsConfig(flags, 'stream');
expect(config.lazyLoad.viewportMargin).to.equal('5%');
});

it('Should pass 15% when screen size is narrower than 980px and wider than 759px and adOptimizeLazyLoad flag is defined and appName is stream', () => {
sandbox.stub(utils, 'getScreenSize', () => { return 970; });
const flags = { get: (flagName) => {
switch (flagName) {
case 'adOptimizeLazyLoad':
return '50';
break;
default:
return true;
}
}};
const config = oAdsConfig(flags, 'stream');
expect(config.lazyLoad.viewportMargin).to.equal('15%');
});

it('Should pass 0% when adOptimizeLazyLoad flag is defined and appName is article', () => {
sandbox.stub(utils, 'getScreenSize', () => { return 970; });
const flags = { get: (flagName) => {
switch (flagName) {
case 'adOptimizeLazyLoad':
return '50';
break;
default:
return true;
}
}};
const config = oAdsConfig(flags, 'article');
expect(config.lazyLoad.viewportMargin).to.equal('0%');
});
});

describe('o-ads', () => {
Expand Down