]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-vapor): fix v-show value (#70)
authorKaine <121680374+kaine0923@users.noreply.github.com>
Sat, 23 Dec 2023 07:23:34 +0000 (15:23 +0800)
committerGitHub <noreply@github.com>
Sat, 23 Dec 2023 07:23:34 +0000 (15:23 +0800)
packages/runtime-vapor/__tests__/vShow.spec.ts
packages/runtime-vapor/src/directives/vShow.ts

index fb1447e336ffef37657cc3e8ea95668670b94f8e..e3c69d06f8375d4a262d00c38611c05c426dbafc 100644 (file)
@@ -15,26 +15,27 @@ beforeEach(() => {
 afterEach(() => {
   host.remove()
 })
-
+const createDemo = (defaultValue: boolean) =>
+  defineComponent({
+    setup() {
+      const visible = ref(defaultValue)
+      function handleClick() {
+        visible.value = !visible.value
+      }
+      const t0 = template('<button>toggle</button><h1>hello world</h1>')
+      const n0 = t0()
+      const {
+        0: [n1],
+        1: [n2],
+      } = children(n0)
+      withDirectives(n2, [[vShow, () => visible.value]])
+      on(n1 as HTMLElement, 'click', handleClick)
+      return n0
+    },
+  })
 describe('directive: v-show', () => {
   test('basic', async () => {
-    const demo = defineComponent({
-      setup() {
-        const visible = ref(true)
-        function handleClick() {
-          visible.value = !visible.value
-        }
-        const t0 = template('<button>toggle</button><h1>hello world</h1>')
-        const n0 = t0()
-        const {
-          0: [n1],
-          1: [n2],
-        } = children(n0)
-        withDirectives(n2, [[vShow, () => visible.value]])
-        on(n1 as HTMLElement, 'click', (...args) => handleClick(...args))
-        return n0
-      },
-    })
+    const demo = createDemo(true)
     render(demo as any, {}, '#host')
     const btn = host.querySelector('button')
     expect(host.innerHTML).toBe('<button>toggle</button><h1>hello world</h1>')
@@ -44,4 +45,14 @@ describe('directive: v-show', () => {
       '<button>toggle</button><h1 style="display: none;">hello world</h1>',
     )
   })
+  test('should hide content when default value is false', async () => {
+    const demo = createDemo(false)
+    render(demo as any, {}, '#host')
+    const btn = host.querySelector('button')
+    const h1 = host.querySelector('h1')
+    expect(h1?.style.display).toBe('none')
+    btn?.click()
+    await nextTick()
+    expect(h1?.style.display).toBe('')
+  })
 })
index f419053ec6339482134674fb47dbe06d291cc453..07def8b3c8d1b047ed491a40fb693180ae1904db 100644 (file)
@@ -3,7 +3,7 @@ import type { ObjectDirective } from '../directive'
 const vShowMap = new WeakMap<HTMLElement, string>()
 
 export const vShow: ObjectDirective<HTMLElement> = {
-  beforeMount(node, { source: value }) {
+  beforeMount(node, { value }) {
     vShowMap.set(node, node.style.display === 'none' ? '' : node.style.display)
     setDisplay(node, value)
   },
@@ -13,7 +13,7 @@ export const vShow: ObjectDirective<HTMLElement> = {
     setDisplay(node, value)
   },
 
-  beforeUnmount(node, { source: value }) {
+  beforeUnmount(node, { value }) {
     setDisplay(node, value)
   },
 }