Skip to content

Commit

Permalink
fix: Make wrapper.find() enable to find ref in v-for directive (#1723)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiromi2424 authored Aug 23, 2022
1 parent 4bf2141 commit 9a1fb51
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/baseWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ export default abstract class BaseWrapper<ElementType extends Node>
return createWrapperError('DOMWrapper')
}

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 and nodes
if (Array.isArray(result)) {
result = result.length ? result[0] : undefined
}

if (result instanceof Node) {
return createDOMWrapper(result)
Expand Down
16 changes: 16 additions & 0 deletions tests/find.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ describe('find', () => {
expect(wrapper.find({ ref: 'plain' }).element).toBeInstanceOf(Text)
})

it('works when ref is in v-for directive', () => {
const Component = defineComponent({
template: `
<div v-for="item in ['foo', 'bar']" :key="item">
<span :ref="\`span-\${item}\`" class="my-span" />
</div>
`
})

const wrapper = mount(Component)
expect(wrapper.find({ ref: 'span-foo' }).exists()).toBe(true)
expect(wrapper.find({ ref: 'span-foo' }).attributes('class')).toBe(
'my-span'
)
})

it('does not find ref located in the same component but not in current DOM wrapper', () => {
const Component = defineComponent({
render() {
Expand Down

0 comments on commit 9a1fb51

Please sign in to comment.