fix(runtime-core): avoid unmount teleport's children multiple times (#3499)

fix #3497
This commit is contained in:
HcySunYang
2021-03-30 03:15:08 +08:00
committed by GitHub
parent 117a61bc17
commit 3736496006
2 changed files with 51 additions and 13 deletions

View File

@@ -8,7 +8,9 @@ import {
ref,
nextTick,
markRaw,
defineComponent
defineComponent,
withDirectives,
createApp
} from '@vue/runtime-test'
import { createVNode, Fragment } from '../../src/vnode'
import { compile, render as domRender } from 'vue'
@@ -432,4 +434,42 @@ describe('renderer: teleport', () => {
`"<div>teleported</div><span>false</span><!--v-if-->"`
)
})
// #3497
test(`the dir hooks of the Teleport's children should be called correctly`, async () => {
const target = nodeOps.createElement('div')
const root = nodeOps.createElement('div')
const toggle = ref(true)
const dir = {
mounted: jest.fn(),
unmounted: jest.fn()
}
const app = createApp({
setup() {
return () => {
return toggle.value
? h(Teleport, { to: target }, [
withDirectives(h('div', ['foo']), [[dir]])
])
: null
}
}
})
app.mount(root)
expect(serializeInner(root)).toMatchInlineSnapshot(
`"<!--teleport start--><!--teleport end-->"`
)
expect(serializeInner(target)).toMatchInlineSnapshot(`"<div>foo</div>"`)
expect(dir.mounted).toHaveBeenCalledTimes(1)
expect(dir.unmounted).toHaveBeenCalledTimes(0)
toggle.value = false
await nextTick()
expect(serializeInner(root)).toMatchInlineSnapshot(`"<!---->"`)
expect(serializeInner(target)).toMatchInlineSnapshot(`""`)
expect(dir.mounted).toHaveBeenCalledTimes(1)
expect(dir.unmounted).toHaveBeenCalledTimes(1)
})
})