Search code examples
vue.jsbootstrap-4bootstrap-modalbootstrap-vuevue-test-utils

Cannot find button in bootstrap-vue modal (b-modal) int test


I want to test modal behavior in my test case.

  1. open modal by button on page (isVisible false=> true)
  2. close modal by button on modal (isVisible trye=> false)

First step is ok, but 2nd step is failed. I can find modal by ref, but I can't find the button on modal.

Home.vue

<template>
  <div class="home">
    <b-button id='button-open' @click="open">open</b-button>
    <b-modal
      ref="modal-ref"
      hide-footer
    >
      <b-button id='button-close' @click="close">close</b-button>
    </b-modal>
  </div>
</template>


<script>
export default {
  methods: {
    open() {
      console.log('open');
      this.$refs['modal-ref'].show();
    },
    close() {
      console.log('ok');
      this.$refs['modal-ref'].hide();
    },
  },
};
</script>

home.spec.js

import { expect } from 'chai';
import { createLocalVue, mount } from '@vue/test-utils';
import BootstrapVue from 'bootstrap-vue';
import Home from '@/views/Home.vue';

describe('Boostrap Modal', () => {
  it('open and close modal', async () => {
    const localVue = createLocalVue();
    localVue.use(BootstrapVue);
    const wrapper = mount(Home, {
      localVue,
      attachToDocument: true,
    });

    const open = wrapper.find('#button-open');
    const modal = wrapper.find({ ref: 'modal-ref' });
    expect(modal.vm.isVisible).to.equal(false);
    open.trigger('click');
    await wrapper.vm.$nextTick();
    console.log(modal.exists());
    expect(modal.vm.isVisible).to.equal(true);

    // find it from modal
    const close = modal.find('#button-close');
    expect(close.exists()).to.equal(true);
    close.trigger('click');

    await wrapper.vm.$nextTick();
    expect(modal.vm.isVisible).to.equal(false);
    wrapper.destroy();
  });
});

I cannot figure out why close-button cannot be found. (close.exists() => false)


Solution

  • Modal's are portalled by default to be appended to the <body> element. and will not be inside your wrapper when shown.

    You need to query the document to find your button:

    import { expect } from 'chai';
    import { createLocalVue, mount, createWrapper } from '@vue/test-utils';
    import BootstrapVue from 'bootstrap-vue';
    import Home from '@/views/Home.vue';
    
    describe('Boostrap Modal', () => {
      it('open and close modal', async () => {
        const localVue = createLocalVue();
        localVue.use(BootstrapVue);
        const wrapper = mount(Home, {
          localVue,
          attachToDocument: true,
        });
    
        const open = wrapper.find('#button-open');
        const modal = wrapper.find({ ref: 'modal-ref' });
        expect(modal.vm.isVisible).to.equal(false);
        open.trigger('click');
        await wrapper.vm.$nextTick();
        await wrapper.vm.$nextTick();
        console.log(modal.exists());
        expect(modal.vm.isVisible).to.equal(true);
    
        // find it from modal close button in the document
        const closeElement = document.getElementById('button-close');
        expect(closeElement).toBeDefined();
        const closeWrapper = createWrapper(closeElement);
        expect(closeWrapper.exists()).to.equal(true);
        expect(closeWrapper.is('button')).toBe(true);
        closeWrapper.trigger('click');
    
        await wrapper.vm.$nextTick();
        await wrapper.vm.$nextTick();
        expect(modal.vm.isVisible).to.equal(false);
        wrapper.destroy();
      });
    });