I have the following Vue component:
<template>
<div>
<div>
<year-btn @click="incrementYear"
ref="increment-btn"/>
</div>
</div>
</template>
<script>
import yearBtn from "@/components/formInputs/yearBtn.vue";
export default {
components: {
"year-btn": yearBtn,
},
data() {
return {
monthAndYear: {
year: 2000,
},
};
},
methods: {
incrementYear() {
this.monthAndYear.year++;
},
},
};
</script>
And I have the following Vue unit test.
it('test', async () => {
wrapper = factory();
wrapper.setData({
monthAndYear: {
year: 2020,
},
});
wrapper.find({ ref: "increment-btn" }).trigger("click");
await wrapper.vm.$nextTick();
result = wrapper.vm.monthAndYear.year;
expect(result).toEqual(2021);
});
Unfortunately, the above test fails because the click event on the 'year-btn' component fails to be triggered by Vue Test Utils. I know this because, if I replace my template with the following code the test passes.
<template>
<div>
<div>
<button @click="incrementYear"
ref="increment-btn"></button>
</div>
</div>
</template>
My question is, how do I trigger a "click" event on a child component (year-btn) that my component is using?
Below is what the year-btn component looks like:
<template>
<button @click="$emit('click')">
Click me
</button>
</template>