Jest / VueJS的按钮单击测试失败

时间:2019-09-12 22:28:31

标签: vue.js jestjs vue-test-utils

我正在尝试在图像上启动按钮单击,但是在运行测试时,它会不断显示find did not return cal-modal, cannot call trigger() on empty Wrapper错误

我在做什么错?下面的代码非常简化:

CALENDAR.VUE

<template>
                <div class="col-xs-6 text-right">
                    <b-img ref='cal-modal' id='cal-modal' class="cal-icon" @click="displayCal" v-b-modal.date-time-modal src="/static/img/ico.png"></b-img>
                </div>

</template>

测试文件

import {createLocalVue, mount} from "@vue/test-utils";
import Calendar from '@/components/Calendar.vue'
import BootstrapVue from "bootstrap-vue";
const localVue = createLocalVue()
localVue.use(BootstrapVue)


  it('display cal', () => {
      const wrapper = mount(Calendar, {localVue});
      wrapper
          .find('cal-modal')
          .trigger('click')
  })

1 个答案:

答案 0 :(得分:1)

尝试使用ref选择器或id选择器。像这样..

wrapper.find({ ref: 'cal-modal' })

wrapper.find('#cal-modal')