【发布时间】:2021-07-27 04:08:05
【问题描述】:
我有一个像这样的 Vue 组件...
<template>
<div class="mapdiv"></div>
</template>
<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { mapViewModule } from "@/store/modules/MapViewModule";
@Component
export default class GeospatialMap extends Vue {
async mounted(): Promise<void> {
mapViewModule.initializeMapView(this.$el as HTMLDivElement);
}
}
</script>
<style scoped>
.mapdiv {
height: 700px;
width: 1000px;
}
</style>
...我正在尝试测试 mapViewModule.initalizeMapView 函数是否被调用,这是我的 Vuex 模块中的一个操作。
我正在使用 Jest 并查看了其他答案,例如:https://stackoverflow.com/a/66987942/2052752 但没有运气......
import { shallowMount, createLocalVue } from "@vue/test-utils";
import Vuex from "vuex";
import GeospatialMap from "@/components/Geospatial.vue";
describe("GeospatialMap - ", () => {
const localVue = createLocalVue();
localVue.use(Vuex);
const modules = {
mapViewModule: {
state: {},
actions: {
initializeMapView: jest.fn()
},
namespaced: true
}
};
const store = new Vuex.Store({ modules });
shallowMount(GeospatialMap, { localVue, store });
it("when component created, initializes the map view", async () => {
expect(modules.mapViewModule.actions.initializeMapView).toHaveBeenCalled();
});
});
简单地说...... jest.fn 说它没有在控制台中调用......
expect(jest.fn()).toHaveBeenCalled()
Expected number of calls: >= 1
Received number of calls: 0
我不确定哪里出了问题。我不是在嘲笑模块操作吗?
我只是想测试初始化这个组件时是否调用了 Vuex 操作。
【问题讨论】:
标签: vue.js jestjs vuex vue-test-utils vuex-modules