Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(stub): avoid stub cache for teleport for reactive update #2065

Merged
merged 3 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/vnodeTransformers/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,10 @@ export const createVNodeTransformer = ({
if (
cachedTransformation &&
// Don't use cache for root component, as it could use stubbed recursive component
!isRootComponent(rootComponents, componentType, instance)
!isRootComponent(rootComponents, componentType, instance) &&
!isTeleport(originalType) &&
!isKeepAlive(originalType)
) {
// https://github.com/vuejs/test-utils/issues/1829 & https://github.com/vuejs/test-utils/issues/1888
// Teleport/KeepAlive should return child nodes as a function
if (isTeleport(originalType) || isKeepAlive(originalType)) {
return [cachedTransformation, props, () => children, ...restVNodeArgs]
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting that the workaround to get rid of the warnings is no longer needed 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still needed a few lines below

return [cachedTransformation, props, children, ...restVNodeArgs]
}

Expand Down
40 changes: 39 additions & 1 deletion tests/features/teleport.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { defineComponent, h, Teleport } from 'vue'
import { defineComponent, h, ref, Teleport } from 'vue'
import { mount } from '../../src'
import WithTeleportPropsComp from '../components/WithTeleportPropsComp.vue'
import WithTeleportEmitsComp from '../components/WithTeleportEmitsComp.vue'
Expand Down Expand Up @@ -144,4 +144,42 @@ describe('teleport', () => {

expect(withProps.emitted().greet[0]).toEqual(['Hey!'])
})

it('should reactively update content with teleport', async () => {
const wrapper = mount(
defineComponent({
template:
'<div>' +
'<button @click="add">Add</button>' +
'<Teleport to="body"><div id="count">{{ count }}</div></Teleport>' +
'</div>',
setup() {
const count = ref(1)
const add = () => (count.value += 1)
return { count, add }
}
}),
{
global: {
stubs: {
teleport: true
}
}
}
)

expect(wrapper.html()).toBe(
'<div><button>Add</button>\n' +
' <teleport-stub to="body">\n' +
' <div id="count">1</div>\n' +
' </teleport-stub>\n' +
'</div>'
)

expect(wrapper.find('#count').text()).toBe('1')

await wrapper.find('button').trigger('click')

expect(wrapper.find('#count').text()).toBe('2')
})
})