debian-mirror-gitlab/spec/frontend/pipelines/parsing_utils_spec.js

162 lines
4.9 KiB
JavaScript
Raw Normal View History

2021-03-11 19:13:27 +05:30
import { createSankey } from '~/pipelines/components/dag/drawing_utils';
2020-06-23 00:09:42 +05:30
import {
2020-10-24 23:57:45 +05:30
createNodeDict,
2020-06-23 00:09:42 +05:30
makeLinksFromNodes,
filterByAncestors,
2021-06-08 01:23:25 +05:30
generateColumnsFromLayersListBare,
listByLayers,
2020-06-23 00:09:42 +05:30
parseData,
removeOrphanNodes,
getMaxNodes,
2021-01-03 14:25:43 +05:30
} from '~/pipelines/components/parsing_utils';
2020-06-23 00:09:42 +05:30
2021-09-04 01:27:46 +05:30
import { mockParsedGraphQLNodes, missingJob } from './components/dag/mock_data';
2021-06-08 01:23:25 +05:30
import { generateResponse, mockPipelineResponse } from './graph/mock_data';
2020-06-23 00:09:42 +05:30
describe('DAG visualization parsing utilities', () => {
2020-10-24 23:57:45 +05:30
const nodeDict = createNodeDict(mockParsedGraphQLNodes);
const unfilteredLinks = makeLinksFromNodes(mockParsedGraphQLNodes, nodeDict);
const parsed = parseData(mockParsedGraphQLNodes);
2020-06-23 00:09:42 +05:30
describe('makeLinksFromNodes', () => {
it('returns the expected link structure', () => {
2020-10-24 23:57:45 +05:30
expect(unfilteredLinks[0]).toHaveProperty('source', 'build_a');
expect(unfilteredLinks[0]).toHaveProperty('target', 'test_a');
2020-06-23 00:09:42 +05:30
expect(unfilteredLinks[0]).toHaveProperty('value', 10);
});
2021-09-04 01:27:46 +05:30
it('does not generate a link for non-existing jobs', () => {
const sources = unfilteredLinks.map(({ source }) => source);
expect(sources.includes(missingJob)).toBe(false);
});
2020-06-23 00:09:42 +05:30
});
describe('filterByAncestors', () => {
const allLinks = [
{ source: 'job1', target: 'job4' },
{ source: 'job1', target: 'job2' },
{ source: 'job2', target: 'job4' },
];
2021-03-08 18:12:59 +05:30
const dedupedLinks = [
{ source: 'job1', target: 'job2' },
{ source: 'job2', target: 'job4' },
];
2020-06-23 00:09:42 +05:30
const nodeLookup = {
job1: {
name: 'job1',
},
job2: {
name: 'job2',
needs: ['job1'],
},
job4: {
name: 'job4',
needs: ['job1', 'job2'],
category: 'build',
},
};
it('dedupes links', () => {
expect(filterByAncestors(allLinks, nodeLookup)).toMatchObject(dedupedLinks);
});
});
describe('parseData parent function', () => {
it('returns an object containing a list of nodes and links', () => {
// an array of nodes exist and the values are defined
expect(parsed).toHaveProperty('nodes');
expect(Array.isArray(parsed.nodes)).toBe(true);
expect(parsed.nodes.filter(Boolean)).not.toHaveLength(0);
// an array of links exist and the values are defined
expect(parsed).toHaveProperty('links');
expect(Array.isArray(parsed.links)).toBe(true);
expect(parsed.links.filter(Boolean)).not.toHaveLength(0);
});
});
describe('removeOrphanNodes', () => {
it('removes sankey nodes that have no needs and are not needed', () => {
2020-10-24 23:57:45 +05:30
const layoutSettings = {
width: 200,
height: 200,
nodeWidth: 10,
nodePadding: 20,
paddingForLabels: 100,
};
const sankeyLayout = createSankey(layoutSettings)(parsed);
2020-06-23 00:09:42 +05:30
const cleanedNodes = removeOrphanNodes(sankeyLayout.nodes);
2020-10-24 23:57:45 +05:30
/*
These lengths are determined by the mock data.
If the data changes, the numbers may also change.
*/
2021-09-04 01:27:46 +05:30
expect(parsed.nodes).toHaveLength(mockParsedGraphQLNodes.length);
2020-10-24 23:57:45 +05:30
expect(cleanedNodes).toHaveLength(12);
2020-06-23 00:09:42 +05:30
});
});
describe('getMaxNodes', () => {
it('returns the number of nodes in the most populous generation', () => {
const layerNodes = [
{ layer: 0 },
{ layer: 0 },
{ layer: 1 },
{ layer: 1 },
{ layer: 0 },
{ layer: 3 },
{ layer: 2 },
{ layer: 4 },
{ layer: 1 },
{ layer: 3 },
{ layer: 4 },
];
expect(getMaxNodes(layerNodes)).toBe(3);
});
});
2021-06-08 01:23:25 +05:30
describe('generateColumnsFromLayersList', () => {
const pipeline = generateResponse(mockPipelineResponse, 'root/fungi-xoxo');
2021-10-27 15:23:28 +05:30
const { pipelineLayers } = listByLayers(pipeline);
const columns = generateColumnsFromLayersListBare(pipeline, pipelineLayers);
2021-06-08 01:23:25 +05:30
it('returns stage-like objects with default name, id, and status', () => {
columns.forEach((col, idx) => {
expect(col).toMatchObject({
name: '',
status: { action: null },
id: `layer-${idx}`,
});
});
});
it('creates groups that match the list created in listByLayers', () => {
columns.forEach((col, idx) => {
const groupNames = col.groups.map(({ name }) => name);
2021-10-27 15:23:28 +05:30
expect(groupNames).toEqual(pipelineLayers[idx]);
2021-06-08 01:23:25 +05:30
});
});
it('looks up the correct group object', () => {
columns.forEach((col) => {
col.groups.forEach((group) => {
const groupStage = pipeline.stages.find((el) => el.name === group.stageName);
const groupObject = groupStage.groups.find((el) => el.name === group.name);
expect(group).toBe(groupObject);
});
});
});
/*
Just as a fallback in case multiple functions change, so tests pass
but the implementation moves away from case.
*/
it('matches the snapshot', () => {
expect(columns).toMatchSnapshot();
});
});
2020-06-23 00:09:42 +05:30
});