2020-01-09 04:07:51 -05:00
|
|
|
import { shallowMount } from '@vue/test-utils';
|
2022-01-25 10:12:32 -05:00
|
|
|
import Vue, { nextTick } from 'vue';
|
2019-10-17 20:07:45 -04:00
|
|
|
import Tracking from '~/tracking';
|
|
|
|
import TrackEvent from '~/vue_shared/directives/track_event';
|
|
|
|
|
|
|
|
jest.mock('~/tracking');
|
|
|
|
|
2021-01-19 22:11:13 -05:00
|
|
|
const Component = Vue.component('DummyElement', {
|
2019-10-17 20:07:45 -04:00
|
|
|
directives: {
|
|
|
|
TrackEvent,
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
trackingOptions: null,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
template: '<button id="trackable" v-track-event="trackingOptions"></button>',
|
|
|
|
});
|
|
|
|
|
|
|
|
let wrapper;
|
|
|
|
let button;
|
|
|
|
|
|
|
|
describe('Error Tracking directive', () => {
|
|
|
|
beforeEach(() => {
|
2020-01-10 04:07:49 -05:00
|
|
|
wrapper = shallowMount(Component);
|
2019-10-17 20:07:45 -04:00
|
|
|
button = wrapper.find('#trackable');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not track the event if required arguments are not provided', () => {
|
|
|
|
button.trigger('click');
|
|
|
|
expect(Tracking.event).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-01-25 10:12:32 -05:00
|
|
|
it('should track event on click if tracking info provided', async () => {
|
2019-10-17 20:07:45 -04:00
|
|
|
const trackingOptions = {
|
|
|
|
category: 'Tracking',
|
|
|
|
action: 'click_trackable_btn',
|
|
|
|
label: 'Trackable Info',
|
|
|
|
};
|
|
|
|
|
2021-12-29 10:10:45 -05:00
|
|
|
// setData usage is discouraged. See https://gitlab.com/groups/gitlab-org/-/epics/7330 for details
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
2019-10-17 20:07:45 -04:00
|
|
|
wrapper.setData({ trackingOptions });
|
|
|
|
const { category, action, label, property, value } = trackingOptions;
|
2020-01-01 01:09:01 -05:00
|
|
|
|
2022-01-25 10:12:32 -05:00
|
|
|
await nextTick();
|
|
|
|
button.trigger('click');
|
|
|
|
expect(Tracking.event).toHaveBeenCalledWith(category, action, { label, property, value });
|
2019-10-17 20:07:45 -04:00
|
|
|
});
|
|
|
|
});
|