Commit 8dc6d176 authored by Lukas Eipert's avatar Lukas Eipert

Fix EE only karma and jest specs

- Fix nth-child selector weirdness in karma tests
- Refactor alert widget tests to use proper waiting instead of
  Vue.nextTick
- Fix modal selector in review bar test
- Fix jest tests by mocking methods correctly
- Adjust event firing for custom dropdown
parent 58f1442c
...@@ -48,8 +48,8 @@ export default { ...@@ -48,8 +48,8 @@ export default {
isSelected(option) { isSelected(option) {
return this.selection.has(option.id); return this.selection.has(option.id);
}, },
closeDropdown(event) { closeDropdown() {
this.$root.$emit('clicked::link', event); this.$refs.dropdown.$children[0].hide(true);
}, },
}, },
}; };
...@@ -58,7 +58,7 @@ export default { ...@@ -58,7 +58,7 @@ export default {
<template> <template>
<div class="dashboard-filter"> <div class="dashboard-filter">
<strong class="js-name">{{ filter.name }}</strong> <strong class="js-name">{{ filter.name }}</strong>
<gl-dropdown class="d-block mt-1" menu-class="dropdown-extended-height"> <gl-dropdown ref="dropdown" class="d-block mt-1" menu-class="dropdown-extended-height">
<template slot="button-content"> <template slot="button-content">
<span class="text-truncate"> <span class="text-truncate">
{{ selectedOptionText.firstOption }} {{ selectedOptionText.firstOption }}
......
...@@ -12,6 +12,7 @@ describe('custom metrics form fields component', () => { ...@@ -12,6 +12,7 @@ describe('custom metrics form fields component', () => {
const validQueryResponse = { data: { success: true, query: { valid: true, error: '' } } }; const validQueryResponse = { data: { success: true, query: { valid: true, error: '' } } };
const csrfToken = 'mockToken'; const csrfToken = 'mockToken';
const formOperation = 'post'; const formOperation = 'post';
const debouncedValidateQueryMock = jest.fn();
const makeFormData = (data = {}) => ({ const makeFormData = (data = {}) => ({
formData: { formData: {
title: '', title: '',
...@@ -32,6 +33,9 @@ describe('custom metrics form fields component', () => { ...@@ -32,6 +33,9 @@ describe('custom metrics form fields component', () => {
}, },
csrfToken, csrfToken,
sync: false, sync: false,
methods: {
debouncedValidateQuery: debouncedValidateQueryMock,
},
}); });
}; };
...@@ -120,18 +124,14 @@ describe('custom metrics form fields component', () => { ...@@ -120,18 +124,14 @@ describe('custom metrics form fields component', () => {
jest.runAllTimers(); jest.runAllTimers();
}); });
it('checks validity on user input', done => { it('checks validity on user input', () => {
const query = 'changedQuery'; const query = 'changedQuery';
mountComponent(); mountComponent();
const spy = jest.spyOn(component.vm, 'debouncedValidateQuery'); const queryInput = component.find(`input[name="${name}"]`);
const queryInput = getNamedInput(name); queryInput.setValue(query);
queryInput.value = query; queryInput.trigger('input');
queryInput.dispatchEvent(new Event('input'));
expect(debouncedValidateQueryMock).toHaveBeenCalledWith(query);
component.vm.$nextTick(() => {
expect(spy).toHaveBeenCalledWith(query);
done();
});
}); });
describe('when query is invalid', () => { describe('when query is invalid', () => {
......
...@@ -42,7 +42,9 @@ describe('Batch comments review bar component', () => { ...@@ -42,7 +42,9 @@ describe('Batch comments review bar component', () => {
vm.$el.querySelector('.btn.btn-align-content').click(); vm.$el.querySelector('.btn.btn-align-content').click();
vm.$nextTick(() => { vm.$nextTick(() => {
vm.$el.querySelector('.modal .btn-danger').click(); const modal = document.querySelector('#discard-draft-review');
modal.querySelector('.btn-danger').click();
expect(vm.$store.dispatch).toHaveBeenCalled(); expect(vm.$store.dispatch).toHaveBeenCalled();
......
...@@ -17,6 +17,9 @@ describe('LicenseManagementRow', () => { ...@@ -17,6 +17,9 @@ describe('LicenseManagementRow', () => {
let store; let store;
let actions; let actions;
const findNthDropdown = num => [...vm.$el.querySelectorAll('.dropdown-item')][num];
const findNthDropdownIcon = num => findNthDropdown(num).querySelector('svg');
beforeEach(() => { beforeEach(() => {
actions = { actions = {
setLicenseInModal: jasmine.createSpy('setLicenseInModal'), setLicenseInModal: jasmine.createSpy('setLicenseInModal'),
...@@ -60,13 +63,13 @@ describe('LicenseManagementRow', () => { ...@@ -60,13 +63,13 @@ describe('LicenseManagementRow', () => {
describe('template', () => { describe('template', () => {
it('first dropdown element should have a visible icon', () => { it('first dropdown element should have a visible icon', () => {
const firstOption = vm.$el.querySelector('.dropdown-item:nth-child(1) svg'); const firstOption = findNthDropdownIcon(0);
expect(firstOption.classList).toContain(visibleClass); expect(firstOption.classList).toContain(visibleClass);
}); });
it('second dropdown element should have no visible icon', () => { it('second dropdown element should have no visible icon', () => {
const secondOption = vm.$el.querySelector('.dropdown-item:nth-child(2) svg'); const secondOption = findNthDropdownIcon(1);
expect(secondOption.classList).toContain(invisibleClass); expect(secondOption.classList).toContain(invisibleClass);
}); });
...@@ -95,13 +98,13 @@ describe('LicenseManagementRow', () => { ...@@ -95,13 +98,13 @@ describe('LicenseManagementRow', () => {
describe('template', () => { describe('template', () => {
it('first dropdown element should have no visible icon', () => { it('first dropdown element should have no visible icon', () => {
const firstOption = vm.$el.querySelector('.dropdown-item:nth-child(1) svg'); const firstOption = findNthDropdownIcon(0);
expect(firstOption.classList).toContain(invisibleClass); expect(firstOption.classList).toContain(invisibleClass);
}); });
it('second dropdown element should have a visible icon', () => { it('second dropdown element should have a visible icon', () => {
const secondOption = vm.$el.querySelector('.dropdown-item:nth-child(2) svg'); const secondOption = findNthDropdownIcon(1);
expect(secondOption.classList).toContain(visibleClass); expect(secondOption.classList).toContain(visibleClass);
}); });
...@@ -117,14 +120,14 @@ describe('LicenseManagementRow', () => { ...@@ -117,14 +120,14 @@ describe('LicenseManagementRow', () => {
}); });
it('triggering approveLicense by clicking the first dropdown option', () => { it('triggering approveLicense by clicking the first dropdown option', () => {
const linkEl = vm.$el.querySelector('.dropdown-item:nth-child(1)'); const linkEl = findNthDropdown(0);
linkEl.click(); linkEl.click();
expect(actions.approveLicense).toHaveBeenCalled(); expect(actions.approveLicense).toHaveBeenCalled();
}); });
it('triggering approveLicense blacklistLicense by clicking the second dropdown option', () => { it('triggering approveLicense blacklistLicense by clicking the second dropdown option', () => {
const linkEl = vm.$el.querySelector('.dropdown-item:nth-child(2)'); const linkEl = findNthDropdown(1);
linkEl.click(); linkEl.click();
expect(actions.blacklistLicense).toHaveBeenCalled(); expect(actions.blacklistLicense).toHaveBeenCalled();
...@@ -166,12 +169,12 @@ describe('LicenseManagementRow', () => { ...@@ -166,12 +169,12 @@ describe('LicenseManagementRow', () => {
expect(dropdownEl).not.toBeNull(); expect(dropdownEl).not.toBeNull();
const firstOption = dropdownEl.querySelector('.dropdown-item:nth-child(1)'); const firstOption = findNthDropdown(0);
expect(firstOption).not.toBeNull(); expect(firstOption).not.toBeNull();
expect(firstOption.innerText.trim()).toBe('Approved'); expect(firstOption.innerText.trim()).toBe('Approved');
const secondOption = dropdownEl.querySelector('.dropdown-item:nth-child(2)'); const secondOption = findNthDropdown(1);
expect(secondOption).not.toBeNull(); expect(secondOption).not.toBeNull();
expect(secondOption.innerText.trim()).toBe('Blacklisted'); expect(secondOption.innerText.trim()).toBe('Blacklisted');
......
...@@ -2,6 +2,7 @@ import Vue from 'vue'; ...@@ -2,6 +2,7 @@ import Vue from 'vue';
import AlertWidget from 'ee/monitoring/components/alert_widget.vue'; import AlertWidget from 'ee/monitoring/components/alert_widget.vue';
import AlertsService from 'ee/monitoring/services/alerts_service'; import AlertsService from 'ee/monitoring/services/alerts_service';
import mountComponent from 'spec/helpers/vue_mount_component_helper'; import mountComponent from 'spec/helpers/vue_mount_component_helper';
import waitForPromises from 'spec/helpers/wait_for_promises';
describe('AlertWidget', () => { describe('AlertWidget', () => {
let AlertWidgetComponent; let AlertWidgetComponent;
...@@ -184,10 +185,13 @@ describe('AlertWidget', () => { ...@@ -184,10 +185,13 @@ describe('AlertWidget', () => {
vm.$refs.widgetForm.$emit('create', alertParams); vm.$refs.widgetForm.$emit('create', alertParams);
expect(AlertsService.prototype.createAlert).toHaveBeenCalledWith(alertParams); expect(AlertsService.prototype.createAlert).toHaveBeenCalledWith(alertParams);
Vue.nextTick(() => {
expect(vm.isLoading).toEqual(false); waitForPromises()
done(); .then(() => {
}); expect(vm.isLoading).toEqual(false);
done();
})
.catch(done.fail);
}); });
it('updates an alert with an appropriate handler', done => { it('updates an alert with an appropriate handler', done => {
...@@ -195,7 +199,9 @@ describe('AlertWidget', () => { ...@@ -195,7 +199,9 @@ describe('AlertWidget', () => {
const newAlertParams = { operator: '=', threshold: 12 }; const newAlertParams = { operator: '=', threshold: 12 };
spyOn(AlertsService.prototype, 'readAlert').and.returnValue(Promise.resolve(alertParams)); spyOn(AlertsService.prototype, 'readAlert').and.returnValue(Promise.resolve(alertParams));
spyOn(AlertsService.prototype, 'updateAlert').and.returnValue(Promise.resolve({})); spyOn(AlertsService.prototype, 'updateAlert').and.returnValue(
Promise.resolve({ ...alertParams, ...newAlertParams }),
);
vm = mountComponent(AlertWidgetComponent, propsWithAlertData); vm = mountComponent(AlertWidgetComponent, propsWithAlertData);
vm.$on('setAlerts', mockSetAlerts); vm.$on('setAlerts', mockSetAlerts);
...@@ -207,10 +213,12 @@ describe('AlertWidget', () => { ...@@ -207,10 +213,12 @@ describe('AlertWidget', () => {
}); });
expect(AlertsService.prototype.updateAlert).toHaveBeenCalledWith(alertPath, newAlertParams); expect(AlertsService.prototype.updateAlert).toHaveBeenCalledWith(alertPath, newAlertParams);
Vue.nextTick(() => { waitForPromises()
expect(vm.isLoading).toEqual(false); .then(() => {
done(); expect(vm.isLoading).toEqual(false);
}); done();
})
.catch(done.fail);
}); });
it('deletes an alert with an appropriate handler', done => { it('deletes an alert with an appropriate handler', done => {
...@@ -225,10 +233,12 @@ describe('AlertWidget', () => { ...@@ -225,10 +233,12 @@ describe('AlertWidget', () => {
vm.$refs.widgetForm.$emit('delete', { alert: alertPath }); vm.$refs.widgetForm.$emit('delete', { alert: alertPath });
expect(AlertsService.prototype.deleteAlert).toHaveBeenCalledWith(alertPath); expect(AlertsService.prototype.deleteAlert).toHaveBeenCalledWith(alertPath);
Vue.nextTick(() => { waitForPromises()
expect(vm.isLoading).toEqual(false); .then(() => {
expect(vm.alertSummary).toBeFalsy(); expect(vm.isLoading).toEqual(false);
done(); expect(vm.alertSummary).toBeFalsy();
}); done();
})
.catch(done.fail);
}); });
}); });
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