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

1

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

Gascon answered 2/10, 2018 at 17:6 Comment(0)
L
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;
    }
  }
})
Leisured answered 3/10, 2018 at 3:5 Comment(2)
Add namespaced: true to the mock store for namespacingEph
What if I use ...mapGetters({ loading: 'customers/allLoading' } does "loading" need to be mocked separately? Doesnt seem very intuitiveSilurian

© 2022 - 2024 — McMap. All rights reserved.