Skip to content

Commit 98ff201

Browse files
committed
test: add unit testing
1 parent 5b560c5 commit 98ff201

File tree

10 files changed

+538
-107
lines changed

10 files changed

+538
-107
lines changed

packages/nutui-optimize-css/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "dist/index.cjs",
66
"scripts": {
77
"build": "unbuild",
8-
"test": "pnpm build && node test/case.js"
8+
"test": "pnpm build && vitest"
99
},
1010
"keywords": [],
1111
"author": "",
@@ -15,7 +15,8 @@
1515
"@types/node": "^20.14.11",
1616
"@types/postcss-css-variables": "^0.18.3",
1717
"ts-node": "^10.9.2",
18-
"unbuild": "^2.0.0"
18+
"unbuild": "^2.0.0",
19+
"vitest": "^1.5.0"
1920
},
2021
"dependencies": {
2122
"lodash": "^4.17.21",

packages/nutui-optimize-css/src/postcss-plugins/index.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import postcss from 'postcss'
1+
import postcss, { ProcessOptions, Root, Document } from 'postcss'
22
import { merge } from 'lodash'
33
import cssVariables from 'postcss-css-variables'
44
import { parse } from 'postcss-scss'
@@ -39,15 +39,22 @@ async function replaceCssVariables(
3939
exclude: string[] = []
4040
) {
4141
cssVariablesContent.push(root.toResult().css)
42-
const replacedCss = await postcss([
42+
const options: ProcessOptions<Document | Root> = {
43+
parser: parse,
44+
from: undefined,
45+
} as ProcessOptions<Root>
46+
const replacedCss = postcss([
4347
cssVariables({
4448
preserve: (declaration) => {
45-
if (exclude.includes(declaration.prop)) return true
49+
if (exclude.includes(declaration.prop)) {
50+
return true
51+
}
52+
const cssvars = declaration.value.match(/var\((--nutui-[\w\d-]+)\)/)
53+
if (cssvars && exclude.includes(cssvars[1])) return true
4654
return false
4755
},
4856
}),
49-
]).process(cssVariablesContent.join('\n'), { parser: parse, from: undefined })
50-
.css
57+
]).process(cssVariablesContent.join('\n'), options).css
5158

5259
const replacedRoot = postcss.parse(replacedCss)
5360
root.raws = replacedRoot.raws
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`@nutui/optimize-css > remove rtl 1`] = `
4+
"
5+
"
6+
`;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`@nutui/optimize-css > optimize css 1`] = `
4+
":root {
5+
--nutui-color-primary-text: blue;
6+
}
7+
8+
9+
.nut-address-footer-btn {
10+
background: linear-gradient(135deg, yellow 0%, #fa2c19 100%);
11+
color: blue;
12+
color: var(--nutui-color-primary-text)
13+
}
14+
"
15+
`;

packages/nutui-optimize-css/test/case.js

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import postcss from 'postcss'
2+
import { describe, expect, it } from 'vitest'
3+
import optimizeCss from '../dist/index.cjs'
4+
5+
const css = `
6+
[dir=rtl] .ca, .xcdd {
7+
margin-left: 0;
8+
margin-right: 9px
9+
}
10+
[dir=rtl] .nut-address-exist-item-info, .nut-rtl .nut-address-exist-item-info {
11+
margin-left: 0;
12+
margin-right: 9px
13+
}
14+
`
15+
describe('@nutui/optimize-css', () => {
16+
it('remove rtl', async () => {
17+
const a = await postcss([
18+
optimizeCss({
19+
removeRtl: true,
20+
}),
21+
]).process(css, { from: undefined })
22+
const optimizedCsss = a.css.toString()
23+
// @ts-ignore
24+
expect(optimizedCsss).toMatchSnapshot()
25+
})
26+
})
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import postcss from 'postcss'
2+
import path from 'path'
3+
import { describe, expect, it } from 'vitest'
4+
import optimizeCss from '../dist/index.cjs'
5+
6+
const css = `
7+
.nut-address-footer-btn {
8+
background: linear-gradient(135deg, var(--nutui-color-primary-stop-1, #f53d6d) 0%, var(--nutui-color-primary-stop-2, #fa2c19) 100%);
9+
color: var(--nutui-color-primary-text)
10+
}
11+
`
12+
describe('@nutui/optimize-css', () => {
13+
it('optimize css', async () => {
14+
const a = await postcss([
15+
optimizeCss({
16+
cssVariables: {
17+
include: [path.join(__dirname, 'variables.scss')],
18+
exclude: ['--nutui-color-primary-text'],
19+
type: 'replace',
20+
},
21+
}),
22+
]).process(css, { from: undefined })
23+
const optimizedCsss = a.css.toString()
24+
console.log(optimizedCsss)
25+
// @ts-ignore
26+
expect(optimizedCsss).toMatchSnapshot()
27+
})
28+
})
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"allowSyntheticDefaultImports": true,
5+
"experimentalDecorators": true,
6+
"moduleResolution": "node",
7+
"noImplicitAny": false,
8+
"noUnusedLocals": true,
9+
"noUnusedParameters": true,
10+
"removeComments": false,
11+
"resolveJsonModule": true,
12+
"skipLibCheck": true,
13+
"strictNullChecks": true,
14+
"target": "ES2015",
15+
"outDir": "./dist",
16+
"rootDir": "./src",
17+
"module": "ESNext",
18+
"sourceMap": true,
19+
"declaration": true,
20+
"declarationDir": "types",
21+
"isolatedModules": false,
22+
"types": ["node"]
23+
},
24+
"include": [
25+
"./src"
26+
]
27+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
// ... Specify options here.
6+
},
7+
})

0 commit comments

Comments
 (0)