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

feat: findComponent can find component inside a Suspense #168

Merged
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
13 changes: 13 additions & 0 deletions src/utils/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,23 @@ function findAllVNodes(
const nodes: VNode[] = [vnode]
while (nodes.length) {
const node = nodes.shift()!
// match direct children
aggregateChildren(nodes, node.children)
if (node.component) {
// match children of the wrapping component
aggregateChildren(nodes, node.component.subTree.children)
}
if (node.suspense) {
// match children if component is Suspense
const { isResolved, fallbackTree, subTree } = node.suspense
if (isResolved) {
// if the suspense is resolved, we match its children
aggregateChildren(nodes, subTree.children)
} else {
// otherwise we match its fallback tree
aggregateChildren(nodes, fallbackTree.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
31 changes: 30 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,35 @@ describe('findComponent', () => {
expect(wrapper.findComponent(compC).text()).toBe('C')
})

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><CompC/></template>
</Suspense>`,
components: {
AsyncComponent,
CompC: compC
}
})
const wrapper = mount(SuspenseComponent)
expect(wrapper.html()).toContain('<div class="C">C</div>')
expect(wrapper.findComponent(compC).exists()).toBe(true)
expect(wrapper.findComponent(AsyncComponent).exists()).toBe(false)
await nextTick()
await nextTick()
expect(wrapper.html()).toContain('Hello world')
expect(wrapper.findComponent(compC).exists()).toBe(false)
expect(wrapper.findComponent(AsyncComponent).exists()).toBe(true)
expect(wrapper.findComponent(AsyncComponent).text()).toBe('Hello world')
})

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