forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adloader_spec.js
95 lines (83 loc) · 2.91 KB
/
adloader_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
94
95
import * as utils from 'src/utils.js';
import * as adLoader from 'test/mocks/adloaderStub.js';
describe('adLoader', function () {
let utilsinsertElementStub;
let utilsLogErrorStub;
beforeEach(function () {
utilsinsertElementStub = sinon.stub(utils, 'insertElement');
utilsLogErrorStub = sinon.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', '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');
expect(utilsinsertElementStub.called).to.be.false;
});
it('callback function can be passed to the function', function() {
let callback = function() {};
adLoader.loadExternalScript('someURL1', 'debugging', callback);
expect(utilsinsertElementStub.called).to.be.true;
});
it('requires a url to be included once per document', function () {
function getDocSpec() {
return {
createElement: function() {
return {
}
},
getElementsByTagName: function() {
return {
firstChild: {
insertBefore: function() {
}
}
}
}
}
}
const doc1 = getDocSpec();
const doc2 = getDocSpec();
adLoader.loadExternalScript('someURL', 'debugging', () => {}, doc1);
adLoader.loadExternalScript('someURL', 'debugging', () => {}, doc1);
adLoader.loadExternalScript('someURL', 'debugging', () => {}, doc1);
adLoader.loadExternalScript('someURL', 'debugging', () => {}, doc2);
adLoader.loadExternalScript('someURL', '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;
}
}
},
getElementsByTagName: function() {
return {
firstChild: {
insertBefore: function() {}
}
}
}
},
attrs = {'z': 'A', 'y': 2};
let script = adLoader.loadExternalScript('someUrl', 'debugging', undefined, doc, attrs);
expect(script.z).to.equal('A');
expect(script.y).to.equal(2);
});
});