Commit b72848cb authored by Lukas Eipert's avatar Lukas Eipert

Fix emitting of events in jest

Some of the internal changes of jsdom seem to have an effect on events.
Especially with click events on links. These refactors those events to
be triggered by Vue.

In other cases we were manually dispatching events, when it is easier to
just trigger an event directly.
parent 69a528da
...@@ -63,7 +63,6 @@ describe('PackagesApp', () => { ...@@ -63,7 +63,6 @@ describe('PackagesApp', () => {
stubs: { stubs: {
...stubChildren(PackagesApp), ...stubChildren(PackagesApp),
GlDeprecatedButton: false, GlDeprecatedButton: false,
GlLink: false,
GlModal: false, GlModal: false,
GlTab: false, GlTab: false,
GlTabs: false, GlTabs: false,
...@@ -278,7 +277,8 @@ describe('PackagesApp', () => { ...@@ -278,7 +277,8 @@ describe('PackagesApp', () => {
it(`file download link call event with ${TrackingActions.PULL_PACKAGE}`, () => { it(`file download link call event with ${TrackingActions.PULL_PACKAGE}`, () => {
createComponent({ packageEntity: conanPackage }); createComponent({ packageEntity: conanPackage });
firstFileDownloadLink().trigger('click');
firstFileDownloadLink().vm.$emit('click');
expect(eventSpy).toHaveBeenCalledWith( expect(eventSpy).toHaveBeenCalledWith(
category, category,
TrackingActions.PULL_PACKAGE, TrackingActions.PULL_PACKAGE,
......
...@@ -56,6 +56,7 @@ describe('packages_coming_soon', () => { ...@@ -56,6 +56,7 @@ describe('packages_coming_soon', () => {
}, },
stubs: { stubs: {
ApolloQuery, ApolloQuery,
GlLink: true,
}, },
mocks: { mocks: {
$apolloData, $apolloData,
...@@ -115,7 +116,7 @@ describe('packages_coming_soon', () => { ...@@ -115,7 +116,7 @@ describe('packages_coming_soon', () => {
it('tracks when an issue title link is clicked', () => { it('tracks when an issue title link is clicked', () => {
eventSpy.mockClear(); eventSpy.mockClear();
findIssueTitleLink().trigger('click'); findIssueTitleLink().vm.$emit('click');
expect(eventSpy).toHaveBeenCalledWith(undefined, TrackingActions.COMING_SOON_LIST, { expect(eventSpy).toHaveBeenCalledWith(undefined, TrackingActions.COMING_SOON_LIST, {
label: firstIssue.title, label: firstIssue.title,
...@@ -126,7 +127,7 @@ describe('packages_coming_soon', () => { ...@@ -126,7 +127,7 @@ describe('packages_coming_soon', () => {
it('tracks when an issue id link is clicked', () => { it('tracks when an issue id link is clicked', () => {
eventSpy.mockClear(); eventSpy.mockClear();
findIssueIdLink().trigger('click'); findIssueIdLink().vm.$emit('click');
expect(eventSpy).toHaveBeenCalledWith(undefined, TrackingActions.COMING_SOON_LIST, { expect(eventSpy).toHaveBeenCalledWith(undefined, TrackingActions.COMING_SOON_LIST, {
label: firstIssue.title, label: firstIssue.title,
......
...@@ -121,11 +121,6 @@ describe('Tracking', () => { ...@@ -121,11 +121,6 @@ describe('Tracking', () => {
describe('tracking interface events', () => { describe('tracking interface events', () => {
let eventSpy; let eventSpy;
const trigger = (selector, eventName = 'click') => {
const event = new Event(eventName, { bubbles: true });
document.querySelector(selector).dispatchEvent(event);
};
beforeEach(() => { beforeEach(() => {
eventSpy = jest.spyOn(Tracking, 'event'); eventSpy = jest.spyOn(Tracking, 'event');
Tracking.bindDocument('_category_'); // only happens once Tracking.bindDocument('_category_'); // only happens once
...@@ -140,7 +135,7 @@ describe('Tracking', () => { ...@@ -140,7 +135,7 @@ describe('Tracking', () => {
}); });
it('binds to clicks on elements matching [data-track-event]', () => { it('binds to clicks on elements matching [data-track-event]', () => {
trigger('[data-track-event="click_input1"]'); document.querySelector('[data-track-event="click_input1"]').click();
expect(eventSpy).toHaveBeenCalledWith('_category_', 'click_input1', { expect(eventSpy).toHaveBeenCalledWith('_category_', 'click_input1', {
label: '_label_', label: '_label_',
...@@ -149,13 +144,13 @@ describe('Tracking', () => { ...@@ -149,13 +144,13 @@ describe('Tracking', () => {
}); });
it('does not bind to clicks on elements without [data-track-event]', () => { it('does not bind to clicks on elements without [data-track-event]', () => {
trigger('[data-track-eventbogus="click_bogusinput"]'); document.querySelector('[data-track-eventbogus="click_bogusinput"]').click();
expect(eventSpy).not.toHaveBeenCalled(); expect(eventSpy).not.toHaveBeenCalled();
}); });
it('allows value override with the data-track-value attribute', () => { it('allows value override with the data-track-value attribute', () => {
trigger('[data-track-event="click_input2"]'); document.querySelector('[data-track-event="click_input2"]').click();
expect(eventSpy).toHaveBeenCalledWith('_category_', 'click_input2', { expect(eventSpy).toHaveBeenCalledWith('_category_', 'click_input2', {
value: '_value_override_', value: '_value_override_',
...@@ -163,13 +158,15 @@ describe('Tracking', () => { ...@@ -163,13 +158,15 @@ describe('Tracking', () => {
}); });
it('handles checkbox values correctly', () => { it('handles checkbox values correctly', () => {
trigger('[data-track-event="toggle_checkbox"]'); // checking const checkbox = document.querySelector('[data-track-event="toggle_checkbox"]');
checkbox.click(); // unchecking
expect(eventSpy).toHaveBeenCalledWith('_category_', 'toggle_checkbox', { expect(eventSpy).toHaveBeenCalledWith('_category_', 'toggle_checkbox', {
value: false, value: false,
}); });
trigger('[data-track-event="toggle_checkbox"]'); // unchecking checkbox.click(); // checking
expect(eventSpy).toHaveBeenCalledWith('_category_', 'toggle_checkbox', { expect(eventSpy).toHaveBeenCalledWith('_category_', 'toggle_checkbox', {
value: '_value_', value: '_value_',
...@@ -177,17 +174,19 @@ describe('Tracking', () => { ...@@ -177,17 +174,19 @@ describe('Tracking', () => {
}); });
it('handles bootstrap dropdowns', () => { it('handles bootstrap dropdowns', () => {
trigger('[data-track-event="toggle_dropdown"]', 'show.bs.dropdown'); // showing const dropdown = document.querySelector('[data-track-event="toggle_dropdown"]');
dropdown.dispatchEvent(new Event('show.bs.dropdown', { bubbles: true }));
expect(eventSpy).toHaveBeenCalledWith('_category_', 'toggle_dropdown_show', {}); expect(eventSpy).toHaveBeenCalledWith('_category_', 'toggle_dropdown_show', {});
trigger('[data-track-event="toggle_dropdown"]', 'hide.bs.dropdown'); // hiding dropdown.dispatchEvent(new Event('hide.bs.dropdown', { bubbles: true }));
expect(eventSpy).toHaveBeenCalledWith('_category_', 'toggle_dropdown_hide', {}); expect(eventSpy).toHaveBeenCalledWith('_category_', 'toggle_dropdown_hide', {});
}); });
it('handles nested elements inside an element with tracking', () => { it('handles nested elements inside an element with tracking', () => {
trigger('span.nested', 'click'); document.querySelector('span.nested').click();
expect(eventSpy).toHaveBeenCalledWith('_category_', 'nested_event', {}); expect(eventSpy).toHaveBeenCalledWith('_category_', 'nested_event', {});
}); });
......
...@@ -274,7 +274,7 @@ describe('Renamed Diff Viewer', () => { ...@@ -274,7 +274,7 @@ describe('Renamed Diff Viewer', () => {
expect(link.text()).toEqual(linkText); expect(link.text()).toEqual(linkText);
expect(link.attributes('href')).toEqual(DIFF_FILE_VIEW_PATH); expect(link.attributes('href')).toEqual(DIFF_FILE_VIEW_PATH);
link.trigger('click'); link.vm.$emit('click');
expect(clickMock).toHaveBeenCalled(); expect(clickMock).toHaveBeenCalled();
}, },
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment