Repository: vuejs/test-utils (ts)

Issue:
Bug: findComponent doesn't work properly if child component is root of parent component
<!-- Thanks for your interest in the project! We appreciate bugs filed and PRs submitted! -->


**Describe the bug**
This is known issue in vue test utils v1, but the previous workaround doesn't work anymore

```
<template>
  <WelcomeItem :test="2"> </WelcomeItem>
</template>
```
vs
```
<template>
  <div>
    <WelcomeItem :test="2"> </WelcomeItem>
  </div>
</template>
```

**To Reproduce**
<!-- A link to a minimal reproduction (with the minimum code to reproduce the issue). 
It takes just a few minutes to build a repro online with https://stackblitz.com/github/vuejs/create-vue-templates/tree/main/typescript-vitest?file=src%2Fcomponents%2F__tests__%2FHelloWorld.spec.ts
-->
https://stackblitz.com/edit/github-orekbd?file=src%2Fcomponents%2F__tests__%2FTheWelcome.spec.ts

run `yarn test:utils TheWelcome`

**Expected behavior**
<!-- A clear and concise description of what you expected to happen. -->
`findComponent.props()` returns props of child component

**Related information:**
<!-- Output of  `npx envinfo --system --npmPackages vue,@vue/test-utils,vitest,jest` -->
<!-- For test-utils v1 and Vue v2, please report at repository https://github.com/vuejs/vue-test-utils -->

In vue test utils v1 there was workaround: 
```
const childWrapper = createWrapper(wrapper.vm.$children[0])
```
Now there is no `wrapper.vm.$children`


Pull request description:
fix(findComponent): Use correct vm for stubbed component with selector
It seems that using the vm from `currentComponent.subTree.component.proxy` is more accurate in that case instead of `currentComponent.proxy`

fix #2300 

Patch:
diff --git a/src/baseWrapper.ts b/src/baseWrapper.ts
index 71f074b..13562f9 100644
--- a/src/baseWrapper.ts
+++ b/src/baseWrapper.ts
@@ -175,7 +175,12 @@ export default abstract class BaseWrapper<ElementType extends Node>
       matches(currentComponent.vnode, selector) &&
       this.element.contains(currentComponent.vnode.el as Node)
     ) {
-      return createVueWrapper(null, currentComponent.proxy!)
+      return createVueWrapper(
+        null,
+        currentComponent.subTree.component
+          ? currentComponent.subTree.component.proxy!
+          : currentComponent.proxy!
+      )
     }
 
     const [result] = this.findAllComponents(selector)


Test patch:
diff --git a/tests/props.spec.ts b/tests/props.spec.ts
index 503a959..90c2948 100644
--- a/tests/props.spec.ts
+++ b/tests/props.spec.ts
@@ -1,5 +1,5 @@
 import { describe, expect, it } from 'vitest'
-import { mount, shallowMount } from '../src'
+import { mount, shallowMount, VueWrapper } from '../src'
 import WithProps from './components/WithProps.vue'
 import PropWithSymbol from './components/PropWithSymbol.vue'
 import Hello from './components/Hello.vue'
@@ -207,6 +207,31 @@ describe('props', () => {
     expect(wrapper.find('.selectedField').text()).toBe('Cities')
   })
 
+  it('returns props of stubbed root component', async () => {
+    const ChildComponent = defineComponent({
+      props: {
+        value: {
+          type: Number,
+          required: true
+        }
+      },
+      template: '<div>{{ value }}</div>'
+    })
+
+    const TestComponent = defineComponent({
+      components: { ChildComponent },
+      template: '<ChildComponent :value="2"/>'
+    })
+
+    const wrapper = shallowMount(TestComponent)
+    expect(
+      wrapper.findComponent({ name: 'ChildComponent' }).props()
+    ).toStrictEqual({ value: 2 })
+    expect(
+      (wrapper.findComponent('child-component-stub') as VueWrapper).props()
+    ).toStrictEqual({ value: 2 })
+  })
+
   it('returns reactive props on a stubbed component shallow case', async () => {
     const Foo = {
       name: 'Foo',
@@ -242,6 +267,7 @@ describe('props', () => {
       foo: 'new value'
     })
   })
+
   it('https://github.com/vuejs/test-utils/issues/440', async () => {
     const Foo = defineComponent({
       name: 'Foo',