Skip to content

Commit 21f3ab1

Browse files
authored
fix vuejs#1845: add functional component check in component name match (vuejs#1857)
1 parent 82370ab commit 21f3ab1

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

packages/test-utils/src/matches.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@ import {
33
COMPONENT_SELECTOR,
44
FUNCTIONAL_OPTIONS
55
} from 'shared/consts'
6-
import { isConstructor } from 'shared/validators'
6+
import { isConstructor, isFunctionalComponent } from 'shared/validators'
77
import { capitalize, camelize } from 'shared/util'
88

99
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.name || (vm.$options && vm.$options.name) || ''
13+
const componentName = isFunctionalComponent(vm)
14+
? vm.name
15+
: vm.$options && vm.$options.name
16+
1417
return (
1518
!!name &&
19+
!!componentName &&
1620
(componentName === name ||
1721
// testComponent -> TestComponent
1822
componentName === capitalize(name) ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<template>
2+
<div>
3+
<p class="prop-name">{{ name }}</p>
4+
</div>
5+
</template>
6+
7+
<script>
8+
export default {
9+
name: 'component-with-name-prop',
10+
props: ['name']
11+
}
12+
</script>

test/specs/wrapper/find.spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { createLocalVue, shallowMount } from 'packages/test-utils/src'
33
import Vue from 'vue'
44
import VueRouter from 'vue-router'
55
import ComponentWithChild from '~resources/components/component-with-child.vue'
6+
import ComponentWithNameProp from '~resources/components/component-with-name-prop.vue'
67
import ComponentWithoutName from '~resources/components/component-without-name.vue'
78
import ComponentWithSlots from '~resources/components/component-with-slots.vue'
89
import ComponentWithVFor from '~resources/components/component-with-v-for.vue'
@@ -556,6 +557,15 @@ describeWithShallowAndMount('find', mountingMethod => {
556557
expect(wrapper.find({ name: 'CamelCase' }).name()).toEqual('camel-case')
557558
})
558559

560+
it('returns a Wrapper matching a component name if Component has a name prop', () => {
561+
const wrapper = mountingMethod(ComponentWithNameProp, {
562+
propsData: { name: 'prop1' }
563+
})
564+
expect(
565+
wrapper.findComponent({ name: 'component-with-name-prop' }).vnode
566+
).toBeTruthy()
567+
})
568+
559569
it('returns Wrapper of Vue Component matching the ref in options object', () => {
560570
const wrapper = mountingMethod(ComponentWithChild)
561571
expect(wrapper.find({ ref: 'child' }).isVueInstance()).toEqual(true)

0 commit comments

Comments
 (0)