gitlab-org--gitlab-foss/spec/frontend/merge_request_tabs_spec.js

294 lines
8.5 KiB
JavaScript
Raw Normal View History

import $ from 'jquery';
2018-01-31 04:27:30 -05:00
import MockAdapter from 'axios-mock-adapter';
import initMrPage from 'helpers/init_vue_mr_page_helper';
import axios from '~/lib/utils/axios_utils';
2017-12-15 04:31:58 -05:00
import MergeRequestTabs from '~/merge_request_tabs';
2017-05-16 17:57:05 -04:00
import '~/commit/pipelines/pipelines_bundle';
import '~/lib/utils/common_utils';
import 'vendor/jquery.scrollTo';
2018-06-21 08:22:40 -04:00
jest.mock('~/lib/utils/webpack', () => ({
resetServiceWorkersPublicPath: jest.fn(),
}));
describe('MergeRequestTabs', () => {
const testContext = {};
const stubLocation = {};
const setLocation = stubs => {
const defaults = {
2018-06-21 08:22:40 -04:00
pathname: '',
search: '',
hash: '',
2016-07-24 16:45:11 -04:00
};
2018-06-21 08:22:40 -04:00
$.extend(stubLocation, defaults, stubs || {});
};
2018-06-21 08:22:40 -04:00
preloadFixtures(
'merge_requests/merge_request_with_task_list.html',
'merge_requests/diff_comment.html',
2018-06-21 08:22:40 -04:00
);
2017-02-06 05:06:57 -05:00
beforeEach(() => {
initMrPage();
testContext.class = new MergeRequestTabs({ stubLocation });
2018-06-21 08:22:40 -04:00
setLocation();
testContext.spies = {
history: jest.spyOn(window.history, 'pushState').mockImplementation(() => {}),
2018-06-21 08:22:40 -04:00
};
gl.mrWidget = {};
2018-06-21 08:22:40 -04:00
});
describe('opensInNewTab', () => {
const windowTarget = '_blank';
let clickTabParams;
let tabUrl;
beforeEach(() => {
loadFixtures('merge_requests/merge_request_with_task_list.html');
2018-06-21 08:22:40 -04:00
tabUrl = $('.commits-tab a').attr('href');
clickTabParams = {
metaKey: false,
ctrlKey: false,
which: 1,
stopImmediatePropagation() {},
preventDefault() {},
currentTarget: {
getAttribute(attr) {
return attr === 'href' ? tabUrl : null;
},
},
};
2018-06-21 08:22:40 -04:00
});
2018-06-21 08:22:40 -04:00
describe('meta click', () => {
let metakeyEvent;
beforeEach(() => {
2018-06-21 08:22:40 -04:00
metakeyEvent = $.Event('click', { keyCode: 91, ctrlKey: true });
2017-02-06 05:06:57 -05:00
});
2017-04-07 00:19:30 -04:00
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();
2018-06-21 08:22:40 -04:00
$('.merge-request-tabs .commits-tab a').trigger(metakeyEvent);
expect(window.open).toHaveBeenCalled();
2017-02-06 05:06:57 -05:00
});
2017-04-07 00:19:30 -04:00
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();
2018-06-21 08:22:40 -04:00
$('.merge-request-tabs .commits-tab a .badge').trigger(metakeyEvent);
expect(window.open).toHaveBeenCalled();
});
2017-02-06 05:06:57 -05:00
});
it('opens page tab in a new browser tab with Ctrl+Click - Windows/Linux', () => {
jest.spyOn(window, 'open').mockImplementation((url, name) => {
2018-06-21 08:22:40 -04:00
expect(url).toEqual(tabUrl);
expect(name).toEqual(windowTarget);
2016-07-24 16:45:11 -04:00
});
2017-04-07 00:19:30 -04:00
testContext.class.clickTab({ ...clickTabParams, metaKey: true });
expect(window.open).toHaveBeenCalled();
2018-06-21 08:22:40 -04:00
});
2017-04-07 00:19:30 -04:00
it('opens page tab in a new browser tab with Cmd+Click - Mac', () => {
jest.spyOn(window, 'open').mockImplementation((url, name) => {
2018-06-21 08:22:40 -04:00
expect(url).toEqual(tabUrl);
expect(name).toEqual(windowTarget);
2016-07-24 16:45:11 -04:00
});
2017-04-07 00:19:30 -04:00
testContext.class.clickTab({ ...clickTabParams, ctrlKey: true });
expect(window.open).toHaveBeenCalled();
2018-06-21 08:22:40 -04:00
});
2017-04-07 00:19:30 -04:00
it('opens page tab in a new browser tab with Middle-click - Mac/PC', () => {
jest.spyOn(window, 'open').mockImplementation((url, name) => {
2018-06-21 08:22:40 -04:00
expect(url).toEqual(tabUrl);
expect(name).toEqual(windowTarget);
2016-07-24 16:45:11 -04:00
});
2017-04-07 00:19:30 -04:00
testContext.class.clickTab({ ...clickTabParams, which: 2 });
expect(window.open).toHaveBeenCalled();
2016-07-24 16:45:11 -04:00
});
2018-06-21 08:22:40 -04:00
});
describe('setCurrentAction', () => {
2018-06-21 08:22:40 -04:00
let mock;
beforeEach(() => {
2018-06-21 08:22:40 -04:00
mock = new MockAdapter(axios);
mock.onAny().reply({ data: {} });
testContext.subject = testContext.class.setCurrentAction;
2018-06-21 08:22:40 -04:00
});
2018-06-21 08:22:40 -04:00
afterEach(() => {
mock.restore();
window.history.replaceState({}, '', '/');
2018-06-21 08:22:40 -04:00
});
it('changes from commits', () => {
2018-06-21 08:22:40 -04:00
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', () => {
2018-06-21 08:22:40 -04:00
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');
2018-06-21 08:22:40 -04:00
});
it('changes from diffs.html', () => {
2018-06-21 08:22:40 -04:00
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');
2018-06-21 08:22:40 -04:00
});
it('changes from notes', () => {
2018-06-21 08:22:40 -04:00
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');
2018-06-21 08:22:40 -04:00
});
it('includes search parameters and hash string', () => {
2018-06-21 08:22:40 -04:00
setLocation({
pathname: '/foo/bar/-/merge_requests/1/diffs',
2018-06-21 08:22:40 -04:00
search: '?view=parallel',
hash: '#L15-35',
});
expect(testContext.subject('show')).toBe('/foo/bar/-/merge_requests/1?view=parallel#L15-35');
2018-06-21 08:22:40 -04:00
});
it('replaces the current history state', () => {
2018-06-21 08:22:40 -04:00
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(
2018-06-21 08:22:40 -04:00
{
url: newState,
action: 'commits',
2018-06-21 08:22:40 -04:00
},
document.title,
newState,
);
});
it('treats "show" like "notes"', () => {
2018-06-21 08:22:40 -04:00
setLocation({
pathname: '/foo/bar/-/merge_requests/1/commits',
});
2018-06-21 08:22:40 -04:00
expect(testContext.subject('show')).toBe('/foo/bar/-/merge_requests/1');
});
2018-06-21 08:22:40 -04:00
});
describe('expandViewContainer', () => {
2018-06-21 08:22:40 -04:00
beforeEach(() => {
$('body').append(
'<div class="content-wrapper"><div class="container-fluid container-limited"></div></div>',
);
});
2018-06-21 08:22:40 -04:00
afterEach(() => {
$('.content-wrapper').remove();
});
it('removes container-limited from containers', () => {
testContext.class.expandViewContainer();
expect($('.content-wrapper .container-limited')).toHaveLength(0);
2018-06-21 08:22:40 -04:00
});
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', () => {
2018-06-21 08:22:40 -04:00
$('.container-limited').addClass('breadcrumbs');
testContext.class.expandViewContainer();
expect($('.content-wrapper .container-limited')).toHaveLength(1);
});
2016-07-24 16:45:11 -04:00
});
2018-11-27 13:05:16 -05:00
describe('tabShown', () => {
2018-11-27 13:05:16 -05:00
const mainContent = document.createElement('div');
const tabContent = document.createElement('div');
beforeEach(() => {
jest.spyOn(mainContent, 'getBoundingClientRect').mockReturnValue({ top: 10 });
jest.spyOn(tabContent, 'getBoundingClientRect').mockReturnValue({ top: 100 });
jest.spyOn(document, 'querySelector').mockImplementation(selector => {
2018-11-27 13:05:16 -05:00
return selector === '.content-wrapper' ? mainContent : tabContent;
});
testContext.class.currentAction = 'commits';
2018-11-27 13:05:16 -05:00
});
it('calls window scrollTo with options if document has scrollBehavior', () => {
2018-11-27 13:05:16 -05:00
document.documentElement.style.scrollBehavior = '';
jest.spyOn(window, 'scrollTo').mockImplementation(() => {});
2018-11-27 13:05:16 -05:00
testContext.class.tabShown('commits', 'foobar');
2018-11-27 13:05:16 -05:00
expect(window.scrollTo.mock.calls[0][0]).toEqual({ top: 39, behavior: 'smooth' });
2018-11-27 13:05:16 -05:00
});
it('calls window scrollTo with two args if document does not have scrollBehavior', () => {
jest.spyOn(document.documentElement, 'style', 'get').mockReturnValue({});
jest.spyOn(window, 'scrollTo').mockImplementation(() => {});
2018-11-27 13:05:16 -05:00
testContext.class.tabShown('commits', 'foobar');
2018-11-27 13:05:16 -05:00
expect(window.scrollTo.mock.calls[0]).toEqual([0, 39]);
2018-11-27 13:05:16 -05:00
});
});
2018-06-21 08:22:40 -04:00
});