Skip to content

Commit

Permalink
Merge pull request #238 from vuejs/issue-187
Browse files Browse the repository at this point in the history
fix: findComponent with functional components
  • Loading branch information
lmiller1990 committed Nov 8, 2020
2 parents 3ba4e15 + ed40421 commit cf988ce
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/utils/find.ts
Expand Up @@ -20,6 +20,10 @@ export function matches(
// do not return none Vue components
if (!node.component) return false

if (node.type === selector) {
return true
}

if (typeof selector === 'string') {
return node.el?.matches?.(selector)
}
Expand Down
3 changes: 2 additions & 1 deletion src/vueWrapper.ts
Expand Up @@ -23,7 +23,8 @@ export class VueWrapper<T extends ComponentPublicInstance> {
functionalEmits?: Record<string, unknown[]>
) {
this.__app = app
this.rootVM = vm.$root!
// root is null on functional components
this.rootVM = vm?.$root
this.componentVM = vm as T
this.__setProps = setProps
this.__functionalEmits = functionalEmits
Expand Down
22 changes: 21 additions & 1 deletion tests/findComponent.spec.ts
@@ -1,4 +1,4 @@
import { defineComponent, nextTick } from 'vue'
import { defineComponent, h, nextTick } from 'vue'
import { mount } from '../src'
import Hello from './components/Hello.vue'
import ComponentWithoutName from './components/ComponentWithoutName.vue'
Expand Down Expand Up @@ -269,4 +269,24 @@ describe('findComponent', () => {
expect(wrapper.findComponent(compC).exists()).toBe(true)
expect(wrapper.findComponent(SlotMain).exists()).toBe(true)
})

it('finds a functional component', () => {
const Func = () => h('h2')
const wrapper = mount({
setup() {
return () => h('span', {}, h(Func))
}
})
expect(wrapper.findComponent(Func).exists()).toBe(true)
})

it('finds a functional component in a fragment', () => {
const Func = () => h('h2')
const wrapper = mount({
setup() {
return () => [h('span'), h('span', {}, h(Func))]
}
})
expect(wrapper.findComponent(Func).exists()).toBe(true)
})
})

0 comments on commit cf988ce

Please sign in to comment.