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

277 lines
8 KiB
JavaScript
Raw Normal View History

2018-06-21 08:22:40 -04:00
/* eslint-disable no-var, object-shorthand */
import $ from 'jquery';
2018-01-31 04:27:30 -05:00
import MockAdapter from 'axios-mock-adapter';
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 '~/breakpoints';
import '~/lib/utils/common_utils';
import 'vendor/jquery.scrollTo';
2018-06-21 08:22:40 -04:00
import initMrPage from './helpers/init_vue_mr_page_helper';
describe('MergeRequestTabs', function() {
let mrPageMock;
var stubLocation = {};
var setLocation = function(stubs) {
var defaults = {
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.raw',
'merge_requests/diff_comment.html.raw',
);
2017-02-06 05:06:57 -05:00
2018-06-21 08:22:40 -04:00
beforeEach(function() {
mrPageMock = initMrPage();
this.class = new MergeRequestTabs({ stubLocation: stubLocation });
setLocation();
2018-06-21 08:22:40 -04:00
this.spies = {
history: spyOn(window.history, 'replaceState').and.callFake(function() {}),
};
});
2018-06-21 08:22:40 -04:00
afterEach(function() {
this.class.unbindEvents();
this.class.destroyPipelinesView();
mrPageMock.restore();
$('.js-merge-request-test').remove();
2018-06-21 08:22:40 -04:00
});
2018-06-21 08:22:40 -04:00
describe('opensInNewTab', function() {
var tabUrl;
var windowTarget = '_blank';
2018-06-21 08:22:40 -04:00
beforeEach(function() {
loadFixtures('merge_requests/merge_request_with_task_list.html.raw');
2018-06-21 08:22:40 -04:00
tabUrl = $('.commits-tab a').attr('href');
});
2018-06-21 08:22:40 -04:00
describe('meta click', () => {
let metakeyEvent;
beforeEach(function() {
metakeyEvent = $.Event('click', { keyCode: 91, ctrlKey: true });
2017-02-06 05:06:57 -05:00
});
2017-04-07 00:19:30 -04:00
2018-06-21 08:22:40 -04:00
it('opens page when commits link is clicked', function() {
spyOn(window, 'open').and.callFake(function(url, name) {
expect(url).toEqual(tabUrl);
expect(name).toEqual(windowTarget);
});
2018-06-21 08:22:40 -04:00
this.class.bindEvents();
$('.merge-request-tabs .commits-tab a').trigger(metakeyEvent);
2017-02-06 05:06:57 -05:00
});
2017-04-07 00:19:30 -04:00
2018-06-21 08:22:40 -04:00
it('opens page when commits badge is clicked', function() {
spyOn(window, 'open').and.callFake(function(url, name) {
expect(url).toEqual(tabUrl);
expect(name).toEqual(windowTarget);
});
2018-06-21 08:22:40 -04:00
this.class.bindEvents();
$('.merge-request-tabs .commits-tab a .badge').trigger(metakeyEvent);
});
2017-02-06 05:06:57 -05:00
});
2018-06-21 08:22:40 -04:00
it('opens page tab in a new browser tab with Ctrl+Click - Windows/Linux', function() {
spyOn(window, 'open').and.callFake(function(url, name) {
expect(url).toEqual(tabUrl);
expect(name).toEqual(windowTarget);
2016-07-24 16:45:11 -04:00
});
2017-04-07 00:19:30 -04:00
2018-06-21 08:22:40 -04:00
this.class.clickTab({
metaKey: false,
ctrlKey: true,
which: 1,
stopImmediatePropagation: function() {},
2016-07-24 16:45:11 -04:00
});
2018-06-21 08:22:40 -04:00
});
2017-04-07 00:19:30 -04:00
2018-06-21 08:22:40 -04:00
it('opens page tab in a new browser tab with Cmd+Click - Mac', function() {
spyOn(window, 'open').and.callFake(function(url, name) {
expect(url).toEqual(tabUrl);
expect(name).toEqual(windowTarget);
2016-07-24 16:45:11 -04:00
});
2017-04-07 00:19:30 -04:00
2018-06-21 08:22:40 -04:00
this.class.clickTab({
metaKey: true,
ctrlKey: false,
which: 1,
stopImmediatePropagation: function() {},
2016-07-24 16:45:11 -04:00
});
2018-06-21 08:22:40 -04:00
});
2017-04-07 00:19:30 -04:00
2018-06-21 08:22:40 -04:00
it('opens page tab in a new browser tab with Middle-click - Mac/PC', function() {
spyOn(window, 'open').and.callFake(function(url, name) {
expect(url).toEqual(tabUrl);
expect(name).toEqual(windowTarget);
2016-07-24 16:45:11 -04:00
});
2017-04-07 00:19:30 -04:00
2018-06-21 08:22:40 -04:00
this.class.clickTab({
metaKey: false,
ctrlKey: false,
which: 2,
stopImmediatePropagation: function() {},
2016-07-24 16:45:11 -04:00
});
});
2018-06-21 08:22:40 -04:00
});
2018-06-21 08:22:40 -04:00
describe('setCurrentAction', function() {
let mock;
2018-06-21 08:22:40 -04:00
beforeEach(function() {
mock = new MockAdapter(axios);
mock.onAny().reply({ data: {} });
this.subject = this.class.setCurrentAction;
});
2018-06-21 08:22:40 -04:00
afterEach(() => {
mock.restore();
});
2018-06-21 08:22:40 -04:00
it('changes from commits', function() {
setLocation({
pathname: '/foo/bar/merge_requests/1/commits',
});
2018-06-21 08:22:40 -04:00
expect(this.subject('show')).toBe('/foo/bar/merge_requests/1');
expect(this.subject('diffs')).toBe('/foo/bar/merge_requests/1/diffs');
});
2018-06-21 08:22:40 -04:00
it('changes from diffs', function() {
setLocation({
pathname: '/foo/bar/merge_requests/1/diffs',
});
2018-06-21 08:22:40 -04:00
expect(this.subject('show')).toBe('/foo/bar/merge_requests/1');
expect(this.subject('commits')).toBe('/foo/bar/merge_requests/1/commits');
});
2018-06-21 08:22:40 -04:00
it('changes from diffs.html', function() {
setLocation({
pathname: '/foo/bar/merge_requests/1/diffs.html',
});
2018-06-21 08:22:40 -04:00
expect(this.subject('show')).toBe('/foo/bar/merge_requests/1');
expect(this.subject('commits')).toBe('/foo/bar/merge_requests/1/commits');
});
2018-06-21 08:22:40 -04:00
it('changes from notes', function() {
setLocation({
pathname: '/foo/bar/merge_requests/1',
});
2018-06-21 08:22:40 -04:00
expect(this.subject('diffs')).toBe('/foo/bar/merge_requests/1/diffs');
expect(this.subject('commits')).toBe('/foo/bar/merge_requests/1/commits');
});
2018-06-21 08:22:40 -04:00
it('includes search parameters and hash string', function() {
setLocation({
pathname: '/foo/bar/merge_requests/1/diffs',
search: '?view=parallel',
hash: '#L15-35',
});
2018-06-21 08:22:40 -04:00
expect(this.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', function() {
var newState;
setLocation({
pathname: '/foo/bar/merge_requests/1',
});
2018-06-21 08:22:40 -04:00
newState = this.subject('commits');
2018-06-21 08:22:40 -04:00
expect(this.spies.history).toHaveBeenCalledWith(
{
url: newState,
},
document.title,
newState,
);
});
2018-06-21 08:22:40 -04:00
it('treats "show" like "notes"', function() {
setLocation({
pathname: '/foo/bar/merge_requests/1/commits',
});
2018-06-21 08:22:40 -04:00
expect(this.subject('show')).toBe('/foo/bar/merge_requests/1');
});
2018-06-21 08:22:40 -04:00
});
2018-06-21 08:22:40 -04:00
describe('expandViewContainer', function() {
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();
});
2018-06-21 08:22:40 -04:00
it('removes container-limited from containers', function() {
this.class.expandViewContainer();
2018-06-21 08:22:40 -04:00
expect($('.content-wrapper')).not.toContainElement('.container-limited');
});
it('does not add container-limited when fluid layout is prefered', function() {
$('.content-wrapper .container-fluid').removeClass('container-limited');
this.class.expandViewContainer(false);
expect($('.content-wrapper')).not.toContainElement('.container-limited');
});
2018-06-21 08:22:40 -04:00
it('does remove container-limited from breadcrumbs', function() {
$('.container-limited').addClass('breadcrumbs');
this.class.expandViewContainer();
2018-06-21 08:22:40 -04:00
expect($('.content-wrapper')).toContainElement('.container-limited');
});
2016-07-24 16:45:11 -04:00
});
2018-11-27 13:05:16 -05:00
describe('tabShown', function() {
const mainContent = document.createElement('div');
const tabContent = document.createElement('div');
beforeEach(function() {
spyOn(mainContent, 'getBoundingClientRect').and.returnValue({ top: 10 });
spyOn(tabContent, 'getBoundingClientRect').and.returnValue({ top: 100 });
spyOn(document, 'querySelector').and.callFake(function(selector) {
return selector === '.content-wrapper' ? mainContent : tabContent;
});
this.class.currentAction = 'commits';
});
it('calls window scrollTo with options if document has scrollBehavior', function() {
document.documentElement.style.scrollBehavior = '';
spyOn(window, 'scrollTo');
this.class.tabShown('commits', 'foobar');
expect(window.scrollTo.calls.first().args[0]).toEqual({ top: 39, behavior: 'smooth' });
});
it('calls window scrollTo with two args if document does not have scrollBehavior', function() {
spyOnProperty(document.documentElement, 'style', 'get').and.returnValue({});
spyOn(window, 'scrollTo');
this.class.tabShown('commits', 'foobar');
expect(window.scrollTo.calls.first().args).toEqual([0, 39]);
});
});
2018-06-21 08:22:40 -04:00
});