Skip to content

Commit 8a0b1fc

Browse files
author
tonicli
committed
test: add test case
1 parent 2dfb685 commit 8a0b1fc

File tree

1 file changed

+72
-1
lines changed

1 file changed

+72
-1
lines changed

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

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,20 @@ import {
1313
nodeOps,
1414
ref,
1515
render,
16+
serialize,
1617
serializeInner,
1718
withDirectives,
1819
} from '@vue/runtime-test'
19-
import { Fragment, createVNode } from '../../src/vnode'
20+
import {
21+
Fragment,
22+
createBlock,
23+
createCommentVNode,
24+
createTextVNode,
25+
createVNode,
26+
openBlock,
27+
} from '../../src/vnode'
2028
import { compile, render as domRender } from 'vue'
29+
import { toDisplayString } from '@vue/shared'
2130

2231
describe('renderer: teleport', () => {
2332
test('should work', () => {
@@ -129,6 +138,41 @@ describe('renderer: teleport', () => {
129138
expect(serializeInner(target)).toMatchInlineSnapshot(`"teleported"`)
130139
})
131140

141+
test('should traverse comment node after updating in optimize mode', async () => {
142+
const target = nodeOps.createElement('div')
143+
const root = nodeOps.createElement('div')
144+
const count = ref(0)
145+
let teleport
146+
147+
__DEV__ = false
148+
render(
149+
h(() => {
150+
teleport =
151+
(openBlock(),
152+
createBlock(Teleport, { to: target }, [
153+
createCommentVNode('comment in teleport'),
154+
]))
155+
return h('div', null, [
156+
createTextVNode(toDisplayString(count.value)),
157+
teleport,
158+
])
159+
}),
160+
root,
161+
)
162+
const commentNode = teleport!.children[0].el
163+
expect(serializeInner(root)).toMatchInlineSnapshot(`"<div>0</div>"`)
164+
expect(serializeInner(target)).toMatchInlineSnapshot(
165+
`"<!--comment in teleport-->"`,
166+
)
167+
expect(serialize(commentNode)).toBe(`<!--comment in teleport-->`)
168+
169+
count.value = 1
170+
await nextTick()
171+
__DEV__ = true
172+
expect(serializeInner(root)).toMatchInlineSnapshot(`"<div>1</div>"`)
173+
expect(teleport!.children[0].el).toBe(commentNode)
174+
})
175+
132176
test('should remove children when unmounted', () => {
133177
const target = nodeOps.createElement('div')
134178
const root = nodeOps.createElement('div')
@@ -151,6 +195,33 @@ describe('renderer: teleport', () => {
151195
testUnmount({ to: target, disabled: true })
152196
testUnmount({ to: null, disabled: true })
153197
})
198+
// #10747
199+
test('should unmount correctly when using top level comment in teleport', async () => {
200+
const target = nodeOps.createElement('div')
201+
const root = nodeOps.createElement('div')
202+
const count = ref(0)
203+
204+
__DEV__ = false
205+
render(
206+
h(() => {
207+
return h('div', null, [
208+
createTextVNode(toDisplayString(count.value)),
209+
(openBlock(),
210+
createBlock(Teleport, { to: target }, [
211+
createCommentVNode('comment in teleport'),
212+
])),
213+
])
214+
}),
215+
root,
216+
)
217+
218+
count.value = 1
219+
220+
await nextTick()
221+
__DEV__ = true
222+
render(null, root)
223+
expect(root.children.length).toBe(0)
224+
})
154225

155226
test('component with multi roots should be removed when unmounted', () => {
156227
const target = nodeOps.createElement('div')

0 commit comments

Comments
 (0)