And*_*mos 3 javascript vue.js jestjs vuex nuxt.js
我正在Jest尝试vue-test-utils测试子组件是否对$emit父组件中的事件做出反应。
VueJS test-utils 库提供了parentComponent在安装/浅安装组件时传递的选项。
一切都工作正常,除了即使我使用模拟的 Vuex 存储实例化组件,父组件也会抛出一个
类型错误:无法读取未定义的属性“状态”
this.$store.state.something.here在父组件中的一段代码上。
我怎样才能模拟 Vuex 商店呢?
组件安装如下所示:
const wrapper = shallowMount(ChildComponent, {
store,
localVue,
parentComponent: ParentComponent,
mocks: {
$t: msg => msg,
},
});
Run Code Online (Sandbox Code Playgroud)
关于如何解决这个问题有什么想法吗?
小智 5
这可能不是OP问题的完整答案,但由于我在过去的2小时里一直在调试并最终找到了我的问题,我想在这里发布以帮助将来的人。
我试图模拟并安装以下组件:
<template>
<div test="object-list-div">
<h1 test="component-title">{{ objectName }}</h1>
<table class="table">
<thead>
<tr test="table-row-title">
<th scope="col" test="table-column-title" v-for="(value, name, index) in objectData[0]" :key="index">{{ name }}</th>
</tr>
</thead>
<tbody>
<tr test="table-row-data" v-for="(ivalue, iname, i) in objectData" :key="i">
<td test="table-cell-data" v-for="(jvalue, jname, j) in ivalue" :key="j">{{ jvalue }}</td>
</tr>
</tbody>
</table>
</div>
Run Code Online (Sandbox Code Playgroud)
export default {
props: [
'objectName',
'objectData'
],
computed: {
visibleColums() {
return this.$store.state.Config_ShowColumn;
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用以下包装代码
wrapper = shallowMount(ObjectList, {
mocks: {
$store: {
state: {
Config_ShowColumn: [
"Field1",
"Field2",
"Field3",
"Field4",
"Field5",
]
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
我收到了 OP 错误,但就我而言,该组件在创建时需要两个 Prop 。由于它没有收到此信息,因此被卡住了。
这现在正在工作:
import { shallowMount } from "@vue/test-utils";
import { expect } from "chai";
import ObjectList from "@/components/Object-List.vue";
wrapper = shallowMount(ObjectList, {
propsData: {
objectName: "Ticket",
objectData: [
{
Field1: "Field1",
Field2: "Field2",
Field3: "Field3",
Field4: "Field4",
Field5: "Field5",
},
]
},
mocks: {
$store: {
state: {
Config_ShowColumn: [
"Field1",
"Field2",
"Field3",
"Field4",
"Field5",
]
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
希望它能帮助某人。