Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(findComponent): Use correct vm for stubbed component with selector #2327

Merged
merged 1 commit into from Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/baseWrapper.ts
Expand Up @@ -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)
Expand Down
28 changes: 27 additions & 1 deletion 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'
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -242,6 +267,7 @@ describe('props', () => {
foo: 'new value'
})
})

it('https://github.com/vuejs/test-utils/issues/440', async () => {
const Foo = defineComponent({
name: 'Foo',
Expand Down