Skip to content

findComponent throws an error when used on functional wrapper #1577

Closed
@marina-mosti

Description

@marina-mosti

Subject of the issue

When testing a functional component, findComponent to search for components rendered inside throws an error claiming that the component is a DOM element.

Steps to reproduce

Considering the following component, where BaseIcon is a component that renders an icon.

Edit: The inner workings of BaseIcon are not really meaningful, any component that is returned in the render function seems to cause the same problem.

<script>
export default {
  functional: true,
  render (h, context) {
    return h(
      'a',
      {},
      [
        context.slots().default,
        h('BaseIcon', {})
      ]
    )
  }
}
</script>

When running test:

it('shows an icon', () => {
    const wrapper = shallowMount(SimpleLink)
    console.log(wrapper)

    const icon = wrapper.findComponent(BaseIcon)
    expect(icon.exists()).toBe(true)
  })

The following error occurs:

 components/controls/base/SimpleLink.unit.js
  ● Console

    console.log components/controls/base/SimpleLink.unit.js:16
      Wrapper { isFunctionalComponent: true }

  ● SimpleLink › shows an icon

    [vue-test-utils]: You cannot chain findComponent off a DOM element. It can only be used on Vue Components.

      16 |     console.log(wrapper)
      17 | 
    > 18 |     const icon = wrapper.findComponent(BaseIcon)
         |                          ^
      19 |     expect(icon.exists()).toBe(true)
      20 |   })
      21 | })

      at throwError (../node_modules/@vue/test-utils/dist/vue-test-utils.js:1731:9)
      at Wrapper.findComponent (../node_modules/@vue/test-utils/dist/vue-test-utils.js:10500:5)
      at Object.<anonymous> (components/controls/base/SimpleLink.unit.js:18:26)

Expected behaviour

VTU should recognize that the wrapper is a functional component, and not a DOM element, so that I can chain findComponent on it.

Actual behaviour

VTU thinks that the functional component is a DOM element, which makes the use of findComponent an error.
Additionally, when trying to use find instead, it also errors out since BaseIcon is a component.

Possible Solution

🤷‍♀️ No clue, sorry :D

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions