From 3ba522d7979b2e9c25756c471207b71ae8a051a6 Mon Sep 17 00:00:00 2001 From: freakzlike Date: Sun, 4 Feb 2024 11:00:34 +0100 Subject: [PATCH] fix(findComponent): Use correct vm for stubbed component with selector --- src/baseWrapper.ts | 7 ++++++- tests/props.spec.ts | 28 +++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/baseWrapper.ts b/src/baseWrapper.ts index 71f074bf5..13562f933 100644 --- a/src/baseWrapper.ts +++ b/src/baseWrapper.ts @@ -175,7 +175,12 @@ export default abstract class BaseWrapper 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) diff --git a/tests/props.spec.ts b/tests/props.spec.ts index 503a959f9..90c2948e8 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: '
{{ value }}
' + }) + + const TestComponent = defineComponent({ + components: { ChildComponent }, + template: '' + }) + + 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',