debian-mirror-gitlab/spec/frontend/projects/pipelines/charts/components/app_spec.js

180 lines
5.4 KiB
JavaScript
Raw Normal View History

2021-03-08 18:12:59 +05:30
import { GlTabs, GlTab } from '@gitlab/ui';
2021-03-11 19:13:27 +05:30
import { shallowMount } from '@vue/test-utils';
import { merge } from 'lodash';
import setWindowLocation from 'helpers/set_window_location_helper';
import { TEST_HOST } from 'helpers/test_constants';
import { mergeUrlParams, updateHistory, getParameterValues } from '~/lib/utils/url_utility';
2020-03-13 15:44:24 +05:30
import Component from '~/projects/pipelines/charts/components/app.vue';
2021-03-08 18:12:59 +05:30
import PipelineCharts from '~/projects/pipelines/charts/components/pipeline_charts.vue';
2021-02-22 17:27:13 +05:30
2021-03-11 19:13:27 +05:30
jest.mock('~/lib/utils/url_utility');
2020-03-13 15:44:24 +05:30
2021-03-08 18:12:59 +05:30
const DeploymentFrequencyChartsStub = { name: 'DeploymentFrequencyCharts', render: () => {} };
2021-04-29 21:17:54 +05:30
const LeadTimeChartsStub = { name: 'LeadTimeCharts', render: () => {} };
2021-03-08 18:12:59 +05:30
2020-03-13 15:44:24 +05:30
describe('ProjectsPipelinesChartsApp', () => {
let wrapper;
2021-03-08 18:12:59 +05:30
function createComponent(mountOptions = {}) {
wrapper = shallowMount(
Component,
merge(
{},
{
provide: {
2021-04-29 21:17:54 +05:30
shouldRenderDoraCharts: true,
2021-03-08 18:12:59 +05:30
},
stubs: {
DeploymentFrequencyCharts: DeploymentFrequencyChartsStub,
2021-04-29 21:17:54 +05:30
LeadTimeCharts: LeadTimeChartsStub,
2021-03-08 18:12:59 +05:30
},
},
mountOptions,
),
);
2021-02-22 17:27:13 +05:30
}
2020-03-13 15:44:24 +05:30
afterEach(() => {
wrapper.destroy();
});
2021-03-08 18:12:59 +05:30
const findGlTabs = () => wrapper.find(GlTabs);
2021-04-29 21:17:54 +05:30
const findAllGlTabs = () => wrapper.findAll(GlTab);
const findGlTabAtIndex = (index) => findAllGlTabs().at(index);
const findLeadTimeCharts = () => wrapper.find(LeadTimeChartsStub);
2021-03-11 19:13:27 +05:30
const findDeploymentFrequencyCharts = () => wrapper.find(DeploymentFrequencyChartsStub);
const findPipelineCharts = () => wrapper.find(PipelineCharts);
2021-04-29 21:17:54 +05:30
describe('when all charts are available', () => {
2021-03-08 18:12:59 +05:30
beforeEach(() => {
2021-04-29 21:17:54 +05:30
createComponent();
2020-03-13 15:44:24 +05:30
});
2021-04-29 21:17:54 +05:30
it('renders tabs', () => {
2021-03-08 18:12:59 +05:30
expect(findGlTabs().exists()).toBe(true);
2021-04-29 21:17:54 +05:30
expect(findGlTabAtIndex(0).attributes('title')).toBe('Pipelines');
2021-06-08 01:23:25 +05:30
expect(findGlTabAtIndex(1).attributes('title')).toBe('Deployment frequency');
expect(findGlTabAtIndex(2).attributes('title')).toBe('Lead time');
2021-04-29 21:17:54 +05:30
});
it('renders the pipeline charts', () => {
expect(findPipelineCharts().exists()).toBe(true);
});
it('renders the deployment frequency charts', () => {
2021-03-08 18:12:59 +05:30
expect(findDeploymentFrequencyCharts().exists()).toBe(true);
});
2021-03-11 19:13:27 +05:30
2021-04-29 21:17:54 +05:30
it('renders the lead time charts', () => {
expect(findLeadTimeCharts().exists()).toBe(true);
});
2021-03-11 19:13:27 +05:30
it('sets the tab and url when a tab is clicked', async () => {
let chartsPath;
setWindowLocation(`${TEST_HOST}/gitlab-org/gitlab-test/-/pipelines/charts`);
mergeUrlParams.mockImplementation(({ chart }, path) => {
2021-06-08 01:23:25 +05:30
expect(chart).toBe('deployment-frequency');
2021-03-11 19:13:27 +05:30
expect(path).toBe(window.location.pathname);
chartsPath = `${path}?chart=${chart}`;
return chartsPath;
});
updateHistory.mockImplementation(({ url }) => {
expect(url).toBe(chartsPath);
});
const tabs = findGlTabs();
expect(tabs.attributes('value')).toBe('0');
tabs.vm.$emit('input', 1);
await wrapper.vm.$nextTick();
expect(tabs.attributes('value')).toBe('1');
});
it('should not try to push history if the tab does not change', async () => {
setWindowLocation(`${TEST_HOST}/gitlab-org/gitlab-test/-/pipelines/charts`);
mergeUrlParams.mockImplementation(({ chart }, path) => `${path}?chart=${chart}`);
const tabs = findGlTabs();
expect(tabs.attributes('value')).toBe('0');
tabs.vm.$emit('input', 0);
await wrapper.vm.$nextTick();
expect(updateHistory).not.toHaveBeenCalled();
});
});
describe('when provided with a query param', () => {
it.each`
2021-06-08 01:23:25 +05:30
chart | tab
${'lead-time'} | ${'2'}
${'deployment-frequency'} | ${'1'}
${'pipelines'} | ${'0'}
${'fake'} | ${'0'}
${''} | ${'0'}
2021-03-11 19:13:27 +05:30
`('shows the correct tab for URL parameter "$chart"', ({ chart, tab }) => {
setWindowLocation(`${TEST_HOST}/gitlab-org/gitlab-test/-/pipelines/charts?chart=${chart}`);
getParameterValues.mockImplementation((name) => {
expect(name).toBe('chart');
return chart ? [chart] : [];
});
2021-04-29 21:17:54 +05:30
createComponent();
2021-03-11 19:13:27 +05:30
expect(findGlTabs().attributes('value')).toBe(tab);
});
it('should set the tab when the back button is clicked', async () => {
let popstateHandler;
window.addEventListener = jest.fn();
window.addEventListener.mockImplementation((event, handler) => {
if (event === 'popstate') {
popstateHandler = handler;
}
});
getParameterValues.mockImplementation((name) => {
expect(name).toBe('chart');
return [];
});
2021-04-29 21:17:54 +05:30
createComponent();
2021-03-11 19:13:27 +05:30
expect(findGlTabs().attributes('value')).toBe('0');
getParameterValues.mockImplementationOnce((name) => {
expect(name).toBe('chart');
2021-06-08 01:23:25 +05:30
return ['deployment-frequency'];
2021-03-11 19:13:27 +05:30
});
popstateHandler();
await wrapper.vm.$nextTick();
expect(findGlTabs().attributes('value')).toBe('1');
});
2021-03-08 18:12:59 +05:30
});
2020-03-13 15:44:24 +05:30
2021-04-29 21:17:54 +05:30
describe('when the dora charts are not available', () => {
2021-03-08 18:12:59 +05:30
beforeEach(() => {
2021-04-29 21:17:54 +05:30
createComponent({ provide: { shouldRenderDoraCharts: false } });
2021-03-08 18:12:59 +05:30
});
2020-03-13 15:44:24 +05:30
2021-04-29 21:17:54 +05:30
it('does not render tabs', () => {
2021-03-08 18:12:59 +05:30
expect(findGlTabs().exists()).toBe(false);
2021-04-29 21:17:54 +05:30
});
it('renders the pipeline charts', () => {
expect(findPipelineCharts().exists()).toBe(true);
2020-03-13 15:44:24 +05:30
});
});
});