2019-11-25 11:04:26 +08:00
|
|
|
import {
|
|
|
|
withDirectives,
|
2019-12-22 23:58:12 +08:00
|
|
|
defineComponent,
|
2019-11-25 11:04:26 +08:00
|
|
|
h,
|
|
|
|
nextTick,
|
2021-02-25 23:26:54 +08:00
|
|
|
VNode,
|
|
|
|
ref,
|
|
|
|
watch
|
2019-11-25 11:04:26 +08:00
|
|
|
} from '@vue/runtime-core'
|
2021-02-25 23:26:54 +08:00
|
|
|
import { render, Transition, vShow } from '@vue/runtime-dom'
|
2019-11-25 11:04:26 +08:00
|
|
|
|
|
|
|
const withVShow = (node: VNode, exp: any) =>
|
|
|
|
withDirectives(node, [[vShow, exp]])
|
|
|
|
|
2020-01-24 04:05:38 +08:00
|
|
|
let root: any
|
2019-11-25 11:04:26 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-01-24 04:05:38 +08:00
|
|
|
root = document.createElement('div')
|
2019-11-25 11:04:26 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('runtime-dom: v-show directive', () => {
|
|
|
|
test('should check show value is truthy', async () => {
|
2019-12-22 23:58:12 +08:00
|
|
|
const component = defineComponent({
|
2019-11-25 11:04:26 +08:00
|
|
|
data() {
|
|
|
|
return { value: true }
|
|
|
|
},
|
|
|
|
render() {
|
|
|
|
return [withVShow(h('div'), this.value)]
|
|
|
|
}
|
|
|
|
})
|
2020-01-24 04:05:38 +08:00
|
|
|
render(h(component), root)
|
2019-11-25 11:04:26 +08:00
|
|
|
|
|
|
|
const $div = root.querySelector('div')
|
|
|
|
|
|
|
|
expect($div.style.display).toEqual('')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should check show value is falsy', async () => {
|
2019-12-22 23:58:12 +08:00
|
|
|
const component = defineComponent({
|
2019-11-25 11:04:26 +08:00
|
|
|
data() {
|
|
|
|
return { value: false }
|
|
|
|
},
|
|
|
|
render() {
|
|
|
|
return [withVShow(h('div'), this.value)]
|
|
|
|
}
|
|
|
|
})
|
2020-01-24 04:05:38 +08:00
|
|
|
render(h(component), root)
|
2019-11-25 11:04:26 +08:00
|
|
|
|
|
|
|
const $div = root.querySelector('div')
|
|
|
|
|
|
|
|
expect($div.style.display).toEqual('none')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should update show value changed', async () => {
|
2019-12-22 23:58:12 +08:00
|
|
|
const component = defineComponent({
|
2019-11-25 11:04:26 +08:00
|
|
|
data() {
|
|
|
|
return { value: true }
|
|
|
|
},
|
|
|
|
render() {
|
|
|
|
return [withVShow(h('div'), this.value)]
|
|
|
|
}
|
|
|
|
})
|
2020-01-24 04:05:38 +08:00
|
|
|
render(h(component), root)
|
2019-11-25 11:04:26 +08:00
|
|
|
|
|
|
|
const $div = root.querySelector('div')
|
|
|
|
const data = root._vnode.component.data
|
|
|
|
|
|
|
|
expect($div.style.display).toEqual('')
|
|
|
|
|
|
|
|
data.value = false
|
|
|
|
await nextTick()
|
|
|
|
expect($div.style.display).toEqual('none')
|
|
|
|
|
|
|
|
data.value = {}
|
|
|
|
await nextTick()
|
|
|
|
expect($div.style.display).toEqual('')
|
|
|
|
|
|
|
|
data.value = 0
|
|
|
|
await nextTick()
|
|
|
|
expect($div.style.display).toEqual('none')
|
|
|
|
|
|
|
|
data.value = []
|
|
|
|
await nextTick()
|
|
|
|
expect($div.style.display).toEqual('')
|
|
|
|
|
|
|
|
data.value = null
|
|
|
|
await nextTick()
|
|
|
|
expect($div.style.display).toEqual('none')
|
|
|
|
|
|
|
|
data.value = '0'
|
|
|
|
await nextTick()
|
|
|
|
expect($div.style.display).toEqual('')
|
|
|
|
|
|
|
|
data.value = undefined
|
|
|
|
await nextTick()
|
|
|
|
expect($div.style.display).toEqual('none')
|
|
|
|
|
|
|
|
data.value = 1
|
|
|
|
await nextTick()
|
|
|
|
expect($div.style.display).toEqual('')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should respect display value in style attribute', async () => {
|
2019-12-22 23:58:12 +08:00
|
|
|
const component = defineComponent({
|
2019-11-25 11:04:26 +08:00
|
|
|
data() {
|
|
|
|
return { value: true }
|
|
|
|
},
|
|
|
|
render() {
|
|
|
|
return [
|
|
|
|
withVShow(h('div', { style: { display: 'block' } }), this.value)
|
|
|
|
]
|
|
|
|
}
|
|
|
|
})
|
2020-01-24 04:05:38 +08:00
|
|
|
render(h(component), root)
|
2019-11-25 11:04:26 +08:00
|
|
|
|
|
|
|
const $div = root.querySelector('div')
|
|
|
|
const data = root._vnode.component.data
|
|
|
|
|
|
|
|
expect($div.style.display).toEqual('block')
|
|
|
|
|
|
|
|
data.value = false
|
|
|
|
await nextTick()
|
|
|
|
expect($div.style.display).toEqual('none')
|
|
|
|
|
|
|
|
data.value = true
|
|
|
|
await nextTick()
|
|
|
|
expect($div.style.display).toEqual('block')
|
|
|
|
})
|
2021-02-25 23:26:54 +08:00
|
|
|
|
|
|
|
// #2583
|
|
|
|
test('the value of `display` set by v-show should not be overwritten by the style attribute when updated', async () => {
|
|
|
|
const style = ref('width: 100px')
|
|
|
|
const display = ref(false)
|
|
|
|
const component = defineComponent({
|
|
|
|
render() {
|
|
|
|
return withVShow(h('div', { style: style.value }), display.value)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
render(h(component), root)
|
|
|
|
|
|
|
|
const $div = root.querySelector('div')
|
|
|
|
|
|
|
|
expect($div.style.display).toEqual('none')
|
|
|
|
|
|
|
|
style.value = 'width: 50px'
|
|
|
|
await nextTick()
|
|
|
|
expect($div.style.display).toEqual('none')
|
|
|
|
|
|
|
|
display.value = true
|
|
|
|
await nextTick()
|
|
|
|
expect($div.style.display).toEqual('')
|
|
|
|
})
|
|
|
|
|
|
|
|
// #2583, #2757
|
|
|
|
test('the value of `display` set by v-show should not be overwritten by the style attribute when updated (with Transition)', async () => {
|
|
|
|
const style = ref('width: 100px')
|
|
|
|
const display = ref(false)
|
|
|
|
const component = defineComponent({
|
|
|
|
setup() {
|
|
|
|
const innerValue = ref(false)
|
|
|
|
watch(display, val => {
|
|
|
|
innerValue.value = val
|
|
|
|
})
|
|
|
|
return () => {
|
|
|
|
return h(Transition, () =>
|
|
|
|
withVShow(
|
|
|
|
h('div', { style: style.value }, innerValue.value),
|
|
|
|
display.value
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
render(h(component), root)
|
|
|
|
|
|
|
|
const $div = root.querySelector('div')
|
|
|
|
|
|
|
|
expect($div.style.display).toEqual('none')
|
|
|
|
|
|
|
|
style.value = 'width: 50px'
|
|
|
|
await nextTick()
|
|
|
|
expect($div.style.display).toEqual('none')
|
|
|
|
|
|
|
|
display.value = true
|
|
|
|
await nextTick()
|
|
|
|
expect($div.style.display).toEqual('')
|
|
|
|
})
|
2019-11-25 11:04:26 +08:00
|
|
|
})
|