debian-mirror-gitlab/spec/frontend/operation_settings/components/metrics_settings_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

218 lines
6.5 KiB
JavaScript
Raw Normal View History

2020-10-24 23:57:45 +05:30
import { GlButton, GlLink, GlFormGroup, GlFormInput, GlFormSelect } from '@gitlab/ui';
2021-03-11 19:13:27 +05:30
import { mount, shallowMount } from '@vue/test-utils';
2022-04-04 11:22:00 +05:30
import { nextTick } from 'vue';
2020-01-01 13:55:28 +05:30
import { TEST_HOST } from 'helpers/test_constants';
2021-09-04 01:27:46 +05:30
import createFlash from '~/flash';
2021-03-11 19:13:27 +05:30
import axios from '~/lib/utils/axios_utils';
import { refreshCurrentPage } from '~/lib/utils/url_utility';
import { timezones } from '~/monitoring/format_date';
import DashboardTimezone from '~/operation_settings/components/form_group/dashboard_timezone.vue';
import ExternalDashboard from '~/operation_settings/components/form_group/external_dashboard.vue';
2020-06-23 00:09:42 +05:30
import MetricsSettings from '~/operation_settings/components/metrics_settings.vue';
2019-09-04 21:01:54 +05:30
import store from '~/operation_settings/store';
2019-07-31 22:56:46 +05:30
2019-09-04 21:01:54 +05:30
jest.mock('~/lib/utils/url_utility');
jest.mock('~/flash');
2019-07-31 22:56:46 +05:30
describe('operation settings external dashboard component', () => {
let wrapper;
2020-06-23 00:09:42 +05:30
2019-09-04 21:01:54 +05:30
const operationsSettingsEndpoint = `${TEST_HOST}/mock/ops/settings/endpoint`;
2020-06-23 00:09:42 +05:30
const helpPage = `${TEST_HOST}/help/metrics/page/path`;
2019-09-04 21:01:54 +05:30
const externalDashboardUrl = `http://mock-external-domain.com/external/dashboard/url`;
2020-06-23 00:09:42 +05:30
const dashboardTimezoneSetting = timezones.LOCAL;
2019-09-04 21:01:54 +05:30
const mountComponent = (shallow = true) => {
const config = [
2020-06-23 00:09:42 +05:30
MetricsSettings,
2019-09-04 21:01:54 +05:30
{
store: store({
operationsSettingsEndpoint,
2020-06-23 00:09:42 +05:30
helpPage,
2019-09-04 21:01:54 +05:30
externalDashboardUrl,
2020-06-23 00:09:42 +05:30
dashboardTimezoneSetting,
2019-09-04 21:01:54 +05:30
}),
2020-06-23 00:09:42 +05:30
stubs: {
ExternalDashboard,
DashboardTimezone,
},
2019-07-31 22:56:46 +05:30
},
2019-09-04 21:01:54 +05:30
];
wrapper = shallow ? shallowMount(...config) : mount(...config);
};
2019-09-30 21:07:59 +05:30
beforeEach(() => {
jest.spyOn(axios, 'patch').mockImplementation();
});
2019-09-04 21:01:54 +05:30
afterEach(() => {
if (wrapper.destroy) {
wrapper.destroy();
}
axios.patch.mockReset();
refreshCurrentPage.mockReset();
createFlash.mockReset();
2019-07-31 22:56:46 +05:30
});
it('renders header text', () => {
2019-09-04 21:01:54 +05:30
mountComponent();
2021-09-04 01:27:46 +05:30
expect(wrapper.find('.js-section-header').text()).toBe('Metrics');
2019-07-31 22:56:46 +05:30
});
2019-09-04 21:01:54 +05:30
describe('expand/collapse button', () => {
it('renders as an expand button by default', () => {
2021-03-08 18:12:59 +05:30
mountComponent();
2022-10-11 01:57:18 +05:30
const button = wrapper.findComponent(GlButton);
2019-09-04 21:01:54 +05:30
expect(button.text()).toBe('Expand');
});
});
2019-07-31 22:56:46 +05:30
describe('sub-header', () => {
let subHeader;
beforeEach(() => {
2019-09-04 21:01:54 +05:30
mountComponent();
2019-07-31 22:56:46 +05:30
subHeader = wrapper.find('.js-section-sub-header');
});
it('renders descriptive text', () => {
2021-09-04 01:27:46 +05:30
expect(subHeader.text()).toContain('Manage metrics dashboard settings.');
2019-07-31 22:56:46 +05:30
});
it('renders help page link', () => {
2022-10-11 01:57:18 +05:30
const link = subHeader.findComponent(GlLink);
2019-07-31 22:56:46 +05:30
2021-09-04 01:27:46 +05:30
expect(link.text()).toBe('Learn more.');
2020-06-23 00:09:42 +05:30
expect(link.attributes().href).toBe(helpPage);
2019-07-31 22:56:46 +05:30
});
});
describe('form', () => {
2020-06-23 00:09:42 +05:30
describe('dashboard timezone', () => {
describe('field label', () => {
let formGroup;
beforeEach(() => {
mountComponent(false);
2022-10-11 01:57:18 +05:30
formGroup = wrapper.findComponent(DashboardTimezone).findComponent(GlFormGroup);
2020-06-23 00:09:42 +05:30
});
it('uses label text', () => {
expect(formGroup.find('label').text()).toBe('Dashboard timezone');
});
it('uses description text', () => {
const description = formGroup.find('small');
2022-08-27 11:52:29 +05:30
const expectedDescription =
"Choose whether to display dashboard metrics in UTC or the user's local timezone.";
expect(description.text()).toBe(expectedDescription);
2020-06-23 00:09:42 +05:30
});
2019-09-04 21:01:54 +05:30
});
2019-07-31 22:56:46 +05:30
2020-06-23 00:09:42 +05:30
describe('select field', () => {
let select;
2019-07-31 22:56:46 +05:30
2020-06-23 00:09:42 +05:30
beforeEach(() => {
mountComponent();
2022-10-11 01:57:18 +05:30
select = wrapper.findComponent(DashboardTimezone).findComponent(GlFormSelect);
2020-06-23 00:09:42 +05:30
});
it('defaults to externalDashboardUrl', () => {
expect(select.attributes('value')).toBe(dashboardTimezoneSetting);
});
2019-09-04 21:01:54 +05:30
});
});
2019-07-31 22:56:46 +05:30
2020-06-23 00:09:42 +05:30
describe('external dashboard', () => {
describe('input label', () => {
let formGroup;
2019-07-31 22:56:46 +05:30
2020-06-23 00:09:42 +05:30
beforeEach(() => {
mountComponent(false);
2022-10-11 01:57:18 +05:30
formGroup = wrapper.findComponent(ExternalDashboard).findComponent(GlFormGroup);
2020-06-23 00:09:42 +05:30
});
it('uses label text', () => {
expect(formGroup.find('label').text()).toBe('External dashboard URL');
});
2019-07-31 22:56:46 +05:30
2020-06-23 00:09:42 +05:30
it('uses description text', () => {
const description = formGroup.find('small');
2022-08-27 11:52:29 +05:30
const expectedDescription =
'Add a button to the metrics dashboard linking directly to your existing external dashboard.';
expect(description.text()).toBe(expectedDescription);
2020-06-23 00:09:42 +05:30
});
2019-09-04 21:01:54 +05:30
});
2019-07-31 22:56:46 +05:30
2020-06-23 00:09:42 +05:30
describe('input field', () => {
let input;
beforeEach(() => {
mountComponent();
2022-10-11 01:57:18 +05:30
input = wrapper.findComponent(ExternalDashboard).findComponent(GlFormInput);
2020-06-23 00:09:42 +05:30
});
it('defaults to externalDashboardUrl', () => {
expect(input.attributes().value).toBe(externalDashboardUrl);
});
it('uses a placeholder', () => {
expect(input.attributes().placeholder).toBe('https://my-org.gitlab.io/my-dashboards');
});
2019-09-04 21:01:54 +05:30
});
});
2019-07-31 22:56:46 +05:30
2019-09-04 21:01:54 +05:30
describe('submit button', () => {
2022-10-11 01:57:18 +05:30
const findSubmitButton = () => wrapper.find('.settings-content form').findComponent(GlButton);
2019-09-04 21:01:54 +05:30
const endpointRequest = [
operationsSettingsEndpoint,
{
project: {
metrics_setting_attributes: {
2020-06-23 00:09:42 +05:30
dashboard_timezone: dashboardTimezoneSetting,
2019-09-04 21:01:54 +05:30
external_dashboard_url: externalDashboardUrl,
},
},
},
];
it('renders button label', () => {
mountComponent();
const submit = findSubmitButton();
expect(submit.text()).toBe('Save Changes');
2019-07-31 22:56:46 +05:30
});
2022-04-04 11:22:00 +05:30
it('submits form on click', async () => {
2019-09-04 21:01:54 +05:30
mountComponent(false);
axios.patch.mockResolvedValue();
findSubmitButton().trigger('click');
2019-07-31 22:56:46 +05:30
2019-09-04 21:01:54 +05:30
expect(axios.patch).toHaveBeenCalledWith(...endpointRequest);
2019-07-31 22:56:46 +05:30
2022-04-04 11:22:00 +05:30
await nextTick();
expect(refreshCurrentPage).toHaveBeenCalled();
2019-09-04 21:01:54 +05:30
});
2022-04-04 11:22:00 +05:30
it('creates flash banner on error', async () => {
2019-09-04 21:01:54 +05:30
mountComponent(false);
const message = 'mockErrorMessage';
axios.patch.mockRejectedValue({ response: { data: { message } } });
findSubmitButton().trigger('click');
expect(axios.patch).toHaveBeenCalledWith(...endpointRequest);
2022-04-04 11:22:00 +05:30
await nextTick();
await jest.runAllTicks();
expect(createFlash).toHaveBeenCalledWith({
message: `There was an error saving your changes. ${message}`,
});
2019-07-31 22:56:46 +05:30
});
});
});
});