Skip to content

Commit b4f6140

Browse files
freakzlikecexbrayat
authored andcommitted
fix(findComponent): Use correct vm for stubbed component with selector
1 parent ffa8591 commit b4f6140

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/baseWrapper.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,12 @@ export default abstract class BaseWrapper<ElementType extends Node>
175175
matches(currentComponent.vnode, selector) &&
176176
this.element.contains(currentComponent.vnode.el as Node)
177177
) {
178-
return createVueWrapper(null, currentComponent.proxy!)
178+
return createVueWrapper(
179+
null,
180+
currentComponent.subTree.component
181+
? currentComponent.subTree.component.proxy!
182+
: currentComponent.proxy!
183+
)
179184
}
180185

181186
const [result] = this.findAllComponents(selector)

tests/props.spec.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from 'vitest'
2-
import { mount, shallowMount } from '../src'
2+
import { mount, shallowMount, VueWrapper } from '../src'
33
import WithProps from './components/WithProps.vue'
44
import PropWithSymbol from './components/PropWithSymbol.vue'
55
import Hello from './components/Hello.vue'
@@ -207,6 +207,31 @@ describe('props', () => {
207207
expect(wrapper.find('.selectedField').text()).toBe('Cities')
208208
})
209209

210+
it('returns props of stubbed root component', async () => {
211+
const ChildComponent = defineComponent({
212+
props: {
213+
value: {
214+
type: Number,
215+
required: true
216+
}
217+
},
218+
template: '<div>{{ value }}</div>'
219+
})
220+
221+
const TestComponent = defineComponent({
222+
components: { ChildComponent },
223+
template: '<ChildComponent :value="2"/>'
224+
})
225+
226+
const wrapper = shallowMount(TestComponent)
227+
expect(
228+
wrapper.findComponent({ name: 'ChildComponent' }).props()
229+
).toStrictEqual({ value: 2 })
230+
expect(
231+
(wrapper.findComponent('child-component-stub') as VueWrapper).props()
232+
).toStrictEqual({ value: 2 })
233+
})
234+
210235
it('returns reactive props on a stubbed component shallow case', async () => {
211236
const Foo = {
212237
name: 'Foo',
@@ -242,6 +267,7 @@ describe('props', () => {
242267
foo: 'new value'
243268
})
244269
})
270+
245271
it('https://github.com/vuejs/test-utils/issues/440', async () => {
246272
const Foo = defineComponent({
247273
name: 'Foo',

0 commit comments

Comments
 (0)