Skip to content

Commit

Permalink
fix(findComponent): Use correct vm for stubbed component with selector
Browse files Browse the repository at this point in the history
  • Loading branch information
freakzlike committed Feb 4, 2024
1 parent ffa8591 commit 3ba522d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/baseWrapper.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3ba522d

Please sign in to comment.