Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set the data values in a component with Vue-Test-Utils before mounted is called?

I am using Jest with Vue-Test-Utils. The code I have been using looks like this:

beforeEach(() => {
  wrapper = shallow(GridContainer, {
    data: {
      pageSize: count
    },
    propsData: {
      userId,
      managerId
    }
  })
})

In this example, I want to set the pageSize value before the life cycle mounted is called. The problem with the above code is that I have started getting the following warning when the tests run:

[Vue warn]: Do not use built-in or reserved HTML elements as component id: data

When I delete the data property above, the warning goes away.

Am I setting the data correctly? If so, what should I do about the warning?

Should I set the data another way?

like image 639
Daryn Avatar asked Feb 23 '18 17:02

Daryn


People also ask

What is Vue test utils?

Vue Test Utils is the official unit testing utility library for Vue. js. This is the documentation for Vue Test Utils v1, which targets Vue 2 and earlier.

What is mounted method in Vue?

The `mounted()` Hook in Vue The mounted() hook is the most commonly used lifecycle hook in Vue. Vue calls the mounted() hook when your component is added to the DOM. It is most often used to send an HTTP request to fetch data that the component will then render.

What is data () in VUE JS?

The Node. js provides a Vue. js data() method to the user. Basically in vue. js data() we defined collection of logic and stored in component using vue.


1 Answers

Please try like this:

beforeEach(() => {
  wrapper = shallow(GridContainer, {
    propsData: {
     userId,
     managerId
    }
  })
 wrapper.setData({pageSize: count})
})

reference: setData https://vue-test-utils.vuejs.org/en/api/wrapper/setData.html

like image 120
yukihirop Avatar answered Oct 14 '22 02:10

yukihirop