如何为bootstrap-vue模式编写测试用例?

时间:2018-04-27 08:23:52

标签: javascript vue.js karma-jasmine bootstrap-vue

我想测试bootstrap vue模式显示。在项目页面中,当点击按钮时,模式是通过toggleModal方法显示/隐藏。所以模态display样式更改为none or '',反之亦然。在karma测试中,我想用所调用的方法测试该显示,但是它仅更改了数据而且样式属性没有改变,所以我在测试用例中失败了......

  

vm。$ nextTick(()=> {
    期待(modal.style.display).toBe( '')
   })

如何正确实现bootstrap-vue模态显示测试用例?

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 个答案:

答案 0 :(得分:0)

打开浏览器控制台,然后检查style.display是否为Bootstrap-Vue模式,当hide()时值为,值为阻止!重要当show()时。

所以将expect(modal.style.display).toBe('')更改为expect(modal.style.display).toBe('block !important')

我不知道 Karma ,可能存在一种用法,例如expect(modal.style.display).toBe((item)=>{return item!=='none'})

答案 1 :(得分:0)

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('')
 })
})