已经阅读了很多 stackoverflow 和 github 讨论,关于 vue jest 在使用 button.trigger('click') 时遇到问题。今天我一直在为这个问题苦苦挣扎了几个小时,不得不说我很沮丧,并且很惊讶一个简单的函数,比如 trigger('click') 会导致这么多问题。
简而言之,我的代码有一个 b 按钮,@click 会从 vuex 中触发 fetchData 函数。这在浏览器中运行良好,但在测试模式下,不会执行 fetchData。
Vue 组件代码
<template>
<b-button id="loadButton" variant="outline-primary" @click="fetchData">Load Data</b-button>
</template>
<script>
import { mapActions } from 'vuex';
export default {
name: "LoadAndSave",
methods: { ...mapActions(['fetchData']) }
}
</script>
Run Code Online (Sandbox Code Playgroud)
测试代码
import { shallowMount, createLocalVue } from '@vue/test-utils'
import Vuex from 'vuex'
import { BootstrapVue } from 'bootstrap-vue'
import LoadAndSave from '../../resources/components/LoadAndSave'
const localVue = createLocalVue()
localVue.use(BootstrapVue)
localVue.use(Vuex)
describe('LoadAndSave.vue', () => { …Run Code Online (Sandbox Code Playgroud)