Skip to content

Commit 5917b37

Browse files
Improved tests.
1 parent 11fcb0e commit 5917b37

File tree

10 files changed

+260
-269
lines changed

10 files changed

+260
-269
lines changed

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"description": "The library for converting CSS to HTML",
66
"main": "index.js",
77
"scripts": {
8-
"test": "node tests/index.test.js"
8+
"test": "node --test --watch"
99
},
1010
"type": "module",
1111
"repository": {
@@ -32,8 +32,5 @@
3232
"fs-extra": "^11.2.0",
3333
"html-format": "^1.1.7"
3434
},
35-
"license": "ISC",
36-
"devDependencies": {
37-
"chalk": "^5.3.0"
38-
}
35+
"license": "ISC"
3936
}

tests/IO.js

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

tests/IO.test.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import path from 'path'
2+
import fs from 'fs'
3+
import { CssToHtml } from '../cssToHtml.js'
4+
import assert from 'assert'
5+
import test from 'node:test'
6+
7+
8+
const htmlFilePath = path.resolve(import.meta.dirname + '/helpers/test.html')
9+
const cssFilePath = path.resolve(import.meta.dirname + '/helpers/test.css')
10+
11+
test('Writing code to a file should work.', () => {
12+
new CssToHtml({
13+
css: `div {}`,
14+
write: { in: htmlFilePath }
15+
})
16+
17+
assert.equal(fs.readFileSync(htmlFilePath, 'utf8'), '<div></div>\n')
18+
})
19+
20+
test('The code must be written after certain content.', () => {
21+
fs.writeFileSync(htmlFilePath,
22+
`<some-html-content>
23+
</some-html-content>
24+
`)
25+
new CssToHtml({
26+
css: `div {}`,
27+
write: {
28+
in: htmlFilePath,
29+
after: '<some-html-content>'
30+
}
31+
})
32+
33+
assert.equal(
34+
fs.readFileSync(htmlFilePath, 'utf8'),
35+
`<some-html-content>
36+
<div></div>
37+
`)
38+
})
39+
40+
test('The code must be written before and after certain content.', () => {
41+
fs.writeFileSync(htmlFilePath,
42+
`<some-html-content>
43+
</some-html-content>
44+
`)
45+
new CssToHtml({
46+
css: `div {}`,
47+
write: {
48+
in: htmlFilePath,
49+
after: '<some-html-content>',
50+
before: '</some-html-content>'
51+
}
52+
})
53+
54+
assert.equal(fs.readFileSync(htmlFilePath, 'utf8'),
55+
`<some-html-content>
56+
<div></div>
57+
</some-html-content>
58+
`)
59+
})
60+
61+
test('Empty CSS should not be processed.', () => {
62+
fs.writeFileSync(cssFilePath, '')
63+
64+
assert.equal(
65+
new CssToHtml({
66+
css: fs.readFileSync(cssFilePath, 'utf8'),
67+
}).outputHTML,
68+
69+
undefined
70+
)
71+
})
72+
73+
test('The code from the CSS file must be processed.', () => {
74+
fs.writeFileSync(cssFilePath, 'div {} div span { /* @inside text */ }')
75+
76+
assert.equal(
77+
new CssToHtml({
78+
css: fs.readFileSync(cssFilePath, 'utf8'),
79+
}).outputHTML,
80+
81+
`<div>
82+
<span>text</span>
83+
</div>
84+
`)
85+
})
86+
87+
88+
89+
test('Temporary data has been deleted from files', () => {
90+
fs.writeFileSync(htmlFilePath, '')
91+
fs.writeFileSync(cssFilePath, '')
92+
})

tests/attrs.js

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

tests/attrs.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { CssToHtml } from '../cssToHtml.js'
2+
import assert from 'assert'
3+
import test from 'node:test'
4+
import { dot } from 'node:test/reporters'
5+
6+
test('Attributes in the selector must be processed.', () => {
7+
assert.equal(
8+
new CssToHtml({
9+
css: `div[attr-1][data-attr][role="button"] {}`,
10+
})
11+
.outputHTML,
12+
13+
'<div attr-1 data-attr role="button"></div>\n',
14+
)
15+
})
16+
test('Attributes in the rule must be processed.', () => {
17+
assert.equal(
18+
new CssToHtml({
19+
css: `
20+
div {
21+
--attr-href: #;
22+
--attr-role: "button";
23+
--data-attr: 15;
24+
--attrs: "tabindex="0" data-v";
25+
}`,
26+
})
27+
.outputHTML,
28+
29+
'<div href="#" role="button" data-attr="15" tabindex="0" data-v></div>\n',
30+
)
31+
})

tests/general.js

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

tests/general.test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { CssToHtml } from '../cssToHtml.js'
2+
import assert from 'assert'
3+
import test from 'node:test'
4+
5+
6+
test('The code must be converted.', () => {
7+
assert.equal(
8+
new CssToHtml({
9+
css: `div {} div span {} custom-tag {} input {}`,
10+
})
11+
.outputHTML,
12+
13+
`<div>
14+
<span></span>
15+
</div>
16+
<custom-tag></custom-tag>
17+
<input />
18+
`,
19+
)
20+
})
21+
test('With formatting turned off, the code should look ugly.', () => {
22+
assert.notEqual(
23+
new CssToHtml({
24+
css: `div {} div span {}`,
25+
format: false,
26+
}).
27+
outputHTML,
28+
29+
`<div>
30+
<span></span>
31+
</div>
32+
`,
33+
)
34+
})
35+
test('If an empty CSS code is specified, it should return nothing.', () => {
36+
assert.equal(
37+
new CssToHtml({
38+
css: '',
39+
}).outputHTML,
40+
41+
undefined,
42+
)
43+
})
44+
test('The selector must be fully processed.', () => {
45+
assert.equal(
46+
new CssToHtml({
47+
css: `div.some-class.class2#some-id[data-attr] {}`,
48+
})
49+
.outputHTML,
50+
51+
'<div id="some-id" class="some-class class2" data-attr></div>\n',
52+
)
53+
})
54+

tests/index.test.js

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

0 commit comments

Comments
 (0)