debian-mirror-gitlab/spec/frontend/pages/projects/graphs/code_coverage_spec.js

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

183 lines
5.1 KiB
JavaScript
Raw Normal View History

2021-01-03 14:25:43 +05:30
import { GlAlert, GlDropdown, GlDropdownItem } from '@gitlab/ui';
2020-06-23 00:09:42 +05:30
import { GlAreaChart } from '@gitlab/ui/dist/charts';
2021-03-11 19:13:27 +05:30
import { shallowMount } from '@vue/test-utils';
import MockAdapter from 'axios-mock-adapter';
2020-06-23 00:09:42 +05:30
2022-04-04 11:22:00 +05:30
import { nextTick } from 'vue';
2020-10-24 23:57:45 +05:30
import waitForPromises from 'helpers/wait_for_promises';
2020-06-23 00:09:42 +05:30
import axios from '~/lib/utils/axios_utils';
2021-03-11 19:13:27 +05:30
import httpStatusCodes from '~/lib/utils/http_status';
2020-06-23 00:09:42 +05:30
import CodeCoverage from '~/pages/projects/graphs/components/code_coverage.vue';
2020-07-28 23:09:34 +05:30
import { codeCoverageMockData, sortedDataByDates } from './mock_data';
2020-06-23 00:09:42 +05:30
describe('Code Coverage', () => {
let wrapper;
let mockAxios;
const graphEndpoint = '/graph';
2022-08-27 11:52:29 +05:30
const graphStartDate = '13 February';
const graphEndDate = '12 May';
const graphRef = 'master';
const graphCsvPath = 'url/';
2020-06-23 00:09:42 +05:30
2022-10-11 01:57:18 +05:30
const findAlert = () => wrapper.findComponent(GlAlert);
const findAreaChart = () => wrapper.findComponent(GlAreaChart);
const findAllDropdownItems = () => wrapper.findAllComponents(GlDropdownItem);
2020-06-23 00:09:42 +05:30
const findFirstDropdownItem = () => findAllDropdownItems().at(0);
const findSecondDropdownItem = () => findAllDropdownItems().at(1);
2022-08-27 11:52:29 +05:30
const findDownloadButton = () => wrapper.find('[data-testid="download-button"]');
2020-06-23 00:09:42 +05:30
const createComponent = () => {
wrapper = shallowMount(CodeCoverage, {
propsData: {
graphEndpoint,
2022-08-27 11:52:29 +05:30
graphStartDate,
graphEndDate,
graphRef,
graphCsvPath,
2020-06-23 00:09:42 +05:30
},
});
};
afterEach(() => {
wrapper.destroy();
wrapper = null;
});
describe('when fetching data is successful', () => {
beforeEach(() => {
mockAxios = new MockAdapter(axios);
mockAxios.onGet().replyOnce(httpStatusCodes.OK, codeCoverageMockData);
createComponent();
return waitForPromises();
});
afterEach(() => {
mockAxios.restore();
});
it('renders the area chart', () => {
expect(findAreaChart().exists()).toBe(true);
});
2020-07-28 23:09:34 +05:30
it('sorts the dates in ascending order', () => {
expect(wrapper.vm.sortedData).toEqual(sortedDataByDates);
});
2020-06-23 00:09:42 +05:30
it('matches the snapshot', () => {
expect(wrapper.element).toMatchSnapshot();
});
it('shows no error messages', () => {
expect(findAlert().exists()).toBe(false);
});
2022-08-27 11:52:29 +05:30
it('does not render download button', () => {
expect(findDownloadButton().exists()).toBe(true);
});
2020-06-23 00:09:42 +05:30
});
describe('when fetching data fails', () => {
beforeEach(() => {
mockAxios = new MockAdapter(axios);
mockAxios.onGet().replyOnce(httpStatusCodes.BAD_REQUEST);
createComponent();
return waitForPromises();
});
afterEach(() => {
mockAxios.restore();
});
it('renders an error message', () => {
expect(findAlert().exists()).toBe(true);
expect(findAlert().attributes().variant).toBe('danger');
});
it('still renders an empty graph', () => {
expect(findAreaChart().exists()).toBe(true);
});
});
describe('when fetching data succeed but returns an empty state', () => {
beforeEach(() => {
mockAxios = new MockAdapter(axios);
mockAxios.onGet().replyOnce(httpStatusCodes.OK, []);
createComponent();
return waitForPromises();
});
afterEach(() => {
mockAxios.restore();
});
it('renders an information message', () => {
expect(findAlert().exists()).toBe(true);
expect(findAlert().attributes().variant).toBe('info');
});
it('still renders an empty graph', () => {
expect(findAreaChart().exists()).toBe(true);
});
2022-08-27 11:52:29 +05:30
it('does not render download button', () => {
expect(findDownloadButton().exists()).toBe(false);
});
2020-06-23 00:09:42 +05:30
});
describe('dropdown options', () => {
beforeEach(() => {
mockAxios = new MockAdapter(axios);
mockAxios.onGet().replyOnce(httpStatusCodes.OK, codeCoverageMockData);
createComponent();
return waitForPromises();
});
it('renders the dropdown with all custom names as options', () => {
2022-10-11 01:57:18 +05:30
expect(wrapper.findComponent(GlDropdown).exists()).toBeDefined();
2020-06-23 00:09:42 +05:30
expect(findAllDropdownItems()).toHaveLength(codeCoverageMockData.length);
expect(findFirstDropdownItem().text()).toBe(codeCoverageMockData[0].group_name);
});
});
describe('interactions', () => {
beforeEach(() => {
mockAxios = new MockAdapter(axios);
mockAxios.onGet().replyOnce(httpStatusCodes.OK, codeCoverageMockData);
createComponent();
return waitForPromises();
});
it('updates the selected dropdown option with an icon', async () => {
findSecondDropdownItem().vm.$emit('click');
2022-04-04 11:22:00 +05:30
await nextTick();
2020-06-23 00:09:42 +05:30
2022-08-27 11:52:29 +05:30
expect(findFirstDropdownItem().attributes('ischecked')).toBe(undefined);
expect(findSecondDropdownItem().attributes('ischecked')).toBe('true');
2020-06-23 00:09:42 +05:30
});
it('updates the graph data when selecting a different option in dropdown', async () => {
const originalSelectedData = wrapper.vm.selectedDailyCoverage;
const expectedData = codeCoverageMockData[1];
findSecondDropdownItem().vm.$emit('click');
2022-04-04 11:22:00 +05:30
await nextTick();
2020-06-23 00:09:42 +05:30
expect(wrapper.vm.selectedDailyCoverage).not.toBe(originalSelectedData);
expect(wrapper.vm.selectedDailyCoverage).toBe(expectedData);
});
});
});