vue.jsmockingtestunit

Vue.js test-utils How to mock getters from module


In my ContactForm component , I have 2 computed mapGetters

computed: {
  ...mapGetters(["language"]),
  ...mapGetters("authentication", ["loading"]),

the first one is defined in my stoe/getters.js

export const language = state => {
 return state.language;
};

the second one is defined in my store/modules/authentication.js

const authentication = {
 namespaced: true,
 getters: {
   user: state => {
     return state.user
   },
   loading: state => {
     return state.loading
 }

},

I am trying to mock my Vuex store , easy for the first one "language",

        export const storeMock = Object.freeze({
      state: {},
      actions: {},
      getters: {
        language: () => { .    // <= FINE
          return "en";
        },
        authentication: { .   // <= . WRONG
          loading: () => {
            return false;
          }
        }
      }
    })

but how should I mock the second one "loading" from the "authentication" module ??


Solution

  • If you console log the store in the app, namespaced getters have a key of namespace/getterName, so I think this should work

    export const storeMock = Object.freeze({
      state: {},
      actions: {},
      namespaced: true,
      getters: {
        language: () => {     // <= FINE
          return "en";
        },
        'authentication/loading' : () => {
           return false;
        }
      }
    })