Jest Testing Vue-multiselect

问题描述 投票:0回答:1

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

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

目标:

  1. 单击多选下拉列表

  2. 单击选项

  3. 关闭下拉列表,将提交选择内容

EditUser.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>

editUserTest.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: '[email protected]',
                        manager_email: '[email protected]',
                        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);
    });

任何帮助将不胜感激!

谢谢!

javascript vue.js jestjs sinon
1个回答
0
投票

您尝试过使用:

let multiselect = wrapper.find('#multiselectDiv');
multiselect.vm.$emit('open');
© www.soinside.com 2019 - 2024. All rights reserved.