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

218 lines
6.9 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 { merge } from 'lodash';
2022-01-26 12:08:38 +05:30
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
2021-03-11 19:13:27 +05:30
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';
2022-01-26 12:08:38 +05:30
import API from '~/api';
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-12-11 22:18:48 +05:30
const ProjectQualitySummaryStub = { name: 'ProjectQualitySummary', 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 = {}) {
2022-01-26 12:08:38 +05:30
wrapper = shallowMountExtended(
2021-03-08 18:12:59 +05:30
Component,
merge(
{},
{
provide: {
2021-04-29 21:17:54 +05:30
shouldRenderDoraCharts: true,
2021-12-11 22:18:48 +05:30
shouldRenderQualitySummary: true,
2021-03-08 18:12:59 +05:30
},
stubs: {
DeploymentFrequencyCharts: DeploymentFrequencyChartsStub,
2021-04-29 21:17:54 +05:30
LeadTimeCharts: LeadTimeChartsStub,
2021-12-11 22:18:48 +05:30
ProjectQualitySummary: ProjectQualitySummaryStub,
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-12-11 22:18:48 +05:30
const findProjectQualitySummary = () => wrapper.find(ProjectQualitySummaryStub);
2021-03-11 19:13:27 +05:30
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-12-11 22:18:48 +05:30
it('renders the project quality summary', () => {
expect(findProjectQualitySummary().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();
});
2022-01-26 12:08:38 +05:30
describe('event tracking', () => {
it.each`
testId | event
${'pipelines-tab'} | ${'p_analytics_ci_cd_pipelines'}
${'deployment-frequency-tab'} | ${'p_analytics_ci_cd_deployment_frequency'}
${'lead-time-tab'} | ${'p_analytics_ci_cd_lead_time'}
`('tracks the $event event when clicked', ({ testId, event }) => {
jest.spyOn(API, 'trackRedisHllUserEvent');
expect(API.trackRedisHllUserEvent).not.toHaveBeenCalled();
wrapper.findByTestId(testId).vm.$emit('click');
expect(API.trackRedisHllUserEvent).toHaveBeenCalledWith(event);
});
});
2021-03-11 19:13:27 +05:30
});
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-12-11 22:18:48 +05:30
describe('when the dora charts are not available and project quality summary is not available', () => {
2021-03-08 18:12:59 +05:30
beforeEach(() => {
2021-12-11 22:18:48 +05:30
createComponent({
provide: { shouldRenderDoraCharts: false, shouldRenderQualitySummary: 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
});
});
2021-12-11 22:18:48 +05:30
describe('when the project quality summary is not available', () => {
beforeEach(() => {
createComponent({ provide: { shouldRenderQualitySummary: false } });
});
it('does not render the tab', () => {
expect(findProjectQualitySummary().exists()).toBe(false);
});
});
2020-03-13 15:44:24 +05:30
});