Skip to content

Commit 2e4f2ef

Browse files
authored
add tests for stylesheets and atrules (#111)
1 parent bf5605b commit 2e4f2ef

39 files changed

+813
-525
lines changed

src/analyzer/atrules/fontfaces.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ const uniquer = require('../../utils/uniquer')
22

33
module.exports = atRules => {
44
const all = atRules
5-
.filter(rule => rule.type === 'font-face')
6-
.map(rule => rule.descriptors)
5+
.filter(({type}) => type === 'font-face')
6+
.map(({descriptors}) => descriptors)
77

88
// Tricky bit: uniqueness will be based on the `src` of the @font-face
9-
const unique = uniquer(
10-
all.map(ff => ff.src)
11-
).unique.map(item => {
12-
// Once we have a list of unique @font-faces,
13-
// we'll map it back to the original values again
14-
return {
15-
count: item.count,
16-
value: all.find(ff => ff.src === item.value)
9+
const unique = uniquer(all.map(({src}) => src)).unique.map(
10+
({count, value}) => {
11+
// Once we have a list of unique @font-faces,
12+
// we'll map it back to the original values again
13+
return {
14+
count,
15+
value: all.find(({src}) => src === value)
16+
}
1717
}
18-
})
18+
)
1919

2020
return {
2121
total: all.length,

test/analyzer/atrules/documents.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const test = require('ava')
2+
const analyze = require('../../../src/analyzer/atrules/documents')
3+
4+
const FIXTURE = [
5+
{
6+
type: 'document',
7+
params: 'url("https://example.com")'
8+
},
9+
{
10+
type: 'charset',
11+
params: 'A'
12+
},
13+
{
14+
type: 'document',
15+
params: 'A'
16+
}
17+
]
18+
19+
test('it responds with the correct structure', t => [
20+
t.deepEqual(analyze([]), {
21+
total: 0,
22+
totalUnique: 0,
23+
unique: []
24+
})
25+
])
26+
27+
test('it counts documents', t => {
28+
const {total: actual} = analyze(FIXTURE)
29+
30+
t.is(actual, 2)
31+
})
32+
33+
test('it finds all unique documents and counts and sorts them', t => {
34+
const {unique: actual} = analyze(FIXTURE)
35+
36+
t.deepEqual(actual, [
37+
{count: 1, value: 'A'},
38+
{value: 'url("https://example.com")', count: 1}
39+
])
40+
})
41+
42+
test('it counts unique documents', t => {
43+
const {totalUnique: actual} = analyze(FIXTURE)
44+
45+
t.is(actual, 2)
46+
})
47+
48+
test('it does not report non-document atrules as document', t => {
49+
const actual = analyze([
50+
{
51+
type: 'media',
52+
params: 'X'
53+
},
54+
{
55+
type: 'charset',
56+
params: 'X'
57+
}
58+
])
59+
60+
t.deepEqual(actual, {
61+
total: 0,
62+
totalUnique: 0,
63+
unique: []
64+
})
65+
})

test/analyzer/atrules/documents/index.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

test/analyzer/atrules/documents/input.css

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/analyzer/atrules/documents/output.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

test/analyzer/atrules/fontfaces.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
const test = require('ava')
2+
const analyze = require('../../../src/analyzer/atrules/fontfaces')
3+
4+
const FIXTURE = [
5+
{
6+
type: 'font-face',
7+
descriptors: {
8+
'font-family': 'Arial',
9+
src: 'url("http://path.to/arial.woff")'
10+
}
11+
},
12+
{
13+
type: 'font-face',
14+
descriptors: {
15+
'font-display': 'swap',
16+
'font-family': 'monospace',
17+
'font-stretch': 'condensed',
18+
'font-style': 'italic',
19+
'font-weight': 'bold',
20+
'font-variant': 'no-common-ligatures proportional-nums',
21+
'font-feature-settings': '"liga" 0',
22+
'font-variation-settings': '"xhgt" 0.7',
23+
src: 'local("Input Mono")',
24+
'unicode-range': 'U+0025-00FF'
25+
}
26+
},
27+
{
28+
type: 'font-face',
29+
descriptors: {
30+
'font-family': '"Input Mono"',
31+
src: 'local("Input Mono") url("path/to/input_mono.ttf")'
32+
}
33+
},
34+
{
35+
type: 'font-face',
36+
descriptors: {
37+
'font-family': 'MyHelvetica',
38+
src:
39+
'local("Helvetica Neue Bold"), local("HelveticaNeue-Bold"), url(MgOpenModernaBold.ttf)',
40+
'font-weight': 'bold'
41+
}
42+
}
43+
]
44+
45+
test('it responds with the correct structure', t => [
46+
t.deepEqual(analyze([]), {
47+
total: 0,
48+
totalUnique: 0,
49+
unique: []
50+
})
51+
])
52+
53+
test('it counts @font-faces', t => {
54+
const {total: actual} = analyze(FIXTURE)
55+
56+
t.is(actual, 4)
57+
})
58+
59+
test('it finds all unique @font-faces and counts and sorts them', t => {
60+
const {unique: actual} = analyze(FIXTURE)
61+
62+
t.deepEqual(actual, [
63+
{
64+
count: 1,
65+
value: {
66+
'font-family': 'MyHelvetica',
67+
src:
68+
'local("Helvetica Neue Bold"), local("HelveticaNeue-Bold"), url(MgOpenModernaBold.ttf)',
69+
'font-weight': 'bold'
70+
}
71+
},
72+
{
73+
count: 1,
74+
value: {
75+
'font-display': 'swap',
76+
'font-family': 'monospace',
77+
'font-stretch': 'condensed',
78+
'font-style': 'italic',
79+
'font-weight': 'bold',
80+
'font-variant': 'no-common-ligatures proportional-nums',
81+
'font-feature-settings': '"liga" 0',
82+
'font-variation-settings': '"xhgt" 0.7',
83+
src: 'local("Input Mono")',
84+
'unicode-range': 'U+0025-00FF'
85+
}
86+
},
87+
{
88+
count: 1,
89+
value: {
90+
'font-family': '"Input Mono"',
91+
src: 'local("Input Mono") url("path/to/input_mono.ttf")'
92+
}
93+
},
94+
{
95+
count: 1,
96+
value: {
97+
'font-family': 'Arial',
98+
src: 'url("http://path.to/arial.woff")'
99+
}
100+
}
101+
])
102+
})
103+
104+
test('it counts unique @font-faces', t => {
105+
const {totalUnique: actual} = analyze(FIXTURE)
106+
107+
t.is(actual, 4)
108+
})
109+
110+
test('it does not report non-font-face atrules as @font-face', t => {
111+
const actual = analyze([
112+
{
113+
type: 'media',
114+
params: 'X'
115+
},
116+
{
117+
type: 'document',
118+
params: 'X'
119+
}
120+
])
121+
122+
t.deepEqual(actual, {
123+
total: 0,
124+
totalUnique: 0,
125+
unique: []
126+
})
127+
})

test/analyzer/atrules/fontfaces/index.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

test/analyzer/atrules/fontfaces/input.css

Lines changed: 0 additions & 53 deletions
This file was deleted.

test/analyzer/atrules/fontfaces/output.json

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)