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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
import { GlAlert, GlTable, GlLink } from '@gitlab/ui';
import { nextTick } from 'vue';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import MlExperiment from '~/ml/experiment_tracking/components/ml_experiment.vue';
import RegistrySearch from '~/vue_shared/components/registry/registry_search.vue';
import Pagination from '~/vue_shared/components/incubation/pagination.vue';
import setWindowLocation from 'helpers/set_window_location_helper';
import * as urlHelpers from '~/lib/utils/url_utility';
describe('MlExperiment', () => {
let wrapper;
const startCursor = 'eyJpZCI6IjE2In0';
const defaultPageInfo = {
startCursor,
endCursor: 'eyJpZCI6IjIifQ',
hasNextPage: true,
hasPreviousPage: true,
};
const createWrapper = (
candidates = [],
metricNames = [],
paramNames = [],
pageInfo = defaultPageInfo,
) => {
wrapper = mountExtended(MlExperiment, {
provide: { candidates, metricNames, paramNames, pageInfo },
});
};
const candidates = [
{
rmse: 1,
l1_ratio: 0.4,
details: 'link_to_candidate1',
artifact: 'link_to_artifact',
name: 'aCandidate',
created_at: '2023-01-05T14:07:02.975Z',
user: { username: 'root', path: '/root' },
},
{
auc: 0.3,
l1_ratio: 0.5,
details: 'link_to_candidate2',
created_at: '2023-01-05T14:07:02.975Z',
name: null,
user: null,
},
{
auc: 0.3,
l1_ratio: 0.5,
details: 'link_to_candidate3',
created_at: '2023-01-05T14:07:02.975Z',
name: null,
user: null,
},
{
auc: 0.3,
l1_ratio: 0.5,
details: 'link_to_candidate4',
created_at: '2023-01-05T14:07:02.975Z',
name: null,
user: null,
},
{
auc: 0.3,
l1_ratio: 0.5,
details: 'link_to_candidate5',
created_at: '2023-01-05T14:07:02.975Z',
name: null,
user: null,
},
];
const createWrapperWithCandidates = (pageInfo = defaultPageInfo) => {
createWrapper(candidates, ['rmse', 'auc', 'mae'], ['l1_ratio'], pageInfo);
};
const findAlert = () => wrapper.findComponent(GlAlert);
const findPagination = () => wrapper.findComponent(Pagination);
const findEmptyState = () => wrapper.findByText('No candidates to display');
const findRegistrySearch = () => wrapper.findComponent(RegistrySearch);
const findTable = () => wrapper.findComponent(GlTable);
const findTableHeaders = () => findTable().findAll('th');
const findTableRows = () => findTable().findAll('tbody > tr');
const findNthTableRow = (idx) => findTableRows().at(idx);
const findColumnInRow = (row, col) => findNthTableRow(row).findAll('td').at(col);
const hrefInRowAndColumn = (row, col) =>
findColumnInRow(row, col).findComponent(GlLink).attributes().href;
it('shows incubation warning', () => {
createWrapper();
expect(findAlert().exists()).toBe(true);
});
describe('default inputs', () => {
beforeEach(async () => {
createWrapper();
await nextTick();
});
it('shows empty state', () => {
expect(findEmptyState().exists()).toBe(true);
});
it('does not show pagination', () => {
expect(findPagination().exists()).toBe(false);
});
it('there are no columns', () => {
expect(findTable().findAll('th')).toHaveLength(0);
});
it('initializes sorting correctly', () => {
expect(findRegistrySearch().props('sorting')).toMatchObject({
orderBy: 'created_at',
sort: 'desc',
});
});
it('initializes filters correctly', () => {
expect(findRegistrySearch().props('filters')).toMatchObject([{ value: { data: '' } }]);
});
});
describe('Search', () => {
it('shows search box', () => {
createWrapper();
expect(findRegistrySearch().exists()).toBe(true);
});
it('metrics are added as options for sorting', () => {
createWrapper([], ['bar']);
const labels = findRegistrySearch()
.props('sortableFields')
.map((e) => e.orderBy);
expect(labels).toContain('metric.bar');
});
it('sets the component filters based on the querystring', () => {
setWindowLocation('https://blah?name=A&orderBy=B&sort=C');
createWrapper();
expect(findRegistrySearch().props('filters')).toMatchObject([{ value: { data: 'A' } }]);
});
it('sets the component sort based on the querystring', () => {
setWindowLocation('https://blah?name=A&orderBy=B&sort=C');
createWrapper();
expect(findRegistrySearch().props('sorting')).toMatchObject({ orderBy: 'B', sort: 'c' });
});
it('sets the component sort based on the querystring, when order by is a metric', () => {
setWindowLocation('https://blah?name=A&orderBy=B&sort=C&orderByType=metric');
createWrapper();
expect(findRegistrySearch().props('sorting')).toMatchObject({
orderBy: 'metric.B',
sort: 'c',
});
});
describe('Search submit', () => {
beforeEach(() => {
setWindowLocation('https://blah.com/?name=query&orderBy=name&orderByType=column&sort=asc');
jest.spyOn(urlHelpers, 'visitUrl').mockImplementation(() => {});
createWrapper();
});
it('On submit, resets the cursor and reloads to correct page', () => {
findRegistrySearch().vm.$emit('filter:submit');
expect(urlHelpers.visitUrl).toHaveBeenCalledTimes(1);
expect(urlHelpers.visitUrl).toHaveBeenCalledWith(
'https://blah.com/?name=query&orderBy=name&orderByType=column&sort=asc',
);
});
it('On sorting changed, resets cursor and reloads to correct page', () => {
findRegistrySearch().vm.$emit('sorting:changed', { orderBy: 'created_at' });
expect(urlHelpers.visitUrl).toHaveBeenCalledTimes(1);
expect(urlHelpers.visitUrl).toHaveBeenCalledWith(
'https://blah.com/?name=query&orderBy=created_at&orderByType=column&sort=asc',
);
});
it('On sorting changed and is metric, resets cursor and reloads to correct page', () => {
findRegistrySearch().vm.$emit('sorting:changed', { orderBy: 'metric.auc' });
expect(urlHelpers.visitUrl).toHaveBeenCalledTimes(1);
expect(urlHelpers.visitUrl).toHaveBeenCalledWith(
'https://blah.com/?name=query&orderBy=auc&orderByType=metric&sort=asc',
);
});
it('On direction changed, reloads to correct page', () => {
findRegistrySearch().vm.$emit('sorting:changed', { sort: 'desc' });
expect(urlHelpers.visitUrl).toHaveBeenCalledTimes(1);
expect(urlHelpers.visitUrl).toHaveBeenCalledWith(
'https://blah.com/?name=query&orderBy=name&orderByType=column&sort=desc',
);
});
});
});
describe('Pagination behaviour', () => {
beforeEach(() => {
createWrapperWithCandidates();
});
it('should show', () => {
expect(findPagination().exists()).toBe(true);
});
it('Passes pagination to pagination component', () => {
createWrapperWithCandidates();
expect(findPagination().props('startCursor')).toBe(startCursor);
});
});
describe('Candidate table', () => {
const firstCandidateIndex = 0;
const secondCandidateIndex = 1;
const firstCandidate = candidates[firstCandidateIndex];
beforeEach(() => {
createWrapperWithCandidates();
});
it('renders all rows', () => {
expect(findTableRows()).toHaveLength(candidates.length);
});
it('sets the correct columns in the table', () => {
const expectedColumnNames = [
'Name',
'Created at',
'User',
'L1 Ratio',
'Rmse',
'Auc',
'Mae',
'',
'',
];
expect(findTableHeaders().wrappers.map((h) => h.text())).toEqual(expectedColumnNames);
});
describe('Artifact column', () => {
const artifactColumnIndex = -1;
it('shows the a link to the artifact', () => {
expect(hrefInRowAndColumn(firstCandidateIndex, artifactColumnIndex)).toBe(
firstCandidate.artifact,
);
});
it('shows empty state when no artifact', () => {
expect(findColumnInRow(secondCandidateIndex, artifactColumnIndex).text()).toBe('-');
});
});
describe('User column', () => {
const userColumn = 2;
it('creates a link to the user', () => {
const column = findColumnInRow(firstCandidateIndex, userColumn).findComponent(GlLink);
expect(column.attributes().href).toBe(firstCandidate.user.path);
expect(column.text()).toBe(`@${firstCandidate.user.username}`);
});
it('when there is no user shows empty state', () => {
createWrapperWithCandidates();
expect(findColumnInRow(secondCandidateIndex, userColumn).text()).toBe('-');
});
});
describe('Candidate name column', () => {
const nameColumnIndex = 0;
it('Sets the name', () => {
expect(findColumnInRow(firstCandidateIndex, nameColumnIndex).text()).toBe(
firstCandidate.name,
);
});
it('when there is no user shows nothing', () => {
expect(findColumnInRow(secondCandidateIndex, nameColumnIndex).text()).toBe('');
});
});
describe('Detail column', () => {
const detailColumn = -2;
it('is a link to details', () => {
expect(hrefInRowAndColumn(firstCandidateIndex, detailColumn)).toBe(firstCandidate.details);
});
});
});
});
|