Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change selected option of v-select using vue-test-util

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>
</div>

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.

like image 759
EFOE Avatar asked Jun 22 '18 16:06

EFOE


3 Answers

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.

like image 58
Liang Zhou Avatar answered Nov 07 '22 01:11

Liang Zhou


wrapper.findAll('.v-list__tile__title').at(0).trigger('click')

It works for me. By this code, first option is selected.

Here is ref.

I used Vuetify v1.5.5.

like image 2
YoungSeon Ahn Avatar answered Nov 07 '22 02:11

YoungSeon Ahn


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');
like image 1
Adrien SAULNIER Avatar answered Nov 07 '22 02:11

Adrien SAULNIER