diff --git a/src/baseWrapper.ts b/src/baseWrapper.ts index 743f35d2b..d23497cf3 100644 --- a/src/baseWrapper.ts +++ b/src/baseWrapper.ts @@ -125,7 +125,13 @@ export default abstract class BaseWrapper } 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 { diff --git a/tests/findComponent.spec.ts b/tests/findComponent.spec.ts index bc50208f2..0e6c9c90b 100644 --- a/tests/findComponent.spec.ts +++ b/tests/findComponent.spec.ts @@ -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: '{{value}}' + } + + const wrapper = mount({ + components: { ChildComp }, + template: ` +
+
+ +
+
+ ` + }) + 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',