【发布时间】:2020-12-08 21:57:12
【问题描述】:
我正在尝试使用 jest 测试 vuex 商店的 getters 方法。但是结果是不确定的。 下面是我正在使用的代码,
storeConfigurations.ts
---------------------
const state = {
update: false
}
const getters = {
[types.GET_UPDATE_VALUE]: (state: { update: any }) => {
return state.update;
}
}
types.ts
--------
export const GET_UPDATE_VALUE = 'GET/update'
尝试在 updatedValue() 方法中测试 store getters 方法,结果未定义。
update.vue
===========
<div class="updateCss">
<customList :items="updatedValue"></customList>
</div>
export default class UpdateTest extends Vue {
get updatedValue() {
const updatedValue = this.$store.getters[
types.GET_UPDATE_VALUE
];
}
update.spec.ts
==============
import Vuex from 'vuex'
import storeConfigurations from '@/store/modules/storeDetails'
const localVue = createLocalVue()
localVue.use(Vuex)
describe('Update Component TestSuite', () => {
beforeEach(() => {
let store
let updateWrapper
store = new Vuex.Store({
modules: {
storeConfigurations: {
state: { update:false},
getters: {
updatedValue: jest.fn()
}
}
}
})
updateWrapper = shallowMount(UpdateTest , {
store,
localVue
}
我在这里尝试从 store 中获取更新的值,但是在模拟 store 时我变得未定义。
【问题讨论】: