I have a component containing 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 checks...
onSelectLink (buttonName) {
buttonName === 'termsAndConditions'
? this.logButtonClick(ANALYTICS.TERMS_AND_CONDITIONS)
: this.logButtonClick(ANALYTICS.PRIVACY_POLICY)
}
Below are details of 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)
})
Some questions I have:
Are these tests calling onSelectLink() function twice each?
Is it necessary to use "wrapper.find('a').trigger('click')" in the test cases?
Do you think this is a strong test for ensuring onSelectLink() takes the correct argument?
Even if I remove "wrapper.find('a').trigger('click')" from the test, it still passes. I thought simulating the DOM event was important before calling the handler.