【问题标题】:change selected option of v-select using vue-test-util使用 vue-test-util 更改 v-select 的选定选项
【发布时间】:2023-04-11 02:20:01
【问题描述】:

我正在尝试测试使用 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>

设置数据后第一次测试就ok了:

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

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

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

还有

componentWrapper.find('el-select')

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

【问题讨论】:

  • 刚来更新我刚刚发现 vuetify 为选项过滤器创建了另一个 div 和一些输入。我已经改变了行为以触发选择

标签: vuetify.js vue-test-utils


【解决方案1】:

【讨论】:

  • 对于这个问题的新手,这里是一个更新的链接,因为上面已经移动了:github.com/vuetifyjs/vuetify/blob/…
  • 优秀的答案!它必须被标记为正确。这对我有用。谢谢@梁周js const select = wrapper.findComponent({ ref: "myselectref" }); expect(select.exists()).toBeTruthy(); select.vm.selectItem("foo"); expect(wrapper.vm.getMyData()).toStrictEqual("foo");
【解决方案2】:
wrapper.findAll('.v-list__tile__title').at(0).trigger('click')

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

这里是ref

我使用了 Vuetify v1.5.5。

【讨论】:

    【解决方案3】:

    我的解决方案。这与YounSeon Ahn 的反应非常相似,只是选项选择器发生了变化。我正在使用 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');
    

    【讨论】:

    • 只是选择器中的一个小错字,您在 findAll('.v-list-item') 中缺少 I
    • 我修好了,谢谢! :)
    【解决方案4】:

    终于让这个适用于 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 的选项很好

    【讨论】:

      【解决方案5】:

      这是我在 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
      

      【讨论】:

      • 这个问题是关于 Vue Test Utils,而不是 Cypress
      【解决方案6】:

      我也一直在努力解决这个问题,终于找到了解决办法。我正在使用 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('');
      });
      

      【讨论】:

      • &lt;v-select&gt; 不呈现默认浏览器
      • 我没有使用 vuetify。只是简单的 HTML + Vue,如我上面的 HTML 答案所示。
      • 降低了这个答案,因为 qusetion 是关于 Vuetify 组件 V-Select,而不是简单的 html 选择元素
      • 它对我不起作用。我不得不使用`click`而不是change
      • 这并不能回答这个问题,因为许多测试套件已经有了与纯 HTML 元素,并使用附加在 DOM 中其他位置的单独菜单小部件来模拟下拉菜单。
      猜你喜欢
      • 1970-01-01
      • 2022-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-18
      • 2021-06-08
      • 2011-11-14
      相关资源
      最近更新 更多