| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- import {
- type VNode,
- defineComponent,
- h,
- nextTick,
- ref,
- watch,
- withDirectives,
- } from '@vue/runtime-core'
- import { Transition, render, vShow } from '@vue/runtime-dom'
- const withVShow = (node: VNode, exp: any) =>
- withDirectives(node, [[vShow, exp]])
- let root: any
- beforeEach(() => {
- root = document.createElement('div')
- })
- describe('runtime-dom: v-show directive', () => {
- test('should check show value is truthy', async () => {
- const component = defineComponent({
- data() {
- return { value: true }
- },
- render() {
- return [withVShow(h('div'), this.value)]
- },
- })
- render(h(component), root)
- const $div = root.children[0]
- expect($div.style.display).toEqual('')
- })
- test('should check show value is falsy', async () => {
- const component = defineComponent({
- data() {
- return { value: false }
- },
- render() {
- return [withVShow(h('div'), this.value)]
- },
- })
- render(h(component), root)
- const $div = root.children[0]
- expect($div.style.display).toEqual('none')
- })
- it('should update show value changed', async () => {
- const component = defineComponent({
- data() {
- return { value: true }
- },
- render() {
- return [withVShow(h('div'), this.value)]
- },
- })
- render(h(component), root)
- const $div = root.children[0]
- 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 () => {
- const component = defineComponent({
- data() {
- return { value: true }
- },
- render() {
- return [
- withVShow(h('div', { style: { display: 'block' } }), this.value),
- ]
- },
- })
- render(h(component), root)
- const $div = root.children[0]
- 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')
- })
- // #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.children[0]
- 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('')
- })
- test('the value of `display` set by v-show should not be overwritten by the style attribute when updated (object value)', async () => {
- const style = ref({
- display: 'block',
- 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.children[0]
- 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('block')
- })
- // #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.children[0]
- 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('')
- })
- // #10151
- test('should respect the display value when v-show value is true', async () => {
- const isVisible = ref(false)
- const useDisplayStyle = ref(true)
- const compStyle = ref({
- display: 'none',
- })
- const withoutDisplayStyle = {
- margin: '10px',
- }
- const Component = {
- setup() {
- return () => {
- return withVShow(
- h('div', {
- style: useDisplayStyle.value
- ? compStyle.value
- : withoutDisplayStyle,
- }),
- isVisible.value,
- )
- }
- },
- }
- render(h(Component), root)
- const $div = root.children[0]
- expect($div.style.display).toEqual('none')
- isVisible.value = true
- await nextTick()
- expect($div.style.display).toEqual('none')
- compStyle.value.display = 'block'
- await nextTick()
- expect($div.style.display).toEqual('block')
- compStyle.value.display = 'inline-block'
- await nextTick()
- expect($div.style.display).toEqual('inline-block')
- isVisible.value = false
- await nextTick()
- expect($div.style.display).toEqual('none')
- isVisible.value = true
- await nextTick()
- expect($div.style.display).toEqual('inline-block')
- useDisplayStyle.value = false
- await nextTick()
- expect($div.style.display).toEqual('')
- expect(getComputedStyle($div).display).toEqual('block')
- isVisible.value = false
- await nextTick()
- expect($div.style.display).toEqual('none')
- isVisible.value = true
- await nextTick()
- expect($div.style.display).toEqual('')
- })
- })
|