【问题标题】:Vue.js test-utils How to mock getters from moduleVue.js test-utils 如何从模块中模拟 getter
【发布时间】:2019-03-07 20:42:00
【问题描述】:

在我的 ContactForm 组件中,我有 2 个计算的 mapGetters

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

第一个在我的 stoe/getters.js 中定义

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

第二个是在我的 store/modules/authentication.js 中定义的

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

},

我正在尝试模拟我的 Vuex 商店,对于第一个“语言”来说很容易,

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

但是我应该如何从“身份验证”模块模拟第二个“加载”?

【问题讨论】:

    标签: vue.js mocking testunit


    【解决方案1】:

    如果您在应用程序中通过控制台登录商店,命名空间的 getter 的密钥为 namespace/getterName,所以我认为这应该可以工作

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

    【讨论】:

    • namespaced: true 添加到模拟存储以进行命名空间
    • 如果我使用 ...mapGetters({ loading: 'customers/allLoading' } "加载" 需要单独模拟吗?看起来不是很直观
    猜你喜欢
    • 2020-06-08
    • 2019-02-27
    • 2021-08-20
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 2020-05-03
    • 2019-05-13
    相关资源
    最近更新 更多