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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
|
import { shallowMount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue';
import { cloneDeep } from 'lodash';
import Vuex from 'vuex';
import { mockTracking, triggerEvent } from 'helpers/tracking_helper';
import DiffFileHeader from '~/diffs/components/diff_file_header.vue';
import { DIFF_FILE_AUTOMATIC_COLLAPSE, DIFF_FILE_MANUAL_COLLAPSE } from '~/diffs/constants';
import { reviewFile } from '~/diffs/store/actions';
import { SET_DIFF_FILE_VIEWED, SET_MR_FILE_REVIEWS } from '~/diffs/store/mutation_types';
import { diffViewerModes } from '~/ide/constants';
import { scrollToElement } from '~/lib/utils/common_utils';
import { truncateSha } from '~/lib/utils/text_utility';
import { __, sprintf } from '~/locale';
import ClipboardButton from '~/vue_shared/components/clipboard_button.vue';
import testAction from '../../__helpers__/vuex_action_helper';
import diffDiscussionsMockData from '../mock_data/diff_discussions';
jest.mock('~/lib/utils/common_utils');
const diffFile = Object.freeze(
Object.assign(diffDiscussionsMockData.diff_file, {
id: '123',
file_hash: 'xyz',
file_identifier_hash: 'abc',
edit_path: 'link:/to/edit/path',
blob: {
id: '848ed9407c6730ff16edb3dd24485a0eea24292a',
path: 'lib/base.js',
name: 'base.js',
mode: '100644',
readable_text: true,
icon: 'doc-text',
},
}),
);
Vue.use(Vuex);
describe('DiffFileHeader component', () => {
let wrapper;
let mockStoreConfig;
const diffHasExpandedDiscussionsResultMock = jest.fn();
const diffHasDiscussionsResultMock = jest.fn();
const defaultMockStoreConfig = {
state: {},
modules: {
diffs: {
namespaced: true,
getters: {
diffHasExpandedDiscussions: () => diffHasExpandedDiscussionsResultMock,
diffHasDiscussions: () => diffHasDiscussionsResultMock,
},
actions: {
toggleFileDiscussions: jest.fn(),
toggleFileDiscussionWrappers: jest.fn(),
toggleFullDiff: jest.fn(),
setCurrentFileHash: jest.fn(),
setFileCollapsedByUser: jest.fn(),
reviewFile: jest.fn(),
},
},
},
};
afterEach(() => {
[
diffHasDiscussionsResultMock,
diffHasExpandedDiscussionsResultMock,
...Object.values(mockStoreConfig.modules.diffs.actions),
].forEach((mock) => mock.mockReset());
});
const findHeader = () => wrapper.findComponent({ ref: 'header' });
const findTitleLink = () => wrapper.findComponent({ ref: 'titleWrapper' });
const findExpandButton = () => wrapper.findComponent({ ref: 'expandDiffToFullFileButton' });
const findFileActions = () => wrapper.find('.file-actions');
const findModeChangedLine = () => wrapper.findComponent({ ref: 'fileMode' });
const findLfsLabel = () => wrapper.find('[data-testid="label-lfs"]');
const findToggleDiscussionsButton = () =>
wrapper.findComponent({ ref: 'toggleDiscussionsButton' });
const findExternalLink = () => wrapper.findComponent({ ref: 'externalLink' });
const findReplacedFileButton = () => wrapper.findComponent({ ref: 'replacedFileButton' });
const findViewFileButton = () => wrapper.findComponent({ ref: 'viewButton' });
const findCollapseIcon = () => wrapper.findComponent({ ref: 'collapseIcon' });
const findEditButton = () => wrapper.findComponent({ ref: 'editButton' });
const findReviewFileCheckbox = () => wrapper.find("[data-testid='fileReviewCheckbox']");
const createComponent = ({ props, options = {} } = {}) => {
mockStoreConfig = cloneDeep(defaultMockStoreConfig);
const store = new Vuex.Store({ ...mockStoreConfig, ...(options.store || {}) });
wrapper = shallowMount(DiffFileHeader, {
propsData: {
diffFile,
canCurrentUserFork: false,
viewDiffsFileByFile: false,
...props,
},
...options,
store,
});
};
it.each`
visibility | collapsible
${'visible'} | ${true}
${'hidden'} | ${false}
`('collapse toggle is $visibility if collapsible is $collapsible', ({ collapsible }) => {
createComponent({ props: { collapsible } });
expect(findCollapseIcon().exists()).toBe(collapsible);
});
it.each`
expanded | icon
${true} | ${'chevron-down'}
${false} | ${'chevron-right'}
`('collapse icon is $icon if expanded is $expanded', ({ icon, expanded }) => {
createComponent({ props: { expanded, collapsible: true } });
expect(findCollapseIcon().props('name')).toBe(icon);
});
it('when header is clicked emits toggleFile', async () => {
createComponent();
findHeader().trigger('click');
await nextTick();
expect(wrapper.emitted().toggleFile).toBeDefined();
});
it('when collapseIcon is clicked emits toggleFile', async () => {
createComponent({ props: { collapsible: true } });
findCollapseIcon().vm.$emit('click', new Event('click'));
await nextTick();
expect(wrapper.emitted().toggleFile).toBeDefined();
});
it('when other element in header is clicked does not emits toggleFile', async () => {
createComponent({ props: { collapsible: true } });
findTitleLink().trigger('click');
await nextTick();
expect(wrapper.emitted().toggleFile).not.toBeDefined();
});
describe('copy to clipboard', () => {
beforeEach(() => {
createComponent();
});
it('displays a copy to clipboard button', () => {
expect(wrapper.findComponent(ClipboardButton).exists()).toBe(true);
});
it('triggers the copy to clipboard tracking event', () => {
const trackingSpy = mockTracking('_category_', wrapper.vm.$el, jest.spyOn);
triggerEvent('[data-testid="diff-file-copy-clipboard"]');
expect(trackingSpy).toHaveBeenCalledWith('_category_', 'click_copy_file_button', {
label: 'diff_copy_file_path_button',
property: 'diff_copy_file',
});
});
});
describe('for submodule', () => {
const submoduleDiffFile = {
...diffFile,
submodule: true,
submodule_link: 'link://to/submodule',
};
it('prefers submodule_tree_url over submodule_link for href', () => {
const submoduleTreeUrl = 'some://tree/url';
createComponent({
props: {
discussionLink: 'discussionLink',
diffFile: {
...submoduleDiffFile,
submodule_tree_url: 'some://tree/url',
},
},
});
expect(findTitleLink().attributes('href')).toBe(submoduleTreeUrl);
});
it('uses submodule_link for href if submodule_tree_url does not exists', () => {
const submoduleLink = 'link://to/submodule';
createComponent({
props: {
discussionLink: 'discussionLink',
diffFile: submoduleDiffFile,
},
});
expect(findTitleLink().attributes('href')).toBe(submoduleLink);
});
it('uses file_path + SHA as link text', () => {
createComponent({
props: {
diffFile: submoduleDiffFile,
},
});
expect(findTitleLink().text()).toContain(
`${diffFile.file_path} @ ${truncateSha(diffFile.blob.id)}`,
);
});
it('does not render file actions', () => {
createComponent({
props: {
diffFile: submoduleDiffFile,
addMergeRequestButtons: true,
},
});
expect(findFileActions().exists()).toBe(false);
});
});
describe('for any file', () => {
const allModes = Object.keys(diffViewerModes).map((m) => [m]);
it.each(allModes)('for %s file mode displays mode changes', (mode) => {
createComponent({
props: {
diffFile: {
...diffFile,
mode_changed: true,
a_mode: 'old-mode',
b_mode: 'new-mode',
viewer: {
...diffFile.viewer,
name: diffViewerModes[mode],
},
},
},
});
expect(findModeChangedLine().text()).toMatch(/old-mode.+new-mode/);
});
it.each(allModes.filter((m) => m[0] !== 'mode_changed'))(
'for %s file mode does not display mode changes',
(mode) => {
createComponent({
props: {
diffFile: {
...diffFile,
mode_changed: false,
a_mode: 'old-mode',
b_mode: 'new-mode',
viewer: {
...diffFile.viewer,
name: diffViewerModes[mode],
},
},
},
});
expect(findModeChangedLine().exists()).toBe(false);
},
);
it('displays the LFS label for files stored in LFS', () => {
createComponent({
props: {
diffFile: { ...diffFile, stored_externally: true, external_storage: 'lfs' },
},
});
expect(findLfsLabel().exists()).toBe(true);
});
it('does not display the LFS label for files stored in repository', () => {
createComponent({
props: {
diffFile: { ...diffFile, stored_externally: false },
},
});
expect(findLfsLabel().exists()).toBe(false);
});
it('does not render view replaced file button if no replaced view path is present', () => {
createComponent({
props: {
diffFile: { ...diffFile, replaced_view_path: null },
},
});
expect(findReplacedFileButton().exists()).toBe(false);
});
describe('when addMergeRequestButtons is false', () => {
it('does not render file actions', () => {
createComponent({ props: { addMergeRequestButtons: false } });
expect(findFileActions().exists()).toBe(false);
});
it('should not render edit button', () => {
createComponent({ props: { addMergeRequestButtons: false } });
expect(findEditButton().exists()).toBe(false);
});
});
describe('when addMergeRequestButtons is true', () => {
describe('without discussions', () => {
it('does not render a toggle discussions button', () => {
diffHasDiscussionsResultMock.mockReturnValue(false);
createComponent({ props: { addMergeRequestButtons: true } });
expect(findToggleDiscussionsButton().exists()).toBe(false);
});
});
describe('with discussions', () => {
it('dispatches toggleFileDiscussionWrappers when user clicks on toggle discussions button', () => {
diffHasDiscussionsResultMock.mockReturnValue(true);
createComponent({ props: { addMergeRequestButtons: true } });
expect(findToggleDiscussionsButton().exists()).toBe(true);
findToggleDiscussionsButton().vm.$emit('click');
expect(
mockStoreConfig.modules.diffs.actions.toggleFileDiscussionWrappers,
).toHaveBeenCalledWith(expect.any(Object), diffFile);
});
});
it('should show edit button', () => {
createComponent({
props: {
addMergeRequestButtons: true,
},
});
expect(findEditButton().exists()).toBe(true);
});
describe('view on environment button', () => {
it('is displayed when external url is provided', () => {
const externalUrl = 'link://to/external';
const formattedExternalUrl = 'link://formatted';
createComponent({
props: {
diffFile: {
...diffFile,
external_url: externalUrl,
formatted_external_url: formattedExternalUrl,
},
addMergeRequestButtons: true,
},
});
expect(findExternalLink().exists()).toBe(true);
});
it('is hidden by default', () => {
createComponent({ props: { addMergeRequestButtons: true } });
expect(findExternalLink().exists()).toBe(false);
});
});
describe('without file blob', () => {
beforeEach(() => {
createComponent({ props: { diffFile: { ...diffFile, blob: false } } });
});
it('should not render toggle discussions button', () => {
expect(findToggleDiscussionsButton().exists()).toBe(false);
});
it('should not render edit button', () => {
expect(findEditButton().exists()).toBe(false);
});
});
describe('with file blob', () => {
it('should render correct file view button', () => {
const viewPath = 'link://view-path';
createComponent({
props: {
diffFile: { ...diffFile, view_path: viewPath },
addMergeRequestButtons: true,
},
});
expect(findViewFileButton().attributes('href')).toBe(viewPath);
expect(findViewFileButton().text()).toEqual(
`View file @ ${diffFile.content_sha.substr(0, 8)}`,
);
});
});
});
describe('expand full file button', () => {
describe('when diff is fully expanded', () => {
it('is not rendered', () => {
createComponent({
props: {
diffFile: {
...diffFile,
is_fully_expanded: true,
},
},
});
expect(findExpandButton().exists()).toBe(false);
});
});
describe('when diff is not fully expanded', () => {
const fullyNotExpandedFileProps = {
diffFile: {
...diffFile,
is_fully_expanded: false,
edit_path: 'link/to/edit/path.txt',
isShowingFullFile: false,
},
addMergeRequestButtons: true,
};
it('renders expand to full file button if not showing full file already', () => {
createComponent({ props: fullyNotExpandedFileProps });
expect(findExpandButton().exists()).toBe(true);
});
it('renders loading icon when loading full file', () => {
createComponent({ props: fullyNotExpandedFileProps });
expect(findExpandButton().exists()).toBe(true);
});
it('toggles full diff on click', () => {
createComponent({ props: fullyNotExpandedFileProps });
findExpandButton().vm.$emit('click');
expect(mockStoreConfig.modules.diffs.actions.toggleFullDiff).toHaveBeenCalled();
});
});
});
it('uses discussionPath for link if it is defined', () => {
const discussionPath = 'link://to/discussion';
createComponent({
props: {
discussionPath,
},
});
expect(findTitleLink().attributes('href')).toBe(discussionPath);
});
it('uses local anchor for link as last resort', () => {
createComponent();
expect(findTitleLink().attributes('href')).toMatch(/^#diff-content/);
});
describe('when local anchor for link is clicked', () => {
beforeEach(() => {
createComponent();
});
it('scrolls to target', () => {
findTitleLink().trigger('click');
expect(scrollToElement).toHaveBeenCalled();
});
it('updates anchor in URL', () => {
findTitleLink().trigger('click');
expect(window.location.href).toMatch(/#diff-content/);
});
});
});
describe('for new file', () => {
it('displays the path', () => {
createComponent({ props: { diffFile: { ...diffFile, new_file: true } } });
expect(findTitleLink().text()).toBe(diffFile.file_path);
});
});
describe('for deleted file', () => {
it('displays the path', () => {
createComponent({ props: { diffFile: { ...diffFile, deleted_file: true } } });
expect(findTitleLink().text()).toBe(
sprintf(__('%{filePath} deleted'), { filePath: diffFile.file_path }, false),
);
});
it('does not show edit button', () => {
createComponent({ props: { diffFile: { ...diffFile, deleted_file: true } } });
expect(findEditButton().exists()).toBe(false);
});
});
describe('for renamed file', () => {
it('displays old and new path if the file was renamed', () => {
createComponent({
props: {
diffFile: {
...diffFile,
renamed_file: true,
old_path_html: 'old',
new_path_html: 'new',
},
},
});
expect(findTitleLink().text()).toMatch(/^old.+new/s);
});
});
describe('for replaced file', () => {
it('renders view replaced file button', () => {
const replacedViewPath = 'some/path';
createComponent({
props: {
diffFile: {
...diffFile,
replaced_view_path: replacedViewPath,
},
addMergeRequestButtons: true,
},
});
expect(findReplacedFileButton().exists()).toBe(true);
});
});
describe('file reviews', () => {
it('calls the action to set the new review', () => {
createComponent({
props: {
diffFile: {
...diffFile,
viewer: {
...diffFile.viewer,
automaticallyCollapsed: false,
manuallyCollapsed: null,
},
},
showLocalFileReviews: true,
addMergeRequestButtons: true,
},
});
const file = wrapper.vm.diffFile;
findReviewFileCheckbox().vm.$emit('change', true);
return testAction(
reviewFile,
{ file, reviewed: true },
{},
[
{ type: SET_DIFF_FILE_VIEWED, payload: { id: file.file_hash, seen: true } },
{
type: SET_MR_FILE_REVIEWS,
payload: { [file.file_identifier_hash]: [file.id, `hash:${file.file_hash}`] },
},
],
[],
);
});
it.each`
description | newReviewedStatus | collapseType | aCollapse | mCollapse | callAction
${'does nothing'} | ${true} | ${DIFF_FILE_MANUAL_COLLAPSE} | ${false} | ${true} | ${false}
${'does nothing'} | ${false} | ${DIFF_FILE_AUTOMATIC_COLLAPSE} | ${true} | ${null} | ${false}
${'does nothing'} | ${true} | ${'not collapsed'} | ${false} | ${null} | ${false}
${'does nothing'} | ${false} | ${'not collapsed'} | ${false} | ${null} | ${false}
${'collapses the file'} | ${true} | ${DIFF_FILE_AUTOMATIC_COLLAPSE} | ${true} | ${null} | ${true}
`(
"$description if the new review status is reviewed = $newReviewedStatus and the file's collapse type is collapse = $collapseType",
({ newReviewedStatus, aCollapse, mCollapse, callAction }) => {
createComponent({
props: {
diffFile: {
...diffFile,
viewer: {
...diffFile.viewer,
automaticallyCollapsed: aCollapse,
manuallyCollapsed: mCollapse,
},
},
showLocalFileReviews: true,
addMergeRequestButtons: true,
},
});
findReviewFileCheckbox().vm.$emit('change', newReviewedStatus);
if (callAction) {
expect(mockStoreConfig.modules.diffs.actions.setFileCollapsedByUser).toHaveBeenCalled();
} else {
expect(
mockStoreConfig.modules.diffs.actions.setFileCollapsedByUser,
).not.toHaveBeenCalled();
}
},
);
it.each`
description | show | visible
${'shows'} | ${true} | ${true}
${'hides'} | ${false} | ${false}
`(
'$description the file review feature given { showLocalFileReviewsProp: $show }',
({ show, visible }) => {
createComponent({
props: {
showLocalFileReviews: show,
addMergeRequestButtons: true,
},
});
expect(findReviewFileCheckbox().exists()).toEqual(visible);
},
);
it.each`
open | status | fires
${true} | ${true} | ${true}
${false} | ${false} | ${true}
${true} | ${false} | ${false}
${false} | ${true} | ${false}
`(
'toggles appropriately when { fileExpanded: $open, newReviewStatus: $status }',
({ open, status, fires }) => {
createComponent({
props: {
diffFile: {
...diffFile,
viewer: {
...diffFile.viewer,
automaticallyCollapsed: false,
manuallyCollapsed: null,
},
},
showLocalFileReviews: true,
addMergeRequestButtons: true,
expanded: open,
},
});
findReviewFileCheckbox().vm.$emit('change', status);
expect(Boolean(wrapper.emitted().toggleFile)).toBe(fires);
},
);
});
});
|