Skip to content

Commit 1b01b0f

Browse files
committed
test: add tests
1 parent e0253c1 commit 1b01b0f

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

packages/runtime-core/__tests__/components/Suspense.spec.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import {
88
KeepAlive,
99
Suspense,
1010
type SuspenseProps,
11+
createBlock,
1112
createCommentVNode,
13+
createElementBlock,
1214
h,
1315
nextTick,
1416
nodeOps,
1517
onErrorCaptured,
1618
onMounted,
1719
onUnmounted,
20+
openBlock,
1821
ref,
1922
render,
2023
resolveDynamicComponent,
@@ -26,6 +29,7 @@ import {
2629
import { computed, createApp, defineComponent, inject, provide } from 'vue'
2730
import type { RawSlots } from 'packages/runtime-core/src/componentSlots'
2831
import { resetSuspenseId } from '../../src/components/Suspense'
32+
import { PatchFlags } from '@vue/shared'
2933

3034
describe('Suspense', () => {
3135
const deps: Promise<any>[] = []
@@ -2161,6 +2165,63 @@ describe('Suspense', () => {
21612165
await Promise.all(deps)
21622166
})
21632167

2168+
// #12920
2169+
test('unmount Suspense after children self-update', async () => {
2170+
const Comp = defineAsyncComponent({
2171+
setup() {
2172+
const show = ref(true)
2173+
onMounted(() => {
2174+
// trigger self-update
2175+
show.value = !show.value
2176+
})
2177+
return () =>
2178+
show.value
2179+
? (openBlock(), createElementBlock('div', { key: 0 }, 'show'))
2180+
: (openBlock(), createElementBlock('div', { key: 1 }, 'hidden'))
2181+
},
2182+
})
2183+
2184+
const toggle = ref(true)
2185+
const root = nodeOps.createElement('div')
2186+
const App = {
2187+
render() {
2188+
return (
2189+
openBlock(),
2190+
createElementBlock(
2191+
Fragment,
2192+
null,
2193+
[
2194+
h('h1', null, toggle.value),
2195+
toggle.value
2196+
? (openBlock(),
2197+
createBlock(
2198+
Suspense,
2199+
{ key: 0 },
2200+
{
2201+
default: h(Comp),
2202+
},
2203+
))
2204+
: createCommentVNode('v-if', true),
2205+
],
2206+
PatchFlags.STABLE_FRAGMENT,
2207+
)
2208+
)
2209+
},
2210+
}
2211+
render(h(App), root)
2212+
expect(serializeInner(root)).toBe(`<h1>true</h1><!---->`)
2213+
2214+
await Promise.all(deps)
2215+
await nextTick()
2216+
expect(serializeInner(root)).toBe(`<h1>true</h1><div>hidden</div>`)
2217+
2218+
// unmount suspense
2219+
toggle.value = false
2220+
await Promise.all(deps)
2221+
await nextTick()
2222+
expect(serializeInner(root)).toBe(`<h1>true</h1><!--v-if-->`)
2223+
})
2224+
21642225
describe('warnings', () => {
21652226
// base function to check if a combination of slots warns or not
21662227
function baseCheckWarn(

packages/runtime-core/src/componentRenderUtils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,10 +461,13 @@ export function updateHOCHostEl(
461461
}
462462
if (root === vnode) {
463463
;(vnode = parent.vnode).el = el
464-
if (suspense && suspense.activeBranch === vnode) suspense.vnode.el = el
465464
parent = parent.parent
466465
} else {
467466
break
468467
}
469468
}
469+
// also update suspense vnode el
470+
if (suspense && suspense.activeBranch === vnode) {
471+
suspense.vnode.el = el
472+
}
470473
}

0 commit comments

Comments
 (0)