Skip to content

Commit 1456f55

Browse files
Added tests
1 parent 33e289d commit 1456f55

File tree

10 files changed

+271
-6
lines changed

10 files changed

+271
-6
lines changed

.gitignore

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
/node_modules
2-
/package-lock.json
3-
/test.js
4-
/style.css
5-
/index.html
2+
/package-lock.json

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/tests

package.json

Lines changed: 5 additions & 2 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 test.js"
8+
"test": "node tests/index.test.js"
99
},
1010
"type": "module",
1111
"repository": {
@@ -32,5 +32,8 @@
3232
"fs-extra": "^11.2.0",
3333
"html-format": "^1.1.7"
3434
},
35-
"license": "ISC"
35+
"license": "ISC",
36+
"devDependencies": {
37+
"chalk": "^5.3.0"
38+
}
3639
}

tests/IO.js

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

tests/attrs.js

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

tests/general.js

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

tests/helpers/test.css

Whitespace-only changes.

tests/helpers/test.html

Whitespace-only changes.

tests/index.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import chalk from 'chalk'
2+
3+
import './text.js'
4+
import './attrs.js'
5+
import './general.js'
6+
import './IO.js'
7+
8+
console.log(chalk.green("*** All checks have been passed! ***"))

tests/text.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { CssToHtml } from '../cssToHtml.js'
2+
import assert from 'assert'
3+
4+
5+
assert.equal(
6+
new CssToHtml(
7+
{
8+
css:
9+
`div { /*
10+
@before before text @inside inner text @after after text
11+
*/ }`,
12+
}
13+
).outputHTML,
14+
15+
`before text<div>inner text</div>after text
16+
`,
17+
18+
'The text without additional spaces should be saved without spaces.'
19+
)
20+
assert.match(
21+
new CssToHtml(
22+
{
23+
css:
24+
`div { /*
25+
@before before text @inside inner text @after after text
26+
*/ }`,
27+
}
28+
).outputHTML,
29+
30+
/ +before text +<div> +inner text +<\/div> +after text[\n]/,
31+
32+
'The text with additional spaces should keep them.'
33+
)
34+
assert.equal(
35+
new CssToHtml({
36+
css:
37+
`div { /*
38+
@before before text @inside inner text @after after text
39+
*/
40+
/*
41+
@inside inner text
42+
*/
43+
/*
44+
@after after text
45+
*/
46+
}`,
47+
})
48+
.outputHTML,
49+
50+
`before text<div>inner text
51+
</div>after text
52+
`,
53+
54+
'The text should be saved from different comments.'
55+
)
56+
assert.equal(
57+
new CssToHtml({
58+
css:
59+
`
60+
div { /*
61+
@before before 1
62+
before 2
63+
@inside
64+
inner 1
65+
inner 2
66+
inner 3
67+
@after after 1
68+
after 2
69+
*/ }`,
70+
})
71+
.outputHTML,
72+
73+
`before 1
74+
before 2
75+
<div>
76+
inner 1
77+
inner 2
78+
inner 3
79+
</div>after 1
80+
after 2
81+
`,
82+
'The text should be processed along with the new lines.'
83+
)

0 commit comments

Comments
 (0)