forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadloader_spec.js
More file actions
140 lines (126 loc) · 4.77 KB
/
adloader_spec.js
File metadata and controls
140 lines (126 loc) · 4.77 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import * as utils from 'src/utils.js';
import * as adLoader from 'test/mocks/adloaderStub.js';
import { LOAD_EXTERNAL_SCRIPT } from '../../src/activities/activities.js';
import { registerActivityControl } from '../../src/activities/rules.js';
import { MODULE_TYPE_PREBID } from '../../src/activities/modules.js';
describe('adLoader', function () {
let sandbox;
let utilsinsertElementStub;
let utilsLogErrorStub;
let scriptEl;
beforeEach(function () {
sandbox = sinon.createSandbox();
scriptEl = null;
utilsinsertElementStub = sandbox.stub(utils, 'insertElement').callsFake((el) => { scriptEl = el });
utilsLogErrorStub = sandbox.stub(utils, 'logError');
});
afterEach(function () {
utilsinsertElementStub.restore();
utilsLogErrorStub.restore();
});
describe('loadExternalScript', function () {
it('requires moduleCode to be included on the request', function () {
adLoader.loadExternalScript('someURL');
expect(utilsLogErrorStub.called).to.be.true;
expect(utilsinsertElementStub.called).to.be.false;
});
it('only allows whitelisted vendors to load scripts', function () {
adLoader.loadExternalScript('someURL', MODULE_TYPE_PREBID, 'debugging');
expect(utilsLogErrorStub.called).to.be.false;
expect(utilsinsertElementStub.called).to.be.true;
});
it('should not load cached script again', function() {
adLoader.loadExternalScript('someURL', 'debugging', MODULE_TYPE_PREBID);
expect(utilsinsertElementStub.called).to.be.false;
});
it('callback function can be passed to the function', function() {
const callback = function() {};
adLoader.loadExternalScript('someURL1', MODULE_TYPE_PREBID, 'debugging', callback);
expect(utilsinsertElementStub.called).to.be.true;
});
it('should run callback when script loads', () => {
const callback = sinon.stub();
adLoader.loadExternalScript('test-1', MODULE_TYPE_PREBID, 'debugging', callback);
scriptEl.onload();
sinon.assert.called(callback);
});
it('should run callback as an object', () => {
const callback = {
success: sinon.stub()
}
adLoader.loadExternalScript('test-2', MODULE_TYPE_PREBID, 'debugging', callback);
scriptEl.onload();
sinon.assert.called(callback.success);
});
it('should run error callback once', () => {
const callback = {
error: sinon.stub()
}
adLoader.loadExternalScript('test-3', MODULE_TYPE_PREBID, 'debugging', callback);
const ev = new Event('error');
scriptEl.dispatchEvent(ev);
scriptEl.dispatchEvent(ev);
sinon.assert.calledWith(callback.error, ev);
sinon.assert.calledOnce(callback.error);
});
it('requires a url to be included once per document', function () {
function getDocSpec() {
return {
createElement: function() {
return {
addEventListener() {}
}
},
getElementsByTagName: function() {
return {
firstChild: {
insertBefore: function() {}
}
}
}
}
}
const doc1 = getDocSpec();
const doc2 = getDocSpec();
adLoader.loadExternalScript('someURL', MODULE_TYPE_PREBID, 'debugging', () => {}, doc1);
adLoader.loadExternalScript('someURL', MODULE_TYPE_PREBID, 'debugging', () => {}, doc1);
adLoader.loadExternalScript('someURL', MODULE_TYPE_PREBID, 'debugging', () => {}, doc1);
adLoader.loadExternalScript('someURL', MODULE_TYPE_PREBID, 'debugging', () => {}, doc2);
adLoader.loadExternalScript('someURL', MODULE_TYPE_PREBID, 'debugging', () => {}, doc2);
expect(utilsinsertElementStub.callCount).to.equal(2);
});
});
it('attaches passed attributes to a script', function () {
const doc = {
createElement: function () {
return {
setAttribute: function (key, value) {
this[key] = value;
},
addEventListener() {}
}
},
getElementsByTagName: function() {
return {
firstChild: {
insertBefore: function() {}
}
}
}
};
const attrs = { 'z': 'A', 'y': 2 };
const script = adLoader.loadExternalScript('someUrl', MODULE_TYPE_PREBID, 'debugging', undefined, doc, attrs);
expect(script.z).to.equal('A');
expect(script.y).to.equal(2);
});
it('should disable loading external script for activity rule set', function () {
let unregisterRule;
try {
unregisterRule = registerActivityControl(LOAD_EXTERNAL_SCRIPT, 'loadExternalScript config', () => ({ allow: false }));
adLoader.loadExternalScript(null, 'debugging');
expect(utilsLogErrorStub.called).to.be.false;
} finally {
unregisterRule?.();
}
})
});