Skip to content

Commit

Permalink
test: add unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
oasis-cloud committed Oct 29, 2024
1 parent 5b560c5 commit 98ff201
Show file tree
Hide file tree
Showing 10 changed files with 538 additions and 107 deletions.
5 changes: 3 additions & 2 deletions packages/nutui-optimize-css/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "dist/index.cjs",
"scripts": {
"build": "unbuild",
"test": "pnpm build && node test/case.js"
"test": "pnpm build && vitest"
},
"keywords": [],
"author": "",
Expand All @@ -15,7 +15,8 @@
"@types/node": "^20.14.11",
"@types/postcss-css-variables": "^0.18.3",
"ts-node": "^10.9.2",
"unbuild": "^2.0.0"
"unbuild": "^2.0.0",
"vitest": "^1.5.0"
},
"dependencies": {
"lodash": "^4.17.21",
Expand Down
17 changes: 12 additions & 5 deletions packages/nutui-optimize-css/src/postcss-plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import postcss from 'postcss'
import postcss, { ProcessOptions, Root, Document } from 'postcss'
import { merge } from 'lodash'
import cssVariables from 'postcss-css-variables'
import { parse } from 'postcss-scss'
Expand Down Expand Up @@ -39,15 +39,22 @@ async function replaceCssVariables(
exclude: string[] = []
) {
cssVariablesContent.push(root.toResult().css)
const replacedCss = await postcss([
const options: ProcessOptions<Document | Root> = {
parser: parse,
from: undefined,
} as ProcessOptions<Root>
const replacedCss = postcss([
cssVariables({
preserve: (declaration) => {
if (exclude.includes(declaration.prop)) return true
if (exclude.includes(declaration.prop)) {
return true
}
const cssvars = declaration.value.match(/var\((--nutui-[\w\d-]+)\)/)
if (cssvars && exclude.includes(cssvars[1])) return true
return false
},
}),
]).process(cssVariablesContent.join('\n'), { parser: parse, from: undefined })
.css
]).process(cssVariablesContent.join('\n'), options).css

const replacedRoot = postcss.parse(replacedCss)
root.raws = replacedRoot.raws
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`@nutui/optimize-css > remove rtl 1`] = `
"
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`@nutui/optimize-css > optimize css 1`] = `
":root {
--nutui-color-primary-text: blue;
}
.nut-address-footer-btn {
background: linear-gradient(135deg, yellow 0%, #fa2c19 100%);
color: blue;
color: var(--nutui-color-primary-text)
}
"
`;
33 changes: 0 additions & 33 deletions packages/nutui-optimize-css/test/case.js

This file was deleted.

26 changes: 26 additions & 0 deletions packages/nutui-optimize-css/test/remove-rtl.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import postcss from 'postcss'
import { describe, expect, it } from 'vitest'
import optimizeCss from '../dist/index.cjs'

const css = `
[dir=rtl] .ca, .xcdd {
margin-left: 0;
margin-right: 9px
}
[dir=rtl] .nut-address-exist-item-info, .nut-rtl .nut-address-exist-item-info {
margin-left: 0;
margin-right: 9px
}
`
describe('@nutui/optimize-css', () => {
it('remove rtl', async () => {
const a = await postcss([
optimizeCss({
removeRtl: true,
}),
]).process(css, { from: undefined })
const optimizedCsss = a.css.toString()
// @ts-ignore
expect(optimizedCsss).toMatchSnapshot()
})
})
28 changes: 28 additions & 0 deletions packages/nutui-optimize-css/test/replace-css-var.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import postcss from 'postcss'
import path from 'path'
import { describe, expect, it } from 'vitest'
import optimizeCss from '../dist/index.cjs'

const css = `
.nut-address-footer-btn {
background: linear-gradient(135deg, var(--nutui-color-primary-stop-1, #f53d6d) 0%, var(--nutui-color-primary-stop-2, #fa2c19) 100%);
color: var(--nutui-color-primary-text)
}
`
describe('@nutui/optimize-css', () => {
it('optimize css', async () => {
const a = await postcss([
optimizeCss({
cssVariables: {
include: [path.join(__dirname, 'variables.scss')],
exclude: ['--nutui-color-primary-text'],
type: 'replace',
},
}),
]).process(css, { from: undefined })
const optimizedCsss = a.css.toString()
console.log(optimizedCsss)
// @ts-ignore
expect(optimizedCsss).toMatchSnapshot()
})
})
27 changes: 27 additions & 0 deletions packages/nutui-optimize-css/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"noImplicitAny": false,
"noUnusedLocals": true,
"noUnusedParameters": true,
"removeComments": false,
"resolveJsonModule": true,
"skipLibCheck": true,
"strictNullChecks": true,
"target": "ES2015",
"outDir": "./dist",
"rootDir": "./src",
"module": "ESNext",
"sourceMap": true,
"declaration": true,
"declarationDir": "types",
"isolatedModules": false,
"types": ["node"]
},
"include": [
"./src"
]
}
7 changes: 7 additions & 0 deletions packages/nutui-optimize-css/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
// ... Specify options here.
},
})
Loading

0 comments on commit 98ff201

Please sign in to comment.