1
votes

I want to test bootstrap vue modal displaying . In project page , modal is show/hide by toggleModal method when click button .So modal display style is changed to none or '' vice versa. In karma test , I want to test that display with method called , but its changed data only and style property is not changing so I got failed in test case ...

vm.$nextTick(() => {
     expect(modal.style.display).toBe('')
})

How can I acheive bootstrap-vue modal display test case correctly !?

index.vue

<div id="example">
  <button @click="toggleModal">Show</button>
  <b-modal v-model="show" centered size="lg" title="確認">
  <h1>Modal Test</h1>
  <button @click="toggleModal">Hide</button>
  </b-modal>
</div>
var vm = new Vue({
  el: '#example',
  data: {
    show: false,
  },
  methods: {
   toggleModal: function () {
     this.show = !this.show
   }
  }
})

index.spec.js

import BootstrapVue from 'bootstrap-vue/dist/bootstrap-vue.esm'
import Vue from 'vue'
import Index from '~/components/index'
// Mock our router, store and nuxt-link
import Vuex from 'vuex'
// import NuxtLink from '~/.nuxt/components/nuxt-link'
import VueRouter from 'vue-router'

Vue.config.productionTip = false
Vue.use(BootstrapVue)
Vue.use(VueRouter)
Vue.use(Vuex)

describe('/components/index.vue', () => {
  let vm

  beforeEach(() => {
    const Constructor = Vue.extend(Index)
    vm = new Constructor().$mount()
  })

  it('check modal method', () => {
     const modal = vm.$el.querySelector('.modal')
     expect(modal.style.display).toBe('none')
     modal.toggleModal()
     vm.$nextTick(() => {
       expect(modal.style.display).toBe('') //expect to pass , but fail
     })
  })
})
2
@Ferrybig Sorry that my typing error on question ! I fixed now - David Jaw Hpan

2 Answers

0
votes

Open your browser console, then check style.display for the Bootstrap-Vue Modal, the value is none when hide(), the value is block !important when show().

so change expect(modal.style.display).toBe('') to expect(modal.style.display).toBe('block !important')

or

I don't know Karma, probably exists one usage like expect(modal.style.display).toBe((item)=>{return item!=='none'})

0
votes
it('check modal method', () => {
 const fm = new Vue(Index).$mount()
 const modal = fm.$el.querySelector('.modal')
 expect(modal.style.display).toBe('none')
 modal.toggleModal()
 fm.$nextTick(() => {
   expect(modal.style.display).toBe('')
 })
})