forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sizeMapping_spec.js
123 lines (108 loc) · 3.48 KB
/
sizeMapping_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
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
import { expect } from 'chai';
import * as sizeMapping from 'src/sizeMapping';
var validAdUnit = {
'sizes': [300, 250],
'sizeMapping': [
{
'minWidth': 1024,
'sizes': [[300, 250], [728, 90]]
},
{
'minWidth': 480,
'sizes': [120, 60]
},
{
'minWidth': 0,
'sizes': [20, 20]
}
]
};
var invalidAdUnit = {
'sizes': [300, 250],
'sizeMapping': {} // wrong type
};
var invalidAdUnit2 = {
'sizes': [300, 250],
'sizeMapping': [{
foo: 'bar' // bad
}]
};
let mockWindow = {};
function resetMockWindow() {
mockWindow = {
document: {
body: {
clientWidth: 1024
},
documentElement: {
clientWidth: 1024
}
},
innerWidth: 1024
};
}
describe('sizeMapping', function() {
beforeEach(resetMockWindow);
it('minWidth should be inclusive', function() {
mockWindow.innerWidth = 1024;
sizeMapping.setWindow(mockWindow);
let sizes = sizeMapping.mapSizes(validAdUnit);
expect(sizes).to.deep.equal([[300, 250], [728, 90]]);
});
it('mapSizes 1029 width', function() {
mockWindow.innerWidth = 1029;
sizeMapping.setWindow(mockWindow);
let sizes = sizeMapping.mapSizes(validAdUnit);
expect(sizes).to.deep.equal([[300, 250], [728, 90]]);
expect(validAdUnit.sizes).to.deep.equal([300, 250]);
});
it('mapSizes 400 width', function() {
mockWindow.innerWidth = 400;
sizeMapping.setWindow(mockWindow);
let sizes = sizeMapping.mapSizes(validAdUnit);
expect(sizes).to.deep.equal([20, 20]);
expect(validAdUnit.sizes).to.deep.equal([300, 250]);
});
it('mapSizes - invalid adUnit - should return sizes', function() {
mockWindow.innerWidth = 1029;
sizeMapping.setWindow(mockWindow);
let sizes = sizeMapping.mapSizes(invalidAdUnit);
expect(sizes).to.deep.equal([300, 250]);
expect(invalidAdUnit.sizes).to.deep.equal([300, 250]);
mockWindow.innerWidth = 400;
sizeMapping.setWindow(mockWindow);
sizes = sizeMapping.mapSizes(invalidAdUnit);
expect(sizes).to.deep.equal([300, 250]);
expect(invalidAdUnit.sizes).to.deep.equal([300, 250]);
});
it('mapSizes - should return desktop (largest) sizes if screen width not detected', function() {
mockWindow.innerWidth = 0;
mockWindow.document.body.clientWidth = 0;
mockWindow.document.documentElement.clientWidth = 0;
sizeMapping.setWindow(mockWindow);
let sizes = sizeMapping.mapSizes(validAdUnit);
expect(sizes).to.deep.equal([[300, 250], [728, 90]]);
expect(validAdUnit.sizes).to.deep.equal([300, 250]);
});
it('mapSizes - should return sizes if sizemapping improperly defined ', function() {
mockWindow.innerWidth = 0;
mockWindow.document.body.clientWidth = 0;
mockWindow.document.documentElement.clientWidth = 0;
sizeMapping.setWindow(mockWindow);
let sizes = sizeMapping.mapSizes(invalidAdUnit2);
expect(sizes).to.deep.equal([300, 250]);
expect(validAdUnit.sizes).to.deep.equal([300, 250]);
});
it('getScreenWidth', function() {
mockWindow.innerWidth = 900;
mockWindow.document.body.clientWidth = 900;
mockWindow.document.documentElement.clientWidth = 900;
expect(sizeMapping.getScreenWidth(mockWindow)).to.equal(900);
});
it('getScreenWidth - should return 0 if it cannot deteremine size', function() {
mockWindow.innerWidth = null;
mockWindow.document.body.clientWidth = null;
mockWindow.document.documentElement.clientWidth = null;
expect(sizeMapping.getScreenWidth(mockWindow)).to.equal(0);
});
});