I have a fairly simple VueJS component called "Reveal"; it hides a message until somebody clicks on it, like so:
<template>
<div>
<div v-show="!isShowing">
<span v-on:click="show">ANSWER (Click to reveal)</span>
</div>
<div v-show="isShowing">
<span v-on:click="hide">{{ message }} (Click to hide)</span>
</div>
</div>
</template>
<script>
export default {
props: ['message'],
data() {
return {
isShowing: false
};
},
methods: {
show: function() {
this.isShowing = true;
},
hide: function() {
this.isShowing = false;
}
}
}
</script>
Now I'm trying to test it with the following Reveal.spec.js, using Mocha+Chai, like so:
import { expect } from 'chai'
import { shallowMount } from '@vue/test-utils'
import Reveal from '@/components/Reveal.vue'
describe('Reveal.vue', () => {
it('doesn\'t reveal the message when instantiated', () => {
const msg = 'secret message'
const wrapper = shallowMount(Reveal, {
propsData: {
message: msg
}
})
const spans = wrapper.findAll('span')
expect(spans.length).to.equal(2);
expect(spans.at(0).text()).to.include("ANSWER");
expect(spans.at(0).isVisible()).to.be.true;
expect(spans.at(1).text()).to.include(msg);
expect(spans.at(1).isVisible()).to.be.false;
})
it('reveals the message when clicked', () => {
const msg = 'secret message'
const wrapper = shallowMount(Reveal, {
propsData: {
message: msg
}
})
wrapper.trigger('click')
const spans = wrapper.findAll('span')
expect(spans.length).to.equal(2);
expect(spans.at(0).text()).to.include("ANSWER");
expect(spans.at(0).isVisible()).to.be.false;
expect(spans.at(1).text()).to.include(msg);
expect(spans.at(1).isVisible()).to.be.true;
})
})
My problem is that the call to wrapper.trigger() doesn't seem to actually trigger the click--or, perhaps more accurately, the "v-show" doesn't seem to be accurately reflected in the resulting DOM nodes, since the first span appears to remain visible even though after the click the isShowing boolean should be flipping its value.
I'm absolutely certain I'm misunderstanding what's going on here, because something this simple should be blindingly straightforward. Where am I going wrong?
click()on it, works every time.wrapper.find('button').click()should work.