Skip to content

Commit 908d664

Browse files
committed
test: added inject cssvars unit test
1 parent b858a6f commit 908d664

File tree

4 files changed

+77
-99
lines changed

4 files changed

+77
-99
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`inject cssvars > injectCSSVars: basic 1`] = `
4+
"<style>.foo{ color: red }</style>
5+
<style scoped>\\\\n/* created by @unplugin-vue-cssvars */\\\\n/* <inject start> */\\\\n.foo{color:v-bind(color)}\\\\n/* <inject end> */\\\\n
6+
</style>"
7+
`;
8+
9+
exports[`inject cssvars > injectCSSVars: multiple value 1`] = `
10+
"<style>.foo{ color: red }</style>
11+
<style scoped>\\\\n/* created by @unplugin-vue-cssvars */\\\\n/* <inject start> */\\\\n.foo{color:v-bind(color)}\\\\n/* <inject end> */\\\\n\\\\n/* created by @unplugin-vue-cssvars */\\\\n/* <inject start> */\\\\n.bar{color:v-bind(color)}\\\\n/* <inject end> */\\\\n
12+
</style>"
13+
`;
14+
15+
exports[`inject cssvars > injectCSSVars: unmatched key 1`] = `"<style>.foo{ color: red }</style>"`;
16+
17+
exports[`inject cssvars > injectCSSVars: vBindCode is null 1`] = `"<style>.foo{ color: red }</style>"`;
Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,50 @@
1-
import { describe, expect, test } from 'vitest'
1+
import { beforeEach, describe, expect, test } from 'vitest'
2+
import { injectCSSVars } from '../inject-cssvars'
23
describe('inject cssvars', () => {
3-
test('basic', () => {
4-
expect(1).toBe(1)
4+
const mockCode = '<style>.foo{ color: red }</style>'
5+
const vBindValue = new Set()
6+
vBindValue.add('\\n/* created by @unplugin-vue-cssvars */\\n/* <inject start> */\\n.foo{color:v-bind(color)}\\n/* <inject end> */\\n')
7+
let mockCSSModule = [] as any
8+
const mockVariableName = {
9+
color: ' ',
10+
}
11+
let expectContent = ''
12+
beforeEach(() => {
13+
mockCSSModule = [
14+
{
15+
vBindCode: {
16+
color: vBindValue,
17+
},
18+
},
19+
]
20+
expectContent = `${mockCode}\n<style scoped>${[...mockCSSModule[0].vBindCode.color]}\n</style>`
21+
})
22+
23+
test('injectCSSVars: basic', () => {
24+
const res = injectCSSVars(mockCode, mockCSSModule as any, mockVariableName as any)
25+
expect(res).toBe(expectContent)
26+
expect(res).matchSnapshot()
27+
})
28+
29+
test('injectCSSVars: vBindCode is null', () => {
30+
mockCSSModule[0].vBindCode = null
31+
const res = injectCSSVars(mockCode, mockCSSModule as any, mockVariableName as any)
32+
expect(res).toBe(mockCode)
33+
expect(res).matchSnapshot()
34+
})
35+
36+
test('injectCSSVars: unmatched key', () => {
37+
const res = injectCSSVars(mockCode, mockCSSModule as any, { foo: '' } as any)
38+
expect(res).toBe(mockCode)
39+
expect(res).matchSnapshot()
40+
})
41+
42+
test('injectCSSVars: multiple value', () => {
43+
vBindValue.add('\\n/* created by @unplugin-vue-cssvars */\\n/* <inject start> */\\n.bar{color:v-bind(color)}\\n/* <inject end> */\\n')
44+
expectContent = `${mockCode}\n<style scoped>${[...mockCSSModule[0].vBindCode.color].join('')}\n</style>`
45+
const res = injectCSSVars(mockCode, mockCSSModule as any, mockVariableName as any)
46+
console.log(res)
47+
expect(res).toBe(expectContent)
48+
expect(res).matchSnapshot()
549
})
650
})

packages/core/inject/inject-cssvars.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export const injectCSSVars = (
1616
})
1717
}
1818
})
19-
code = `${code}\n<style scoped>${injectCSSSet.join('')}\n</style>`
19+
if (injectCSSSet.length > 0)
20+
code = `${code}\n<style scoped>${injectCSSSet.join('')}\n</style>`
21+
2022
return code
2123
}

pnpm-lock.yaml

Lines changed: 10 additions & 95 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)