Skip to content

Commit bc8b817

Browse files
committed
test: added unit test
1 parent bff626c commit bc8b817

File tree

14 files changed

+183
-15
lines changed

14 files changed

+183
-15
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,26 +73,26 @@
7373
"test:coverage": "vitest --coverage"
7474
},
7575
"peerDependencies": {
76+
"baiwusanyu-utils": "^1.0.5",
7677
"chalk": "^4.1.2",
7778
"estree-walker-ts": "^1.0.0-beta.2",
7879
"fast-glob": "^3.2.12",
7980
"fs-extra": "^11.1.1",
8081
"hash-sum": "^2.0.0",
8182
"magic-string": "^0.30.0",
8283
"unplugin": "^1.3.1",
83-
"vue": "^3.2.47",
84-
"baiwusanyu-utils": "^1.0.5"
84+
"vue": "^3.2.47"
8585
},
8686
"dependencies": {
87+
"baiwusanyu-utils": "^1.0.5",
8788
"chalk": "^4.1.2",
8889
"estree-walker-ts": "^1.0.0-beta.2",
8990
"fast-glob": "^3.2.12",
9091
"fs-extra": "^11.1.1",
9192
"hash-sum": "^2.0.0",
9293
"magic-string": "^0.30.0",
9394
"unplugin": "^1.3.1",
94-
"vue": "^3.2.47",
95-
"baiwusanyu-utils": "^1.0.5"
95+
"vue": "^3.2.47"
9696
},
9797
"devDependencies": {
9898
"@babel/parser": "^7.20.15",

packages/core/hmr/hmr.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export function viteHMR(
1818
reloadSFCModules(CSSFileModuleMap, userOptions, sfcModulesPathList!, file, server)
1919
}
2020

21-
// TODO: unit test
2221
export function webpackHMR(
2322
CSSFileModuleMap: ICSSFileMap,
2423
userOptions: Options,

packages/core/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createUnplugin } from 'unplugin'
2-
import { NAME, transformSymbol} from '@unplugin-vue-cssvars/utils'
2+
import { NAME, transformSymbol } from '@unplugin-vue-cssvars/utils'
33
import { log, setGlobalPrefix } from 'baiwusanyu-utils'
44
import { createFilter } from '@rollup/pluginutils'
55
import MagicString from 'magic-string'

packages/core/runtime/__test__/__snapshots__/pre-process-css.spec.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ exports[`pre process css > preProcessCSS: basic 2`] = `
1515
exports[`pre process css > preProcessCSS: basic 3`] = `
1616
[
1717
"core/hmr/__test__/style/foo.css",
18+
"core/runtime/__test__/style/foo.css",
1819
"core/runtime/__test__/style/test.css",
1920
"core/runtime/__test__/style/test2.css",
2021
]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { describe, expect, test } from 'vitest'
2+
import MagicString from 'magic-string'
3+
import { handleInjectCss } from '../handle-inject-css'
4+
import code from './sfc/mock-code.txt?raw'
5+
describe('handle inject css', () => {
6+
test('handleInjectCss: basic', () => {
7+
const mockId = 'D:/project-github/unplugin-vue-cssvars/play/vite/src/views/app/App.vue'
8+
const mockCode = code
9+
const mockMgc = new MagicString(code)
10+
const mockContext = {
11+
vbindVariableList: new Map(),
12+
isScriptSetup: true,
13+
}
14+
mockContext.vbindVariableList.set(mockId, [
15+
{ value: 'sassColor', has: true, isRef: true },
16+
{ value: 'color', has: true, isRef: true },
17+
])
18+
const res = handleInjectCss(mockId, mockCode, mockMgc, mockContext as any)
19+
expect(res).toMatchSnapshot()
20+
})
21+
})
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { describe, expect, test } from 'vitest'
2+
import { handleVBindVariable } from '../handle-variable'
3+
import code from './sfc/mock-sfc.vue?raw'
4+
const mockId = 'D:/project-github/unplugin-vue-cssvars/play/vite/src/views/app/App.vue'
5+
const mockCode = code
6+
const initMockCtx = () => {
7+
return {
8+
CSSFileModuleMap: new Map(),
9+
vbindVariableList: new Map(),
10+
isScriptSetup: false,
11+
isServer: true,
12+
userOptions: {
13+
alias: {
14+
'@': 'D:\\project-github\\unplugin-vue-cssvars\\play\\vite\\src',
15+
},
16+
},
17+
}
18+
}
19+
const initMockCSSFileModuleMap = (mockContext: any) => {
20+
mockContext.CSSFileModuleMap.set(
21+
'D:/project-github/unplugin-vue-cssvars/play/vite/src/assets/css/foo.css',
22+
{
23+
vBindCode: ['sassColor', 'color'],
24+
content: '#foo{\n'
25+
+ ' color: v-bind-m(sassColor);\n'
26+
+ ' background: #ffebf8;\n'
27+
+ ' width: 200px;\n'
28+
+ ' height: 30px;\n'
29+
+ '}\n'
30+
+ 'p {\n'
31+
+ ' color: v-bind-m(color);\n'
32+
+ '}\n',
33+
lang: 'css',
34+
importer: new Set(),
35+
},
36+
)
37+
}
38+
describe('handle variable', () => {
39+
test('handleVBindVariable: basic', () => {
40+
const mockContext = initMockCtx()
41+
initMockCSSFileModuleMap(mockContext)
42+
handleVBindVariable(mockCode, mockId, mockContext as any)
43+
expect(mockContext.isScriptSetup).toBeTruthy()
44+
expect(mockContext.vbindVariableList.get(mockId)).toMatchObject([
45+
{ value: 'sassColor', has: true, isRef: true },
46+
{ value: 'color', has: true, isRef: true },
47+
])
48+
})
49+
50+
test('handleVBindVariable: server is false', () => {
51+
const mockContext = initMockCtx()
52+
mockContext.isServer = false
53+
initMockCSSFileModuleMap(mockContext)
54+
const res = handleVBindVariable(mockCode, mockId, mockContext as any)
55+
expect(mockContext.isScriptSetup).toBeTruthy()
56+
expect(mockContext.vbindVariableList.get(mockId)).toMatchObject([
57+
{ value: 'sassColor', has: true, isRef: true },
58+
{ value: 'color', has: true, isRef: true },
59+
])
60+
expect(res.injectCSSContent).toMatchSnapshot()
61+
})
62+
})

packages/core/runtime/__test__/pre-process-css.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ describe('pre process css', () => {
136136
const files = getAllCSSFilePath(['**/**.css'], resolve('packages'))
137137
expect(files).toMatchObject([
138138
'core/hmr/__test__/style/foo.css',
139+
'core/runtime/__test__/style/foo.css',
139140
'core/runtime/__test__/style/test.css',
140141
'core/runtime/__test__/style/test2.css',
141142
])
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { createElementBlock as _createElementBlock, createElementVNode as _createElementVNode, createVNode as _createVNode, defineComponent as _defineComponent, openBlock as _openBlock, popScopeId as _popScopeId, pushScopeId as _pushScopeId, reactive, ref } from 'vue'
2+
3+
import Comp from '../../comp.vue'
4+
import Comp2 from '../../comp2.vue'
5+
6+
import 'D:/project-github/unplugin-vue-cssvars/play/vite/src/views/app/App.vue?vue&type=style&index=0&scoped=229090c3&lang.scss'
7+
import _export_sfc from '\0plugin-vue:export-helper'
8+
const _sfc_main = /* @__PURE__ */ _defineComponent({
9+
__name: 'App',
10+
setup(__props, { expose }) {
11+
expose()
12+
const color = ref('blue')
13+
const appAsd = () => 'red'
14+
const fooColor = appAsd()
15+
const appTheme2 = 'blue'
16+
const lessColor = 'greenyellow'
17+
const sassColor = ref('#94c9ff')
18+
const stylColor = '#fd1d7c'
19+
const appTheme3 = ref('red')
20+
const appTheme4 = reactive({ color: 'red' })
21+
const appTheme5 = { color: 'red' }
22+
const appTheme6 = () => 'red'
23+
const __returned__ = { color, appAsd, fooColor, appTheme2, lessColor, sassColor, stylColor, appTheme3, appTheme4, appTheme5, appTheme6, Comp, Comp2 }
24+
Object.defineProperty(__returned__, '__isScriptSetup', { enumerable: false, value: true })
25+
return __returned__
26+
},
27+
})
28+
const _withScopeId = n => (_pushScopeId('data-v-229090c3'), n = n(), _popScopeId(), n)
29+
const _hoisted_1 = /* @__PURE__ */ _withScopeId(() => /* @__PURE__ */ _createElementVNode(
30+
'p',
31+
null,
32+
'app122',
33+
-1,
34+
/* HOISTED */
35+
))
36+
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
37+
return _openBlock(), _createElementBlock('div', {
38+
id: 'foo',
39+
class: 'scss',
40+
onClick: _cache[0] || (_cache[0] = $event => $setup.sassColor = 'red'),
41+
}, [
42+
_hoisted_1,
43+
_createVNode($setup.Comp),
44+
_createVNode($setup.Comp2),
45+
])
46+
}
47+
_sfc_main.__hmrId = '229090c3'
48+
typeof __VUE_HMR_RUNTIME__ !== 'undefined' && __VUE_HMR_RUNTIME__.createRecord(_sfc_main.__hmrId, _sfc_main)
49+
import.meta.hot.accept((mod) => {
50+
if (!mod)
51+
return
52+
const { default: updated, _rerender_only } = mod
53+
if (_rerender_only)
54+
__VUE_HMR_RUNTIME__.rerender(updated.__hmrId, updated.render)
55+
else
56+
__VUE_HMR_RUNTIME__.reload(updated.__hmrId, updated)
57+
})
58+
export default /* @__PURE__ */ _export_sfc(_sfc_main, [['render', _sfc_render], ['__scopeId', 'data-v-229090c3'], ['__file', 'D:/project-github/unplugin-vue-cssvars/play/vite/src/views/app/App.vue']])
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<script setup lang="ts">
2+
import { reactive, ref } from 'vue'
3+
const color = ref('blue')
4+
const appAsd = () => 'red'
5+
const fooColor = appAsd()
6+
const appTheme2 = 'blue'
7+
const lessColor = 'greenyellow'
8+
const sassColor = ref('#94c9ff')
9+
const stylColor = '#fd1d7c'
10+
const appTheme3 = ref('red')
11+
const appTheme4 = reactive({ color: 'red' })
12+
const appTheme5 = { color: 'red' }
13+
const appTheme6 = () => 'red'
14+
</script>
15+
16+
<template>
17+
<div id="foo" class="scss" @click="sassColor = 'red'">
18+
<p>app122</p>
19+
</div>
20+
</template>
21+
22+
<style lang="scss" scoped>
23+
@import '@/assets/css/foo.css';
24+
</style>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#foo{
2+
color: v-bind-m(sassColor);
3+
background: #ffebf8;
4+
width: 200px;
5+
height: 30px;
6+
}
7+
p {
8+
color: v-bind-m(color);
9+
}

0 commit comments

Comments
 (0)