1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
import { shallowMount } from '@vue/test-utils';
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import LinksInner from '~/pipelines/components/graph_shared/links_inner.vue';
import { parseData } from '~/pipelines/components/parsing_utils';
import { createJobsHash } from '~/pipelines/utils';
import {
jobRect,
largePipelineData,
parallelNeedData,
pipelineData,
pipelineDataWithNoNeeds,
rootRect,
sameStageNeeds,
} from '../pipeline_graph/mock_data';
describe('Links Inner component', () => {
const containerId = 'pipeline-graph-container';
const defaultProps = {
containerId,
containerMeasurements: { width: 1019, height: 445 },
pipelineId: 1,
pipelineData: [],
totalGroups: 10,
};
let wrapper;
const createComponent = (props) => {
const currentPipelineData = props?.pipelineData || defaultProps.pipelineData;
wrapper = shallowMount(LinksInner, {
propsData: {
...defaultProps,
...props,
linksData: parseData(currentPipelineData.flatMap(({ groups }) => groups)).links,
},
});
};
const findLinkSvg = () => wrapper.find('#link-svg');
const findAllLinksPath = () => findLinkSvg().findAll('path');
// We create fixture so that each job has an empty div that represent
// the JobPill in the DOM. Each `JobPill` would have different coordinates,
// so we increment their coordinates on each iteration to simulate different positions.
const setHTMLFixtureLocal = ({ stages }) => {
const jobs = createJobsHash(stages);
const arrayOfJobs = Object.keys(jobs);
const linksHtmlElements = arrayOfJobs.map((job) => {
return `<div id=${job}-${defaultProps.pipelineId} />`;
});
setHTMLFixture(`<div id="${containerId}">${linksHtmlElements.join(' ')}</div>`);
// We are mocking the clientRect data of each job and the container ID.
jest
.spyOn(document.getElementById(containerId), 'getBoundingClientRect')
.mockImplementation(() => rootRect);
arrayOfJobs.forEach((job, index) => {
jest
.spyOn(
document.getElementById(`${job}-${defaultProps.pipelineId}`),
'getBoundingClientRect',
)
.mockImplementation(() => {
const newValue = 10 * index;
const { left, right, top, bottom, x, y } = jobRect;
return {
...jobRect,
left: left + newValue,
right: right + newValue,
top: top + newValue,
bottom: bottom + newValue,
x: x + newValue,
y: y + newValue,
};
});
});
};
afterEach(() => {
jest.restoreAllMocks();
resetHTMLFixture();
});
describe('basic SVG creation', () => {
beforeEach(() => {
createComponent();
});
it('renders an SVG of the right size', () => {
expect(findLinkSvg().exists()).toBe(true);
expect(findLinkSvg().attributes('width')).toBe(
`${defaultProps.containerMeasurements.width}px`,
);
expect(findLinkSvg().attributes('height')).toBe(
`${defaultProps.containerMeasurements.height}px`,
);
});
});
describe('no pipeline data', () => {
beforeEach(() => {
createComponent();
});
it('renders the component', () => {
expect(findLinkSvg().exists()).toBe(true);
expect(findAllLinksPath()).toHaveLength(0);
});
});
describe('pipeline data with no needs', () => {
beforeEach(() => {
createComponent({ pipelineData: pipelineDataWithNoNeeds.stages });
});
it('renders no links', () => {
expect(findLinkSvg().exists()).toBe(true);
expect(findAllLinksPath()).toHaveLength(0);
});
});
describe('with one need', () => {
beforeEach(() => {
setHTMLFixtureLocal(pipelineData);
createComponent({ pipelineData: pipelineData.stages });
});
it('renders one link', () => {
expect(findAllLinksPath()).toHaveLength(1);
});
it('path does not contain NaN values', () => {
expect(wrapper.html()).not.toContain('NaN');
});
it('matches snapshot and has expected path', () => {
expect(wrapper.html()).toMatchSnapshot();
});
});
describe('with a parallel need', () => {
beforeEach(() => {
setHTMLFixtureLocal(parallelNeedData);
createComponent({ pipelineData: parallelNeedData.stages });
});
it('renders only one link for all the same parallel jobs', () => {
expect(findAllLinksPath()).toHaveLength(1);
});
it('path does not contain NaN values', () => {
expect(wrapper.html()).not.toContain('NaN');
});
it('matches snapshot and has expected path', () => {
expect(wrapper.html()).toMatchSnapshot();
});
});
describe('with same stage needs', () => {
beforeEach(() => {
setHTMLFixtureLocal(sameStageNeeds);
createComponent({ pipelineData: sameStageNeeds.stages });
});
it('renders the correct number of links', () => {
expect(findAllLinksPath()).toHaveLength(2);
});
it('path does not contain NaN values', () => {
expect(wrapper.html()).not.toContain('NaN');
});
it('matches snapshot and has expected path', () => {
expect(wrapper.html()).toMatchSnapshot();
});
});
describe('with a large number of needs', () => {
beforeEach(() => {
setHTMLFixtureLocal(largePipelineData);
createComponent({ pipelineData: largePipelineData.stages });
});
it('renders the correct number of links', () => {
expect(findAllLinksPath()).toHaveLength(5);
});
it('path does not contain NaN values', () => {
expect(wrapper.html()).not.toContain('NaN');
});
it('matches snapshot and has expected path', () => {
expect(wrapper.html()).toMatchSnapshot();
});
});
describe('interactions', () => {
beforeEach(() => {
setHTMLFixtureLocal(largePipelineData);
createComponent({ pipelineData: largePipelineData.stages });
});
it('highlight needs on hover', async () => {
const firstLink = findAllLinksPath().at(0);
const defaultColorClass = 'gl-stroke-gray-200';
const hoverColorClass = 'gl-stroke-blue-400';
expect(firstLink.classes(defaultColorClass)).toBe(true);
expect(firstLink.classes(hoverColorClass)).toBe(false);
// Because there is a watcher, we need to set the props after the component
// has mounted.
await wrapper.setProps({ highlightedJob: 'test_1' });
expect(firstLink.classes(defaultColorClass)).toBe(false);
expect(firstLink.classes(hoverColorClass)).toBe(true);
});
});
});
|