Skip to content

Commit eec2d2d

Browse files
authored
Merge pull request #214 from vuejs/issue-211
fix: allow finding root component
2 parents db9fb9a + 910e3f4 commit eec2d2d

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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,23 @@ describe('findComponent', () => {
8283
expect(wrapper.findComponent({ name: 'component-c' }).exists()).toBeTruthy()
8384
})
8485

86+
it('finds root component', async () => {
87+
const Comp = defineComponent({
88+
name: 'C',
89+
template: `
90+
<input v-model="msg" />
91+
{{ msg }}
92+
`,
93+
data() {
94+
return { msg: 'foo' }
95+
}
96+
})
97+
const wrapper = mount(Comp)
98+
expect(wrapper.findComponent(Comp).exists()).toBe(true)
99+
await wrapper.find('input').setValue('bar')
100+
expect(wrapper.html()).toContain('bar')
101+
})
102+
85103
it('finds component without a name by using its object definition', () => {
86104
const Component = {
87105
template: '<div><component-without-name/></div>',

0 commit comments

Comments
 (0)