8

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 ??

1 Answer 1

9

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;
    }
  }
})
Sign up to request clarification or add additional context in comments.

2 Comments

Add namespaced: true to the mock store for namespacing
What if I use ...mapGetters({ loading: 'customers/allLoading' } does "loading" need to be mocked separately? Doesnt seem very intuitive

Your Answer

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