Repository navigation
Expand file tree
/
Copy pathdevtoolsMcpLoader_spec.js
More file actions
106 lines (91 loc) · 3.45 KB
/
Copy pathdevtoolsMcpLoader_spec.js
File metadata and controls
106 lines (91 loc) · 3.45 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
import { devtoolsMcpLoader, shouldLoadDevtoolsMcp, initDevtoolsMcp } from '../../src/devtoolsMcp.js';
import { getGlobal } from '../../src/prebidGlobal.js';
import { config } from '../../src/config.js';
describe('devtoolsMcp loader', () => {
describe('module loader', () => {
let script, scriptResult, alreadyInstalled, deps, loader;
beforeEach(() => {
deps = { marker: 'deps' };
script = sinon.stub().callsFake(() => {
getGlobal()._installDevtoolsMcp = sinon.stub();
return scriptResult;
});
alreadyInstalled = sinon.stub();
loader = devtoolsMcpLoader({ alreadyInstalled, script, deps });
});
afterEach(() => {
delete getGlobal()._installDevtoolsMcp;
});
it('does not load if the module is already installed', () => {
alreadyInstalled.returns(true);
return loader().then(() => {
expect(script.called).to.equal(false);
});
});
it('does not load twice', () => {
alreadyInstalled.returns(false);
scriptResult = Promise.resolve();
return Promise.all([loader(), loader()]).then(() => {
expect(script.callCount).to.equal(1);
});
});
it('installs with the injected deps after loading, planting a sentinel first', () => {
alreadyInstalled.returns(false);
scriptResult = Promise.resolve();
return loader().then(() => {
expect(getGlobal()._installDevtoolsMcp.calledOnceWith(deps)).to.equal(true);
});
});
it('does not install if the load fails', () => {
const error = new Error('boom');
alreadyInstalled.returns(false);
scriptResult = Promise.reject(error);
scriptResult.catch(() => null);
return loader().then(() => {
throw new Error('loader should not resolve');
}, (err) => {
expect(err).to.equal(error);
expect(getGlobal()._installDevtoolsMcp.called).to.equal(false);
});
});
});
describe('shouldLoadDevtoolsMcp', () => {
afterEach(() => {
config.setConfig({ debug: false });
});
it('is true when debug is on, regardless of automation', () => {
config.setConfig({ debug: true });
expect(shouldLoadDevtoolsMcp()).to.equal(true);
config.setConfig({ debug: false });
expect(shouldLoadDevtoolsMcp()).to.equal(false);
});
});
describe('initDevtoolsMcp', () => {
it('loads immediately when the condition already holds', () => {
const load = sinon.stub().returns(Promise.resolve());
const unsubscribe = initDevtoolsMcp({ load, shouldLoad: () => true });
expect(load.calledOnce).to.equal(true);
unsubscribe();
});
it('does not load when the condition does not hold', () => {
const load = sinon.stub().returns(Promise.resolve());
const unsubscribe = initDevtoolsMcp({ load, shouldLoad: () => false });
expect(load.called).to.equal(false);
unsubscribe();
});
it('loads (once) when debug is enabled later via config', () => {
const load = sinon.stub().returns(Promise.resolve());
let ready = false;
const unsubscribe = initDevtoolsMcp({ load, shouldLoad: () => ready });
expect(load.called).to.equal(false);
ready = true;
config.setConfig({ debug: true });
expect(load.calledOnce).to.equal(true);
// a further change does not load again
config.setConfig({ debug: false });
expect(load.calledOnce).to.equal(true);
unsubscribe();
config.setConfig({ debug: false });
});
});
});