Skip to content

Commit 09d8f61

Browse files
committed
test: css validation unit tests
1 parent 2c11f68 commit 09d8f61

File tree

3 files changed

+158
-1
lines changed

3 files changed

+158
-1
lines changed

test/css-worker-task.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,19 @@ function getAllSymbols(data) {
1515
postMessage({type: "getAllSymbols", symbols});
1616
}
1717

18+
function validateCSS(data) {
19+
const cssMode = self.CSSLanguageService.CSS_MODES[data.cssMode];
20+
const diag = self.CSSLanguageService.validateCSS(data.text, cssMode, data.filePath, data.lintSettings);
21+
postMessage({type: "validateCSS", diag});
22+
}
23+
1824
self.addEventListener('message', (event) => {
1925
console.log('Worker received: ', event);
2026
const command = event.data.command;
2127
switch (command) {
2228
case 'workerOK': workerOK(); break;
2329
case 'getAllSymbols': getAllSymbols(event.data); break;
30+
case 'validateCSS': validateCSS(event.data); break;
2431
default: console.error('unknown worker command: ', command);
2532
}
2633
}, false);

test/test-css.worker.js

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* global expect*/
22

3-
describe(`web worker CSS Language tests`, function () {
3+
describe(`web worker CSS Language tests`, async function () {
44
let worker;
55
let messageFromWorker = null;
66

@@ -73,4 +73,73 @@ describe(`web worker CSS Language tests`, function () {
7373
const symbols = output.symbols;
7474
expect(symbols).to.deep.equal(['.info', '.alert', '#success']);
7575
});
76+
77+
/**
78+
* - "compatibleVendorPrefixes": Unnecessary vendor prefixes checker.
79+
* - "vendorPrefix": Warns on missing vendor prefixes.
80+
* - "boxModel": Warns if CSS box model is potentially misused.
81+
* - "universalSelector": Warns against the use of the universal selector (*).
82+
* - "fontFaceProperties": Ensures necessary properties are included in @font-face declarations.
83+
* - "hexColorLength": Enforces consistency in hex color definitions.
84+
* - "argumentsInColorFunction": Validates arguments within color functions.
85+
* - "unknownProperties": Warns on unrecognized or mistyped CSS properties.
86+
* - "ieHack": Warns about CSS hacks for older versions of Internet Explorer.
87+
* - "unknownVendorSpecificProperties": Flags vendor-specific properties that might not be universally recognized.
88+
* - "propertyIgnoredDueToDisplay": Notifies when CSS properties are ignored due to the `display` setting of an element.
89+
* - "important": Warns against the excessive use of `!important`.
90+
* - "float": Advises on the use of `float`, recommending modern layout alternatives.
91+
* - "idSelector": Advises against using ID selectors for styling.
92+
*/
93+
94+
it("should validate css zeroUnits", async function () {
95+
const cssValidationData = await (await fetch("test-files/cssValidationData.json")).json();
96+
messageFromWorker = null;
97+
const text = `.box { width: 0px;}`;
98+
worker.postMessage({
99+
command: `validateCSS`, text, cssMode: "CSS", filePath: "file:///c.css", lintSettings: {
100+
zeroUnits: "warning"
101+
}
102+
});
103+
let output = await waitForWorkerMessage(`validateCSS`, 1000);
104+
const symbols = output.diag;
105+
expect(symbols).to.deep.equal(cssValidationData["zeroUnits"]);
106+
});
107+
108+
it("should validate css duplicateProperties", async function () {
109+
const cssValidationData = await (await fetch("test-files/cssValidationData.json")).json();
110+
messageFromWorker = null;
111+
const text = `.box { color: red; color: blue; }`;
112+
worker.postMessage({
113+
command: `validateCSS`, text, cssMode: "CSS", filePath: "file:///c.css", lintSettings: {
114+
duplicateProperties: "warning"
115+
}
116+
});
117+
let output = await waitForWorkerMessage(`validateCSS`, 1000);
118+
const symbols = output.diag;
119+
expect(symbols).to.deep.equal(cssValidationData["duplicateProperties"]);
120+
});
121+
122+
it("should validate css importStatement", async function () {
123+
const cssValidationData = await (await fetch("test-files/cssValidationData.json")).json();
124+
messageFromWorker = null;
125+
const text = `@import "a.css"`;
126+
worker.postMessage({command: `validateCSS`, text, cssMode: "CSS", filePath: "file:///c.css", lintSettings: {
127+
importStatement: "warning"
128+
}});
129+
let output = await waitForWorkerMessage(`validateCSS`, 1000);
130+
const symbols = output.diag;
131+
expect(symbols).to.deep.equal(cssValidationData["importStatement"]);
132+
});
133+
134+
it("should validate css emptyRules", async function () {
135+
const cssValidationData = await (await fetch("test-files/cssValidationData.json")).json();
136+
messageFromWorker = null;
137+
const text = `.box {}`;
138+
worker.postMessage({command: `validateCSS`, text, cssMode: "CSS", filePath: "file:///c.css", lintSettings: {
139+
compatibleVendorPrefixes: "warning"
140+
}});
141+
let output = await waitForWorkerMessage(`validateCSS`, 1000);
142+
const symbols = output.diag;
143+
expect(symbols).to.deep.equal(cssValidationData["compatibleVendorPrefixes"]);
144+
});
76145
});
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"duplicateProperties": [{
3+
"code": "duplicateProperties",
4+
"source": "css",
5+
"message": "Do not use duplicate style definitions",
6+
"severity": 2,
7+
"range": {
8+
"start": {
9+
"line": 0,
10+
"character": 7
11+
},
12+
"end": {
13+
"line": 0,
14+
"character": 17
15+
}
16+
}
17+
},{
18+
"code": "duplicateProperties",
19+
"source": "css",
20+
"message": "Do not use duplicate style definitions",
21+
"severity": 2,
22+
"range": {
23+
"start": {
24+
"line": 0,
25+
"character": 19
26+
},
27+
"end": {
28+
"line": 0,
29+
"character": 30
30+
}
31+
}
32+
}],
33+
"importStatement": [{
34+
"code": "importStatement",
35+
"source": "css",
36+
"message": "Import statements do not load in parallel",
37+
"severity": 2,
38+
"range": {
39+
"start": {
40+
"line": 0,
41+
"character": 0
42+
},
43+
"end": {
44+
"line": 0,
45+
"character": 15
46+
}
47+
}
48+
}],
49+
"compatibleVendorPrefixes": [{
50+
"code": "emptyRules",
51+
"source": "css",
52+
"message": "Do not use empty rulesets",
53+
"severity": 2,
54+
"range": {
55+
"start": {
56+
"line": 0,
57+
"character": 0
58+
},
59+
"end": {
60+
"line": 0,
61+
"character": 4
62+
}
63+
}
64+
}],
65+
"zeroUnits": [{
66+
"code": "zeroUnits",
67+
"source": "css",
68+
"message": "No unit for zero needed",
69+
"severity": 2,
70+
"range": {
71+
"start": {
72+
"line": 0,
73+
"character": 14
74+
},
75+
"end": {
76+
"line": 0,
77+
"character": 17
78+
}
79+
}
80+
}]
81+
}

0 commit comments

Comments
 (0)