diff --git a/.eslintrc.json b/.eslintrc.json
index 8d751c9..2168c69 100644
--- a/.eslintrc.json
+++ b/.eslintrc.json
@@ -4,7 +4,12 @@
"react"
],
"globals": {
- "googletag": true
+ "googletag": true,
+ "describe": true,
+ "expect": true,
+ "it": true,
+ "beforeEach": true,
+ "afterEach": true
},
"ecmaFeatures": {
"experimentalObjectRestSpread": true
diff --git a/Readme.md b/Readme.md
index 1b1c8ed..bf91ace 100644
--- a/Readme.md
+++ b/Readme.md
@@ -87,6 +87,10 @@ DFPManager.load();
| init | ```fcn() => Promise ```| ```DFPManager.init(); ```| Initializes the dfp manager (fetches the gpt scripts from network). Returns a promise that resolves when the gpt api is ready for usage. |
| attachSlotRenderEnded | ``` fcn( fcn({slotId, event}) ) ``` | ``` DFPManager.attachSlotRenderEnded((id, event) => {console.log(event.size); }) ``` | Attaches a callback that will be called when an ad slot is rendered (or refreshed). slotId is the id of slot. event is the gpt event data. |
| detachSlotRenderEnded | ``` fcn(callback) ``` | ``` DFPManager.detachSlotRenderEnded(myCallback) ``` | Detaches the callback. |
+| getRegisteredSlots | ``` fcn() => {} ``` | ``` Object.keys(DFPManager.getRegisteredSlots()) ``` | Returns an object whose attributes are the registered slots. Example: ``` { slotId: { data }, .... }```|
+| getRefreshableSlots | ``` fcn() => { slotId:{ slot }, ... } ``` | ``` console.log(DFPManager.getRegisteredSlots().length); ``` | Returns an object whose properties are slots that can be refreshed (see property ```shouldRefresh``` ). |
+| getTargetingArguments | ``` fcn() => {} ``` | ``` Object.keys(DFPManager.getTargetingArguments()) ``` | Returns an object that contains the targeting arguments (configured through ```DFPManager.setTargetingArguments()```) |
+| getSlotTargetingArguments | ``` fcn(slotId) => {} ``` | ``` console.log(DFPManager.getSlotTargetingArguments('slot-five')['the-key']); ``` | Returns an object that contains the custom targeting arguments that were set for the given slot (slotId). |
## Wanna help?
I certainly know that testcases need to be improved, but, as long as your syntax is clean, submit testscases and, of course, all the interfaces are kept working, all kind of contribution is welcome.
diff --git a/grunt/eslint.js b/grunt/eslint.js
index 5f7fabc..d3c45e7 100644
--- a/grunt/eslint.js
+++ b/grunt/eslint.js
@@ -2,5 +2,5 @@ module.exports = {
options: {
config: '.eslintrc.json'
},
- all: [ './js/*.js' ]
+ all: [ './js/*.js', './spec/*.js' ]
};
diff --git a/js/adslot.js b/js/adslot.js
index 33c2a08..0c66628 100644
--- a/js/adslot.js
+++ b/js/adslot.js
@@ -68,7 +68,7 @@ export const AdSlot = React.createClass({
);
},
- componentWillUmount() {
+ componentWillUnmount() {
this.unregisterSlot();
},
diff --git a/js/manager.js b/js/manager.js
index 4dc944d..81ee6dc 100644
--- a/js/manager.js
+++ b/js/manager.js
@@ -13,20 +13,33 @@ export const DFPManager = Object.assign(new EventEmitter(), {
Object.assign(globalTargetingArguments, data);
},
+ getTargetingArguments() {
+ return { ...globalTargetingArguments };
+ },
+
+ getSlotTargetingArguments(slotId) {
+ const slot = this.getRegisteredSlots()[slotId];
+ let ret = null;
+ if (slot !== undefined && slot.targetingArguments !== undefined) {
+ ret = { ...slot.targetingArguments };
+ }
+ return ret;
+ },
+
init() {
if (managerAlreadyInitialized === false) {
managerAlreadyInitialized = true;
- this.getGoogletag().then(() => {
+ this.getGoogletag().then((googletag) => {
googletag.cmd.push(() => {
pubadsService = googletag.pubads();
pubadsService.addEventListener('slotRenderEnded', (event) => {
const slotId = event.slot.getSlotElementId();
this.emit('slotRenderEnded', { slotId, event });
});
-
- Object.keys(globalTargetingArguments).forEach((varName) => {
- if (globalTargetingArguments.hasOwnProperty(varName)) {
- pubadsService.setTargeting(varName, globalTargetingArguments[varName]);
+ const targetingArguments = this.getTargetingArguments();
+ Object.keys(targetingArguments).forEach((varName) => {
+ if (targetingArguments.hasOwnProperty(varName)) {
+ pubadsService.setTargeting(varName, targetingArguments[varName]);
}
});
});
@@ -52,58 +65,74 @@ export const DFPManager = Object.assign(new EventEmitter(), {
} else {
availableSlots = registeredSlots;
}
-
- Object.keys(availableSlots).forEach((currentSlotId) => {
- availableSlots[currentSlotId].loaded = true;
- googletag.cmd.push(() => {
- const slot = availableSlots[currentSlotId];
- let gptSlot;
- const adUnit = `${slot.dfpNetworkId}/${slot.adUnit}`;
- if (slot.renderOutOfThePage === true) {
- gptSlot = googletag.defineOutOfPageSlot(adUnit, currentSlotId);
- } else {
- gptSlot = googletag.defineSlot(adUnit, slot.sizes, currentSlotId);
- }
- slot.gptSlot = gptSlot;
- if (slot.targetingArguments) {
- Object.keys(slot.targetingArguments).forEach((varName) => {
- if (slot.targetingArguments.hasOwnProperty(varName)) {
- slot.gptSlot.setTargeting(varName, slot.targetingArguments[varName]);
- }
- });
- }
- slot.gptSlot.addService(googletag.pubads());
- if (slot.sizeMapping) {
- let smbuilder = googletag.sizeMapping();
- slot.sizeMapping.forEach((mapping) => {
- smbuilder = smbuilder.addSize(mapping.viewport, mapping.sizes);
- });
- slot.gptSlot.defineSizeMapping(smbuilder.build());
- }
+ this.getGoogletag().then((googletag) => {
+ Object.keys(availableSlots).forEach((currentSlotId) => {
+ availableSlots[currentSlotId].loaded = true;
+ googletag.cmd.push(() => {
+ const slot = availableSlots[currentSlotId];
+ let gptSlot;
+ const adUnit = `${slot.dfpNetworkId}/${slot.adUnit}`;
+ if (slot.renderOutOfThePage === true) {
+ gptSlot = googletag.defineOutOfPageSlot(adUnit, currentSlotId);
+ } else {
+ gptSlot = googletag.defineSlot(adUnit, slot.sizes, currentSlotId);
+ }
+ slot.gptSlot = gptSlot;
+ const slotTargetingArguments = this.getSlotTargetingArguments(currentSlotId);
+ if (slotTargetingArguments !== null) {
+ Object.keys(slotTargetingArguments).forEach((varName) => {
+ if (slotTargetingArguments.hasOwnProperty(varName)) {
+ slot.gptSlot.setTargeting(varName, slotTargetingArguments[varName]);
+ }
+ });
+ }
+ slot.gptSlot.addService(googletag.pubads());
+ if (slot.sizeMapping) {
+ let smbuilder = googletag.sizeMapping();
+ slot.sizeMapping.forEach((mapping) => {
+ smbuilder = smbuilder.addSize(mapping.viewport, mapping.sizes);
+ });
+ slot.gptSlot.defineSizeMapping(smbuilder.build());
+ }
+ });
});
- });
- googletag.cmd.push(() => {
- googletag.pubads().enableSingleRequest();
- googletag.enableServices();
- Object.keys(availableSlots).forEach((_slotId) => {
- if (availableSlots.hasOwnProperty(_slotId)) {
- googletag.display(_slotId);
- }
+ googletag.cmd.push(() => {
+ googletag.pubads().enableSingleRequest();
+ googletag.enableServices();
+ Object.keys(availableSlots).forEach((_slotId) => {
+ if (availableSlots.hasOwnProperty(_slotId)) {
+ googletag.display(_slotId);
+ }
+ });
});
});
loadAlreadyCalled = true;
},
+ getRefreshableSlots() {
+ const slotsToRefresh = Object.keys(registeredSlots).map(
+ (k) => registeredSlots[k]
+ );
+ return slotsToRefresh.reduce( (val, slot) => {
+ if (slot.slotShouldRefresh() === true) {
+ val[slot.slotId] = slot;
+ }
+ return val;
+ }, {});
+ },
+
refresh() {
if (loadAlreadyCalled === false) {
this.load();
} else {
- googletag.cmd.push(() => {
- let slotsToRefresh = Object.keys(registeredSlots).map((k) => registeredSlots[k]);
- slotsToRefresh = slotsToRefresh.filter((slotData) => slotData.slotShouldRefresh());
- slotsToRefresh = slotsToRefresh.map((slotData) => slotData.gptSlot);
- googletag.pubads().refresh(slotsToRefresh);
+ this.getGoogletag().then((googletag) => {
+ const slotsToRefresh = this.getRefreshableSlots();
+ googletag.cmd.push(() => {
+ googletag.pubads().refresh(
+ Object.keys(slotsToRefresh).map(slotId => slotsToRefresh[slotId].gptSlot)
+ );
+ });
});
}
},
@@ -124,13 +153,17 @@ export const DFPManager = Object.assign(new EventEmitter(), {
unregisterSlot({ slotId }) {
delete registeredSlots[slotId];
},
-
+
+ getRegisteredSlots() {
+ return registeredSlots;
+ },
+
attachSlotRenderEnded(cb) {
this.on('slotRenderEnded', cb);
},
detachSlotRenderEnded(cb) {
- this.off('slotRenderEnded', cb);
+ this.removeListener('slotRenderEnded', cb);
},
});
diff --git a/karma.conf.js b/karma.conf.js
index 13402d2..914711b 100644
--- a/karma.conf.js
+++ b/karma.conf.js
@@ -1,16 +1,17 @@
module.exports = function(config) {
config.set({
basePath: '',
- frameworks: ['browserify', 'jasmine'],
+ frameworks: ['jasmine', 'browserify'],
files: [
- 'spec/*.js',
'lib/*.js',
+ 'spec/*.js'
],
preprocessors: {
- 'spec/*.js': [ 'browserify'],
- 'lib/*.js': [ 'browserify'],
+ 'lib/*.js': [ 'browserify' ],
+ 'spec/*.js': [ 'browserify' ],
},
browserify: {
+ debug: true,
configure: function browserify(bundle) {
bundle.once('prebundle', function prebundle() {
bundle.transform('babelify', { presets: ['es2015', 'react'] });
@@ -20,12 +21,12 @@ module.exports = function(config) {
client: {
captureConsole: true,
},
- reporters: ['progress'],
- port: 9876,
+ reporters: ['mocha'],
+ port: 9877,
colors: true,
- logLevel: config.LOG_INFO,
autoWatch: false,
- browsers: ['PhantomJS'],
+ browsers: ['jsdom'],
singleRun: true,
+ browserNoActivityTimeout: 2000
});
};
\ No newline at end of file
diff --git a/lib/adslot.js b/lib/adslot.js
index 0874496..62a7c6e 100644
--- a/lib/adslot.js
+++ b/lib/adslot.js
@@ -74,7 +74,7 @@ var AdSlot = exports.AdSlot = _react2.default.createClass({
className: 'adBox'
}), ' ');
},
- componentWillUmount: function componentWillUmount() {
+ componentWillUnmount: function componentWillUnmount() {
this.unregisterSlot();
},
diff --git a/lib/manager.js b/lib/manager.js
index 76baf12..52b9d29 100644
--- a/lib/manager.js
+++ b/lib/manager.js
@@ -5,6 +5,8 @@ Object.defineProperty(exports, "__esModule", {
});
exports.DFPManager = undefined;
+var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
+
var _utils = require('./utils');
var Utils = _interopRequireWildcard(_utils);
@@ -24,22 +26,33 @@ var DFPManager = exports.DFPManager = Object.assign(new _events.EventEmitter(),
setTargetingArguments: function setTargetingArguments(data) {
Object.assign(globalTargetingArguments, data);
},
+ getTargetingArguments: function getTargetingArguments() {
+ return _extends({}, globalTargetingArguments);
+ },
+ getSlotTargetingArguments: function getSlotTargetingArguments(slotId) {
+ var slot = this.getRegisteredSlots()[slotId];
+ var ret = null;
+ if (slot !== undefined && slot.targetingArguments !== undefined) {
+ ret = _extends({}, slot.targetingArguments);
+ }
+ return ret;
+ },
init: function init() {
var _this = this;
if (managerAlreadyInitialized === false) {
managerAlreadyInitialized = true;
- this.getGoogletag().then(function () {
+ this.getGoogletag().then(function (googletag) {
googletag.cmd.push(function () {
pubadsService = googletag.pubads();
pubadsService.addEventListener('slotRenderEnded', function (event) {
var slotId = event.slot.getSlotElementId();
_this.emit('slotRenderEnded', { slotId: slotId, event: event });
});
-
- Object.keys(globalTargetingArguments).forEach(function (varName) {
- if (globalTargetingArguments.hasOwnProperty(varName)) {
- pubadsService.setTargeting(varName, globalTargetingArguments[varName]);
+ var targetingArguments = _this.getTargetingArguments();
+ Object.keys(targetingArguments).forEach(function (varName) {
+ if (targetingArguments.hasOwnProperty(varName)) {
+ pubadsService.setTargeting(varName, targetingArguments[varName]);
}
});
});
@@ -53,6 +66,8 @@ var DFPManager = exports.DFPManager = Object.assign(new _events.EventEmitter(),
return googleGPTScriptLoadPromise;
},
load: function load(slotId) {
+ var _this2 = this;
+
this.init();
var availableSlots = {};
if (loadAlreadyCalled === true) {
@@ -63,65 +78,76 @@ var DFPManager = exports.DFPManager = Object.assign(new _events.EventEmitter(),
} else {
availableSlots = registeredSlots;
}
-
- Object.keys(availableSlots).forEach(function (currentSlotId) {
- availableSlots[currentSlotId].loaded = true;
- googletag.cmd.push(function () {
- var slot = availableSlots[currentSlotId];
- var gptSlot = void 0;
- var adUnit = slot.dfpNetworkId + '/' + slot.adUnit;
- if (slot.renderOutOfThePage === true) {
- gptSlot = googletag.defineOutOfPageSlot(adUnit, currentSlotId);
- } else {
- gptSlot = googletag.defineSlot(adUnit, slot.sizes, currentSlotId);
- }
- slot.gptSlot = gptSlot;
- if (slot.targetingArguments) {
- Object.keys(slot.targetingArguments).forEach(function (varName) {
- if (slot.targetingArguments.hasOwnProperty(varName)) {
- slot.gptSlot.setTargeting(varName, slot.targetingArguments[varName]);
- }
- });
- }
- slot.gptSlot.addService(googletag.pubads());
- if (slot.sizeMapping) {
- (function () {
- var smbuilder = googletag.sizeMapping();
- slot.sizeMapping.forEach(function (mapping) {
- smbuilder = smbuilder.addSize(mapping.viewport, mapping.sizes);
+ this.getGoogletag().then(function (googletag) {
+ Object.keys(availableSlots).forEach(function (currentSlotId) {
+ availableSlots[currentSlotId].loaded = true;
+ googletag.cmd.push(function () {
+ var slot = availableSlots[currentSlotId];
+ var gptSlot = void 0;
+ var adUnit = slot.dfpNetworkId + '/' + slot.adUnit;
+ if (slot.renderOutOfThePage === true) {
+ gptSlot = googletag.defineOutOfPageSlot(adUnit, currentSlotId);
+ } else {
+ gptSlot = googletag.defineSlot(adUnit, slot.sizes, currentSlotId);
+ }
+ slot.gptSlot = gptSlot;
+ var slotTargetingArguments = _this2.getSlotTargetingArguments(currentSlotId);
+ if (slotTargetingArguments !== null) {
+ Object.keys(slotTargetingArguments).forEach(function (varName) {
+ if (slotTargetingArguments.hasOwnProperty(varName)) {
+ slot.gptSlot.setTargeting(varName, slotTargetingArguments[varName]);
+ }
});
- slot.gptSlot.defineSizeMapping(smbuilder.build());
- })();
- }
+ }
+ slot.gptSlot.addService(googletag.pubads());
+ if (slot.sizeMapping) {
+ (function () {
+ var smbuilder = googletag.sizeMapping();
+ slot.sizeMapping.forEach(function (mapping) {
+ smbuilder = smbuilder.addSize(mapping.viewport, mapping.sizes);
+ });
+ slot.gptSlot.defineSizeMapping(smbuilder.build());
+ })();
+ }
+ });
});
- });
- googletag.cmd.push(function () {
- googletag.pubads().enableSingleRequest();
- googletag.enableServices();
- Object.keys(availableSlots).forEach(function (_slotId) {
- if (availableSlots.hasOwnProperty(_slotId)) {
- googletag.display(_slotId);
- }
+ googletag.cmd.push(function () {
+ googletag.pubads().enableSingleRequest();
+ googletag.enableServices();
+ Object.keys(availableSlots).forEach(function (_slotId) {
+ if (availableSlots.hasOwnProperty(_slotId)) {
+ googletag.display(_slotId);
+ }
+ });
});
});
loadAlreadyCalled = true;
},
+ getRefreshableSlots: function getRefreshableSlots() {
+ var slotsToRefresh = Object.keys(registeredSlots).map(function (k) {
+ return registeredSlots[k];
+ });
+ return slotsToRefresh.reduce(function (val, slot) {
+ if (slot.slotShouldRefresh() === true) {
+ val[slot.slotId] = slot;
+ }
+ return val;
+ }, {});
+ },
refresh: function refresh() {
+ var _this3 = this;
+
if (loadAlreadyCalled === false) {
this.load();
} else {
- googletag.cmd.push(function () {
- var slotsToRefresh = Object.keys(registeredSlots).map(function (k) {
- return registeredSlots[k];
- });
- slotsToRefresh = slotsToRefresh.filter(function (slotData) {
- return slotData.slotShouldRefresh();
- });
- slotsToRefresh = slotsToRefresh.map(function (slotData) {
- return slotData.gptSlot;
+ this.getGoogletag().then(function (googletag) {
+ var slotsToRefresh = _this3.getRefreshableSlots();
+ googletag.cmd.push(function () {
+ googletag.pubads().refresh(Object.keys(slotsToRefresh).map(function (slotId) {
+ return slotsToRefresh[slotId].gptSlot;
+ }));
});
- googletag.pubads().refresh(slotsToRefresh);
});
}
},
@@ -150,10 +176,13 @@ var DFPManager = exports.DFPManager = Object.assign(new _events.EventEmitter(),
delete registeredSlots[slotId];
},
+ getRegisteredSlots: function getRegisteredSlots() {
+ return registeredSlots;
+ },
attachSlotRenderEnded: function attachSlotRenderEnded(cb) {
this.on('slotRenderEnded', cb);
},
detachSlotRenderEnded: function detachSlotRenderEnded(cb) {
- this.off('slotRenderEnded', cb);
+ this.removeListener('slotRenderEnded', cb);
}
});
diff --git a/package.json b/package.json
index 53a7967..4ed9867 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "react-dfp",
- "version": "0.3.2",
+ "version": "0.3.3",
"author": {
"name": "Jonatan Alexis Anauati",
"email": "barakawins@gmail.com"
@@ -47,28 +47,24 @@
"grunt-babel": "^6.0.0",
"grunt-contrib-clean": "^0.4.1",
"grunt-eslint": "^18.1.0",
- "grunt-karma": "^0.9.0",
- "grunt-mocha": "~0.4.10",
+ "grunt-karma": "^1.0.0",
"jasmine-core": "^2.4.1",
- "karma": "^0.12.31",
+ "karma": "^0.13.22",
"karma-babel-preprocessor": "^6.0.1",
"karma-browserify": "^2.0.0",
"karma-chai": "^0.1.0",
- "karma-coverage": "^0.2.7",
"karma-jasmine": "^1.0.2",
+ "karma-jsdom-launcher": "^3.0.0",
"karma-mocha": "^0.1.10",
"karma-mocha-reporter": "^0.3.1",
- "karma-phantomjs-launcher": "~0.1.4",
- "karma-sauce-launcher": "^0.2.6",
"karma-script-launcher": "~0.1.0",
"karma-sinon": "^1.0.4",
"load-grunt-config": "^0.7.2",
"mocha": "^1.18.2",
- "phantomjs-polyfill": "0.0.2",
"react": "^15.0.2",
"react-addons-test-utils": "^15.0.2",
"react-dom": "^15.0.2",
- "sinon": "^1.12.2",
+ "sinon": "^1.17.4",
"sinon-chai": "^2.6.0",
"time-grunt": "^0.2.10",
"watchify": "^2.4.0"
diff --git a/spec/test-adslot.js b/spec/test-adslot.js
index 556b26c..775e0ef 100644
--- a/spec/test-adslot.js
+++ b/spec/test-adslot.js
@@ -1,20 +1,143 @@
import 'babel-polyfill';
import React from 'react';
+import ReactDOM from 'react-dom';
+
import TestUtils from 'react-addons-test-utils';
-import { AdSlot } from '../js/adslot';
+import { expect } from 'chai';
+import sinon from 'sinon';
+
+import { AdSlot, DFPManager } from '../lib';
+describe('AdSlot', () => {
+ describe('Component markup', () => {
+ let component;
+ beforeEach(() => {
+ component = TestUtils.renderIntoDocument(
+
+ );
+ });
-describe('AdSlot', function adSlot() {
- beforeEach(function beforeEach() {
- this.component = TestUtils.renderIntoDocument(
-
- );
+ it('renders an adBox with the given elementId', () => {
+ const box = TestUtils.findRenderedDOMComponentWithClass(component,
+ 'adBox');
+ expect(box.id).to.equal('testElement');
+ });
});
- it('renders an adBox with the given elementId', function itRendersAnAdBox() {
- const box = TestUtils.findRenderedDOMComponentWithClass(this.component,
- 'adBox');
- expect(box.id).toEqual('testElement');
+ describe('DFPManager Interaction', () => {
+ beforeEach(() => {
+ DFPManager.registerSlot = sinon.spy(DFPManager, 'registerSlot');
+ DFPManager.unregisterSlot = sinon.spy(DFPManager, 'unregisterSlot');
+ });
+
+ it('Registers an AdSlot', () => {
+ const compProps = {
+ dfpNetworkId: 1000,
+ adUnit: 'foo/bar/baz',
+ slotId: 'testElement1',
+ sizes: [[728, 90]],
+ };
+
+ TestUtils.renderIntoDocument(
+
+ );
+
+ sinon.assert.calledOnce(DFPManager.registerSlot);
+ sinon.assert.calledWithMatch(DFPManager.registerSlot, compProps);
+ });
+
+ it('Registers a refreshable AdSlot', () => {
+ const compProps = {
+ dfpNetworkId: 1000,
+ adUnit: 'foo/bar/baz',
+ slotId: 'testElement2',
+ sizes: [[728, 90]],
+ };
+
+ TestUtils.renderIntoDocument(
+
+ );
+
+ expect(DFPManager.getRefreshableSlots()).to.contain.all.keys([compProps.slotId]);
+ expect(DFPManager.getRefreshableSlots()[compProps.slotId]).to.contain.all.keys(compProps);
+ });
+
+ it('Registers a non refreshable AdSlot', () => {
+ const compProps = {
+ dfpNetworkId: 1000,
+ adUnit: 'foo/bar/baz',
+ slotId: 'testElement3',
+ sizes: [[728, 90]],
+ shouldRefresh: () => false,
+ };
+
+ TestUtils.renderIntoDocument(
+
+ );
+ expect(Object.keys(DFPManager.getRefreshableSlots()).length).to.equal(0);
+ });
+
+ it('Registers an AdSlot with custom targeting arguments', () => {
+ const compProps = {
+ dfpNetworkId: 1000,
+ adUnit: 'foo/bar/baz',
+ slotId: 'testElement4',
+ sizes: [[728, 90]],
+ targetingArguments: { team: 'river plate', player: 'pisculichi' },
+ };
+
+ TestUtils.renderIntoDocument(
+
+ );
+ expect(DFPManager.getSlotTargetingArguments(compProps.slotId))
+ .to.contain.all.keys(compProps.targetingArguments);
+ });
+
+ it('Registers an AdSlot without custom targeting arguments', () => {
+ const compProps = {
+ dfpNetworkId: 1000,
+ adUnit: 'foo/bar/baz',
+ slotId: 'testElement5',
+ sizes: [[728, 90]],
+ };
+
+ TestUtils.renderIntoDocument(
+
+ );
+ expect(DFPManager.getSlotTargetingArguments(compProps.slotId)).to.equal(null);
+ });
+
+
+ it('Unregisters an AdSlot', () => {
+ const compProps = {
+ dfpNetworkId: 1000,
+ adUnit: 'foo/bar/baz',
+ slotId: 'testElement6',
+ sizes: [[728, 90]],
+ };
+
+ const component = TestUtils.renderIntoDocument(
+
+ );
+
+ ReactDOM.unmountComponentAtNode(ReactDOM.findDOMNode(component).parentNode);
+
+ sinon.assert.calledOnce(DFPManager.unregisterSlot);
+ sinon.assert.calledWithMatch(DFPManager.unregisterSlot,
+ { slotId: compProps.slotId });
+ });
+
+ afterEach(() => {
+ DFPManager.registerSlot.restore();
+ DFPManager.unregisterSlot.restore();
+ Object.keys(DFPManager.getRegisteredSlots()).forEach((slotId) => {
+ DFPManager.unregisterSlot({ slotId });
+ });
+ });
});
});
diff --git a/spec/test-manager.js b/spec/test-manager.js
new file mode 100644
index 0000000..62d33d5
--- /dev/null
+++ b/spec/test-manager.js
@@ -0,0 +1,44 @@
+import { expect } from 'chai';
+import { DFPManager } from '../lib';
+
+describe('DFPManager', function adSlot() {
+
+ describe('Targeting arguments', function dfpManagerInteraction() {
+ beforeEach(function beforeEach() {
+ this.argsList1 = {'k': 'yeah'};
+ this.argsList2 = {'k': 'yeah'};
+ DFPManager.setTargetingArguments(this.argsList1);
+ DFPManager.setTargetingArguments(this.argsList2);
+ });
+
+ it('Registers global targeting arguments', function registersAdSlot() {
+ expect(DFPManager.getTargetingArguments()).to.contain.keys({ ...this.argsList1, ...this.argsList2});
+ });
+
+ });
+
+ describe('Creation of ad slots ', function dfpManagerInteraction() {
+ beforeEach(function beforeEach() {
+ this.slotProps = {
+ dfpNetworkId: 1000,
+ adUnit: 'foo/bar/baz',
+ slotId: 'testElement',
+ sizes: [[728, 90]],
+ slotShouldRefresh: () => true,
+ };
+ DFPManager.registerSlot(this.slotProps);
+ });
+
+ it('Registers an adslot', function registersAdSlot() {
+ expect(Object.keys(DFPManager.getRegisteredSlots()).length).to.equal(1);
+ expect(DFPManager.getRegisteredSlots()).to.contain.all
+ .keys([this.slotProps.slotId]);
+ expect(DFPManager.getRegisteredSlots()[this.slotProps.slotId])
+ .to.contain.all.keys(this.slotProps);
+ });
+
+ afterEach(function afterEach() {
+ DFPManager.unregisterSlot(this.slotProps);
+ });
+ });
+});