Skip to content

Commit

Permalink
(refactor) move mockGPT references to test files
Browse files Browse the repository at this point in the history
  • Loading branch information
potench committed Jul 30, 2017
1 parent 5ae8f88 commit 775fe26
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/Bling.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,9 @@ class Bling extends Component {
Bling._adManager.updateCorrelator();
}

static createTestManager() {
Bling._adManager = createManager({test: true});
static set testManager(testManager) {
invariant(testManager, "Pass in createManagerTest to mock GPT");
Bling._adManager = testManager;
}

state = {
Expand Down
18 changes: 13 additions & 5 deletions src/createManager.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import EventEmitter from "eventemitter3";
import debounce from "debounce";
import invariant from "fbjs/lib/invariant";
import {canUseDOM} from "fbjs/lib/ExecutionEnvironment";
import Events from "./Events";
import isInViewport from "./utils/isInViewport";
import {GPTMock} from "./utils/mockGPT";

// based on https://developers.google.com/doubleclick-gpt/reference?hl=en
export const pubadsAPI = [
Expand Down Expand Up @@ -44,7 +44,7 @@ export class AdManager extends EventEmitter {
super(config);

if (config.test) {
this.testMode = config.test;
this.testMode = config;
}
}

Expand Down Expand Up @@ -84,9 +84,17 @@ export class AdManager extends EventEmitter {
if (process.env.NODE_ENV === "production") {
return;
}
this._googletag = new GPTMock(config);
const {test, GPTMock} = config;
this._isLoaded = true;
this._testMode = !!config;
this._testMode = !!test;

if (test) {
invariant(
test && GPTMock,
"Must provide GPTMock to enable testMode. config{GPTMock}"
);
this._googletag = new GPTMock(config);
}
}

_processPubadsQueue() {
Expand Down Expand Up @@ -443,7 +451,7 @@ export class AdManager extends EventEmitter {
reject(new Error("window.googletag is not available"));
}
};
if (window.googletag && googletag.apiReady) {
if (window.googletag && window.googletag.apiReady) {
onLoad();
} else {
const script = document.createElement("script");
Expand Down
10 changes: 10 additions & 0 deletions src/utils/createManagerTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {createManager} from "../createManager.js";
import {GPTMock} from "./mockGPT";

export function createManagerTest(config) {
return createManager({
...config,
test: true,
GPTMock
});
}
3 changes: 2 additions & 1 deletion test/Bling.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import Bling from "../src/Bling";
import Events from "../src/Events";
import {pubadsAPI, APIToCallBeforeServiceEnabled} from "../src/createManager";
import {gptVersion, pubadsVersion} from "../src/utils/apiList";
import {createManagerTest} from "../src/utils/createManagerTest";

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

beforeEach(() => {
Bling.configure({renderWhenViewable: false});
Bling.createTestManager();
Bling.testManager = createManagerTest();
googletag = Bling._adManager.googletag;
});

Expand Down
3 changes: 2 additions & 1 deletion test/createManager.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {createManager, AdManager, pubadsAPI, APIToCallBeforeServiceEnabled} from "../src/createManager";
import Events from "../src/Events";
import {gptVersion} from "../src/utils/apiList";
import {createManagerTest} from "../src/utils/createManagerTest";

describe("createManager", () => {
let googletag;
let adManager;

beforeEach(() => {
adManager = createManager({test: true});
adManager = createManagerTest();
googletag = adManager.googletag;
});

Expand Down

0 comments on commit 775fe26

Please sign in to comment.