-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtests.js
285 lines (204 loc) · 7.96 KB
/
tests.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
describe('window', function () {
it('should have slimmage', function () {
expect(window).to.have.property('slimmage');
});
});
describe('slimmage', function () {
var s;
before(function(){
s = window.slimmage;
});
it('should be an object', function () {
expect(s).to.be.an('object');
});
it('should have checkResponsiveImages', function () {
expect(s).to.have.property('checkResponsiveImages');
});
it('should have widthStep', function () {
expect(s).to.have.property('widthStep');
});
it('should have jpegQuality', function () {
expect(s).to.have.property('jpegQuality');
});
it('should have jpegRetinaQuality', function () {
expect(s).to.have.property('jpegRetinaQuality');
});
describe('checkResponsiveImages',function() {
after(function() {
s.readyCallback.restore();
});
it('should call readyCallback',function() {
// create a function
s.readyCallback = function() {
expect(this).to.be(s);
};
// spy on it
sinon.spy(s, "readyCallback"); // spy on callback attached to slimmage 's'
// call parent 'calling' function
s.checkResponsiveImages();
// our spy should have been called as a result, in this case, only once
expect(s.readyCallback.calledOnce).to.be(true);
});
it('should call adjustImageSrc ',function() {
// create a function
s.adjustImageSrc = function() {
expect(this).to.be(s);
};
// spy on it
sinon.spy(s, "adjustImageSrc"); // spy on callback attached to slimmage 's'
// call parent 'calling' function
s.checkResponsiveImages();
// our spy should have been called as a result, in this case, only once
expect(s.adjustImageSrc.calledOnce).to.be(true);
s.adjustImageSrc.restore();
});
it('should call beforeAdjustSrc', function(){
s.beforeAdjustSrc = function(){
expect(this).to.be(s);
};
sinon.spy(s, "beforeAdjustSrc");
//act - call parent 'calling' function
s.checkResponsiveImages();
// our spy should have been called as a result, in this case, only once
expect(s.beforeAdjustSrc.calledOnce).to.be(true);
s.beforeAdjustSrc.restore();
delete s.beforeAdjustSrc;
});
});
describe('mutateUrl', function(){
it('should only affect the querystring', function(){
var result = s.mutateUrl(
"path?a=true&b=false#hash",
function(){},
function(p, d, k, v){
return d + k + "=cats";
}, function(q){return q;});
expect(result).to.be("path?a=cats&b=cats#hash");
});
it('should clean up delimiters', function(){
var result = s.mutateUrl(
"path?&a=true&&b=false#hash",
function(){},
function(p, d, k, v){
return (k && v) ? ('&' + d + k + "=cats") : '';
}, function(q){return q;});
expect(result).to.be("path?a=cats&b=cats#hash");
});
});
describe('getImageInfo', function(){
it('should call adjustImageParameters with valid data', function(){
s.adjustImageParameters = function(data){
expect(data.width).to.be.above(10);
expect(data.src).to.be("http://z.zr.io/ri/1s.jpg?width=150");
expect(data.dpr).to.be.within(1,3);
expect(data.requestedWidth).to.be(Math.round(Math.ceil(100 * data.dpr / 160) * 160));
expect(data.quality).to.be.within(10,100);
data.requestedWidth=200;
};
sinon.spy(s, "adjustImageParameters");
//act
var result = s.getImageInfo(100,"http://z.zr.io/ri/1s.jpg?width=150",0);
expect(s.adjustImageParameters.calledOnce).to.be(true);
expect(result.src).to.be("http://z.zr.io/ri/1s.jpg?width=200");
expect(result["data-pixel-width"]).to.be(200);
s.adjustImageParameters.restore();
delete s.adjustImageParameters;
});
it('should always round up', function(){
var dpr = window.devicePixelRatio || 1;
var info = s.getImageInfo(159/dpr,"im?width=5",0);
expect(info["data-pixel-width"]).to.be(160);
info = s.getImageInfo(1,"im?width=5",0);
expect(info["data-pixel-width"]).to.be(160);
info = s.getImageInfo(160/dpr,"im?width=5",0);
expect(info["data-pixel-width"]).to.be(160);
info = s.getImageInfo(161/dpr,"im?width=5",0);
expect(info['data-pixel-width']).to.be(320);
});
it('should not request a zero-width image', function(){
var result = s.getImageInfo(0,"im?width=5",0);
expect(result).to.be(null);
});
it('should adjust height if present, maintaining aspect ratio', function(){
var dpr = window.devicePixelRatio || 1;
var result = s.getImageInfo(100 /dpr,"im?width=16&height=9",0);
expect(result.src).to.be("im?width=160&height=90");
});
it('should account for applied zoom', function(){
var dpr = window.devicePixelRatio || 1;
var result = s.getImageInfo(100/dpr,"im?width=16&height=9&zoom=2",0);
expect(result['data-pixel-width']).to.be(160); //160px is the bitmap width we request
expect(result.src).to.be("im?width=80&height=45&zoom=2"); //But only because how zoom is interpreted.
});
});
var css_ids = ['twenty_px','twenty_per','two_em','two_cm','two_in'];
var css_value_pattern = /[0-9.]+(em|px|%|cm|in)/;
var decimal_pattern = /[0-9.]+/;
var $id = function(id){return document.getElementById(id);};
describe('getCssPixels', function() {
it ('should return 20 for 20px', function(){
expect(s.getCssPixels($id('twenty_px'), 'max-width')).to.be(20);
});
it ('should return 32 for two_em', function(){
expect(s.getCssPixels($id('two_em'), 'max-width')).to.be(32);
});
it ('should return 76 +/- 1 for two_cm', function(){
expect(s.getCssPixels($id('two_cm'), 'max-width')).to.be.within(75,77);
});
it ('should return 192 for two_in', function(){
expect(s.getCssPixels($id('two_in'), 'max-width')).to.be(192);
});
it ('should return 20% of parent width (+/- 5px) for twenty_per', function(){
var parentWidth = $id('twenty_per').parentNode.offsetWidth;
expect(parentWidth).to.be.above(1);
expect(s.getCssPixels($id('twenty_per'), 'max-width')).to.be.within(parentWidth /5 - 5, parentWidth/5 + 5);
});
var makeTestFn = function(id){
it('should return a decimal number for the max-width of #' + id,function(){
var value = s.getCssPixels($id(id), 'max-width');
expect(value).to.match(decimal_pattern);
});
};
for (var i = 0; i < css_ids.length; i++) {
makeTestFn.call(this,css_ids[i]);
}
});
describe('getCssValue', function() {
var makeTestFn = function(id){
it('should return a css value for the max-width of #' + id,function(){
var value = s.getCssValue($id(id), 'max-width');
expect(value).to.match(css_value_pattern);
});
};
for (var i = 0; i < css_ids.length; i++) {
makeTestFn.call(this,css_ids[i]);
}
});
describe('setAttribute', function(){
it('should copy the class attribute',function(){
var div = document.createElement("div");
s.setAttribute(div, "class", "t" );
expect(div.className).to.be("t");
});
it('should copy the id attribute',function(){
var div = document.createElement("div");
s.setAttribute(div, "id", "t" );
expect(div.id).to.be("t");
});
it('should copy the src attribute',function(){
var img = document.createElement("img");
s.setAttribute(img, "src", "http://example.com/i.jpg" );
expect(img.src).to.be("http://example.com/i.jpg");
});
it('should copy the usemap attribute',function(){
var img = document.createElement("img");
s.setAttribute(img, "usemap", "http://example.com/map" );
expect(img.useMap).to.be("http://example.com/map");
});
it('should copy the tabIndex attribute',function(){
var div = document.createElement("div");
s.setAttribute(div, "tabindex", "10" );
expect(div.tabIndex).to.be(10);
});
});
});