Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest Testing Vue-Multiselect

I have a Vue application using Jest/Sinon for testing.

Currently having issues testing the vue-multiselect html element. I can't seem to be able to click on it and show the options. I want to watch some methods get executed as I click but since I can't seem to register a click on the darn thing I don't see any changes :(

Goal:

  1. Click on multiselect drop down

  2. Click on an option

  3. Close dropdown which will submit selection

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);
    });

Any help would be greatly appreciated!

Thanks!

like image 205
Alan Richards Avatar asked Jul 25 '26 11:07

Alan Richards


2 Answers

What worked for me was this:

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");

Critically input.setValue is important if you want to find something in a list, rather than knowing its fixed offset in advance.

like image 115
Glyph Avatar answered Jul 27 '26 02:07

Glyph


Did you try it with:

let multiselect = wrapper.find('#multiselectDiv');
multiselect.vm.$emit('open');
like image 20
DealZg Avatar answered Jul 27 '26 02:07

DealZg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!