Skip to content

Commit 5569325

Browse files
authored
Merge pull request #1398 from vuejs/feature/issue-1232
Allow find to work on both Pascal case and camel case
2 parents 4dae3be + e8f3266 commit 5569325

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

Diff for: packages/test-utils/src/matches.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,22 @@ import {
44
FUNCTIONAL_OPTIONS
55
} from 'shared/consts'
66
import { isConstructor } from 'shared/validators'
7+
import { capitalize, camelize } from 'shared/util'
78

8-
export function vmMatchesName(vm, name) {
9+
function vmMatchesName(vm, name) {
10+
// We want to mirror how Vue resolves component names in SFCs:
11+
// For example, <test-component />, <TestComponent /> and `<testComponent />
12+
// all resolve to the same component
13+
const componentName = (vm.$options && vm.$options.name) || ''
914
return (
10-
!!name && (vm.name === name || (vm.$options && vm.$options.name === name))
15+
!!name &&
16+
(componentName === name ||
17+
// testComponent -> TestComponent
18+
componentName === capitalize(name) ||
19+
// test-component -> TestComponent
20+
componentName === capitalize(camelize(name)) ||
21+
// same match as above, but the component name vs query
22+
capitalize(camelize(componentName)) === name)
1123
)
1224
}
1325

Diff for: test/specs/wrapper/find.spec.js

+27
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,33 @@ describeWithShallowAndMount('find', mountingMethod => {
431431
)
432432
})
433433

434+
it('returns a Wrapper matching a camelCase name option and a Pascal Case component name ', () => {
435+
const component = {
436+
name: 'CamelCase',
437+
render: h => h('div')
438+
}
439+
const wrapper = mountingMethod(component)
440+
expect(wrapper.find({ name: 'camelCase' }).name()).to.equal('CamelCase')
441+
})
442+
443+
it('returns a Wrapper matching a kebab-case name option and a Pascal Case component name ', () => {
444+
const component = {
445+
name: 'CamelCase',
446+
render: h => h('div')
447+
}
448+
const wrapper = mountingMethod(component)
449+
expect(wrapper.find({ name: 'camel-case' }).name()).to.equal('CamelCase')
450+
})
451+
452+
it('returns a Wrapper matching a Pascal Case name option and a kebab-casecomponent name ', () => {
453+
const component = {
454+
name: 'camel-case',
455+
render: h => h('div')
456+
}
457+
const wrapper = mountingMethod(component)
458+
expect(wrapper.find({ name: 'CamelCase' }).name()).to.equal('camel-case')
459+
})
460+
434461
it('returns Wrapper of Vue Component matching the ref in options object', () => {
435462
const wrapper = mountingMethod(ComponentWithChild)
436463
expect(wrapper.find({ ref: 'child' }).isVueInstance()).to.equal(true)

0 commit comments

Comments
 (0)