Skip to content

Commit

Permalink
fix #1845: add functional component check in component name match (#1857
Browse files Browse the repository at this point in the history
)
  • Loading branch information
pimlie committed Jun 14, 2021
1 parent 82370ab commit 21f3ab1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/test-utils/src/matches.js
Expand Up @@ -3,16 +3,20 @@ import {
COMPONENT_SELECTOR,
FUNCTIONAL_OPTIONS
} from 'shared/consts'
import { isConstructor } from 'shared/validators'
import { isConstructor, isFunctionalComponent } from 'shared/validators'
import { capitalize, camelize } from 'shared/util'

function vmMatchesName(vm, name) {
// We want to mirror how Vue resolves component names in SFCs:
// For example, <test-component />, <TestComponent /> and `<testComponent />
// all resolve to the same component
const componentName = vm.name || (vm.$options && vm.$options.name) || ''
const componentName = isFunctionalComponent(vm)
? vm.name
: vm.$options && vm.$options.name

return (
!!name &&
!!componentName &&
(componentName === name ||
// testComponent -> TestComponent
componentName === capitalize(name) ||
Expand Down
12 changes: 12 additions & 0 deletions test/resources/components/component-with-name-prop.vue
@@ -0,0 +1,12 @@
<template>
<div>
<p class="prop-name">{{ name }}</p>
</div>
</template>

<script>
export default {
name: 'component-with-name-prop',
props: ['name']
}
</script>
10 changes: 10 additions & 0 deletions test/specs/wrapper/find.spec.js
Expand Up @@ -3,6 +3,7 @@ import { createLocalVue, shallowMount } from 'packages/test-utils/src'
import Vue from 'vue'
import VueRouter from 'vue-router'
import ComponentWithChild from '~resources/components/component-with-child.vue'
import ComponentWithNameProp from '~resources/components/component-with-name-prop.vue'
import ComponentWithoutName from '~resources/components/component-without-name.vue'
import ComponentWithSlots from '~resources/components/component-with-slots.vue'
import ComponentWithVFor from '~resources/components/component-with-v-for.vue'
Expand Down Expand Up @@ -556,6 +557,15 @@ describeWithShallowAndMount('find', mountingMethod => {
expect(wrapper.find({ name: 'CamelCase' }).name()).toEqual('camel-case')
})

it('returns a Wrapper matching a component name if Component has a name prop', () => {
const wrapper = mountingMethod(ComponentWithNameProp, {
propsData: { name: 'prop1' }
})
expect(
wrapper.findComponent({ name: 'component-with-name-prop' }).vnode
).toBeTruthy()
})

it('returns Wrapper of Vue Component matching the ref in options object', () => {
const wrapper = mountingMethod(ComponentWithChild)
expect(wrapper.find({ ref: 'child' }).isVueInstance()).toEqual(true)
Expand Down

0 comments on commit 21f3ab1

Please sign in to comment.