vShow.spec.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import {
  2. withDirectives,
  3. createComponent,
  4. h,
  5. nextTick,
  6. VNode
  7. } from '@vue/runtime-core'
  8. import { createApp, vShow } from '@vue/runtime-dom'
  9. const withVShow = (node: VNode, exp: any) =>
  10. withDirectives(node, [[vShow, exp]])
  11. let app: any, root: any
  12. beforeEach(() => {
  13. app = createApp()
  14. root = document.createElement('div') as any
  15. })
  16. describe('runtime-dom: v-show directive', () => {
  17. test('should check show value is truthy', async () => {
  18. const component = createComponent({
  19. data() {
  20. return { value: true }
  21. },
  22. render() {
  23. return [withVShow(h('div'), this.value)]
  24. }
  25. })
  26. app.mount(component, root)
  27. const $div = root.querySelector('div')
  28. expect($div.style.display).toEqual('')
  29. })
  30. test('should check show value is falsy', async () => {
  31. const component = createComponent({
  32. data() {
  33. return { value: false }
  34. },
  35. render() {
  36. return [withVShow(h('div'), this.value)]
  37. }
  38. })
  39. app.mount(component, root)
  40. const $div = root.querySelector('div')
  41. expect($div.style.display).toEqual('none')
  42. })
  43. it('should update show value changed', async () => {
  44. const component = createComponent({
  45. data() {
  46. return { value: true }
  47. },
  48. render() {
  49. return [withVShow(h('div'), this.value)]
  50. }
  51. })
  52. app.mount(component, root)
  53. const $div = root.querySelector('div')
  54. const data = root._vnode.component.data
  55. expect($div.style.display).toEqual('')
  56. data.value = false
  57. await nextTick()
  58. expect($div.style.display).toEqual('none')
  59. data.value = {}
  60. await nextTick()
  61. expect($div.style.display).toEqual('')
  62. data.value = 0
  63. await nextTick()
  64. expect($div.style.display).toEqual('none')
  65. data.value = []
  66. await nextTick()
  67. expect($div.style.display).toEqual('')
  68. data.value = null
  69. await nextTick()
  70. expect($div.style.display).toEqual('none')
  71. data.value = '0'
  72. await nextTick()
  73. expect($div.style.display).toEqual('')
  74. data.value = undefined
  75. await nextTick()
  76. expect($div.style.display).toEqual('none')
  77. data.value = 1
  78. await nextTick()
  79. expect($div.style.display).toEqual('')
  80. })
  81. test('should respect display value in style attribute', async () => {
  82. const component = createComponent({
  83. data() {
  84. return { value: true }
  85. },
  86. render() {
  87. return [
  88. withVShow(h('div', { style: { display: 'block' } }), this.value)
  89. ]
  90. }
  91. })
  92. app.mount(component, root)
  93. const $div = root.querySelector('div')
  94. const data = root._vnode.component.data
  95. expect($div.style.display).toEqual('block')
  96. data.value = false
  97. await nextTick()
  98. expect($div.style.display).toEqual('none')
  99. data.value = true
  100. await nextTick()
  101. expect($div.style.display).toEqual('block')
  102. })
  103. })