2020-07-10 21:44:00 +08:00
|
|
|
import {
|
|
|
|
render,
|
2020-07-13 06:04:09 +08:00
|
|
|
useCssVars,
|
2020-07-10 21:44:00 +08:00
|
|
|
h,
|
|
|
|
reactive,
|
|
|
|
nextTick,
|
|
|
|
ComponentOptions
|
|
|
|
} from '@vue/runtime-dom'
|
|
|
|
|
|
|
|
describe('useCssVars', () => {
|
|
|
|
async function assertCssVars(
|
|
|
|
getApp: (state: any) => ComponentOptions,
|
|
|
|
scopeId?: string
|
|
|
|
) {
|
|
|
|
const state = reactive({ color: 'red' })
|
|
|
|
const App = getApp(state)
|
|
|
|
const root = document.createElement('div')
|
2020-07-11 06:47:31 +08:00
|
|
|
const prefix = scopeId ? `${scopeId.replace(/^data-v-/, '')}-` : ``
|
2020-07-10 21:44:00 +08:00
|
|
|
|
|
|
|
render(h(App), root)
|
|
|
|
for (const c of [].slice.call(root.children as any)) {
|
2020-07-11 06:47:31 +08:00
|
|
|
expect(
|
|
|
|
(c as HTMLElement).style.getPropertyValue(`--${prefix}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-07-11 06:47:31 +08:00
|
|
|
expect(
|
|
|
|
(c as HTMLElement).style.getPropertyValue(`--${prefix}color`)
|
|
|
|
).toBe('green')
|
2020-07-10 21:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
test('basic', async () => {
|
|
|
|
await assertCssVars(state => ({
|
|
|
|
setup() {
|
|
|
|
// test receiving render context
|
2020-07-13 06:04:09 +08:00
|
|
|
useCssVars((ctx: any) => ({
|
2020-07-10 21:44:00 +08:00
|
|
|
color: ctx.color
|
|
|
|
}))
|
|
|
|
return state
|
|
|
|
},
|
|
|
|
render() {
|
|
|
|
return h('div')
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('on fragment root', async () => {
|
|
|
|
await assertCssVars(state => ({
|
|
|
|
setup() {
|
2020-07-13 06:04:09 +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-07-13 06:04:09 +08:00
|
|
|
useCssVars(() => state)
|
2020-07-10 21:44:00 +08:00
|
|
|
return () => h(Child)
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
2020-07-10 22:19:16 +08:00
|
|
|
test('with <style scoped>', async () => {
|
2020-07-11 06:47:31 +08:00
|
|
|
const id = 'data-v-12345'
|
2020-07-10 21:44:00 +08:00
|
|
|
|
|
|
|
await assertCssVars(
|
|
|
|
state => ({
|
2020-07-10 22:19:16 +08:00
|
|
|
__scopeId: id,
|
2020-07-10 21:44:00 +08:00
|
|
|
setup() {
|
2020-07-13 06:04:09 +08:00
|
|
|
useCssVars(() => state, true)
|
2020-07-10 21:44:00 +08:00
|
|
|
return () => h('div')
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
id
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|