Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button Click Test Failing with Jest / VueJS

I'm trying to initiate a button click on a image, but when running the test, it keeps erroring out with find did not return cal-modal, cannot call trigger() on empty Wrapper

What am I doing wrong? Very very simplified code below:

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>

TEST FILE

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')
  })
like image 954
SteveV Avatar asked Jan 26 '23 20:01

SteveV


1 Answers

Try using a ref selector or an id selector. Like this ..

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

Or

wrapper.find('#cal-modal')

like image 184
Husam Ibrahim Avatar answered Jan 30 '23 08:01

Husam Ibrahim