Skip to content

Commit 875afe2

Browse files
committed
test: css validation unit tests
1 parent 09d8f61 commit 875afe2

2 files changed

Lines changed: 129 additions & 3 deletions

File tree

test/test-css.worker.js

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ describe(`web worker CSS Language tests`, async function () {
7575
});
7676

7777
/**
78+
* // todo these tests
7879
* - "compatibleVendorPrefixes": Unnecessary vendor prefixes checker.
7980
* - "vendorPrefix": Warns on missing vendor prefixes.
8081
* - "boxModel": Warns if CSS box model is potentially misused.
@@ -91,6 +92,48 @@ describe(`web worker CSS Language tests`, async function () {
9192
* - "idSelector": Advises against using ID selectors for styling.
9293
*/
9394

95+
/**
96+
* Best defaults to use:
97+
* duplicateProperties: "warning"
98+
* zeroUnits: "warning"
99+
* emptyRules: "warning"
100+
* // leave default
101+
* importStatement: none
102+
* boxModel: none
103+
*/
104+
105+
it("should validate css boxModel", async function () {
106+
const cssValidationData = await (await fetch("test-files/cssValidationData.json")).json();
107+
messageFromWorker = null;
108+
const text = `.box {
109+
width: 300px;
110+
padding: 50px;
111+
border: 5px solid black;
112+
}`;
113+
worker.postMessage({
114+
command: `validateCSS`, text, cssMode: "CSS", filePath: "file:///c.css", lintSettings: {
115+
boxModel: "warning"
116+
}
117+
});
118+
let output = await waitForWorkerMessage(`validateCSS`, 1000);
119+
const symbols = output.diag;
120+
expect(symbols).to.deep.equal(cssValidationData["boxModel"]);
121+
});
122+
123+
it("should not validate css boxModel by default", async function () {
124+
messageFromWorker = null;
125+
const text = `.box {
126+
width: 300px;
127+
padding: 50px;
128+
border: 5px solid black;
129+
}`;
130+
worker.postMessage({
131+
command: `validateCSS`, text, cssMode: "CSS", filePath: "file:///c.css"});
132+
let output = await waitForWorkerMessage(`validateCSS`, 1000);
133+
const symbols = output.diag;
134+
expect(symbols).to.deep.equal([]);
135+
});
136+
94137
it("should validate css zeroUnits", async function () {
95138
const cssValidationData = await (await fetch("test-files/cssValidationData.json")).json();
96139
messageFromWorker = null;
@@ -105,6 +148,15 @@ describe(`web worker CSS Language tests`, async function () {
105148
expect(symbols).to.deep.equal(cssValidationData["zeroUnits"]);
106149
});
107150

151+
it("should not validate css zeroUnits by default", async function () {
152+
messageFromWorker = null;
153+
const text = `.box { width: 0px;}`;
154+
worker.postMessage({command: `validateCSS`, text, cssMode: "CSS", filePath: "file:///c.css"});
155+
let output = await waitForWorkerMessage(`validateCSS`, 1000);
156+
const symbols = output.diag;
157+
expect(symbols).to.deep.equal([]);
158+
});
159+
108160
it("should validate css duplicateProperties", async function () {
109161
const cssValidationData = await (await fetch("test-files/cssValidationData.json")).json();
110162
messageFromWorker = null;
@@ -119,6 +171,16 @@ describe(`web worker CSS Language tests`, async function () {
119171
expect(symbols).to.deep.equal(cssValidationData["duplicateProperties"]);
120172
});
121173

174+
it("should not validate css duplicateProperties by default", async function () {
175+
messageFromWorker = null;
176+
const text = `.box { color: red; color: blue; }`;
177+
worker.postMessage({
178+
command: `validateCSS`, text, cssMode: "CSS", filePath: "file:///c.css"});
179+
let output = await waitForWorkerMessage(`validateCSS`, 1000);
180+
const symbols = output.diag;
181+
expect(symbols).to.deep.equal([]);
182+
});
183+
122184
it("should validate css importStatement", async function () {
123185
const cssValidationData = await (await fetch("test-files/cssValidationData.json")).json();
124186
messageFromWorker = null;
@@ -131,15 +193,33 @@ describe(`web worker CSS Language tests`, async function () {
131193
expect(symbols).to.deep.equal(cssValidationData["importStatement"]);
132194
});
133195

196+
it("should not validate css importStatement by default", async function () {
197+
messageFromWorker = null;
198+
const text = `@import "a.css"`;
199+
worker.postMessage({command: `validateCSS`, text, cssMode: "CSS", filePath: "file:///c.css"});
200+
let output = await waitForWorkerMessage(`validateCSS`, 1000);
201+
const symbols = output.diag;
202+
expect(symbols).to.deep.equal([]);
203+
});
204+
134205
it("should validate css emptyRules", async function () {
135206
const cssValidationData = await (await fetch("test-files/cssValidationData.json")).json();
136207
messageFromWorker = null;
137208
const text = `.box {}`;
138209
worker.postMessage({command: `validateCSS`, text, cssMode: "CSS", filePath: "file:///c.css", lintSettings: {
139-
compatibleVendorPrefixes: "warning"
210+
emptyRules: "warning"
140211
}});
141212
let output = await waitForWorkerMessage(`validateCSS`, 1000);
142213
const symbols = output.diag;
143-
expect(symbols).to.deep.equal(cssValidationData["compatibleVendorPrefixes"]);
214+
expect(symbols).to.deep.equal(cssValidationData["emptyRules"]);
215+
});
216+
it("should validate css emptyRules by default", async function () {
217+
const cssValidationData = await (await fetch("test-files/cssValidationData.json")).json();
218+
messageFromWorker = null;
219+
const text = `.box {}`;
220+
worker.postMessage({command: `validateCSS`, text, cssMode: "CSS", filePath: "file:///c.css"});
221+
let output = await waitForWorkerMessage(`validateCSS`, 1000);
222+
const symbols = output.diag;
223+
expect(symbols).to.deep.equal(cssValidationData["emptyRules"]);
144224
});
145225
});

test/test-files/cssValidationData.json

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
}
4747
}
4848
}],
49-
"compatibleVendorPrefixes": [{
49+
"emptyRules": [{
5050
"code": "emptyRules",
5151
"source": "css",
5252
"message": "Do not use empty rulesets",
@@ -77,5 +77,51 @@
7777
"character": 17
7878
}
7979
}
80+
}],
81+
"boxModel": [{
82+
"code": "boxModel",
83+
"source": "css",
84+
"message": "Do not use width or height when using padding or border",
85+
"severity": 2,
86+
"range": {
87+
"start": {
88+
"line": 2,
89+
"character": 12
90+
},
91+
"end": {
92+
"line": 2,
93+
"character": 25
94+
}
95+
}
96+
}, {
97+
"code": "boxModel",
98+
"source": "css",
99+
"message": "Do not use width or height when using padding or border",
100+
"severity": 2,
101+
"range": {
102+
"start": {
103+
"line": 3,
104+
"character": 12
105+
},
106+
"end": {
107+
"line": 3,
108+
"character": 35
109+
}
110+
}
111+
}, {
112+
"code": "boxModel",
113+
"source": "css",
114+
"message": "Do not use width or height when using padding or border",
115+
"severity": 2,
116+
"range": {
117+
"start": {
118+
"line": 1,
119+
"character": 12
120+
},
121+
"end": {
122+
"line": 1,
123+
"character": 24
124+
}
125+
}
80126
}]
81127
}

0 commit comments

Comments
 (0)