Vue 单元测试如何测试mapState
发布于 3 年前 作者 banyungong 956 次浏览 来自 问答
粉丝福利 : 关注VUE中文社区公众号,回复视频领取粉丝福利

按照官网给出的实例,自己做了测试,但是还是说找不到mapState中的变量,不知道大家是如何解决的?
hello.vue

<template>
  <div class="hello">
    <p>{{submitLocationTotal}}</p>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  data() {
    return {};
  },
  methods: {},
  beforeCreate() {},
  mounted() {},
  watch: {},
  computed: {
    ...mapState({
      submitLocationTotal: state => state.claim.submitLocationTotal
    })
  },
  components: {}
};
</script>

<style lang='less'>
</style>

hello.spec.js

// import Vue from 'vue';
import hello from '@/hello.vue';
import Vuex from 'vuex';
import {
    shallowMount,
    createLocalVue
} from '@vue/test-utils'
const localVue = createLocalVue()
localVue.use(Vuex)

describe('hello', () => {
    let store


    // beforeEach(() => {
    //     store = new Vuex.Store({
    //         state: {
    //             submitLocationTotal: 123
    //         },
    //     })
    // })
    it('test value...', () => {
        const wrapper = shallowMount(hello, {
            mocks: {
                $store: {
                    state: {
                        submitLocationTotal: [{
                            name: 123
                        }]
                    }
                }
            },
            localVue
        })
    })
})
回到顶部