debian-mirror-gitlab/spec/frontend/grafana_integration/components/grafana_integration_spec.js

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

123 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-10-24 23:57:45 +05:30
import { GlButton } from '@gitlab/ui';
2021-09-04 01:27:46 +05:30
import { 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 { mountExtended } from 'helpers/vue_test_utils_helper';
2023-05-27 22:25:52 +05:30
import { createAlert } from '~/alert';
2019-12-26 22:10:19 +05:30
import GrafanaIntegration from '~/grafana_integration/components/grafana_integration.vue';
import { createStore } from '~/grafana_integration/store';
import axios from '~/lib/utils/axios_utils';
import { refreshCurrentPage } from '~/lib/utils/url_utility';
jest.mock('~/lib/utils/url_utility');
2023-05-27 22:25:52 +05:30
jest.mock('~/alert');
2019-12-26 22:10:19 +05:30
describe('grafana integration component', () => {
let wrapper;
let store;
const operationsSettingsEndpoint = `${TEST_HOST}/mock/ops/settings/endpoint`;
const grafanaIntegrationUrl = `${TEST_HOST}`;
const grafanaIntegrationToken = 'someToken';
beforeEach(() => {
store = createStore({
operationsSettingsEndpoint,
grafanaIntegrationUrl,
grafanaIntegrationToken,
});
});
afterEach(() => {
if (wrapper.destroy) {
wrapper.destroy();
2022-11-25 23:54:43 +05:30
createAlert.mockReset();
2019-12-26 22:10:19 +05:30
refreshCurrentPage.mockReset();
}
});
describe('default state', () => {
it('to match the default snapshot', () => {
wrapper = shallowMount(GrafanaIntegration, { store });
expect(wrapper.element).toMatchSnapshot();
});
});
it('renders header text', () => {
wrapper = shallowMount(GrafanaIntegration, { store });
2020-10-24 23:57:45 +05:30
expect(wrapper.find('.js-section-header').text()).toBe('Grafana authentication');
2019-12-26 22:10:19 +05:30
});
describe('expand/collapse button', () => {
it('renders as an expand button by default', () => {
wrapper = shallowMount(GrafanaIntegration, { store });
2021-09-04 01:27:46 +05:30
const button = wrapper.findComponent(GlButton);
2019-12-26 22:10:19 +05:30
expect(button.text()).toBe('Expand');
});
});
describe('sub-header', () => {
it('renders descriptive text', () => {
wrapper = shallowMount(GrafanaIntegration, { store });
expect(wrapper.find('.js-section-sub-header').text()).toContain(
2021-04-17 20:07:23 +05:30
'Set up Grafana authentication to embed Grafana panels in GitLab Flavored Markdown.\n Learn more.',
2019-12-26 22:10:19 +05:30
);
});
});
describe('form', () => {
beforeEach(() => {
jest.spyOn(axios, 'patch').mockImplementation();
2021-09-04 01:27:46 +05:30
wrapper = mountExtended(GrafanaIntegration, { store });
2019-12-26 22:10:19 +05:30
});
afterEach(() => {
axios.patch.mockReset();
});
describe('submit button', () => {
2021-09-04 01:27:46 +05:30
const findSubmitButton = () => wrapper.findByTestId('save-grafana-settings-button');
2019-12-26 22:10:19 +05:30
const endpointRequest = [
operationsSettingsEndpoint,
{
project: {
grafana_integration_attributes: {
grafana_url: grafanaIntegrationUrl,
token: grafanaIntegrationToken,
enabled: false,
},
},
},
];
2022-04-04 11:22:00 +05:30
it('submits form on click', async () => {
2019-12-26 22:10:19 +05:30
axios.patch.mockResolvedValue();
findSubmitButton(wrapper).trigger('click');
expect(axios.patch).toHaveBeenCalledWith(...endpointRequest);
2022-04-04 11:22:00 +05:30
await nextTick();
expect(refreshCurrentPage).toHaveBeenCalled();
2019-12-26 22:10:19 +05:30
});
2023-05-27 22:25:52 +05:30
it('creates alert banner on error', async () => {
2019-12-26 22:10:19 +05:30
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();
2022-11-25 23:54:43 +05:30
expect(createAlert).toHaveBeenCalledWith({
2022-04-04 11:22:00 +05:30
message: `There was an error saving your changes. ${message}`,
});
2019-12-26 22:10:19 +05:30
});
});
});
});