2020-07-10 21:44:00 +08:00
|
|
|
import {
|
2020-10-14 03:37:45 +08:00
|
|
|
ref,
|
2020-07-10 21:44:00 +08:00
|
|
|
render,
|
2020-07-13 06:04:09 +08:00
|
|
|
useCssVars,
|
2021-07-15 06:08:12 +08:00
|
|
|
createStaticVNode,
|
2020-07-10 21:44:00 +08:00
|
|
|
h,
|
|
|
|
reactive,
|
|
|
|
nextTick,
|
2020-07-29 03:30:18 +08:00
|
|
|
ComponentOptions,
|
2021-07-17 00:29:09 +08:00
|
|
|
Suspense,
|
|
|
|
FunctionalComponent
|
2020-07-10 21:44:00 +08:00
|
|
|
} from '@vue/runtime-dom'
|
|
|
|
|
|
|
|
describe('useCssVars', () => {
|
2020-11-17 07:27:15 +08:00
|
|
|
async function assertCssVars(getApp: (state: any) => ComponentOptions) {
|
2020-07-10 21:44:00 +08:00
|
|
|
const state = reactive({ color: 'red' })
|
|
|
|
const App = getApp(state)
|
|
|
|
const root = document.createElement('div')
|
|
|
|
|
|
|
|
render(h(App), root)
|
2020-11-17 07:27:15 +08:00
|
|
|
await nextTick()
|
2020-07-10 21:44:00 +08:00
|
|
|
for (const c of [].slice.call(root.children as any)) {
|
2020-11-18 02:03:47 +08:00
|
|
|
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
|
2020-07-10 21:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
state.color = 'green'
|
|
|
|
await nextTick()
|
|
|
|
for (const c of [].slice.call(root.children as any)) {
|
2020-11-18 02:03:47 +08:00
|
|
|
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('green')
|
2020-07-10 21:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
test('basic', async () => {
|
|
|
|
await assertCssVars(state => ({
|
|
|
|
setup() {
|
|
|
|
// test receiving render context
|
2020-11-18 02:03:47 +08:00
|
|
|
useCssVars((ctx: any) => ({
|
|
|
|
color: ctx.color
|
|
|
|
}))
|
2020-07-10 21:44:00 +08:00
|
|
|
return state
|
|
|
|
},
|
|
|
|
render() {
|
|
|
|
return h('div')
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('on fragment root', async () => {
|
|
|
|
await assertCssVars(state => ({
|
|
|
|
setup() {
|
2020-11-18 02:03:47 +08:00
|
|
|
useCssVars(() => state)
|
2020-07-10 21:44:00 +08:00
|
|
|
return () => [h('div'), h('div')]
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('on HOCs', async () => {
|
|
|
|
const Child = () => [h('div'), h('div')]
|
|
|
|
|
|
|
|
await assertCssVars(state => ({
|
|
|
|
setup() {
|
2020-11-18 02:03:47 +08:00
|
|
|
useCssVars(() => state)
|
2020-07-10 21:44:00 +08:00
|
|
|
return () => h(Child)
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
2020-07-29 03:30:18 +08:00
|
|
|
test('on suspense root', async () => {
|
|
|
|
const state = reactive({ color: 'red' })
|
|
|
|
const root = document.createElement('div')
|
|
|
|
|
2020-11-17 07:27:15 +08:00
|
|
|
let resolveAsync: any
|
|
|
|
let asyncPromise: any
|
|
|
|
|
2020-07-29 03:30:18 +08:00
|
|
|
const AsyncComp = {
|
2020-11-17 07:27:15 +08:00
|
|
|
setup() {
|
|
|
|
asyncPromise = new Promise(r => {
|
|
|
|
resolveAsync = () => {
|
|
|
|
r(() => h('p', 'default'))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return asyncPromise
|
2020-07-29 03:30:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const App = {
|
|
|
|
setup() {
|
2020-11-18 02:03:47 +08:00
|
|
|
useCssVars(() => state)
|
2020-07-29 03:30:18 +08:00
|
|
|
return () =>
|
|
|
|
h(Suspense, null, {
|
|
|
|
default: h(AsyncComp),
|
|
|
|
fallback: h('div', 'fallback')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render(h(App), root)
|
2020-11-17 07:27:15 +08:00
|
|
|
await nextTick()
|
2020-07-29 03:30:18 +08:00
|
|
|
// css vars use with fallback tree
|
|
|
|
for (const c of [].slice.call(root.children as any)) {
|
2020-11-18 02:03:47 +08:00
|
|
|
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
|
2020-07-29 03:30:18 +08:00
|
|
|
}
|
|
|
|
// AsyncComp resolve
|
2020-11-17 07:27:15 +08:00
|
|
|
resolveAsync()
|
|
|
|
await asyncPromise.then(() => {})
|
2020-07-29 03:30:18 +08:00
|
|
|
// Suspense effects flush
|
|
|
|
await nextTick()
|
|
|
|
// css vars use with default tree
|
|
|
|
for (const c of [].slice.call(root.children as any)) {
|
2020-11-18 02:03:47 +08:00
|
|
|
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
|
2020-07-29 03:30:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
state.color = 'green'
|
|
|
|
await nextTick()
|
|
|
|
for (const c of [].slice.call(root.children as any)) {
|
2020-11-18 02:03:47 +08:00
|
|
|
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('green')
|
2020-07-29 03:30:18 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-10-14 03:37:45 +08:00
|
|
|
test('with subTree changed', async () => {
|
|
|
|
const state = reactive({ color: 'red' })
|
|
|
|
const value = ref(true)
|
|
|
|
const root = document.createElement('div')
|
|
|
|
|
|
|
|
const App = {
|
|
|
|
setup() {
|
2020-11-18 02:03:47 +08:00
|
|
|
useCssVars(() => state)
|
2020-10-14 03:37:45 +08:00
|
|
|
return () => (value.value ? [h('div')] : [h('div'), h('div')])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render(h(App), root)
|
2020-11-17 07:27:15 +08:00
|
|
|
await nextTick()
|
2020-10-14 03:37:45 +08:00
|
|
|
// css vars use with fallback tree
|
|
|
|
for (const c of [].slice.call(root.children as any)) {
|
2020-11-18 02:03:47 +08:00
|
|
|
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
|
2020-10-14 03:37:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
value.value = false
|
|
|
|
await nextTick()
|
|
|
|
for (const c of [].slice.call(root.children as any)) {
|
2020-11-18 02:03:47 +08:00
|
|
|
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
|
2020-10-14 03:37:45 +08:00
|
|
|
}
|
|
|
|
})
|
2021-07-15 06:08:12 +08:00
|
|
|
|
2021-07-17 00:29:09 +08:00
|
|
|
// #3894
|
|
|
|
test('with subTree change inside HOC', async () => {
|
|
|
|
const state = reactive({ color: 'red' })
|
|
|
|
const value = ref(true)
|
|
|
|
const root = document.createElement('div')
|
|
|
|
|
|
|
|
const Child: FunctionalComponent = (_, { slots }) => slots.default!()
|
|
|
|
|
|
|
|
const App = {
|
|
|
|
setup() {
|
|
|
|
useCssVars(() => state)
|
|
|
|
return () =>
|
|
|
|
h(
|
|
|
|
Child,
|
|
|
|
null,
|
|
|
|
() => (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`)
|
|
|
|
}
|
|
|
|
|
|
|
|
value.value = false
|
|
|
|
await nextTick()
|
|
|
|
for (const c of [].slice.call(root.children as any)) {
|
|
|
|
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-07-15 06:08:12 +08:00
|
|
|
test('with createStaticVNode', async () => {
|
|
|
|
const state = reactive({ color: 'red' })
|
|
|
|
const root = document.createElement('div')
|
|
|
|
|
|
|
|
const App = {
|
|
|
|
setup() {
|
|
|
|
useCssVars(() => state)
|
|
|
|
return () => [
|
|
|
|
h('div'),
|
|
|
|
createStaticVNode('<div>1</div><div><span>2</span></div>', 2),
|
|
|
|
h('div')
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render(h(App), root)
|
|
|
|
await nextTick()
|
|
|
|
for (const c of [].slice.call(root.children as any)) {
|
|
|
|
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
|
|
|
|
}
|
|
|
|
})
|
2020-07-10 21:44:00 +08:00
|
|
|
})
|