7

我正在尝试测试使用 vuetify 构建的组件。为此,我正在使用 vue-test-utils。我的目标是在 v-select 字段中执行更改,并断言执行了突变。这是我的代码:

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

当我设置数据时,第一个测试是好的:

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

我现在想更改我尝试的选定值:

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

并且

componentWrapper.find('el-select')

有人可以帮我检索选择然后更改选择的值吗?谢谢你的支持。

4

6 回答 6

9

使用wrapper.vm.selectItem('foo'). 这个对我有用。

我在 vuetifyv-select测试中发现了这一点:

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

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

编辑:更新链接。

于 2018-07-26T03:02:44.447 回答
2
wrapper.findAll('.v-list__tile__title').at(0).trigger('click')

这个对我有用。通过此代码,选择了第一个选项。

这是参考

我使用了 Vuetify v1.5.5。

于 2019-07-15T06:38:34.787 回答
1

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');
于 2021-01-28T10:41:04.183 回答
0

终于让它适用于 vuetify 1.5。如果你的 v-select 看起来像这样:

 <v-select      
        :items="projectManagers"
        :value="selectedProjectManager"
        key="UserId"
        label="Choose Name"
        item-text="FirstName"
        item-value="UserId"        
        id="nameSelect"
        @input="(val)=>{this.handleNameChange(val)}"
 />

然后这样做:

wrapper.findAll('#nameSelect').at(0).trigger('input')

这行得通,但由于某种原因,如果我在(1)处这样做,它就不起作用。谢天谢地,对于我的测试用例,位置 0 的选项很好

于 2020-06-11T16:26:47.680 回答
-1

这是我在 cypressjs 中测试 Vuetify VSelect 组件的最终解决方案:

  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
于 2019-11-06T09:41:25.773 回答
-4

我也一直在为此苦苦挣扎,终于找到了一种方法。我正在使用 Jest 和 vue-test-utils。基本上,您必须通过两个单独的步骤来完成。将选项标记为选中,然后触发选择的更改。

HTML部分是:

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

测试是:

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('');
});
于 2018-07-24T16:33:46.707 回答