5

I'm currently trying to mock an action from a store's module. I can't seem to properly stub it, as I continue to get a message in my unit tests that says:

[vuex] unknown action type: moduleA/filterData

Here is a simplified version of the component under test:

Item.vue

<template>
    <li class="list-item"
    @click="toggleActive()">
        {{ itemName }}
    </li>
</template>

<script>
import store from '../store'

export default {
    name: 'item',
    props: {
        itemName: {
            type: String
        }
    },
    data () {
        return {
            store,
            isActive: false
        }
    },

    methods: {
        toggleActive () {
            this.isActive = !this.isActive;
            this.$store.dispatch('moduleA/filterData', { name: itemName } );
        }
    }

}
</script>

store.js

import Vue from 'vue'
import Vuex from 'vuex'
import moduleA from './modules/moduleA'

Vue.use(Vuex)



const store = new Vuex.Store({
    modules: {
        moduleA
    }
});

export default store;

moduleA.js

/* imports */

const state = {
    /* state */
}

const mutations = {
    /* ... */
}

const actions = {

    filterData({ state, commit }, payload) {
        /* filter data and commit mutations */
    }
}

const getters = {
    /* getters */
}

export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}

Item.spec.js

import Vue from 'vue'
import { mount } from '@vue/test-utils'
import { expect } from 'chai'
import sinon from 'sinon'
import Item from '../src/components/Item.vue'
import Vuex from 'vuex'

Vue.use(Vuex);

describe('Item.vue', () => {
    let componentProps = {};
    let wrapper;
    let actions;
    let store;
    beforeEach(() => {
        let name = 'Item Name';
        // mock vuex action on click
        actions = {
            filterData: sinon.stub()
        }

        let moduleA = {
            state: {},
            actions
        }

        store = new Vuex.Store({
            modules: {
                moduleA
            }
        });


        componentProps.itemName = name;
        wrapper = mount(Item, {
            store: store,
            propsData: componentProps
        });
    })

    it('Has a root element of list-item', () => {
        expect(wrapper.is('.list-item')).to.equal(true);
    })

    it('Item getting prop name', () => {
        expect(wrapper.text()).to.equal('Item Name');
    })

    it('Item is not active on load', () => {
        expect(wrapper.vm.$data.isActive).to.equal(false);
    })

    it('Item is active after click', () => {
        wrapper.trigger('click');
        expect(wrapper.vm.$data.isActive).to.equal(true);

    })

    it('Item is not active after two clicks', () => {
        wrapper.trigger('click');
        wrapper.trigger('click');
        expect(wrapper.vm.$data.isActive).to.equal(false);
    })
})

This isn't causing my tests to fail, but I've been unable to find out how to properly mock/stub module actions from Vuex. Any help is appreciated.

1 Answer 1

14

So I've looked into this, and it turns out that I wasn't defining that my store within my test was namespaced, hence it wasn't recognizing my action:

beforeEach(() => {
    let name = 'Item Name';
    // mock vuex action on click
    actions = {
        filterData: sinon.stub()
    }

    let moduleA = {
        namespaced: true,
        state: {},
        actions
    }

    store = new Vuex.Store({
        modules: {
            moduleA
        }
    });


    componentProps.itemName = name;
    wrapper = mount(Item, {
        store: store,
        propsData: componentProps
    });
})

After including this my errors went away.

1
  • 1
    Thank you for this. May I ask how would you access the module action if you are trying to test whether it has been called? I can see that wrapper.vm.$store._actions is available but _actions includes moduleA/filterData and clearly that can't be called
    – bba278
    Nov 22, 2019 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.