Skip to content

Commit cf988ce

Browse files
authored
Merge pull request #238 from vuejs/issue-187
fix: findComponent with functional components
2 parents 3ba4e15 + ed40421 commit cf988ce

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/utils/find.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ export function matches(
2020
// do not return none Vue components
2121
if (!node.component) return false
2222

23+
if (node.type === selector) {
24+
return true
25+
}
26+
2327
if (typeof selector === 'string') {
2428
return node.el?.matches?.(selector)
2529
}

src/vueWrapper.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export class VueWrapper<T extends ComponentPublicInstance> {
2323
functionalEmits?: Record<string, unknown[]>
2424
) {
2525
this.__app = app
26-
this.rootVM = vm.$root!
26+
// root is null on functional components
27+
this.rootVM = vm?.$root
2728
this.componentVM = vm as T
2829
this.__setProps = setProps
2930
this.__functionalEmits = functionalEmits

tests/findComponent.spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineComponent, nextTick } from 'vue'
1+
import { defineComponent, h, nextTick } from 'vue'
22
import { mount } from '../src'
33
import Hello from './components/Hello.vue'
44
import ComponentWithoutName from './components/ComponentWithoutName.vue'
@@ -269,4 +269,24 @@ describe('findComponent', () => {
269269
expect(wrapper.findComponent(compC).exists()).toBe(true)
270270
expect(wrapper.findComponent(SlotMain).exists()).toBe(true)
271271
})
272+
273+
it('finds a functional component', () => {
274+
const Func = () => h('h2')
275+
const wrapper = mount({
276+
setup() {
277+
return () => h('span', {}, h(Func))
278+
}
279+
})
280+
expect(wrapper.findComponent(Func).exists()).toBe(true)
281+
})
282+
283+
it('finds a functional component in a fragment', () => {
284+
const Func = () => h('h2')
285+
const wrapper = mount({
286+
setup() {
287+
return () => [h('span'), h('span', {}, h(Func))]
288+
}
289+
})
290+
expect(wrapper.findComponent(Func).exists()).toBe(true)
291+
})
272292
})

0 commit comments

Comments
 (0)