4

我正在尝试对根据鼠标悬停显示 v-card-actions 的 @mouseover 和 @mouseleave 事件进行单元测试。我在我的 vuejs2 webpack 应用程序中使用 vue-test-utils。这是我在网上找到的:vue-utlis 鼠标单击示例。提前感谢任何提供帮助的人

这是我的代码实际的 html 模板代码:

  <v-card class="menuCard pa-1 elevation-2 f-basis-0 my-2" 
@mouseover="isBlockAct = true" @mouseleave="isBlockAct = (!checked? 
false:true)">
 <v-checkbox v-model="checked" id="checkbox" class="diCheckbox ma-0" hide- 
details></v-checkbox>
  <v-card-actions class="myUpHere pa-0">
  <v-layout row :class="{'isAct': isBlockAct, 'isNotAct': !isBlockAct}">
 </v-card>

这是我在我的 spec.js 代码中尝试的:

  describe("Over event", () => {
     it("shows the icons if the card is over or not", () => {
      const wrapper = mount(MenuRepasRecetteCard, {
        propsData: {}
      });
      wrapper.find(".menuCard").trigger("mouseover");
      expect(wrapper.find(".isAct").text()).contain("remove_red_eye");
    });
4

3 回答 3

7

你需要等待事件被 vue 处理。

describe("Over event", (done) => {
    it("shows the icons if the card is over or not", () => {
        const wrapper = mount(MenuRepasRecetteCard, {
            propsData: {}
        });
        wrapper.find(".menuCard").trigger("mouseover");
        wrapper.vm.$nextTick( () => {
            expect(wrapper.find(".isAct").text()).contain("remove_red_eye");
            done();
        });
    });
});
于 2018-06-20T13:32:57.870 回答
2

关键字可await用于等待触发器完成:

await wrapper.find(".menuCard").trigger("mouseover");

async还需要添加到闭包中:

it("shows the icons if the card is over or not", async () => {
于 2021-07-05T19:01:54.580 回答
1

不幸的是,我无法评论拉斯的回答。文档说不需要 nextTick :

Vue Test Utils 同步触发事件。因此,不需要 Vue.nextTick。

-来源:https ://vue-test-utils.vuejs.org/guides/dom-events.html#important

于 2019-04-16T08:28:27.710 回答