Skip to content

Commit

Permalink
fix(stub): avoid stub cache for teleport for reactive update (#2065)
Browse files Browse the repository at this point in the history
  • Loading branch information
freakzlike authored Jun 5, 2023
1 parent 2f8858a commit 648683d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
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]
}
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')
})
})

0 comments on commit 648683d

Please sign in to comment.