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: do not crash findComponent if ref is pointing to html element #817

Merged
merged 3 commits into from
Aug 10, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,10 @@ test('findComponent', () => {
})
```

:::warning
If `ref` in component points to HTML element, `findComponent` will return empty wrapper. This is intended behaviour
:::

**NOTE** `getComponent` and `findComponent` will not work on functional components, because they do not have an internal Vue instance (this is what makes functional components more performant). That means the following will **not** work:

```js
Expand Down
4 changes: 3 additions & 1 deletion src/vueWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,10 @@ export class VueWrapper<T extends ComponentPublicInstance>
): VueWrapper<T> {
if (typeof selector === 'object' && 'ref' in selector) {
const result = this.vm.$refs[selector.ref]
if (result) {
if (result && !(result instanceof HTMLElement)) {
return createWrapper(null, result as T)
} else {
return createWrapperError('VueWrapper')
}
}

Expand Down
11 changes: 10 additions & 1 deletion tests/findComponent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,16 @@ describe('findComponent', () => {
it('finds component by ref', () => {
const wrapper = mount(compA)
// find by ref
expect(wrapper.findComponent({ ref: 'hello' })).toBeTruthy()
expect(wrapper.findComponent({ ref: 'hello' }).exists()).toBe(true)
})

it('does not find plain dom element by ref', () => {
const ComponentWithRefOnDomElement = defineComponent({
template: '<div ref="hello">Hello!</div>'
})
const wrapper = mount(ComponentWithRefOnDomElement)

expect(wrapper.findComponent({ ref: 'hello' }).exists()).toBe(false)
})

it('finds component by dom selector', () => {
Expand Down