I have a component with an anchor tag with an associated handler, onSelectLink().
<a
:href="$t('footer.termsUrl')"
target="_blank"
name="termsAndConditions"
@click="onSelectLink($event.target.name)">{{ $t('terms') }}</a>
The onSelectLink() function...
onSelectLink (buttonName) {
buttonName === 'termsAndConditions'
? this.logButtonClick(ANALYTICS.TERMS_AND_CONDITIONS)
: this.logButtonClick(ANALYTICS.PRIVACY_POLICY)
}
My unit test
describe('onSelectLink()', () => {
it('[positive] should track analytics when user selects `Terms and Conditions` link', () => {
const buttonName = 'termsAndConditions'
jest.spyOn(wrapper.vm, 'onSelectLink')
wrapper.find('a').trigger('click')
wrapper.vm.onSelectLink(buttonName)
expect(wrapper.vm.logButtonClick).toHaveBeenCalledWith(ANALYTICS.TERMS_AND_CONDITIONS)
})
it('[positive] should track analytics when user selects `Privacy Policy` link', () => {
const buttonName = 'privacyPolicy'
jest.spyOn(wrapper.vm, 'onSelectLink')
wrapper.find('a').trigger('click')
wrapper.vm.onSelectLink(buttonName)
expect(wrapper.vm.logButtonClick).toHaveBeenCalledWith(ANALYTICS.PRIVACY_POLICY)
})
My questions are:
Are these tests each invoking onSelectLink() twice?
Do I actually need to use "wrapper.find('a').trigger('click')"
Is this a good/robust test to make sure onSelectLink() is called with right argument?
When I comment out "wrapper.find('a').trigger('click')" the test still seem to pass. I thought I had to trigger the DOM event and then call the handler.