Skip to content

Commit 6cb26ee

Browse files
authored
Merge pull request #168 from cexbrayat/feat/find-component-in-suspense
feat: findComponent can find component inside a Suspense
2 parents 4c7c9dc + 4ef9a0e commit 6cb26ee

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

src/utils/find.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,23 @@ function findAllVNodes(
106106
const nodes: VNode[] = [vnode]
107107
while (nodes.length) {
108108
const node = nodes.shift()!
109+
// match direct children
109110
aggregateChildren(nodes, node.children)
110111
if (node.component) {
112+
// match children of the wrapping component
111113
aggregateChildren(nodes, node.component.subTree.children)
112114
}
115+
if (node.suspense) {
116+
// match children if component is Suspense
117+
const { isResolved, fallbackTree, subTree } = node.suspense
118+
if (isResolved) {
119+
// if the suspense is resolved, we match its children
120+
aggregateChildren(nodes, subTree.children)
121+
} else {
122+
// otherwise we match its fallback tree
123+
aggregateChildren(nodes, fallbackTree.children)
124+
}
125+
}
113126
if (matches(node, selector)) {
114127
matchingNodes.push(node)
115128
}

tests/find.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { defineComponent, h } from 'vue'
22

33
import { mount } from '../src'
44
import SuspenseComponent from './components/Suspense.vue'
5-
import Hello from './components/Hello.vue'
65

76
describe('find', () => {
87
it('find using single root node', () => {

tests/findComponent.spec.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineComponent } from 'vue'
1+
import { defineComponent, nextTick } from 'vue'
22
import { mount } from '../src'
33
import Hello from './components/Hello.vue'
44
import ComponentWithoutName from './components/ComponentWithoutName.vue'
@@ -112,6 +112,35 @@ describe('findComponent', () => {
112112
expect(wrapper.findComponent(compC).text()).toBe('C')
113113
})
114114

115+
it('finds component in a Suspense', async () => {
116+
const AsyncComponent = defineComponent({
117+
template: '{{ result }}',
118+
async setup() {
119+
return { result: 'Hello world' }
120+
}
121+
})
122+
const SuspenseComponent = defineComponent({
123+
template: `<Suspense>
124+
<template #default><AsyncComponent/></template>
125+
<template #fallback><CompC/></template>
126+
</Suspense>`,
127+
components: {
128+
AsyncComponent,
129+
CompC: compC
130+
}
131+
})
132+
const wrapper = mount(SuspenseComponent)
133+
expect(wrapper.html()).toContain('<div class="C">C</div>')
134+
expect(wrapper.findComponent(compC).exists()).toBe(true)
135+
expect(wrapper.findComponent(AsyncComponent).exists()).toBe(false)
136+
await nextTick()
137+
await nextTick()
138+
expect(wrapper.html()).toContain('Hello world')
139+
expect(wrapper.findComponent(compC).exists()).toBe(false)
140+
expect(wrapper.findComponent(AsyncComponent).exists()).toBe(true)
141+
expect(wrapper.findComponent(AsyncComponent).text()).toBe('Hello world')
142+
})
143+
115144
it('finds a stub by name', () => {
116145
const wrapper = mount(compA, {
117146
global: {

0 commit comments

Comments
 (0)