-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.js
135 lines (124 loc) · 4.15 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
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
'use strict'
const test = require('tape')
test('child combinators, sass', t => {
runLint(t, { files: [fixture('child.scss')] }, res => {
const warnings = res.results[0].warnings
t.equal(warnings.length, 1)
t.equal(warnings[0].rule,
'rscss/no-descendant-combinator')
t.equal(warnings[0].text,
"Descendant combinator not allowed: '.component-name .badelement' (rscss/no-descendant-combinator)")
})
})
test('child combinators', t => {
runLint(t, { files: [fixture('child.css')] }, res => {
const warnings = res.results[0].warnings
t.equal(warnings.length, 1)
t.equal(warnings[0].text,
"Descendant combinator not allowed: 'a.bad-component .xyz' (rscss/no-descendant-combinator)")
})
})
test('class format', t => {
runLint(t, { files: [fixture('class_format.css')] }, res => {
const warnings = res.results[0].warnings
t.equal(warnings.length, 10)
t.equal(warnings[0].text,
"Invalid component name: '.badcomponent' (rscss/class-format)")
t.equal(warnings[1].text,
"Invalid component name: '.badcomponent.-xyz' (rscss/class-format)")
t.equal(warnings[2].text,
"Invalid component name: '.badcomponent.-abc' (rscss/class-format)")
t.equal(warnings[3].text,
"Only one component name is allowed: '.too-many.component-names' (rscss/class-format)")
t.equal(warnings[4].text,
"Invalid helper name: '._badhelper.-variant' (rscss/class-format)")
t.equal(warnings[5].text,
'Invalid helper name: \'._badhelper.element\' (rscss/class-format)')
t.equal(warnings[6].text,
'Invalid element name: \'.bad_element\' (rscss/class-format)')
t.equal(warnings[7].text,
'Invalid variant name: \'.badvariant\' (rscss/class-format)')
t.equal(warnings[8].text,
'Invalid element name: \'.bad_nesting\' (rscss/class-format)')
t.equal(warnings[9].text,
'Variant has no element: \'.-badvariant\' (rscss/class-format)')
})
})
test('class format, pascal case', t => {
runLint(t, {
files: [fixture('class_format-react.css')],
config: {
extends: './config',
rules: {
'rscss/class-format': [
true,
{
component: 'pascal-case'
}
]
}
}
}, res => {
const warnings = res.results[0].warnings
t.equal(warnings.length, 3)
t.equal(warnings[0].text,
"Invalid component name: '.bad-component' (rscss/class-format)")
t.equal(warnings[1].text,
"Invalid component name: '.bad-component.-xyz' (rscss/class-format)")
t.equal(warnings[2].text,
"Invalid component name: '.bad-component.-abc' (rscss/class-format)")
})
})
test('class format, pascal case', t => {
runLint(t, {
files: [fixture('class_format-react.css')],
config: {
extends: './config',
rules: {
'rscss/class-format': [
true,
{
component: '^([A-Z][a-z0-9]*)+$'
}
]
}
}
}, res => {
const warnings = res.results[0].warnings
t.equal(warnings.length, 3)
t.equal(warnings[0].text,
"Invalid component name: '.bad-component' (rscss/class-format)")
t.equal(warnings[1].text,
"Invalid component name: '.bad-component.-xyz' (rscss/class-format)")
t.equal(warnings[2].text,
"Invalid component name: '.bad-component.-abc' (rscss/class-format)")
})
})
test('class format - depth', t => {
runLint(t, { files: [fixture('class_format-depth.css')] }, res => {
const warnings = res.results[0].warnings
t.equal(warnings.length, 1)
t.equal(warnings[0].text,
'Component too deep: \'.my-component > .ok > .ok > .ok > .bad\' (rscss/class-format)')
})
})
test('class format - whitelist', t => {
runLint(t, { files: [fixture('class_format-whitelist.css')] }, res => {
const warnings = res.results[0].warnings
t.equal(warnings.length, 1)
t.equal(warnings[0].text,
'Invalid variant name: \'.bad\' (rscss/class-format)')
})
})
/*
* Helpers
*/
function fixture (path) {
return require('path').join(__dirname, 'fixtures', path)
}
function runLint (t, options, fn) {
require('stylelint').lint(options)
.then(result => fn(result))
.then(() => t.end())
.catch(err => t.end(err))
}