Skip to content

Commit 96a6498

Browse files
committed
fix: allow find stubbed functional component by name
1 parent 62dec1c commit 96a6498

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

packages/test-utils/src/matches.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function vmMatchesName(vm, name) {
1010
// We want to mirror how Vue resolves component names in SFCs:
1111
// For example, <test-component />, <TestComponent /> and `<testComponent />
1212
// all resolve to the same component
13-
const componentName = (vm.$options && vm.$options.name) || ''
13+
const componentName = vm.name || (vm.$options && vm.$options.name) || ''
1414
return (
1515
!!name &&
1616
(componentName === name ||
@@ -56,13 +56,7 @@ export function matches(node, selector) {
5656
return element && element.matches && element.matches(selector.value)
5757
}
5858

59-
const isFunctionalSelector = isConstructor(selector.value)
60-
? selector.value.options.functional
61-
: selector.value.functional
62-
63-
const componentInstance = isFunctionalSelector
64-
? node[FUNCTIONAL_OPTIONS]
65-
: node.child
59+
const componentInstance = node[FUNCTIONAL_OPTIONS] || node.child
6660

6761
if (!componentInstance) {
6862
return false

test/specs/wrapper/find.spec.js

+25
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,31 @@ describeWithShallowAndMount('find', mountingMethod => {
257257
}
258258
})
259259

260+
it('returns functional component with name by name', () => {
261+
const TestFunctionalComponent = {
262+
render: h => h('div'),
263+
functional: true,
264+
name: 'test-functional-component'
265+
}
266+
const TestComponent = {
267+
template: '<div><test-functional-component /></div>',
268+
components: {
269+
TestFunctionalComponent
270+
}
271+
}
272+
const wrapper = mountingMethod(TestComponent)
273+
if (vueVersion < 2.3) {
274+
const message =
275+
'[vue-test-utils]: find for functional components is not supported in Vue < 2.3'
276+
const fn = () => wrapper.find({ name: 'test-functional-component' })
277+
expect(fn).toThrow(message)
278+
} else {
279+
expect(
280+
wrapper.find({ name: 'test-functional-component' }).exists()
281+
).toEqual(true)
282+
}
283+
})
284+
260285
it('returns extended functional component', () => {
261286
const TestFunctionalComponent = Vue.extend({
262287
render: h => h('div'),

0 commit comments

Comments
 (0)