How do you unit test a Vue.js functional component with a render function that returns any array of elements?

Steven Lambert

In Vue.js, a functional component can return multiple root nodes by using a render function that returns an array of createdElements.

export default {
  functional: true,
  props: ['cellData'],
  render: function (h, context) {
    return [
      h('td', context.props.cellData.category),
      h('td', context.props.cellData.description)
    ]
  }
}

This works great but I'm having trouble trying to create a unit test for such a component. Using shallowMount on the component results in [Vue warn]: Multiple root nodes returned from render function. Render function should return a single root node.

import { shallowMount } from '@vue/test-utils'
import Cell from '@/components/Cell'

wrapper = shallowMount(Cell, {
  context: {
    props: {
      cellData {
        category: 'foo',
        description: 'bar'
      }
    }
  }
});

This github issue suggests that the component needs to be wrapped in a single root node to actually render it, but trying that results in [vue-test-utils]: mount.context can only be used when mounting a functional component

import { shallowMount } from '@vue/test-utils'
import Cell from '@/components/Cell'

wrapper = shallowMount('<div><Cell></div>', {
  context: {
    props: {
      cellData {
        category: 'foo',
        description: 'bar'
      }
    }
  }
});

So how do I test a functional component that returns multiple root nodes?

Husam Ibrahim

You could create a higher order, transparent wrapper component that passes all props and event listeners to the inner Cell component using v-bind="$attrs"[1] and v-on="$listeners"[2]. Then you can use propsData to pass props to the wrapper component ..

import { mount } from '@vue/test-utils'
import Cell from '@/components/Cell'

const WrappedCell = {
  components: { Cell },
  template: `
    <div>
      <Cell v-bind="$attrs" v-on="$listeners" />
    </div>
  `
}

const wrapper = mount(WrappedCell, {
  propsData: {
    cellData: {
      category: 'foo',
      description: 'bar'
    }
  }
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React functional component - how do I pass refs back to parent when component returns an array of elements?

How to Unit Test a Method in a Vue.js Component using jest

Vue.js Unit Test - How to check if component has a method?

How to write unit test for render component

How do you write a unit test for a function that is not exported?

How can you render a sub list in an array using Vue JS?

How do you unit test office.js app code?

Cannot render Vue component in my unit test with Jest

how to render component from a component in vue js

How to pass function from FUNCTIONAL to CLASS component and access it outside of render( without prop ), using context in react js?

How do you test a React function component state with Jest?

Vue - How do you render sub-rows in a table when you are only allowed one root component?

How do I test a method defined within a functional component, that interacts with DOM elements and has no arguments

Vue.js warns You may have an infinite update loop in a component render function

How do you test for null elements in System.Array

Unit/functional test for a mailing function

How do you Render a React Component to a String

How do I convert an <any> handleChange Function from class to functional component

How to Fix Vue Warning: You may have an infinite update loop in a component render function

How to fix or suppress a false positive "You may have an infinite update loop in a component render function" Vue warning

In a React/Jest unit test, how do you simulate an event prop being called by a mocked component?

How do you unit test a binding with a different name in an Angular 1.5 component?

How do you unit test a Celery task?

How do you unit test a Celery task?

How do you skip a unit test in Django?

How do you use test-unit?

How do you Unit Test Python DataFrames

How do you unit test private methods?

How do you unit test Django RawQuerySets