debian-mirror-gitlab/spec/frontend/pipelines/graph_shared/links_layer_spec.js

233 lines
6.5 KiB
JavaScript
Raw Normal View History

2021-06-08 01:23:25 +05:30
import { shallowMount } from '@vue/test-utils';
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import {
PIPELINES_DETAIL_LINK_DURATION,
PIPELINES_DETAIL_LINKS_TOTAL,
PIPELINES_DETAIL_LINKS_JOB_RATIO,
} from '~/performance/constants';
import * as perfUtils from '~/performance/utils';
import * as Api from '~/pipelines/components/graph_shared/api';
2021-03-08 18:12:59 +05:30
import LinksInner from '~/pipelines/components/graph_shared/links_inner.vue';
2021-03-11 19:13:27 +05:30
import LinksLayer from '~/pipelines/components/graph_shared/links_layer.vue';
2021-06-08 01:23:25 +05:30
import * as sentryUtils from '~/pipelines/utils';
2021-03-08 18:12:59 +05:30
import { generateResponse, mockPipelineResponse } from '../graph/mock_data';
describe('links layer component', () => {
let wrapper;
const findLinksInner = () => wrapper.find(LinksInner);
const pipeline = generateResponse(mockPipelineResponse, 'root/fungi-xoxo');
const containerId = `pipeline-links-container-${pipeline.id}`;
const slotContent = "<div>Ceci n'est pas un graphique</div>";
const defaultProps = {
containerId,
containerMeasurements: { width: 400, height: 400 },
pipelineId: pipeline.id,
pipelineData: pipeline.stages,
2021-06-08 01:23:25 +05:30
showLinks: false,
2021-03-08 18:12:59 +05:30
};
const createComponent = ({ mountFn = shallowMount, props = {} } = {}) => {
wrapper = mountFn(LinksLayer, {
propsData: {
...defaultProps,
...props,
},
slots: {
default: slotContent,
},
stubs: {
'links-inner': true,
},
});
};
afterEach(() => {
wrapper.destroy();
});
2021-06-08 01:23:25 +05:30
describe('with show links off', () => {
2021-03-08 18:12:59 +05:30
beforeEach(() => {
createComponent();
});
it('renders the default slot', () => {
expect(wrapper.html()).toContain(slotContent);
});
2021-06-08 01:23:25 +05:30
it('does not render inner links component', () => {
expect(findLinksInner().exists()).toBe(false);
});
});
describe('with show links on', () => {
beforeEach(() => {
createComponent({
props: {
showLinks: true,
},
});
});
it('renders the default slot', () => {
expect(wrapper.html()).toContain(slotContent);
});
2021-03-08 18:12:59 +05:30
it('renders the inner links component', () => {
expect(findLinksInner().exists()).toBe(true);
});
});
2021-06-08 01:23:25 +05:30
describe('with width or height measurement at 0', () => {
beforeEach(() => {
createComponent({ props: { containerMeasurements: { width: 0, height: 100 } } });
});
2021-03-08 18:12:59 +05:30
2021-06-08 01:23:25 +05:30
it('renders the default slot', () => {
expect(wrapper.html()).toContain(slotContent);
});
2021-03-08 18:12:59 +05:30
2021-06-08 01:23:25 +05:30
it('does not render the inner links component', () => {
expect(findLinksInner().exists()).toBe(false);
});
});
2021-03-08 18:12:59 +05:30
2021-06-08 01:23:25 +05:30
describe('performance metrics', () => {
const metricsPath = '/root/project/-/ci/prometheus_metrics/histograms.json';
let markAndMeasure;
let reportToSentry;
let reportPerformance;
let mock;
beforeEach(() => {
jest.spyOn(window, 'requestAnimationFrame').mockImplementation((cb) => cb());
markAndMeasure = jest.spyOn(perfUtils, 'performanceMarkAndMeasure');
reportToSentry = jest.spyOn(sentryUtils, 'reportToSentry');
reportPerformance = jest.spyOn(Api, 'reportPerformance');
2021-03-08 18:12:59 +05:30
});
2021-06-08 01:23:25 +05:30
describe('with no metrics config object', () => {
2021-04-17 20:07:23 +05:30
beforeEach(() => {
2021-06-08 01:23:25 +05:30
createComponent();
2021-04-17 20:07:23 +05:30
});
2021-06-08 01:23:25 +05:30
it('is not called', () => {
expect(markAndMeasure).not.toHaveBeenCalled();
expect(reportToSentry).not.toHaveBeenCalled();
expect(reportPerformance).not.toHaveBeenCalled();
2021-04-17 20:07:23 +05:30
});
2021-06-08 01:23:25 +05:30
});
2021-04-17 20:07:23 +05:30
2021-06-08 01:23:25 +05:30
describe('with metrics config set to false', () => {
beforeEach(() => {
createComponent({
props: {
metricsConfig: {
collectMetrics: false,
metricsPath: '/path/to/metrics',
},
},
});
2021-04-17 20:07:23 +05:30
});
2021-06-08 01:23:25 +05:30
it('is not called', () => {
expect(markAndMeasure).not.toHaveBeenCalled();
expect(reportToSentry).not.toHaveBeenCalled();
expect(reportPerformance).not.toHaveBeenCalled();
2021-04-17 20:07:23 +05:30
});
});
2021-06-08 01:23:25 +05:30
describe('with no metrics path', () => {
2021-03-08 18:12:59 +05:30
beforeEach(() => {
2021-06-08 01:23:25 +05:30
createComponent({
props: {
metricsConfig: {
collectMetrics: true,
metricsPath: '',
},
},
});
2021-03-08 18:12:59 +05:30
});
2021-06-08 01:23:25 +05:30
it('is not called', () => {
expect(markAndMeasure).not.toHaveBeenCalled();
expect(reportToSentry).not.toHaveBeenCalled();
expect(reportPerformance).not.toHaveBeenCalled();
});
});
describe('with metrics path and collect set to true', () => {
const duration = 875;
const numLinks = 7;
const totalGroups = 8;
const metricsData = {
histograms: [
{ name: PIPELINES_DETAIL_LINK_DURATION, value: duration / 1000 },
{ name: PIPELINES_DETAIL_LINKS_TOTAL, value: numLinks },
{
name: PIPELINES_DETAIL_LINKS_JOB_RATIO,
value: numLinks / totalGroups,
},
],
};
describe('when no duration is obtained', () => {
beforeEach(() => {
jest.spyOn(window.performance, 'getEntriesByName').mockImplementation(() => {
return [];
});
createComponent({
props: {
metricsConfig: {
collectMetrics: true,
path: metricsPath,
},
},
});
});
it('attempts to collect metrics', () => {
expect(markAndMeasure).toHaveBeenCalled();
expect(reportPerformance).not.toHaveBeenCalled();
expect(reportToSentry).not.toHaveBeenCalled();
});
2021-03-08 18:12:59 +05:30
});
2021-06-08 01:23:25 +05:30
describe('with duration and no error', () => {
beforeEach(() => {
mock = new MockAdapter(axios);
mock.onPost(metricsPath).reply(200, {});
jest.spyOn(window.performance, 'getEntriesByName').mockImplementation(() => {
return [{ duration }];
});
createComponent({
props: {
metricsConfig: {
collectMetrics: true,
path: metricsPath,
},
},
});
});
afterEach(() => {
mock.restore();
});
it('it calls reportPerformance with expected arguments', () => {
expect(markAndMeasure).toHaveBeenCalled();
expect(reportPerformance).toHaveBeenCalled();
expect(reportPerformance).toHaveBeenCalledWith(metricsPath, metricsData);
expect(reportToSentry).not.toHaveBeenCalled();
});
2021-03-08 18:12:59 +05:30
});
});
});
});