b按钮上的触发事件未显示在wrapper.emitted()上

时间:2019-04-16 18:00:16

标签: vue.js nuxt.js bootstrap-vue vue-test-utils

我刚开始测试vue组​​件,现在我正尝试测试单击b按钮组件会触发事件并调用方法。

该测试似乎失败了,因为没有触发'click'事件。

还请注意,我正在使用Nuxt.js

包含b按钮的组件如下:

<template>
  <div>
    <b-form-input name="username" type="text" v-model="username"></b-form-input>
    <b-form-input name="password" type="password" v-model="password"></b-form-input>
    <b-button @click="login">Login</b-button>
  </div>
</template>

<script>
import { mapActions } from "vuex";

export default {
  data() {
    return {
      username: "",
      password: ""
    };
  },
  methods: {
    ...mapActions({
      dispatchLogin: "auth/login"
    }),
    login: () => {
      const response = this.dispatchLogin(this.username, this.password);
      response.then(this.$router.push("/dashboard"));
    }
  }
};
</script>

测试是这样写的:

    test('call login when Login button gets clicked', async () => {

        expect.assertions(2);

        moxios.stubRequest('/api/auth/login', {
            response: 200
        });

        const store = new Vuex.Store(storeConfig);
        const mocks = {
            login: jest.fn()
        }
        const wrapper = shallowMount(login, { localVue, store, mocks });
        debugger;
        // Note that b-button is stubbed as <button></button>
        wrapper.find('button').trigger('click');  

        await flushPromises()

        expect(wrapper.emitted('click')).toHaveLength(1);
        expect(auth.actions.login).toHaveBeenCalledTimes(1);
    });

问题是我在wrapper.emitted()中找不到任何点击事件。

0 个答案:

没有答案