forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
debugging_spec.js
93 lines (81 loc) · 2.67 KB
/
debugging_spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import {ready, loadSession, getConfig, reset, debuggingModuleLoader, debuggingControls} from '../../src/debugging.js';
import {getGlobal} from '../../src/prebidGlobal.js';
import {defer} from '../../src/utils/promise.js';
import funHooks from 'fun-hooks/no-eval/index.js';
describe('Debugging', () => {
beforeEach(() => {
reset();
});
after(() => {
reset();
});
describe('module loader', () => {
let script, scriptResult, alreadyInstalled, loader;
beforeEach(() => {
script = sinon.stub().callsFake(() => {
getGlobal()._installDebugging = sinon.stub();
return scriptResult;
});
alreadyInstalled = sinon.stub();
loader = debuggingModuleLoader({alreadyInstalled, script});
});
afterEach(() => {
delete getGlobal()._installDebugging;
})
it('should not attempt to load if debugging module is already installed', () => {
alreadyInstalled.returns(true);
return loader().then(() => {
expect(script.called).to.be.false;
});
});
it('should not attempt to load twice', () => {
alreadyInstalled.returns(false);
scriptResult = Promise.resolve();
return Promise.all([loader(), loader()]).then(() => {
expect(script.callCount).to.equal(1);
});
});
it('should call _installDebugging after loading', () => {
alreadyInstalled.returns(false);
scriptResult = Promise.resolve();
return loader().then(() => {
expect(getGlobal()._installDebugging.called).to.be.true;
});
});
it('should not call _installDebugging if load fails', () => {
const error = new Error();
alreadyInstalled.returns(false);
scriptResult = Promise.reject(error)
return loader().then(() => {
throw new Error('loader should not resolve');
}).catch((err) => {
expect(err).to.equal(error);
expect(getGlobal()._installDebugging.called).to.be.false;
});
});
});
describe('debugging controls', () => {
let debugging, loader, hook, hookRan;
beforeEach(() => {
loader = defer();
hookRan = false;
hook = funHooks()('sync', () => { hookRan = true });
debugging = debuggingControls({load: sinon.stub().returns(loader.promise), hook});
})
it('should delay execution of hook until module is loaded', () => {
debugging.enable();
hook();
expect(hookRan).to.be.false;
loader.resolve();
return loader.promise.then(() => {
expect(hookRan).to.be.true;
});
});
it('should restore hook behavior when disabled', () => {
debugging.enable();
debugging.disable();
hook();
expect(hookRan).to.be.true;
})
});
});