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 with ref inside v-for #1443

Merged
merged 1 commit into from
Apr 20, 2022
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
8 changes: 7 additions & 1 deletion src/baseWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,13 @@ export default abstract class BaseWrapper<ElementType extends Node>
}

if (typeof selector === 'object' && 'ref' in selector) {
const result = currentComponent.refs[selector.ref]
let result = currentComponent.refs[selector.ref]

// When using ref inside v-for, then refs contains array of component instances
if (Array.isArray(result)) {
result = result.length ? result[0] : undefined
}

if (result && !(result instanceof HTMLElement)) {
return createVueWrapper(null, result as ComponentPublicInstance)
} else {
Expand Down
23 changes: 23 additions & 0 deletions tests/findComponent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,29 @@ describe('findComponent', () => {
expect(compB[0].vm.$el.querySelector('.content').textContent).toBe('1')
})

it('finds single by ref in v-for', () => {
const ChildComp = {
props: {
value: Number
},
template: '<span>{{value}}</span>'
}

const wrapper = mount({
components: { ChildComp },
template: `
<div>
<div v-for="value in 3" :key="value">
<ChildComp ref="child" :value="value" />
</div>
</div>
`
})
const child = wrapper.findComponent({ ref: 'child' })
expect(child.exists()).toBe(true)
expect(child.props('value')).toBe(1)
})

// https://github.com/vuejs/test-utils/pull/188
const slotComponent = defineComponent({
name: 'slotA',
Expand Down