Skip to content

Commit 08a9b1c

Browse files
authored
(refactor) allow ['fluid'] slotSize fixes #72 (#74)
* (refactor) allow ['fluid'] slotSize * (refactor) remove logs and broken test
1 parent 24f6ace commit 08a9b1c

File tree

4 files changed

+106
-46
lines changed

4 files changed

+106
-46
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ lib/
44
dist/
55
coverage/
66
npm-debug.log
7+
yarn-error.log
78
.DS_Store

src/Bling.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/* eslint-disable react/sort-comp */
2-
import React, {Component} from "react";
2+
import React, { Component } from "react";
33
import PropTypes from "prop-types";
44
import ReactDOM from "react-dom";
55
import invariant from "invariant";
66
import deepEqual from "deep-equal";
77
import hoistStatics from "hoist-non-react-statics";
88
import Events from "./Events";
99
import filterPropsSimple from "./utils/filterProps";
10-
import {createManager, pubadsAPI} from "./createManager";
10+
import { createManager, pubadsAPI } from "./createManager";
1111
/**
1212
* An Ad Component using Google Publisher Tags.
1313
* This component should work standalone w/o context.
@@ -387,8 +387,8 @@ class Bling extends Component {
387387
}
388388

389389
componentWillReceiveProps(nextProps) {
390-
const {propsEqual} = Bling._config;
391-
const {sizeMapping} = this.props;
390+
const { propsEqual } = Bling._config;
391+
const { sizeMapping } = this.props;
392392
if (
393393
(nextProps.sizeMapping || sizeMapping) &&
394394
!propsEqual(nextProps.sizeMapping, sizeMapping)
@@ -400,7 +400,7 @@ class Bling extends Component {
400400
shouldComponentUpdate(nextProps, nextState) {
401401
// if adUnitPath changes, need to create a new slot, re-render
402402
// otherwise, just refresh
403-
const {scriptLoaded, inViewport} = nextState;
403+
const { scriptLoaded, inViewport } = nextState;
404404
const notInViewport = this.notInViewport(nextProps, nextState);
405405
const inViewportChanged = this.state.inViewport !== inViewport;
406406
const isScriptLoaded = this.state.scriptLoaded !== scriptLoaded;
@@ -412,7 +412,7 @@ class Bling extends Component {
412412
return true;
413413
}
414414

415-
const {filterProps, propsEqual} = Bling._config;
415+
const { filterProps, propsEqual } = Bling._config;
416416
const refreshableProps = filterProps(
417417
Bling.refreshableProps,
418418
this.props,
@@ -430,7 +430,6 @@ class Bling extends Component {
430430
const shouldRefresh =
431431
!shouldRender &&
432432
!propsEqual(refreshableProps.props, refreshableProps.nextProps);
433-
// console.log(`shouldRefresh: ${shouldRefresh}, shouldRender: ${shouldRender}, isScriptLoaded: ${isScriptLoaded}, syncCorrelator: ${Bling._adManager._syncCorrelator}`);
434433

435434
if (shouldRefresh) {
436435
this.configureSlot(this._adSlot, nextProps);
@@ -479,12 +478,12 @@ class Bling extends Component {
479478
}
480479

481480
onScriptLoaded() {
482-
const {onScriptLoaded} = this.props;
481+
const { onScriptLoaded } = this.props;
483482

484483
if (this.getRenderWhenViewable()) {
485484
this.foldCheck();
486485
}
487-
this.setState({scriptLoaded: true}, onScriptLoaded); // eslint-disable-line react/no-did-mount-set-state
486+
this.setState({ scriptLoaded: true }, onScriptLoaded); // eslint-disable-line react/no-did-mount-set-state
488487
}
489488

490489
onScriptError(err) {
@@ -509,7 +508,10 @@ class Bling extends Component {
509508
if (Array.isArray(slotSize) && Array.isArray(slotSize[0])) {
510509
slotSize = slotSize[0];
511510
}
512-
if (slotSize === "fluid") {
511+
if (
512+
slotSize === "fluid" ||
513+
(Array.isArray(slotSize) && slotSize[0] === "fluid")
514+
) {
513515
slotSize = [0, 0];
514516
}
515517

@@ -519,7 +521,7 @@ class Bling extends Component {
519521
this.viewableThreshold
520522
);
521523
if (inViewport) {
522-
this.setState({inViewport: true});
524+
this.setState({ inViewport: true });
523525
}
524526
}
525527

@@ -594,12 +596,12 @@ class Bling extends Component {
594596
}
595597

596598
notInViewport(props = this.props, state = this.state) {
597-
const {inViewport} = state;
599+
const { inViewport } = state;
598600
return this.getRenderWhenViewable(props) && !inViewport;
599601
}
600602

601603
defineSlot() {
602-
const {adUnitPath, outOfPage} = this.props;
604+
const { adUnitPath, outOfPage } = this.props;
603605
const divId = this._divId;
604606
const slotSize = this.getSlotSize();
605607

@@ -690,7 +692,7 @@ class Bling extends Component {
690692
}
691693

692694
display() {
693-
const {content} = this.props;
695+
const { content } = this.props;
694696
const divId = this._divId;
695697
const adSlot = this._adSlot;
696698

@@ -735,8 +737,8 @@ class Bling extends Component {
735737
}
736738

737739
render() {
738-
const {scriptLoaded} = this.state;
739-
const {id, outOfPage, style} = this.props;
740+
const { scriptLoaded } = this.state;
741+
const { id, outOfPage, style } = this.props;
740742
const shouldNotRender = this.notInViewport(this.props, this.state);
741743

742744
if (!scriptLoaded || shouldNotRender) {
@@ -753,7 +755,10 @@ class Bling extends Component {
753755
slotSize = slotSize[0];
754756
}
755757
// https://developers.google.com/doubleclick-gpt/reference?hl=en#googletag.NamedSize
756-
if (slotSize === "fluid") {
758+
if (
759+
slotSize === "fluid" ||
760+
(Array.isArray(slotSize) && slotSize[0] === "fluid")
761+
) {
757762
slotSize = ["auto", "auto"];
758763
}
759764
const emptyStyle = slotSize && {
@@ -780,7 +785,8 @@ class Bling extends Component {
780785
export default hoistStatics(
781786
Bling,
782787
pubadsAPI.reduce((api, method) => {
783-
api[method] = (...args) => Bling._adManager.pubadsProxy({method, args});
788+
api[method] = (...args) =>
789+
Bling._adManager.pubadsProxy({ method, args });
784790
return api;
785791
}, {})
786792
);

test/Bling.spec.js

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
/* eslint-disable react/no-multi-comp */
2-
import React, {Component} from "react";
2+
import React, { Component } from "react";
33
import PropTypes from "prop-types";
44
import ReactTestUtils from "react-dom/test-utils";
55
import ShallowRenderer from "react-test-renderer/shallow";
66
import Bling from "../src/Bling";
77
import Events from "../src/Events";
8-
import {pubadsAPI, APIToCallBeforeServiceEnabled} from "../src/createManager";
9-
import {gptVersion, pubadsVersion} from "../src/utils/apiList";
10-
import {createManagerTest} from "../src/utils/createManagerTest";
8+
import { pubadsAPI, APIToCallBeforeServiceEnabled } from "../src/createManager";
9+
import { gptVersion, pubadsVersion } from "../src/utils/apiList";
10+
import { createManagerTest } from "../src/utils/createManagerTest";
1111

