简体   繁体   English

jest mock vuex useStore() with vue 3 composition api

[英]jest mock vuex useStore() with vue 3 composition api

I'm trying to unit test a component where you click a button which should then call store.dispatch('favoritesState/deleteFavorite').我正在尝试对一个组件进行单元测试,您可以在其中单击一个按钮,然后该按钮应该调用 store.dispatch('favoritesState/deleteFavorite')。

This action then calls an api and does it's thing.然后此操作调用 api 并完成它的事情。 I don't want to test the implementation of the vuex store, just that the vuex action is called when you click the button in the component.我不想测试 vuex store 的实现,只是当你点击组件中的按钮时调用 vuex 动作。

The Component looks like this组件看起来像这样

<template>
    <ion-item :id="favorite.key">
        <ion-thumbnail class="clickable-item remove-favorite-item" @click="removeFavorite()" slot="end" id="favorite-star-thumbnail">           
        </ion-thumbnail>
    </ion-item>
</template>

import {useStore} from "@/store";
export default defineComponent({
     setup(props) {
        const store = useStore();
    
        function removeFavorite() {
            store.dispatch("favoritesState/deleteFavorite", props.item.id);
        }

        return {
            removeFavorite,
        }
     }
});

The jest test笑话测试

import {store} from "@/store";

test(`${index}) Test remove favorite for : ${mockItemObj.kind}`, async () => {

    const wrapper = mount(FavoriteItem, {
        propsData: {
            favorite: mockItemObj
        },
        global: {
            plugins: [store]
        }
    });
    const spyDispatch = jest.spyOn(store, 'dispatch').mockImplementation();

    await wrapper.find('.remove-favorite-item').trigger('click');
    expect(spyDispatch).toHaveBeenCalledTimes(1);
});

I have tried different solutions with the same outcome.我尝试了不同的解决方案,但结果相同。 Whenever the "trigger('click')" is run it throws this error:每当运行“触发器('点击')”时,它都会抛出此错误:

Cannot read properties of undefined (reading 'dispatch') TypeError: Cannot read properties of undefined (reading 'dispatch')无法读取未定义的属性(读取“调度”)类型错误:无法读取未定义的属性(读取“调度”)

The project is written in vue3 with typescript using composition API and vuex4该项目使用组合 API 和 vuex4 用 typescript 用 vue3 编写

I found a solution to my problem.我找到了解决问题的办法。 This is the solution I ended up with.这是我最终得到的解决方案。

favorite.spec.ts最喜欢的.spec.ts

import {key} from '@/store';

let storeMock: any;

beforeEach(async () => {
    storeMock = createStore({});
});

test(`Should remove favorite`, async () => {

        const wrapper = mount(Component, {
            propsData: {
                item: mockItemObj
            },
            global: {
                plugins: [[storeMock, key]],
            }
        });
        const spyDispatch = jest.spyOn(storeMock, 'dispatch').mockImplementation();

        await wrapper.find('.remove-favorite-item').trigger('click');
        expect(spyDispatch).toHaveBeenCalledTimes(1);
        expect(spyDispatch).toHaveBeenCalledWith("favoritesState/deleteFavorite", favoriteId);
    });

This is the Component method:这是组件方法:

  setup(props) {
    const store = useStore();
   
    function removeFavorite() {
        store.dispatch("favoritesState/deleteFavorite", favoriteId);
    }

    return {
        removeFavorite
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM