feat(compiler-sfc): new SFC css varaible injection implementation
ref: https://github.com/vuejs/rfcs/pull/231
This commit is contained in:
@@ -10,28 +10,26 @@ import {
|
||||
} from '@vue/runtime-dom'
|
||||
|
||||
describe('useCssVars', () => {
|
||||
async function assertCssVars(
|
||||
getApp: (state: any) => ComponentOptions,
|
||||
scopeId?: string
|
||||
) {
|
||||
const id = 'xxxxxx'
|
||||
async function assertCssVars(getApp: (state: any) => ComponentOptions) {
|
||||
const state = reactive({ color: 'red' })
|
||||
const App = getApp(state)
|
||||
const root = document.createElement('div')
|
||||
const prefix = scopeId ? `${scopeId.replace(/^data-v-/, '')}-` : ``
|
||||
|
||||
render(h(App), root)
|
||||
await nextTick()
|
||||
for (const c of [].slice.call(root.children as any)) {
|
||||
expect(
|
||||
(c as HTMLElement).style.getPropertyValue(`--${prefix}color`)
|
||||
).toBe(`red`)
|
||||
expect((c as HTMLElement).style.getPropertyValue(`--${id}-color`)).toBe(
|
||||
`red`
|
||||
)
|
||||
}
|
||||
|
||||
state.color = 'green'
|
||||
await nextTick()
|
||||
for (const c of [].slice.call(root.children as any)) {
|
||||
expect(
|
||||
(c as HTMLElement).style.getPropertyValue(`--${prefix}color`)
|
||||
).toBe('green')
|
||||
expect((c as HTMLElement).style.getPropertyValue(`--${id}-color`)).toBe(
|
||||
'green'
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,9 +37,12 @@ describe('useCssVars', () => {
|
||||
await assertCssVars(state => ({
|
||||
setup() {
|
||||
// test receiving render context
|
||||
useCssVars((ctx: any) => ({
|
||||
color: ctx.color
|
||||
}))
|
||||
useCssVars(
|
||||
(ctx: any) => ({
|
||||
color: ctx.color
|
||||
}),
|
||||
id
|
||||
)
|
||||
return state
|
||||
},
|
||||
render() {
|
||||
@@ -53,7 +54,7 @@ describe('useCssVars', () => {
|
||||
test('on fragment root', async () => {
|
||||
await assertCssVars(state => ({
|
||||
setup() {
|
||||
useCssVars(() => state)
|
||||
useCssVars(() => state, id)
|
||||
return () => [h('div'), h('div')]
|
||||
}
|
||||
}))
|
||||
@@ -64,7 +65,7 @@ describe('useCssVars', () => {
|
||||
|
||||
await assertCssVars(state => ({
|
||||
setup() {
|
||||
useCssVars(() => state)
|
||||
useCssVars(() => state, id)
|
||||
return () => h(Child)
|
||||
}
|
||||
}))
|
||||
@@ -74,15 +75,23 @@ describe('useCssVars', () => {
|
||||
const state = reactive({ color: 'red' })
|
||||
const root = document.createElement('div')
|
||||
|
||||
let resolveAsync: any
|
||||
let asyncPromise: any
|
||||
|
||||
const AsyncComp = {
|
||||
async setup() {
|
||||
return () => h('p', 'default')
|
||||
setup() {
|
||||
asyncPromise = new Promise(r => {
|
||||
resolveAsync = () => {
|
||||
r(() => h('p', 'default'))
|
||||
}
|
||||
})
|
||||
return asyncPromise
|
||||
}
|
||||
}
|
||||
|
||||
const App = {
|
||||
setup() {
|
||||
useCssVars(() => state)
|
||||
useCssVars(() => state, id)
|
||||
return () =>
|
||||
h(Suspense, null, {
|
||||
default: h(AsyncComp),
|
||||
@@ -92,39 +101,42 @@ describe('useCssVars', () => {
|
||||
}
|
||||
|
||||
render(h(App), root)
|
||||
await nextTick()
|
||||
// css vars use with fallback tree
|
||||
for (const c of [].slice.call(root.children as any)) {
|
||||
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
|
||||
expect((c as HTMLElement).style.getPropertyValue(`--${id}-color`)).toBe(
|
||||
`red`
|
||||
)
|
||||
}
|
||||
// AsyncComp resolve
|
||||
await nextTick()
|
||||
resolveAsync()
|
||||
await asyncPromise.then(() => {})
|
||||
// Suspense effects flush
|
||||
await nextTick()
|
||||
// css vars use with default tree
|
||||
for (const c of [].slice.call(root.children as any)) {
|
||||
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
|
||||
expect((c as HTMLElement).style.getPropertyValue(`--${id}-color`)).toBe(
|
||||
`red`
|
||||
)
|
||||
}
|
||||
|
||||
state.color = 'green'
|
||||
await nextTick()
|
||||
for (const c of [].slice.call(root.children as any)) {
|
||||
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('green')
|
||||
expect((c as HTMLElement).style.getPropertyValue(`--${id}-color`)).toBe(
|
||||
'green'
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
test('with <style scoped>', async () => {
|
||||
const id = 'data-v-12345'
|
||||
|
||||
await assertCssVars(
|
||||
state => ({
|
||||
__scopeId: id,
|
||||
setup() {
|
||||
useCssVars(() => state, true)
|
||||
return () => h('div')
|
||||
}
|
||||
}),
|
||||
id
|
||||
)
|
||||
await assertCssVars(state => ({
|
||||
__scopeId: id,
|
||||
setup() {
|
||||
useCssVars(() => state, id)
|
||||
return () => h('div')
|
||||
}
|
||||
}))
|
||||
})
|
||||
|
||||
test('with subTree changed', async () => {
|
||||
@@ -134,21 +146,26 @@ describe('useCssVars', () => {
|
||||
|
||||
const App = {
|
||||
setup() {
|
||||
useCssVars(() => state)
|
||||
useCssVars(() => state, id)
|
||||
return () => (value.value ? [h('div')] : [h('div'), h('div')])
|
||||
}
|
||||
}
|
||||
|
||||
render(h(App), root)
|
||||
await nextTick()
|
||||
// css vars use with fallback tree
|
||||
for (const c of [].slice.call(root.children as any)) {
|
||||
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
|
||||
expect((c as HTMLElement).style.getPropertyValue(`--${id}-color`)).toBe(
|
||||
`red`
|
||||
)
|
||||
}
|
||||
|
||||
value.value = false
|
||||
await nextTick()
|
||||
for (const c of [].slice.call(root.children as any)) {
|
||||
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
|
||||
expect((c as HTMLElement).style.getPropertyValue(`--${id}-color`)).toBe(
|
||||
'red'
|
||||
)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user