如何在nuxt.js中编写vue bootstrap的$bvModal.msgBoxConfirm的测试用例

Posted

技术标签:

【中文标题】如何在nuxt.js中编写vue bootstrap的$bvModal.msgBoxConfirm的测试用例【英文标题】:How to write testcase of $bvModal.msgBoxConfirm of vuebootstrap in nuxt.js 【发布时间】:2020-11-28 02:59:22 【问题描述】:

如何为此组件编写测试用例,如何为此编写单元用例,附加到 dom 现在可能在这种情况下工作,

如何为此组件编写测试用例,如何为此编写单元用例,附加到 dom 现在可能在这种情况下工作,

<template>
  <div>
    <div class="mb-2">
     <b-button @click="showMsgBoxOne">Simple msgBoxConfirm</b-button>
     Return value:  String(boxOne) 
    </div>
    <div class="mb-1">
     <b-button @click="showMsgBoxTwo">msgBoxConfirm with options</b-button>
     Return value:  String(boxTwo) 
    </div>
  </div>
</template>

<script>
  export default 
    data() 
      return 
        boxOne: '',
        boxTwo: ''
      
    ,
    methods: 
      showMsgBoxOne() 
        this.boxOne = ''
        this.$bvModal.msgBoxConfirm('Are you sure?')
          .then(value => 
            this.boxOne = value
          )
          .catch(err => 
            // An error occurred
          )
      ,
      showMsgBoxTwo() 
        this.boxTwo = ''
        this.$bvModal.msgBoxConfirm('Please confirm that you want to delete everything.', 
          title: 'Please Confirm',
          size: 'sm',
          buttonSize: 'sm',
          okVariant: 'danger',
          okTitle: 'YES',
          cancelTitle: 'NO',
          footerClass: 'p-2',
          hideHeaderClose: false,
          centered: true
        )
          .then(value => 
            this.boxTwo = value
          )
          .catch(err => 
            // An error occurred
          )
      
    
  
</script>

【问题讨论】:

【参考方案1】:
async showMsgBoxTwo() 
    this.boxTwo = ''
    try 
        const res = await this.$bvModal.msgBoxConfirm('Please confirm that you want to delete everything.', 
            title: 'Please Confirm',
            size: 'sm',
            buttonSize: 'sm',
            okVariant: 'danger',
            okTitle: 'YES',
            cancelTitle: 'NO',
            footerClass: 'p-2',
            hideHeaderClose: false,
            centered: true
        )
        this.boxTwo = res
     catch (e) 
        // error
    

然后

it('test', () => 
    const wrapper = shallowMount(Component, 
        store,
        localVue,
        propsData: ,
    )
    const spy = jest.spyOn(wrapper.vm.$bvModal, 'msgBoxConfirm')
    spy.mockImplementation(() => Promise.resolve(some value))
    wrapper.vm.showMsgBoxTwo()
    wrapper.vm.$nextTick(() => 
        expect(spy).toHaveBeenCalled()
    )
)

【讨论】:

以上是关于如何在nuxt.js中编写vue bootstrap的$bvModal.msgBoxConfirm的测试用例的主要内容,如果未能解决你的问题,请参考以下文章

如何在 nuxt.js 中编写全局路由器功能

Vue.js 和 Nuxt.js

如何将 BootstrapVue 在 Nuxt.js 中使用 Purgecss 所需的所有内容列入白名单

如何在 nuxt.js 中挑选 bootstrap-vue.js 模块?

如何在 nuxt.js (vue.js) 中配置动态 og 标签?

如何在 Nuxt.js (Vue.js) 中使用 vue-infinite-loading