| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- import {
- withDirectives,
- createComponent,
- h,
- nextTick,
- VNode
- } from '@vue/runtime-core'
- import { createApp, vShow } from '@vue/runtime-dom'
- const withVShow = (node: VNode, exp: any) =>
- withDirectives(node, [[vShow, exp]])
- let app: any, root: any
- beforeEach(() => {
- app = createApp()
- root = document.createElement('div') as any
- })
- describe('runtime-dom: v-show directive', () => {
- test('should check show value is truthy', async () => {
- const component = createComponent({
- data() {
- return { value: true }
- },
- render() {
- return [withVShow(h('div'), this.value)]
- }
- })
- app.mount(component, root)
- const $div = root.querySelector('div')
- expect($div.style.display).toEqual('')
- })
- test('should check show value is falsy', async () => {
- const component = createComponent({
- data() {
- return { value: false }
- },
- render() {
- return [withVShow(h('div'), this.value)]
- }
- })
- app.mount(component, root)
- const $div = root.querySelector('div')
- expect($div.style.display).toEqual('none')
- })
- it('should update show value changed', async () => {
- const component = createComponent({
- data() {
- return { value: true }
- },
- render() {
- return [withVShow(h('div'), this.value)]
- }
- })
- app.mount(component, root)
- 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 () => {
- const component = createComponent({
- data() {
- return { value: true }
- },
- render() {
- return [
- withVShow(h('div', { style: { display: 'block' } }), this.value)
- ]
- }
- })
- app.mount(component, root)
- 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')
- })
- })
|