Skip to content

Commit 648683d

Browse files
authored
fix(stub): avoid stub cache for teleport for reactive update (#2065)
1 parent 2f8858a commit 648683d

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

src/vnodeTransformers/util.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,10 @@ export const createVNodeTransformer = ({
6767
if (
6868
cachedTransformation &&
6969
// Don't use cache for root component, as it could use stubbed recursive component
70-
!isRootComponent(rootComponents, componentType, instance)
70+
!isRootComponent(rootComponents, componentType, instance) &&
71+
!isTeleport(originalType) &&
72+
!isKeepAlive(originalType)
7173
) {
72-
// https://github.com/vuejs/test-utils/issues/1829 & https://github.com/vuejs/test-utils/issues/1888
73-
// Teleport/KeepAlive should return child nodes as a function
74-
if (isTeleport(originalType) || isKeepAlive(originalType)) {
75-
return [cachedTransformation, props, () => children, ...restVNodeArgs]
76-
}
7774
return [cachedTransformation, props, children, ...restVNodeArgs]
7875
}
7976

tests/features/teleport.spec.ts

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { beforeEach, describe, expect, it, vi } from 'vitest'
2-
import { defineComponent, h, Teleport } from 'vue'
2+
import { defineComponent, h, ref, Teleport } from 'vue'
33
import { mount } from '../../src'
44
import WithTeleportPropsComp from '../components/WithTeleportPropsComp.vue'
55
import WithTeleportEmitsComp from '../components/WithTeleportEmitsComp.vue'
@@ -144,4 +144,42 @@ describe('teleport', () => {
144144

145145
expect(withProps.emitted().greet[0]).toEqual(['Hey!'])
146146
})
147+
148+
it('should reactively update content with teleport', async () => {
149+
const wrapper = mount(
150+
defineComponent({
151+
template:
152+
'<div>' +
153+
'<button @click="add">Add</button>' +
154+
'<Teleport to="body"><div id="count">{{ count }}</div></Teleport>' +
155+
'</div>',
156+
setup() {
157+
const count = ref(1)
158+
const add = () => (count.value += 1)
159+
return { count, add }
160+
}
161+
}),
162+
{
163+
global: {
164+
stubs: {
165+
teleport: true
166+
}
167+
}
168+
}
169+
)
170+
171+
expect(wrapper.html()).toBe(
172+
'<div><button>Add</button>\n' +
173+
' <teleport-stub to="body">\n' +
174+
' <div id="count">1</div>\n' +
175+
' </teleport-stub>\n' +
176+
'</div>'
177+
)
178+
179+
expect(wrapper.find('#count').text()).toBe('1')
180+
181+
await wrapper.find('button').trigger('click')
182+
183+
expect(wrapper.find('#count').text()).toBe('2')
184+
})
147185
})

0 commit comments

Comments
 (0)