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

repro: findComponent fails inside a Suspense #88

Closed
Closed
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
4 changes: 4 additions & 0 deletions src/utils/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ function findAllVNodes(vnode: VNode, selector: any): VNode[] {
const nodes = [vnode]
while (nodes.length) {
const node = nodes.shift()
// match direct children
aggregateChildren(nodes, node.children)
// match children of the wrapping component
aggregateChildren(nodes, node.component?.subTree.children)
// match children if component is Suspense
aggregateChildren(nodes, node.suspense?.subTree.children)
if (matches(node, selector)) {
matchingNodes.push(node)
}
Expand Down
1 change: 0 additions & 1 deletion tests/find.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { defineComponent, h } from 'vue'

import { mount } from '../src'
import SuspenseComponent from './components/Suspense.vue'
import Hello from './components/Hello.vue'

describe('find', () => {
it('find using single root node', () => {
Expand Down
47 changes: 46 additions & 1 deletion tests/findComponent.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineComponent } from 'vue'
import { defineComponent, nextTick } from 'vue'
import { mount } from '../src'
import Hello from './components/Hello.vue'
import ComponentWithoutName from './components/ComponentWithoutName.vue'
Expand Down Expand Up @@ -112,6 +112,51 @@ describe('findComponent', () => {
expect(wrapper.findComponent(compC).text()).toBe('C')
})

it('finds component in a portal', async () => {
const Foo = {
name: 'Foo',
template: '<div>Foo</div>'
}

const Comp = defineComponent({
components: { Foo },
template: `
<div id="dest"></div>
<Teleport to="#dest">
<Foo />
</Teleport>
`
})
const wrapper = mount(Comp)

expect(wrapper.find('#dest').text()).toContain('Foo')
expect(wrapper.findComponent({ name: 'Foo' }).text()).toContain('Foo')
})

it('finds component in a Suspense', async () => {
const AsyncComponent = defineComponent({
template: '{{ result }}',
async setup() {
return { result: 'Hello world' }
}
})
const SuspenseComponent = defineComponent({
template: `<Suspense>
<template #default><AsyncComponent/></template>
<template #fallback>Loading...</template>
</Suspense>`,
components: {
AsyncComponent
}
})
const wrapper = mount(SuspenseComponent)
expect(wrapper.html()).toContain('Loading')
await nextTick()
await nextTick()
expect(wrapper.html()).toContain('Hello world')
expect(wrapper.findComponent(AsyncComponent).text()).toBe('Hello world')
})

it('finds a stub by name', () => {
const wrapper = mount(compA, {
global: {
Expand Down