forked from stylelint/stylelint
/
checkAgainstRule.test.js
145 lines (118 loc) · 3.04 KB
/
checkAgainstRule.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
136
137
138
139
140
141
142
143
144
145
'use strict';
const report = require('../report');
const validateOptions = require('../validateOptions');
const checkAgainstRule = require('../checkAgainstRule');
const postcss = require('postcss');
const mockRuleName = 'custom/no-empty-source';
const mockResult = {
stylelint: {
config: {
pluginFunctions: {
[mockRuleName]: (primary) => (root, result) => {
const validOptions = validateOptions(result, mockRuleName, {
actual: primary,
});
if (!validOptions || root.source.input.css) return;
report({
result,
message: 'Unexpected empty source',
ruleName: mockRuleName,
node: root,
});
},
},
},
},
};
describe('checkAgainstRule', () => {
it('does nothing with no errors', () => {
const root = postcss.parse('a {} @media {}');
const warnings = [];
checkAgainstRule(
{
ruleName: 'at-rule-name-case',
ruleSettings: 'lower',
root,
},
(warning) => warnings.push(warning),
);
expect(warnings).toHaveLength(0);
});
it('handles non-array rule settings', () => {
const root = postcss.parse('a {} @media {}');
const warnings = [];
checkAgainstRule(
{
ruleName: 'at-rule-name-case',
ruleSettings: 'upper',
root,
},
(warning) => warnings.push(warning),
);
expect(warnings).toHaveLength(1);
expect(warnings[0].rule).toBe('at-rule-name-case');
expect(warnings[0].line).toBe(1);
expect(warnings[0].column).toBe(6);
});
it('handles array rule settings', () => {
const root = postcss.parse('@import horse.css\n@media {}\n@import dog.css;');
const warnings = [];
checkAgainstRule(
{
ruleName: 'at-rule-empty-line-before',
ruleSettings: ['always', { ignoreAtRules: ['media'] }],
root,
},
(warning) => warnings.push(warning),
);
expect(warnings).toHaveLength(1);
expect(warnings[0].rule).toBe('at-rule-empty-line-before');
expect(warnings[0].line).toBe(3);
expect(warnings[0].column).toBe(1);
});
it('checks against custom rule (passing)', () => {
const root = postcss.parse('.not-empty {}');
const warnings = [];
checkAgainstRule(
{
ruleName: mockRuleName,
result: mockResult,
ruleSettings: true,
root,
},
(warning) => warnings.push(warning),
);
expect(warnings).toHaveLength(0);
});
it('checks against custom rule (failing)', () => {
const root = postcss.parse('');
const warnings = [];
checkAgainstRule(
{
ruleName: mockRuleName,
result: mockResult,
ruleSettings: true,
root,
},
(warning) => warnings.push(warning),
);
expect(warnings).toHaveLength(1);
expect(warnings[0].rule).toBe(mockRuleName);
expect(warnings[0].line).toBe(1);
expect(warnings[0].column).toBe(1);
});
test('throws when checking against custom rule without result object', () => {
expect(() => {
const root = postcss.parse('');
const warnings = [];
checkAgainstRule(
{
ruleName: mockRuleName,
ruleSettings: true,
root,
},
(warning) => warnings.push(warning),
);
}).toThrow(`Rule '${mockRuleName}' does not exist`);
});
});