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

Bug: wrapper.find({ ref }) cannot catch ref in v-for directive #1722

Closed
hiromi2424 opened this issue Aug 16, 2022 · 0 comments · Fixed by #1723
Closed

Bug: wrapper.find({ ref }) cannot catch ref in v-for directive #1722

hiromi2424 opened this issue Aug 16, 2022 · 0 comments · Fixed by #1723
Labels
bug Something isn't working

Comments

@hiromi2424
Copy link
Contributor

hiromi2424 commented Aug 16, 2022

Describe the bug

wrapper.find({ ref }) returns correct ref when it is not in v-for directive.
wrapper.find({ ref }) does NOT return correct ref when it is in v-for directive.
wrapper.findComponent({ ref }) returns correct ref whenever it is in v-for directive or not.

To Reproduce

TestComponent.vue

<script setup>
const buttonNames = ['back', 'next'];
</script>

<template>
  <div v-for="buttonName in buttonNames" :key="buttonName">
    <button :ref="`button-${buttonName}`">{{ buttonName }}</button>
  </div>
</template>

TestComponent.spec.js (jest)

import { mount } from '@vue/test-utils';
import TestComponent from './TestComponent';

describe('TestComponent', () => {
  test('back button ref exists', () => {
    const wrapper = mount(TestComponent);
    const backButton = wrapper.find({ ref: 'button-back' });
    expect(backButton.text()).toBe('back');
  });
});

That test fails with:

Error: Cannot call text on an empty DOMWrapper.

Expected behavior

The test above passes and correct ref returns(as not array?)

Related information:

  • @vue/test-utils version: 2.0.2
  • Vue version: 3.2.37
  • node version: v14.18.2
  • npm (or yarn) version: npm 8.14.0

Additional context

Related code pointing to this bug:

const result = currentComponent.refs[selector.ref]
if (result instanceof Node) {
return createDOMWrapper(result)
} else {
return createWrapperError('DOMWrapper')
}
}

is not treat array returns.
On the other hand, findComponent() take care of v-for:
// When using ref inside v-for, then refs contains array of component instances
if (Array.isArray(result)) {
result = result.length ? result[0] : undefined
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant