Skip to content

Commit cda399f

Browse files
author
Victor Fernandez
committed
fix: use Teleport with disabled=true instead of stub
1 parent cd26fc8 commit cda399f

File tree

5 files changed

+103
-11
lines changed

5 files changed

+103
-11
lines changed

src/vnodeTransformers/util.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,16 @@ export const createVNodeTransformer = ({
8585
registerStub({ source: originalType, stub: transformedType })
8686
// https://github.com/vuejs/test-utils/issues/1829 & https://github.com/vuejs/test-utils/issues/1888
8787
// Teleport/KeepAlive should return child nodes as a function
88-
if (isTeleport(originalType) || isKeepAlive(originalType)) {
88+
if (isTeleport(originalType)) {
89+
return [
90+
originalType,
91+
{ ...props, disabled: true },
92+
children,
93+
...restVNodeArgs
94+
]
95+
}
96+
97+
if (isKeepAlive(originalType)) {
8998
return [transformedType, props, () => children, ...restVNodeArgs]
9099
}
91100
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<script setup lang="ts">
2+
import { onMounted, onUnmounted } from 'vue'
3+
4+
const emit = defineEmits<{ myEvent: ['mount' | 'unmount'] }>()
5+
6+
onMounted(() => {
7+
emit('myEvent', 'mount')
8+
})
9+
10+
onUnmounted(() => {
11+
emit('myEvent', 'unmount')
12+
})
13+
</script>
14+
15+
<template>
16+
<span>child</span>
17+
</template>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<script setup lang="ts">
2+
import { ref } from 'vue'
3+
import Child from './TeleportRemountChild.vue'
4+
5+
const parentValue = ref<Array<'mount' | 'unmount'>>([])
6+
7+
const handleEmit = (e: 'mount' | 'unmount') => {
8+
parentValue.value.push(e)
9+
}
10+
</script>
11+
<template>
12+
<div>
13+
<p id="parent-value">{{ JSON.stringify(parentValue) }}</p>
14+
<Teleport to="#teleport-target">
15+
<Child @my-event="handleEmit" />
16+
</Teleport>
17+
</div>
18+
</template>

tests/features/teleport.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ describe('teleport', () => {
170170

171171
expect(wrapper.html()).toBe(
172172
'<div><button>Add</button>\n' +
173-
' <teleport-stub to="body">\n' +
174-
' <div id="count">1</div>\n' +
175-
' </teleport-stub>\n' +
173+
' <!--teleport start-->\n' +
174+
' <div id="count">1</div>\n' +
175+
' <!--teleport end-->\n' +
176176
'</div>'
177177
)
178178

tests/mountingOptions/global.stubs.spec.ts

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2-
import { h, defineComponent, defineAsyncComponent, Directive } from 'vue'
2+
import {
3+
h,
4+
defineComponent,
5+
defineAsyncComponent,
6+
Directive,
7+
nextTick
8+
} from 'vue'
39
import { config, flushPromises, mount, RouterLinkStub } from '../../src'
410
import Hello from '../components/Hello.vue'
511
import ComponentWithoutName from '../components/ComponentWithoutName.vue'
612
import ComponentWithSlots from '../components/ComponentWithSlots.vue'
713
import ScriptSetupWithChildren from '../components/ScriptSetupWithChildren.vue'
814
import AutoImportScriptSetup from '../components/AutoImportScriptSetup.vue'
15+
import TeleportRemountParent from '../components/TeleportRemountParent.vue'
916

1017
describe('mounting options: stubs', () => {
1118
const configStubsSave = config.global.stubs
@@ -603,9 +610,9 @@ describe('mounting options: stubs', () => {
603610
})
604611

605612
expect(wrapper.html()).toBe(
606-
'<teleport-stub to="body">\n' +
607-
' <div id="content"></div>\n' +
608-
'</teleport-stub>'
613+
'<!--teleport start-->\n' +
614+
'<div id="content"></div>\n' +
615+
'<!--teleport end-->'
609616
)
610617
// Make sure that we don't have a warning when stubbing teleport
611618
// https://github.com/vuejs/test-utils/issues/1829
@@ -643,11 +650,52 @@ describe('mounting options: stubs', () => {
643650
})
644651

645652
expect(wrapper.html()).toBe(
646-
'<teleport-stub to="body">\n' +
647-
' <div id="content-global-stubs-teleport"></div>\n' +
648-
'</teleport-stub>'
653+
'<!--teleport start-->\n' +
654+
'<div id="content-global-stubs-teleport"></div>\n' +
655+
'<!--teleport end-->'
649656
)
650657
})
658+
659+
describe('should not unmount the content', () => {
660+
it('without stub', async () => {
661+
const div = document.createElement('div')
662+
div.id = 'teleport-target'
663+
document.body.appendChild(div)
664+
665+
const wrapper = mount(TeleportRemountParent, {
666+
global: {
667+
stubs: {
668+
teleport: false
669+
}
670+
}
671+
})
672+
673+
await nextTick()
674+
675+
const childElement = div.innerHTML
676+
expect(childElement).toBe('<span>child</span>')
677+
678+
const parentValue = wrapper.find('#parent-value').text()
679+
expect(JSON.parse(parentValue!)).toStrictEqual(['mount'])
680+
681+
document.body.innerHTML = ''
682+
})
683+
684+
it('with stub', async () => {
685+
const wrapper = mount(TeleportRemountParent, {
686+
global: {
687+
stubs: {
688+
teleport: true
689+
}
690+
}
691+
})
692+
693+
await nextTick()
694+
695+
const parentValue = wrapper.find('#parent-value').text()
696+
expect(JSON.parse(parentValue)).toStrictEqual(['mount'])
697+
})
698+
})
651699
})
652700

653701
describe('keep-alive', () => {

0 commit comments

Comments
 (0)