Skip to content

Commit

Permalink
size mapping throws warning if mediaQuery missing (#2114)
Browse files Browse the repository at this point in the history
* size mapping throws warning if mediaQuery missing

* added unit test for missing mediaQuery in sizeConfig
  • Loading branch information
snapwich authored and jaiminpanchal27 committed Feb 14, 2018
1 parent 9cb9f2a commit 2973f76
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/sizeMapping.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { config } from 'src/config';
import { logWarn } from 'src/utils';
import includes from 'core-js/library/fn/array/includes';

let sizeConfig = [];
Expand Down Expand Up @@ -64,17 +65,20 @@ function evaluateSizeConfig(configs) {
return configs.reduce((results, config) => {
if (
typeof config === 'object' &&
typeof config.mediaQuery === 'string' &&
matchMedia(config.mediaQuery).matches
typeof config.mediaQuery === 'string'
) {
if (Array.isArray(config.sizesSupported)) {
results.shouldFilter = true;
if (matchMedia(config.mediaQuery).matches) {
if (Array.isArray(config.sizesSupported)) {
results.shouldFilter = true;
}
['labels', 'sizesSupported'].forEach(
type => (config[type] || []).forEach(
thing => results[type][thing] = true
)
);
}
['labels', 'sizesSupported'].forEach(
type => (config[type] || []).forEach(
thing => results[type][thing] = true
)
);
} else {
logWarn('sizeConfig rule missing required property "mediaQuery"');
}
return results;
}, {
Expand Down
14 changes: 14 additions & 0 deletions test/spec/sizeMapping_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { expect } from 'chai';
import { resolveStatus, setSizeConfig } from 'src/sizeMapping';
import includes from 'core-js/library/fn/array/includes';

let utils = require('src/utils');
let deepClone = utils.deepClone;

describe('sizeMapping', () => {
var testSizes = [[970, 90], [728, 90], [300, 250], [300, 100], [80, 80]];

Expand Down Expand Up @@ -68,6 +71,17 @@ describe('sizeMapping', () => {
});

describe('when handling sizes', () => {
it('should log a warning when mediaQuery property missing from sizeConfig', () => {
let errorConfig = deepClone(sizeConfig);

delete errorConfig[0].mediaQuery;

sandbox.stub(utils, 'logWarn');

resolveStatus(undefined, testSizes, errorConfig);
expect(utils.logWarn.firstCall.args[0]).to.match(/missing.+?mediaQuery/);
});

it('when one mediaQuery block matches, it should filter the adUnit.sizes passed in', () => {
matchMediaOverride = (str) => str === '(min-width: 1200px)' ? {matches: true} : {matches: false};

Expand Down

0 comments on commit 2973f76

Please sign in to comment.