forked from sindresorhus/srcset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
58 lines (49 loc) · 1.97 KB
/
test.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
import test from 'ava';
import srcset from '.';
test('.parse() should parse srcset', t => {
const fixture = ' banner-HD.jpeg 2x, banner-HD.jpeg 2x, banner-HD.jpeg 2x, banner-phone.jpeg 100w, http://site.com/image.jpg?foo=bar,lorem 1x ';
t.deepEqual(srcset.parse(fixture), [
{url: 'banner-HD.jpeg', density: 2},
{url: 'banner-phone.jpeg', width: 100},
{url: 'http://site.com/image.jpg?foo=bar,lorem', density: 1}
]);
});
test('.parse() should parse URLs with commas', t => {
const fixture = 'https://i.kinja-img.com/gawker-media/image/upload/c_fill,f_auto,fl_progressive,g_center,h_180,q_80,w_320/rbx48jwtuvpwum29aarr.jpg 320w, https://i.kinja-img.com/gawker-media/image/upload/c_fill,f_auto,fl_progressive,g_center,h_264,q_80,w_470/rbx48jwtuvpwum29aarr.jpg 470w, https://i.kinja-img.com/gawker-media/image/upload/c_fill,f_auto,fl_progressive,g_center,h_80,q_80,w_80/rbx48jwtuvpwum29aarr.jpg 80w';
t.deepEqual(srcset.parse(fixture), [
{
url: 'https://i.kinja-img.com/gawker-media/image/upload/c_fill,f_auto,fl_progressive,g_center,h_180,q_80,w_320/rbx48jwtuvpwum29aarr.jpg',
width: 320
},
{
url: 'https://i.kinja-img.com/gawker-media/image/upload/c_fill,f_auto,fl_progressive,g_center,h_264,q_80,w_470/rbx48jwtuvpwum29aarr.jpg',
width: 470
},
{
url: 'https://i.kinja-img.com/gawker-media/image/upload/c_fill,f_auto,fl_progressive,g_center,h_80,q_80,w_80/rbx48jwtuvpwum29aarr.jpg',
width: 80
}
]);
});
test('.parse() should not parse invalid srcset', t => {
t.throws(() => {
srcset.parse('banner-phone-HD.jpeg 100w 2x');
});
t.throws(() => {
srcset.parse('banner-phone-HD.jpeg -100w');
});
t.throws(() => {
srcset.parse('banner-phone-HD.jpeg -2x');
});
});
test('.stringify() should stringify srcset', t => {
const fixture = [
{url: 'banner-HD.jpeg', density: 2},
{url: 'banner-HD.jpeg', density: 2},
{url: 'banner-phone.jpeg', width: 100}
];
t.is(
srcset.stringify(fixture),
'banner-HD.jpeg 2x, banner-phone.jpeg 100w'
);
});