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
|
import MockAdapter from 'axios-mock-adapter';
import $ from 'jquery';
import { loadHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import initMrPage from 'helpers/init_vue_mr_page_helper';
import { stubPerformanceWebAPI } from 'helpers/performance';
import axios from '~/lib/utils/axios_utils';
import MergeRequestTabs from '~/merge_request_tabs';
import Diff from '~/diff';
import '~/lib/utils/common_utils';
import '~/lib/utils/url_utility';
jest.mock('~/lib/utils/webpack', () => ({
resetServiceWorkersPublicPath: jest.fn(),
}));
describe('MergeRequestTabs', () => {
const testContext = {};
const stubLocation = {};
const setLocation = (stubs) => {
const defaults = {
pathname: '',
search: '',
hash: '',
};
$.extend(stubLocation, defaults, stubs || {});
};
beforeEach(() => {
stubPerformanceWebAPI();
initMrPage();
testContext.class = new MergeRequestTabs({ stubLocation });
setLocation();
testContext.spies = {
history: jest.spyOn(window.history, 'pushState').mockImplementation(() => {}),
};
gl.mrWidget = {};
});
describe('clickTab', () => {
let params;
beforeEach(() => {
document.documentElement.scrollTop = 100;
params = {
metaKey: false,
ctrlKey: false,
which: 1,
stopImmediatePropagation() {},
preventDefault() {},
currentTarget: {
getAttribute(attr) {
return attr === 'href' ? 'a/tab/url' : null;
},
},
};
});
it("stores the current scroll position if there's an active tab", () => {
testContext.class.currentTab = 'someTab';
testContext.class.clickTab(params);
expect(testContext.class.scrollPositions.someTab).toBe(100);
});
it("doesn't store a scroll position if there's no active tab", () => {
// this happens on first load, and we just don't want to store empty values in the `null` property
testContext.class.currentTab = null;
testContext.class.clickTab(params);
expect(testContext.class.scrollPositions).toEqual({});
});
});
describe('opensInNewTab', () => {
const windowTarget = '_blank';
let clickTabParams;
let tabUrl;
beforeEach(() => {
loadHTMLFixture('merge_requests/merge_request_with_task_list.html');
tabUrl = $('.commits-tab a').attr('href');
clickTabParams = {
metaKey: false,
ctrlKey: false,
which: 1,
stopImmediatePropagation() {},
preventDefault() {},
currentTarget: {
getAttribute(attr) {
return attr === 'href' ? tabUrl : null;
},
},
};
});
afterEach(() => {
resetHTMLFixture();
});
describe('meta click', () => {
let metakeyEvent;
beforeEach(() => {
metakeyEvent = $.Event('click', { keyCode: 91, ctrlKey: true });
});
it('opens page when commits link is clicked', () => {
jest.spyOn(window, 'open').mockImplementation((url, name) => {
expect(url).toEqual(tabUrl);
expect(name).toEqual(windowTarget);
});
testContext.class.bindEvents();
$('.merge-request-tabs .commits-tab a').trigger(metakeyEvent);
expect(window.open).toHaveBeenCalled();
});
it('opens page when commits badge is clicked', () => {
jest.spyOn(window, 'open').mockImplementation((url, name) => {
expect(url).toEqual(tabUrl);
expect(name).toEqual(windowTarget);
});
testContext.class.bindEvents();
$('.merge-request-tabs .commits-tab a .badge').trigger(metakeyEvent);
expect(window.open).toHaveBeenCalled();
});
});
it('opens page tab in a new browser tab with Ctrl+Click - Windows/Linux', () => {
jest.spyOn(window, 'open').mockImplementation((url, name) => {
expect(url).toEqual(tabUrl);
expect(name).toEqual(windowTarget);
});
testContext.class.clickTab({ ...clickTabParams, metaKey: true });
expect(window.open).toHaveBeenCalled();
});
it('opens page tab in a new browser tab with Cmd+Click - Mac', () => {
jest.spyOn(window, 'open').mockImplementation((url, name) => {
expect(url).toEqual(tabUrl);
expect(name).toEqual(windowTarget);
});
testContext.class.clickTab({ ...clickTabParams, ctrlKey: true });
expect(window.open).toHaveBeenCalled();
});
it('opens page tab in a new browser tab with Middle-click - Mac/PC', () => {
jest.spyOn(window, 'open').mockImplementation((url, name) => {
expect(url).toEqual(tabUrl);
expect(name).toEqual(windowTarget);
});
testContext.class.clickTab({ ...clickTabParams, which: 2 });
expect(window.open).toHaveBeenCalled();
});
});
describe('setCurrentAction', () => {
let mock;
beforeEach(() => {
mock = new MockAdapter(axios);
mock.onAny().reply({ data: {} });
testContext.subject = testContext.class.setCurrentAction;
});
afterEach(() => {
mock.restore();
window.history.replaceState({}, '', '/');
});
it('changes from commits', () => {
setLocation({
pathname: '/foo/bar/-/merge_requests/1/commits',
});
expect(testContext.subject('show')).toBe('/foo/bar/-/merge_requests/1');
expect(testContext.subject('diffs')).toBe('/foo/bar/-/merge_requests/1/diffs');
});
it('changes from diffs', () => {
setLocation({
pathname: '/foo/bar/-/merge_requests/1/diffs',
});
expect(testContext.subject('show')).toBe('/foo/bar/-/merge_requests/1');
expect(testContext.subject('commits')).toBe('/foo/bar/-/merge_requests/1/commits');
});
it('changes from diffs.html', () => {
setLocation({
pathname: '/foo/bar/-/merge_requests/1/diffs.html',
});
expect(testContext.subject('show')).toBe('/foo/bar/-/merge_requests/1');
expect(testContext.subject('commits')).toBe('/foo/bar/-/merge_requests/1/commits');
});
it('changes from notes', () => {
setLocation({
pathname: '/foo/bar/-/merge_requests/1',
});
expect(testContext.subject('diffs')).toBe('/foo/bar/-/merge_requests/1/diffs');
expect(testContext.subject('commits')).toBe('/foo/bar/-/merge_requests/1/commits');
});
it('includes search parameters and hash string', () => {
setLocation({
pathname: '/foo/bar/-/merge_requests/1/diffs',
search: '?view=parallel',
hash: '#L15-35',
});
expect(testContext.subject('show')).toBe('/foo/bar/-/merge_requests/1?view=parallel#L15-35');
});
it('replaces the current history state', () => {
setLocation({
pathname: '/foo/bar/-/merge_requests/1',
});
window.history.replaceState(
{
url: window.location.href,
action: 'show',
},
document.title,
window.location.href,
);
const newState = testContext.subject('commits');
expect(testContext.spies.history).toHaveBeenCalledWith(
{
url: newState,
action: 'commits',
},
document.title,
newState,
);
});
it('treats "show" like "notes"', () => {
setLocation({
pathname: '/foo/bar/-/merge_requests/1/commits',
});
expect(testContext.subject('show')).toBe('/foo/bar/-/merge_requests/1');
});
});
describe('expandViewContainer', () => {
beforeEach(() => {
$('body').append(
'<div class="content-wrapper"><div class="container-fluid container-limited"></div></div>',
);
});
afterEach(() => {
$('.content-wrapper').remove();
});
it('removes container-limited from containers', () => {
testContext.class.expandViewContainer();
expect($('.content-wrapper .container-limited')).toHaveLength(0);
});
it('does not add container-limited when fluid layout is prefered', () => {
$('.content-wrapper .container-fluid').removeClass('container-limited');
testContext.class.expandViewContainer(false);
expect($('.content-wrapper .container-limited')).toHaveLength(0);
});
it('does remove container-limited from breadcrumbs', () => {
$('.container-limited').addClass('breadcrumbs');
testContext.class.expandViewContainer();
expect($('.content-wrapper .container-limited')).toHaveLength(1);
});
});
describe('tabShown', () => {
const mainContent = document.createElement('div');
const tabContent = document.createElement('div');
beforeEach(() => {
$.fn.renderGFM = jest.fn();
jest.spyOn(mainContent, 'getBoundingClientRect').mockReturnValue({ top: 10 });
jest.spyOn(tabContent, 'getBoundingClientRect').mockReturnValue({ top: 100 });
jest.spyOn(window, 'scrollTo').mockImplementation(() => {});
jest.spyOn(document, 'querySelector').mockImplementation((selector) => {
return selector === '.content-wrapper' ? mainContent : tabContent;
});
testContext.class.currentAction = 'commits';
});
it('calls window scrollTo with options if document has scrollBehavior', () => {
document.documentElement.style.scrollBehavior = '';
testContext.class.tabShown('commits', 'foobar');
expect(window.scrollTo.mock.calls[0][0]).toEqual({ top: 39, behavior: 'smooth' });
});
it('calls window scrollTo with two args if document does not have scrollBehavior', () => {
jest.spyOn(document.documentElement, 'style', 'get').mockReturnValue({});
testContext.class.tabShown('commits', 'foobar');
expect(window.scrollTo.mock.calls[0]).toEqual([0, 39]);
});
it.each`
tab | hides | hidesText
${'show'} | ${false} | ${'shows'}
${'diffs'} | ${true} | ${'hides'}
${'commits'} | ${true} | ${'hides'}
`('$hidesText expand button on $tab tab', ({ tab, hides }) => {
window.gon = { features: { movedMrSidebar: true } };
const expandButton = document.createElement('div');
expandButton.classList.add('js-expand-sidebar');
const tabsContainer = document.createElement('div');
tabsContainer.innerHTML =
'<div class="tab-content"><div id="diff-notes-app"></div><div class="commits tab-pane"></div></div>';
tabsContainer.classList.add('merge-request-tabs-container');
tabsContainer.appendChild(expandButton);
document.body.appendChild(tabsContainer);
testContext.class = new MergeRequestTabs({ stubLocation });
testContext.class.tabShown(tab, 'foobar');
testContext.class.expandSidebar.forEach((el) => {
expect(el.classList.contains('gl-display-none!')).toBe(hides);
});
});
describe('when switching tabs', () => {
const SCROLL_TOP = 100;
beforeEach(() => {
jest.spyOn(window, 'scrollTo').mockImplementation(() => {});
testContext.class.mergeRequestTabs = document.createElement('div');
testContext.class.mergeRequestTabPanes = document.createElement('div');
testContext.class.currentTab = 'tab';
testContext.class.scrollPositions = { newTab: SCROLL_TOP };
});
it('scrolls to the stored position, if one is stored', () => {
testContext.class.tabShown('newTab');
jest.advanceTimersByTime(250);
expect(window.scrollTo.mock.calls[0][0]).toEqual({
top: SCROLL_TOP,
left: 0,
behavior: 'auto',
});
});
it('scrolls to 0, if no position is stored', () => {
testContext.class.tabShown('unknownTab');
jest.advanceTimersByTime(250);
expect(window.scrollTo.mock.calls[0][0]).toEqual({ top: 0, left: 0, behavior: 'auto' });
});
});
});
describe('tabs <-> diff interactions', () => {
beforeEach(() => {
jest.spyOn(testContext.class, 'loadDiff').mockImplementation(() => {});
});
describe('switchViewType', () => {
it('marks the class as having not loaded diffs already', () => {
testContext.class.diffsLoaded = true;
testContext.class.switchViewType({});
expect(testContext.class.diffsLoaded).toBe(false);
});
it('reloads the diffs', () => {
testContext.class.switchViewType({ source: 'a new url' });
expect(testContext.class.loadDiff).toHaveBeenCalledWith({
endpoint: 'a new url',
strip: false,
});
});
});
describe('createDiff', () => {
it("creates a Diff if there isn't one", () => {
expect(testContext.class.diffsClass).toBe(null);
testContext.class.createDiff();
expect(testContext.class.diffsClass).toBeInstanceOf(Diff);
});
it("doesn't create a Diff if one already exists", () => {
testContext.class.diffsClass = 'truthy';
testContext.class.createDiff();
expect(testContext.class.diffsClass).toBe('truthy');
});
it('sets the available MR Tabs event hub to the new Diff', () => {
expect(testContext.class.diffsClass).toBe(null);
testContext.class.createDiff();
expect(testContext.class.diffsClass.mrHub).toBe(testContext.class.eventHub);
});
});
describe('setHubToDiff', () => {
it('sets the MR Tabs event hub to the child Diff', () => {
testContext.class.diffsClass = {};
testContext.class.setHubToDiff();
expect(testContext.class.diffsClass.mrHub).toBe(testContext.class.eventHub);
});
it('does not fatal if theres no child Diff', () => {
testContext.class.diffsClass = null;
expect(() => {
testContext.class.setHubToDiff();
}).not.toThrow();
});
});
});
});
|