Skip to content

Commit b3fc8b9

Browse files
authored
add tests for declarations (#112)
1 parent 2e4f2ef commit b3fc8b9

File tree

8 files changed

+190
-164
lines changed

8 files changed

+190
-164
lines changed
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
module.exports = declarations => {
22
const all = declarations.filter(value => value.important)
33

4-
const share = (() => {
5-
// Catch divide by zero exception
6-
if (declarations.length === 0) {
7-
return 0
8-
}
9-
10-
return all.length / declarations.length
11-
})()
12-
134
return {
145
total: all.length,
15-
share
6+
share: declarations.length === 0 ? 0 : all.length / declarations.length
167
}
178
}

src/analyzer/declarations/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
module.exports = declarations => {
22
const all = declarations
3+
const totalUnique = new Set(
4+
all.map(({property, value}) => `${property} : ${value}`)
5+
).size
36
const importants = require('./importants')(all)
4-
const unique = [...new Set(all.map(declaration => {
5-
return `${declaration.property} : ${declaration.value}`
6-
}))].sort()
77

88
return {
99
total: all.length,
10-
totalUnique: unique.length,
10+
totalUnique,
1111
importants
1212
}
1313
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const test = require('ava')
2+
const analyze = require('../../../src/analyzer/declarations/importants')
3+
4+
const FIXTURE = [
5+
{
6+
property: 'color',
7+
value: 'red',
8+
important: true
9+
},
10+
{
11+
property: 'border-width',
12+
value: '1px',
13+
important: true
14+
},
15+
{
16+
property: 'font-size',
17+
value: '16px',
18+
important: false
19+
}
20+
]
21+
22+
test('it responds with the correct structure', t => {
23+
t.deepEqual(analyze([]), {
24+
total: 0,
25+
share: 0
26+
})
27+
})
28+
29+
test('it counts !importants', t => {
30+
const {total: actual} = analyze(FIXTURE)
31+
32+
t.is(actual, 2)
33+
})
34+
35+
test('it calculates the share of !important declarations', t => {
36+
const {share: actual} = analyze(FIXTURE)
37+
38+
t.is(actual, (1 / 3) * 2)
39+
})
Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,64 @@
11
const test = require('ava')
2-
const testScope = require('../../utils/scope-tester.js')
2+
const analyze = require('../../../src/analyzer/declarations')
33

4-
const SCOPE = 'declarations'
4+
test('it responds with the correct structure', t => {
5+
const actual = analyze([])
6+
const expected = {
7+
total: 0,
8+
totalUnique: 0,
9+
importants: {
10+
total: 0,
11+
share: 0
12+
}
13+
}
514

6-
test(SCOPE, async t => {
7-
const {actual, expected} = await testScope(SCOPE)
8-
t.deepEqual(actual[SCOPE], expected)
15+
t.deepEqual(actual, expected)
16+
})
17+
18+
test('it counts declarations', t => {
19+
const fixture = [
20+
{
21+
property: 'color',
22+
value: 'red',
23+
important: false
24+
},
25+
{
26+
property: 'border',
27+
value: '1px solid blue',
28+
important: false
29+
},
30+
{
31+
property: 'font-size',
32+
value: '16px',
33+
important: false
34+
},
35+
{
36+
// Duplicate
37+
property: 'font-size',
38+
value: '16px',
39+
important: false
40+
}
41+
]
42+
const {total, totalUnique} = analyze(fixture)
43+
44+
t.is(total, 4)
45+
t.is(totalUnique, 3)
46+
})
47+
48+
test('it ignores !importants when looking for unique declarations', t => {
49+
const fixture = [
50+
{
51+
property: 'font-size',
52+
value: '16px',
53+
important: false
54+
},
55+
{
56+
property: 'font-size',
57+
value: '16px',
58+
important: true
59+
}
60+
]
61+
const {totalUnique: actual} = analyze(fixture)
62+
63+
t.deepEqual(actual, 1)
964
})

test/analyzer/declarations/input.css

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

test/analyzer/declarations/output.json

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

test/parser/atrules.js

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,61 @@
11
const test = require('ava')
22
const parser = require('../../src/parser')
33

4-
let fixture
4+
const FIXTURE = `
5+
/* FIXTURE */
6+
@charset "UTF-8";
7+
@import "some.css";
8+
@supports (display: grid) {
9+
@media screen {
10+
@media (min-width: 300px) {
11+
.foo { color:blue }
12+
}
13+
}
14+
}
15+
`
516

6-
test.beforeEach(() => {
7-
fixture = '/* FIXTURE */' +
8-
'@charset "UTF-8";' +
9-
'@import "some.css";' +
10-
'@supports (display: grid) { ' +
11-
'@media screen { ' +
12-
'@media (min-width: 300px) { ' +
13-
'.foo { color:blue } ' +
14-
'}' +
15-
'}' +
16-
'}'
17+
test('atRules are found correctly', async t => {
18+
const {atRules: actual} = await parser(FIXTURE)
19+
const expected = [
20+
{
21+
type: 'charset',
22+
params: '"UTF-8"'
23+
},
24+
{
25+
type: 'import',
26+
params: '"some.css"'
27+
},
28+
{
29+
type: 'supports',
30+
params: '(display: grid)'
31+
},
32+
{
33+
type: 'media',
34+
params: 'screen'
35+
},
36+
{
37+
type: 'media',
38+
params: '(min-width: 300px)'
39+
}
40+
]
41+
t.deepEqual(actual, expected)
1742
})
1843

19-
test('atRules are found correctly', async t => {
20-
const actual = await parser(fixture)
21-
const expected = [{
22-
type: 'charset',
23-
params: '"UTF-8"'
24-
}, {
25-
type: 'import',
26-
params: '"some.css"'
27-
}, {
28-
type: 'supports',
29-
params: '(display: grid)'
30-
}, {
31-
type: 'media',
32-
params: 'screen'
33-
}, {
34-
type: 'media',
35-
params: '(min-width: 300px)'
36-
}]
37-
t.deepEqual(actual.atRules, expected)
44+
test('descriptors in @font-face are parsed to descriptors and not declarations', async t => {
45+
const fixture = `
46+
@font-face {
47+
src: url("http://example.com");
48+
font-family: MyFont;
49+
}
50+
`
51+
const {
52+
atRules: [fontface]
53+
} = await parser(fixture)
54+
const expected = {
55+
src: 'url("http://example.com")',
56+
'font-family': 'MyFont'
57+
}
58+
59+
t.deepEqual(fontface.descriptors, expected)
60+
t.is(typeof fontface.declarations, 'undefined')
3861
})

0 commit comments

Comments
 (0)