Skip to content

Commit

Permalink
Merge pull request #168 from cexbrayat/feat/find-component-in-suspense
Browse files Browse the repository at this point in the history
feat: findComponent can find component inside a Suspense
  • Loading branch information
lmiller1990 authored Aug 1, 2020
2 parents 4c7c9dc + 4ef9a0e commit 6cb26ee
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
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

0 comments on commit 6cb26ee

Please sign in to comment.