Skip to content

Commit 775fe26

Browse files
committed
(refactor) move mockGPT references to test files
1 parent 5ae8f88 commit 775fe26

File tree

5 files changed

+30
-9
lines changed

5 files changed

+30
-9
lines changed

src/Bling.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,9 @@ class Bling extends Component {
357357
Bling._adManager.updateCorrelator();
358358
}
359359

360-
static createTestManager() {
361-
Bling._adManager = createManager({test: true});
360+
static set testManager(testManager) {
361+
invariant(testManager, "Pass in createManagerTest to mock GPT");
362+
Bling._adManager = testManager;
362363
}
363364

364365
state = {

src/createManager.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import EventEmitter from "eventemitter3";
22
import debounce from "debounce";
3+
import invariant from "fbjs/lib/invariant";
34
import {canUseDOM} from "fbjs/lib/ExecutionEnvironment";
45
import Events from "./Events";
56
import isInViewport from "./utils/isInViewport";
6-
import {GPTMock} from "./utils/mockGPT";
77

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

4646
if (config.test) {
47-
this.testMode = config.test;
47+
this.testMode = config;
4848
}
4949
}
5050

@@ -84,9 +84,17 @@ export class AdManager extends EventEmitter {
8484
if (process.env.NODE_ENV === "production") {
8585
return;
8686
}
87-
this._googletag = new GPTMock(config);
87+
const {test, GPTMock} = config;
8888
this._isLoaded = true;
89-
this._testMode = !!config;
89+
this._testMode = !!test;
90+
91+
if (test) {
92+
invariant(
93+
test && GPTMock,
94+
"Must provide GPTMock to enable testMode. config{GPTMock}"
95+
);
96+
this._googletag = new GPTMock(config);
97+
}
9098
}
9199

92100
_processPubadsQueue() {
@@ -443,7 +451,7 @@ export class AdManager extends EventEmitter {
443451
reject(new Error("window.googletag is not available"));
444452
}
445453
};
446-
if (window.googletag && googletag.apiReady) {
454+
if (window.googletag && window.googletag.apiReady) {
447455
onLoad();
448456
} else {
449457
const script = document.createElement("script");

src/utils/createManagerTest.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {createManager} from "../createManager.js";
2+
import {GPTMock} from "./mockGPT";
3+
4+
export function createManagerTest(config) {
5+
return createManager({
6+
...config,
7+
test: true,
8+
GPTMock
9+
});
10+
}

test/Bling.spec.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import Bling from "../src/Bling";
66
import Events from "../src/Events";
77
import {pubadsAPI, APIToCallBeforeServiceEnabled} from "../src/createManager";
88
import {gptVersion, pubadsVersion} from "../src/utils/apiList";
9+
import {createManagerTest} from "../src/utils/createManagerTest";
910

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

1415
beforeEach(() => {
1516
Bling.configure({renderWhenViewable: false});
16-
Bling.createTestManager();
17+
Bling.testManager = createManagerTest();
1718
googletag = Bling._adManager.googletag;
1819
});
1920

test/createManager.spec.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import {createManager, AdManager, pubadsAPI, APIToCallBeforeServiceEnabled} from "../src/createManager";
22
import Events from "../src/Events";
33
import {gptVersion} from "../src/utils/apiList";
4+
import {createManagerTest} from "../src/utils/createManagerTest";
45

56
describe("createManager", () => {
67
let googletag;
78
let adManager;
89

910
beforeEach(() => {
10-
adManager = createManager({test: true});
11+
adManager = createManagerTest();
1112
googletag = adManager.googletag;
1213
});
1314

0 commit comments

Comments
 (0)