gpt4 book ai didi

javascript - Jest 测试 Vue-Multiselect

转载 作者:行者123 更新时间:2023-12-05 03:57:57 27 4
gpt4 key购买 nike

我有一个使用 Jest/Sinon 进行测试的 Vue 应用程序。

目前在测试 vue-multiselect html 元素时遇到问题。我似乎无法点击它并显示选项。我想在单击时观看某些方法的执行,但是由于我似乎无法在该死的东西上注册单击,所以我看不到任何更改:(

目标:

  1. 点击多选下拉菜单

  2. 点击一个选项

  3. 关闭将提交选择的下拉菜单

编辑用户.vue

....
<div class="col2">
<form>
<label for="firstName">First Name: {{editUser.first_name}}</label>
<label for="lastname">Last Name: {{editUser.last_name}}</label>
<label for="email2">Email: {{editUser.id}}</label>
<label for="managerEmail">Manager Email: {{editUser.manager_email}}</label>
<label v-for="role in editUser.role" v-bind:key="role">Current Roles: {{role}}</label>
<label class="typo__label">
<strong>Select Roles OVERWRITES existing roles:</strong>
</label>
<div id="multiselectDiv" :class="{'invalid' : isInvalid}">
<multiselect
v-model="value"
:options="options"
:multiple="true"
:close-on-select="false"
:clear-on-select="false"
:preserve-search="true"
:allow-empty="false"
placeholder="Select All Applicable Roles"
:preselect-first="false"
@open="onTouch()"
@close="submitRoles(value)"
></multiselect>
<label class="typo__label form__label" v-show="isInvalid">Must have at least one value</label>
</div>
<button class="button" @click="removeRoles">Remove all roles</button>
</form>
</div>
....

<script>
import { mapState } from "vuex";
import Multiselect from "vue-multiselect";
const fb = require("../firebaseConfig.js");
import { usersCollection } from "../firebaseConfig";

export default {
computed: {
...mapState(["currentUser", "userProfile", "users", "editUser"]),
isInvalid() {
return this.isTouched && this.value.length === 0;
}
},
methods: {
onTouch() {
this.isTouched = true;
},
submitRoles(value) {
fb.usersCollection
.doc(this.editUser.id)
.update({
role: value
})
.catch(err => {
console.log(err);
});
this.$router.push("/dashboard");
},
removeRoles() {
fb.usersCollection
.doc(this.editUser.id)
.update({
role: []
})
.catch(err => {
console.log(err);
});
this.$router.push("/dashboard");
}
},
components: { Multiselect },
data() {
return {
value: [],
options: ["admin", "sales", "auditor"],
isTouched: false
};
}
};
</script>

编辑UserTest.spec.js

test('OnTouch method triggers on open of select menu', () => {
const wrapper = mount(EditUser, {
store,
localVue,
propsData: {
options: ["admin", "sales", "auditor"],
},
computed: {
editUser() {
return {
first_name: 'Bob',
last_name: 'Calamezo',
id: 'bob@email.com',
manager_email: 'someManager@email.com',
role: ['admin', 'sales'],
};
},
},
});

expect(wrapper.vm.$data.isTouched).toBe(false);
expect(wrapper.find('#multiselectDiv').exists()).toBe(true);
const selectIt = wrapper.find('#multiselectDiv').element;
selectIt.dispatchEvent(new Event('click'));
console.log(wrapper.find('.col2').html());
// expect(wrapper.vm.$data.isTouched).toBe(true);
});

如有任何帮助,我们将不胜感激!

谢谢!

最佳答案

对我有用的是:

import { Multiselect } from 'vue-multiselect';

const multiselect = wrapper.findComponent(Multiselect);
const input = multiselect.find("input");
input.setValue("substring of a label");
input.trigger("keydown.enter");

重要的是,如果您想在列表中查找某些内容,而不是提前知道其固定偏移量,input.setValue 很重要。

关于javascript - Jest 测试 Vue-Multiselect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58293065/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com