Skip to content

Commit f804697

Browse files
committed
fix: allow finding route component
1 parent 3110633 commit f804697

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

src/utils/find.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import { matchName } from './matchName'
1313
* @param selector
1414
* @return {boolean | ((value: any) => boolean)}
1515
*/
16-
function matches(node: VNode, selector: FindAllComponentsSelector): boolean {
16+
export function matches(
17+
node: VNode,
18+
selector: FindAllComponentsSelector
19+
): boolean {
1720
// do not return none Vue components
1821
if (!node.component) return false
1922

@@ -54,7 +57,9 @@ function matches(node: VNode, selector: FindAllComponentsSelector): boolean {
5457
}
5558
}
5659
// we may have one or both missing names
57-
return matchName(selectorName, componentName)
60+
if (selectorName && componentName) {
61+
return matchName(selectorName, componentName)
62+
}
5863
}
5964
}
6065

src/vueWrapper.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from './types'
1111
import { createWrapperError } from './errorWrapper'
1212
import { TriggerOptions } from './createDomEvent'
13-
import { find } from './utils/find'
13+
import { find, matches } from './utils/find'
1414
import { isFunctionalComponent } from './utils'
1515

1616
export class VueWrapper<T extends ComponentPublicInstance> {
@@ -164,6 +164,16 @@ export class VueWrapper<T extends ComponentPublicInstance> {
164164
})
165165
}
166166

167+
// https://github.com/vuejs/vue-test-utils-next/issues/211
168+
// VTU v1 supported finding the component mounted itself.
169+
// eg: mount(Comp).findComponent(Comp)
170+
// this is the same as doing `wrapper.vm`, but we keep this behavior for back compat.
171+
if (matches(this.vm.$.vnode, selector)) {
172+
return createWrapper(null, this.vm.$.vnode.component.proxy, {
173+
isFunctionalComponent: false
174+
})
175+
}
176+
167177
return createWrapperError('VueWrapper')
168178
}
169179

tests/findComponent.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Hello from './components/Hello.vue'
44
import ComponentWithoutName from './components/ComponentWithoutName.vue'
55

66
const compC = defineComponent({
7-
name: 'ComponentC',
7+
// name: 'ComponentC',
88
template: '<div class="C">C</div>'
99
})
1010

@@ -28,6 +28,7 @@ const compB = defineComponent({
2828
})
2929

3030
const compA = defineComponent({
31+
name: 'A',
3132
template: `
3233
<div class="A">
3334
<comp-b />
@@ -82,6 +83,11 @@ describe('findComponent', () => {
8283
expect(wrapper.findComponent({ name: 'component-c' }).exists()).toBeTruthy()
8384
})
8485

86+
it('finds root component', () => {
87+
const wrapper = mount(compA)
88+
expect(wrapper.findComponent(compA).exists()).toBe(true)
89+
})
90+
8591
it('finds component without a name by using its object definition', () => {
8692
const Component = {
8793
template: '<div><component-without-name/></div>',

0 commit comments

Comments
 (0)