Skip to content

Allow find to work on both Pascal case and camel case #1398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions packages/test-utils/src/matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@ import {
FUNCTIONAL_OPTIONS
} from 'shared/consts'
import { isConstructor } from 'shared/validators'
import { capitalize, camelize } from 'shared/util'

export function vmMatchesName(vm, name) {
function vmMatchesName(vm, name) {
// We want to mirror how Vue resolves component names in SFCs:
// For example, <test-component />, <TestComponent /> and `<testComponent />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// For example, <test-component />, <TestComponent /> and `<testComponent />
// For example, <test-component />, <TestComponent /> and <testComponent />

// all resolve to the same component
const componentName = (vm.$options && vm.$options.name) || ''
return (
!!name && (vm.name === name || (vm.$options && vm.$options.name === name))
!!name &&
(componentName === name ||
// testComponent -> TestComponent
componentName === capitalize(name) ||
// test-component -> TestComponent
componentName === capitalize(camelize(name)) ||
// same match as above, but the component name vs query
capitalize(camelize(componentName)) === name)
)
}

Expand Down
27 changes: 27 additions & 0 deletions test/specs/wrapper/find.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,33 @@ describeWithShallowAndMount('find', mountingMethod => {
)
})

it('returns a Wrapper matching a camelCase name option and a Pascal Case component name ', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('returns a Wrapper matching a camelCase name option and a Pascal Case component name ', () => {
it('returns a Wrapper matching a camelCase name option and a PascalCase component name', () => {

const component = {
name: 'CamelCase',
render: h => h('div')
}
const wrapper = mountingMethod(component)
expect(wrapper.find({ name: 'camelCase' }).name()).to.equal('CamelCase')
})

it('returns a Wrapper matching a kebab-case name option and a Pascal Case component name ', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('returns a Wrapper matching a kebab-case name option and a Pascal Case component name ', () => {
it('returns a Wrapper matching a kebab-case name option and a PascalCase component name', () => {

const component = {
name: 'CamelCase',
render: h => h('div')
}
const wrapper = mountingMethod(component)
expect(wrapper.find({ name: 'camel-case' }).name()).to.equal('CamelCase')
})

it('returns a Wrapper matching a Pascal Case name option and a kebab-casecomponent name ', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('returns a Wrapper matching a Pascal Case name option and a kebab-casecomponent name ', () => {
it('returns a Wrapper matching a PascalCase name option and a kebab-case component name', () => {

const component = {
name: 'camel-case',
render: h => h('div')
}
const wrapper = mountingMethod(component)
expect(wrapper.find({ name: 'CamelCase' }).name()).to.equal('camel-case')
})

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