Vue-js-modal: Example of unit testing a component using VueModal

Created on 11 Aug 2017  Β·  7Comments  Β·  Source: euvl/vue-js-modal

I try to make some unit / integration tests but I don't know how to do.

I have configured my main.js file exactly as in the doc.

I have a basic template :

<!-- FeedbackModal.vue -->
<template>
  <div class="feedback-modal-wrapper">
    <modal class="feedback-modal" name="feedback-modal" @before-open="beforeOpen" :height="400">

      <!-- modal header-->
      <div class="feedback-modal__header">
        <h2 class="feedback-modal__title">Contacter le support</h2>
      </div>

      <!-- modal body -->
      <div class="feedback-modal__body">
        <form class="feedback-modal__form">
          <p class="feedback-modal__error" v-if="error" aria-live="polite">{{error}}</p>
          <label class="feedback-modal__label" for="feedback-content">Contenu du message :</label>
          <textarea class="feedback-modal__text" id="feedback-content" v-model="feedback"></textarea>
        </form>
      </div>

      <!-- modal footer -->
      <div class="feedback-modal__footer">
        <div class="feedback-modal__actions">
          <button class="feedback-modal__action feedback-modal__action--send" @click="sendFeedback">Envoyer</button>
          <button class="feedback-modal__action feedback-modal__action--cancel" @click="cancelFeedback">Annuler</button>
        </div>
      </div>

    </modal>
  </div>
</template>

<script>
  export default {

    name: 'FeedbackModal',

    data() {
      return {
        feedback: null,
        error: null,
      };
    },

    methods: {
      beforeOpen() {
        this._resetFeedback();
        this._removeError();
      },

      sendFeedback() {
        // some logic...
      },

      cancelFeedback() {
        this._closeModal();
      },

      // some other methods...

    },
  };

</script>

And my current test class (Mocha) :

import Vue from 'vue';
import FeedbackModal from '@/components/FeedbackModal';

Vue.use(VueModal);

describe('Unit | Component | FeedbackModal.vue', () => {
  let component;

  const feedback = 'Dis-moi petit, as-tu dΓ©jΓ  dansΓ© avec le diable au clair de lune ?';
  const consultant = {
    email: '[email protected]',
    name: 'Bruce Wayne',
  };

  beforeEach(() => {

    // given
    const Constructor = Vue.extend(FeedbackModal);

    // when
    component = new Constructor({
      data: {
        feedback,
      },
    }).$mount();

  });

  it('should be named "FeedbackModal"', () => {
    expect(component.$options.name).to.equal('FeedbackModal');
  });

  describe('rendering', () => {
    it('should display the modal', () => {
      return Vue.nextTick().then(() => {
        console.log(component.$el);
        expect(component.$el.querySelector('.feedback-modal')).to.exist;
      });
    });
  });
});

I can not make my test pass to green.

The result of the console.log is :

LOG LOG: <div data-v-605e0740="" class="feedback-modal-wrapper"><!----></div>

So it means the modal was not interpreted / compiled / rendered.

Do you have some clue please ?

Thanks :-)

question

Most helpful comment

seeing the same issue in our tests. We have a component that renders a modal inside it.
In our tests after we mount the component the element has a comment <!----> in place for the particular modal.
Also tried to explicitly show the modal by calling component.$modal.show('modal-name') but it still renders as a comment.
Is there any other way of testing components that have a modal inside it ?

All 7 comments

Hey @jbuget, thanks for using the plugin. If you will look into rendered modal html or source you would notice :data-modal="name" on the modal itself. I think your expect should look this way:

let selector = 'div[data-modal="feedback-modal"]';
let modal = component.$el.querySelector(selector);

expect(modal).to.exist;

I assume the test worked, closing the issue πŸ‘

seeing the same issue in our tests. We have a component that renders a modal inside it.
In our tests after we mount the component the element has a comment <!----> in place for the particular modal.
Also tried to explicitly show the modal by calling component.$modal.show('modal-name') but it still renders as a comment.
Is there any other way of testing components that have a modal inside it ?

@vaishnavi-prabakar have you solved it? I have the same.

Also running into this issue. I'm also seeing <!----> when I console.log(wrapper.html()) triggering the click on the toggle button seems to have no effect.

Also running into this issue. I'm also seeing <!----> when I console.log(wrapper.html()) triggering the click on the toggle button seems to have no effect.

I also faced with this problem. Did you solve it somehow?

Π’ΠΎΠΆΠ΅ ΡΡ‚Π°Π»ΠΊΠΈΠ²Π°ΡŽΡΡŒ с этой ΠΏΡ€ΠΎΠ±Π»Π΅ΠΌΠΎΠΉ. Π― Ρ‚Π°ΠΊΠΆΠ΅ Π²ΠΈΠ΄Π΅Π» , <!---->ΠΊΠΎΠ³Π΄Π° я console.log(wrapper.html())запуская clickΠ½Π° ΠΊΠ½ΠΎΠΏΠΊΡƒ ΠΏΠ΅Ρ€Π΅ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ , каТСтся , Π½Π΅ ΠΈΠΌΠ΅ΡŽΡ‚ Π½ΠΈΠΊΠ°ΠΊΠΎΠ³ΠΎ эффСкта.

Π― Ρ‚ΠΎΠΆΠ΅ столкнулся с этой ΠΏΡ€ΠΎΠ±Π»Π΅ΠΌΠΎΠΉ. Π’Ρ‹ ΠΊΠ°ΠΊ-Ρ‚ΠΎ Ρ€Π΅ΡˆΠΈΠ»ΠΈ?

I understood what the problem was, I had to create an asynchronous function so that the test would wait until the modal window opens.

import { createLocalVue, mount, shallowMount } from "@vue/test-utils";
import Vuex from "vuex";
// dropdwons componens files
import DropDown from "@/components/Module/DropDown/DropDown.vue";
import DropDownState from "@/components/Module/DropDown/DropDownState.js";

const localVue = createLocalVue();
localVue.use(Vuex);
const createStore = () => new Vuex.Store(DropDownState); 
test("Testing opened dropdown popUp", async () => {
    const wrapper = mount(DropDown, {
      store,
      localVue,
    });
    const button = wrapper.find("button");
    await button.trigger("click");
    expect(wrapper.element).toMatchSnapshot();
  });

A modal window has appeared in the snapshot, although I still do not understand why the html elements in the snapshot do not have css classes. Just bare selectors, maybe this is due to the fact that I am using css modules
exports[General testing DropDown component Testing opened dropdown popUp 1] =

Arrow top Π‘ΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²ΠΊΠ° ΠΏΠΎ:
  • популярности
  • ΠΏΠΎ Ρ†Π΅Π½Π΅
  • ΠΏΠΎ Π°Π»Ρ„Π°Π²ΠΈΡ‚Ρƒ

;

Was this page helpful?
0 / 5 - 0 ratings

Related issues

smholsen picture smholsen  Β·  4Comments

MacgyverMartins picture MacgyverMartins  Β·  4Comments

yyh1102 picture yyh1102  Β·  3Comments

whaoran picture whaoran  Β·  3Comments

ptilli picture ptilli  Β·  3Comments