Jelajahi Sumber

test: add test case

daiwei 1 tahun lalu
induk
melakukan
1373bc7c05

+ 26 - 0
packages/runtime-core/__tests__/helpers/withMemo.spec.ts

@@ -204,6 +204,32 @@ describe('v-memo', () => {
     )
   })
 
+  // #12708
+  test('v-memo should work correctly when toggling v-if with v-for inside', async () => {
+    const [el, vm] = mount({
+      template: `<span v-if="show">
+          <span v-for="elem in [1]" :key="elem" v-memo="[count]">{{count}}</span>
+        </span>`,
+      data: () => ({
+        show: true,
+        count: 0,
+      }),
+    })
+    expect(el.innerHTML).toBe(`<span><span>0</span></span>`)
+
+    vm.show = false
+    await nextTick()
+    expect(el.innerHTML).toBe(`<!--v-if-->`)
+
+    vm.show = true
+    await nextTick()
+    expect(el.innerHTML).toBe(`<span><span>0</span></span>`)
+
+    vm.count++
+    await nextTick()
+    expect(el.innerHTML).toBe(`<span><span>1</span></span>`)
+  })
+
   test('on v-for /w constant expression ', async () => {
     const [el, vm] = mount({
       template: `<div v-for="item in 3"  v-memo="[count < 2 ? true : count]">

+ 1 - 1
packages/runtime-core/src/vnode.ts

@@ -243,7 +243,7 @@ export interface VNode<
   memo?: any[]
   /**
    * @internal index for cleaning v-memo cache
-   * cacheIndex will be an array when vnode in vFor
+   * cacheIndex will be an array when vnode in vFor + vMemo
    */
   cacheIndex?: number | number[]
   /**