debian-mirror-gitlab/spec/frontend/blob/viewer/index_spec.js

180 lines
4.5 KiB
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
/* eslint-disable no-new */
2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2018-03-17 18:26:18 +05:30
import MockAdapter from 'axios-mock-adapter';
2017-08-17 22:00:37 +05:30
import BlobViewer from '~/blob/viewer/index';
2018-03-17 18:26:18 +05:30
import axios from '~/lib/utils/axios_utils';
2017-08-17 22:00:37 +05:30
describe('Blob viewer', () => {
let blob;
2018-03-17 18:26:18 +05:30
let mock;
2020-04-08 14:13:33 +05:30
const jQueryMock = {
tooltip: jest.fn(),
};
2019-07-07 11:18:12 +05:30
preloadFixtures('snippets/show.html');
2017-08-17 22:00:37 +05:30
beforeEach(() => {
2020-04-08 14:13:33 +05:30
$.fn.extend(jQueryMock);
2018-03-17 18:26:18 +05:30
mock = new MockAdapter(axios);
2019-07-07 11:18:12 +05:30
loadFixtures('snippets/show.html');
2017-08-17 22:00:37 +05:30
$('#modal-upload-blob').remove();
blob = new BlobViewer();
2018-03-17 18:26:18 +05:30
mock.onGet('http://test.host/snippets/1.json?viewer=rich').reply(200, {
html: '<div>testing</div>',
});
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
mock.onGet('http://test.host/snippets/1.json?viewer=simple').reply(200, {
html: '<div>testing</div>',
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
2020-04-08 14:13:33 +05:30
jest.spyOn(axios, 'get');
2017-08-17 22:00:37 +05:30
});
afterEach(() => {
2018-03-17 18:26:18 +05:30
mock.restore();
2018-11-08 19:23:39 +05:30
window.location.hash = '';
2017-08-17 22:00:37 +05:30
});
2018-12-13 13:39:08 +05:30
it('loads source file after switching views', done => {
2017-08-17 22:00:37 +05:30
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
2020-04-08 14:13:33 +05:30
setImmediate(() => {
2017-08-17 22:00:37 +05:30
expect(
2018-12-13 13:39:08 +05:30
document
.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]')
2017-08-17 22:00:37 +05:30
.classList.contains('hidden'),
).toBeFalsy();
done();
});
});
2018-12-13 13:39:08 +05:30
it('loads source file when line number is in hash', done => {
2018-11-08 19:23:39 +05:30
window.location.hash = '#L1';
2017-08-17 22:00:37 +05:30
new BlobViewer();
2020-04-08 14:13:33 +05:30
setImmediate(() => {
2017-08-17 22:00:37 +05:30
expect(
2018-12-13 13:39:08 +05:30
document
.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]')
2017-08-17 22:00:37 +05:30
.classList.contains('hidden'),
).toBeFalsy();
done();
});
});
2020-04-08 14:13:33 +05:30
it('doesnt reload file if already loaded', () => {
2018-12-13 13:39:08 +05:30
const asyncClick = () =>
new Promise(resolve => {
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
setImmediate(resolve);
2018-12-13 13:39:08 +05:30
});
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
return asyncClick()
2018-03-17 18:26:18 +05:30
.then(() => asyncClick())
2017-08-17 22:00:37 +05:30
.then(() => {
expect(
document.querySelector('.blob-viewer[data-type="simple"]').getAttribute('data-loaded'),
).toBe('true');
});
});
describe('copy blob button', () => {
let copyButton;
beforeEach(() => {
copyButton = document.querySelector('.js-copy-blob-source-btn');
});
it('disabled on load', () => {
2018-12-13 13:39:08 +05:30
expect(copyButton.classList.contains('disabled')).toBeTruthy();
2017-08-17 22:00:37 +05:30
});
it('has tooltip when disabled', () => {
2020-04-08 14:13:33 +05:30
expect(copyButton.getAttribute('title')).toBe(
2019-12-21 20:55:43 +05:30
'Switch to the source to copy the file contents',
2018-12-13 13:39:08 +05:30
);
2017-08-17 22:00:37 +05:30
});
it('is blurred when clicked and disabled', () => {
2020-04-08 14:13:33 +05:30
jest.spyOn(copyButton, 'blur').mockImplementation(() => {});
2017-08-17 22:00:37 +05:30
copyButton.click();
expect(copyButton.blur).toHaveBeenCalled();
});
it('is not blurred when clicked and not disabled', () => {
2020-04-08 14:13:33 +05:30
jest.spyOn(copyButton, 'blur').mockImplementation(() => {});
2017-08-17 22:00:37 +05:30
copyButton.classList.remove('disabled');
copyButton.click();
expect(copyButton.blur).not.toHaveBeenCalled();
});
2018-12-13 13:39:08 +05:30
it('enables after switching to simple view', done => {
2017-08-17 22:00:37 +05:30
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
2020-04-08 14:13:33 +05:30
setImmediate(() => {
2018-12-13 13:39:08 +05:30
expect(copyButton.classList.contains('disabled')).toBeFalsy();
2017-08-17 22:00:37 +05:30
done();
});
});
2018-12-13 13:39:08 +05:30
it('updates tooltip after switching to simple view', done => {
2017-08-17 22:00:37 +05:30
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
2020-04-08 14:13:33 +05:30
setImmediate(() => {
expect(copyButton.getAttribute('title')).toBe('Copy file contents');
2017-08-17 22:00:37 +05:30
done();
});
});
});
describe('switchToViewer', () => {
it('removes active class from old viewer button', () => {
blob.switchToViewer('simple');
expect(
document.querySelector('.js-blob-viewer-switch-btn.active[data-viewer="rich"]'),
).toBeNull();
});
it('adds active class to new viewer button', () => {
const simpleBtn = document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]');
2020-04-08 14:13:33 +05:30
jest.spyOn(simpleBtn, 'blur').mockImplementation(() => {});
2017-08-17 22:00:37 +05:30
blob.switchToViewer('simple');
2018-12-13 13:39:08 +05:30
expect(simpleBtn.classList.contains('active')).toBeTruthy();
2017-08-17 22:00:37 +05:30
expect(simpleBtn.blur).toHaveBeenCalled();
});
it('sends AJAX request when switching to simple view', () => {
blob.switchToViewer('simple');
2018-03-17 18:26:18 +05:30
expect(axios.get).toHaveBeenCalled();
2017-08-17 22:00:37 +05:30
});
it('does not send AJAX request when switching to rich view', () => {
blob.switchToViewer('simple');
blob.switchToViewer('rich');
2020-04-08 14:13:33 +05:30
expect(axios.get.mock.calls.length).toBe(1);
2017-08-17 22:00:37 +05:30
});
});
});