1212
describe("Bling", () => {
1313
let googletag;
1414
const stubs = [];
1515

1616
beforeEach(() => {
17-
Bling.configure({renderWhenViewable: false});
17+
Bling.configure({ renderWhenViewable: false });
1818
Bling.testManager = createManagerTest();
1919
googletag = Bling._adManager.googletag;
2020
});
@@ -43,7 +43,27 @@ describe("Bling", () => {
4343
);
4444
const result = renderer.getRenderOutput();
4545
expect(result.type).to.equal("div");
46-
expect(result.props.style).to.eql({width: 728, height: 90});
46+
expect(result.props.style).to.eql({ width: 728, height: 90 });
47+
});
48+
49+
it("renders fluid to auto width and height", () => {
50+
const renderer = new ShallowRenderer();
51+
renderer.render(
52+
<Bling adUnitPath="/4595/nfl.test.open" slotSize={"fluid"} />
53+
);
54+
const result = renderer.getRenderOutput();
55+
expect(result.type).to.equal("div");
56+
expect(result.props.style).to.eql({ width: "auto", height: "auto" });
57+
});
58+
59+
it("renders ['fluid'] to auto width and height", () => {
60+
const renderer = new ShallowRenderer();
61+
renderer.render(
62+
<Bling adUnitPath="/4595/nfl.test.open" slotSize={["fluid"]} />
63+
);
64+
const result = renderer.getRenderOutput();
65+
expect(result.type).to.equal("div");
66+
expect(result.props.style).to.eql({ width: "auto", height: "auto" });
4767
});
4868

4969
it("returns gpt version", done => {
@@ -239,7 +259,7 @@ describe("Bling", () => {
239259

240260
it("calls getServices on adSlot on clear", () => {
241261
const instance = new Bling();
242-
const adSlot = sinon.mock({getServices: () => {}});
262+
const adSlot = sinon.mock({ getServices: () => {} });
243263
adSlot.expects("getServices").once();
244264
instance._adSlot = adSlot;
245265
instance.clear();
@@ -296,16 +316,16 @@ describe("Bling", () => {
296316
<Bling
297317
adUnitPath="/4595/nfl.test.open"
298318
sizeMapping={[
299-
{viewport: [0, 0], slot: [320, 50]},
300-
{viewport: [750, 200], slot: [728, 90]},
301-
{viewport: [1050, 200], slot: [1024, 120]}
319+
{ viewport: [0, 0], slot: [320, 50] },
320+
{ viewport: [750, 200], slot: [728, 90] },
321+
{ viewport: [1050, 200], slot: [1024, 120] }
302322
]}
303323
/>
304324
);
305325
});
306326

307327
it("reflects targeting props to adSlot", done => {
308-
const targeting = {t1: "v1", t2: [1, 2, 3]};
328+
const targeting = { t1: "v1", t2: [1, 2, 3] };
309329

310330
Bling.once(Events.RENDER, () => {
311331
const adSlot = instance.adSlot;
@@ -372,7 +392,7 @@ describe("Bling", () => {
372392
const instance = ReactTestUtils.renderIntoDocument(
373393
<Bling
374394
adUnitPath="/4595/nfl.test.open"
375-
attributes={{attr1: "val1", attr2: "val2"}}
395+
attributes={{ attr1: "val1", attr2: "val2" }}
376396
slotSize={[300, 250]}
377397
/>
378398
);
@@ -575,20 +595,53 @@ describe("Bling", () => {
575595
);
576596
});
577597

598+
it("renders slotSize with an array", () => {
599+
const renderer = new ShallowRenderer();
600+
renderer.render(
601+
<Bling adUnitPath="/4595/nfl.test.open" slotSize={[[300, 250]]} />
602+
);
603+
const result = renderer.getRenderOutput();
604+
expect(result.type).to.equal("div");
605+
expect(result.props.style).to.eql({ width: 300, height: 250 });
606+
});
607+
608+
it("sets slotSize to 0,0 on foldCheck of 'fluid' or ['fluid']", done => {
609+
const isInViewport = sinon.spy(Bling._adManager, "isInViewport");
610+
611+
class Wrapper extends Component {
612+
onSlotRenderEnded = () => {
613+
expect(isInViewport.args[0][1].join()).to.equal([0, 0].join());
614+
done();
615+
};
616+
render() {
617+
return (
618+
<Bling
619+
adUnitPath="/4595/nfl.test.open"
620+
renderWhenViewable={true}
621+
slotSize={["fluid"]}
622+
onSlotRenderEnded={this.onSlotRenderEnded}
623+
/>
624+
);
625+
}
626+
}
627+
628+
ReactTestUtils.renderIntoDocument(<Wrapper />);
629+
});
630+
578631
it("refreshes ad when refreshable prop changes", done => {
579632
let count = 0;
580633

581634
Bling.syncCorrelator();
582635

583636
class Wrapper extends Component {
584637
state = {
585-
targeting: {prop: "val"}
638+
targeting: { prop: "val" }
586639
};
587640
onSlotRenderEnded = event => {
588641
if (count === 0) {
589642
expect(event.slot.getTargeting("prop")).to.equal("val");
590643
this.setState({
591-
targeting: {prop: "val2"}
644+
targeting: { prop: "val2" }
592645
});
593646
count++;
594647
} else {
@@ -597,7 +650,7 @@ describe("Bling", () => {
597650
}
598651
};
599652
render() {
600-
const {targeting} = this.state;
653+
const { targeting } = this.state;
601654
return (
602655
<Bling
603656
adUnitPath="/4595/nfl.test.open"
@@ -619,13 +672,13 @@ describe("Bling", () => {
619672

620673
class Wrapper extends Component {
621674
state = {
622-
targeting: {prop: "val"}
675+
targeting: { prop: "val" }
623676
};
624677
onSlotRenderEnded = event => {
625678
if (count === 0) {
626679
expect(event.slot.getTargeting("prop")).to.equal("val");
627680
this.setState({
628-
targeting: {prop: "val2"}
681+
targeting: { prop: "val2" }
629682
});
630683
count++;
631684
} else {
@@ -634,7 +687,7 @@ describe("Bling", () => {
634687
}
635688
};
636689
render() {
637-
const {targeting} = this.state;
690+
const { targeting } = this.state;
638691
return (
639692
<Bling
640693
adUnitPath="/4595/nfl.test.open"
@@ -675,7 +728,7 @@ describe("Bling", () => {
675728
}
676729
};
677730
render() {
678-
const {adUnitPath} = this.state;
731+
const { adUnitPath } = this.state;
679732
return (
680733
<Bling
681734
adUnitPath={adUnitPath}

yarn.lock

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4371,9 +4371,9 @@ preserve@^0.2.0:
43714371
version "0.2.0"
43724372
resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b"
43734373

4374-
prettier@^1.7.0:
4375-
version "1.7.0"
4376-
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.7.0.tgz#47481588f41f7c90f63938feb202ac82554e7150"
4374+
prettier@^1.9.2:
4375+
version "1.11.1"
4376+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.11.1.tgz#61e43fc4cd44e68f2b0dfc2c38cd4bb0fccdcc75"
43774377

43784378
prettycli@^1.4.3:
43794379
version "1.4.3"
@@ -4530,9 +4530,9 @@ react-addons-test-utils@^15.0.1:
45304530
version "15.6.0"
45314531
resolved "https://registry.yarnpkg.com/react-addons-test-utils/-/react-addons-test-utils-15.6.0.tgz#062d36117fe8d18f3ba5e06eb33383b0b85ea5b9"
45324532

4533-
"react-dom@^15.0.1 || ^16.0.0":
4534-
version "16.0.0"
4535-
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.0.0.tgz#9cc3079c3dcd70d4c6e01b84aab2a7e34c303f58"
4533+
react-dom@^16.0.0:
4534+
version "16.2.0"
4535+
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.2.0.tgz#69003178601c0ca19b709b33a83369fe6124c044"
45364536
dependencies:
45374537
fbjs "^0.8.16"
45384538
loose-envify "^1.1.0"
@@ -4546,9 +4546,9 @@ react-test-renderer@^16.0.0:
45464546
fbjs "^0.8.16"
45474547
object-assign "^4.1.1"
45484548

4549-
"react@^15.0.1 || ^16.0.0":
4550-
version "16.0.0"
4551-
resolved "https://registry.yarnpkg.com/react/-/react-16.0.0.tgz#ce7df8f1941b036f02b2cca9dbd0cb1f0e855e2d"
4549+
react@^16.0.0:
4550+
version "16.2.0"
4551+
resolved "https://registry.yarnpkg.com/react/-/react-16.2.0.tgz#a31bd2dab89bff65d42134fa187f24d054c273ba"
45524552
dependencies:
45534553
fbjs "^0.8.16"
45544554
loose-envify "^1.1.0"

0 commit comments

Comments
 (0)