Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to test a component builded with vuetify. For that I'm using vue-test-utils. My goal is to perform a change in the v-select field, and assert hat a mutation is performed. here is my code:

<div id="app">
  <v-app id="inspire">
    <v-container fluid>
      <v-layout row wrap>
        <v-flex xs6>
          <v-subheader>Standard</v-subheader>
        </v-flex>
        <v-flex xs6>
          <v-select
            :items="items"
            v-model="e1"
            label="Select"
            single-line
          ></v-select>
        </v-flex>
      </v-layout>
    </v-container>
  </v-app>

The first test is ok when i set the data:

componentWrapper.setData({items: storesList})
expect(componentWrapper.vm.$data.items).toHaveLength(2)

I now want to change the selected value I try:

componentWrapper.findAll('#option').at(1).setSelected()

And also

componentWrapper.find('el-select')

Can someone help me to retrieve the select and then change the selected value? Thanks for your support.

Just come update i just found that vuetify create another div and some input for the filter of options. i have chage the behaviour in order to trigger a selection – EFOE Jun 26, 2018 at 13:41

Use wrapper.vm.selectItem('foo'). It works for me.

I found this in vuetify v-select tests:

Old: ~~https://github.com/vuetifyjs/vuetify/blob/master/packages/vuetify/test/unit/components/VSelect/VSelect.spec.js#L533~~

New: https://github.com/vuetifyjs/vuetify/blob/b2abe9fa274feeb0c5033bf12cc48276d4ac5a78/packages/vuetify/test/unit/components/VSelect/VSelect.spec.js#L28

Edit: Updated link.

For newcomers to this issue, here's an updated link since the above has moved: github.com/vuetifyjs/vuetify/blob/… – Admiral Potato May 15, 2019 at 20:12 Excellent answer! It must be marked as correct. This works for me. Thanks @Liang Zhou js const select = wrapper.findComponent({ ref: "myselectref" }); expect(select.exists()).toBeTruthy(); select.vm.selectItem("foo"); expect(wrapper.vm.getMyData()).toStrictEqual("foo"); – izumeroot Mar 12, 2021 at 19:53

My solution. It's very equivalent to YounSeon Ahn's response, just the options selector changed. I'm using Vuetify 2.2.11.

// Found the v-select
wrapper.find('[data-testid="mySelect"]').trigger('click'); 
// Wait for DOM updating after the click
await localVue.nextTick();
// Find all options and select the first. If you are multiple v-select in your form, the class ".menuable__content__active" represents the current opened menu
wrapper.find('.menuable__content__active').findAll('.v-list-item').at(0).trigger('click');

This is my final solution to test Vuetify VSelect component in cypressjs:

  cy.get('#YourVSelectComponentId', { timeout: 20000 }).should('not.have.attr', 'disabled', 'disabled').click({ force: true }); //find v-select, verify it's visible and then click.
  cy.get('.menuable__content__active').first().find('.v-list__tile').first().should('be.visible').click(); //find opened dropdown, then find first item in options, then click

I have been struggling with this as well and finally found a way to do it. I'm using Jest and vue-test-utils. Basically, you have to do it with two separate steps. Mark the option as selected and then trigger the change on the select.

The HTML part is:

      <select id="groupid" v-model="groupID">
        <option value="">-Select group-</option>
        <option v-for="g in groupList" 
          v-bind:value="g.groupId">{{g.groupId}} - {{g.groupName}}
        </option>
      </select>

And the test is:

it('should populate the subgroup list when a group is selected', () => {
  expect(cmp.vm.groupID).toBe('');
  expect(cmp.findAll('select#groupid > option').length).toBe(3);
  cmp.findAll('select#groupid > option').at(1).element.selected = true;
  cmp.find('select#groupid').trigger('change');
  expect(cmp.vm.groupID).not.toBe('');
                But <v-select> doesn't render default browser <select> markup. Where did you get your <select> and <option>s from?
– Desprit
                Jan 1, 2019 at 5:03
                lowered this answer, because qusetion is about Vuetify component V-Select, not simple html select element
– saike
                Oct 30, 2019 at 15:44
                This does not answer the question as many testing suites already have methods for interacting with plain HTML <select> elements. What's requested is the equivalent method for Vuetify's <v-select> widget which only uses an <input type=text"> element internally and simulates the drop-down menu with a separate menu widget attached elsewhere in the DOM.
– TwoD
                Mar 17, 2020 at 10:06
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.