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

181 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;
2019-05-18 00:54:41 +05:30
preloadFixtures('snippets/show.html');
2017-08-17 22:00:37 +05:30
beforeEach(() => {
2018-03-17 18:26:18 +05:30
mock = new MockAdapter(axios);
2019-05-18 00:54:41 +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
spyOn(axios, 'get').and.callThrough();
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();
setTimeout(() => {
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();
setTimeout(() => {
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('doesnt reload file if already loaded', done => {
const asyncClick = () =>
new Promise(resolve => {
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
setTimeout(resolve);
});
2017-08-17 22:00:37 +05:30
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');
done();
})
.catch(() => {
fail();
done();
});
});
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', () => {
2018-12-13 13:39:08 +05:30
expect(copyButton.getAttribute('data-original-title')).toBe(
'Switch to the source to copy it to the clipboard',
);
2017-08-17 22:00:37 +05:30
});
it('is blurred when clicked and disabled', () => {
spyOn(copyButton, 'blur');
copyButton.click();
expect(copyButton.blur).toHaveBeenCalled();
});
it('is not blurred when clicked and not disabled', () => {
spyOn(copyButton, 'blur');
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();
setTimeout(() => {
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();
setTimeout(() => {
2018-12-13 13:39:08 +05:30
expect(copyButton.getAttribute('data-original-title')).toBe('Copy source to clipboard');
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"]');
spyOn(simpleBtn, 'blur');
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');
2018-03-17 18:26:18 +05:30
expect(axios.get.calls.count()).toBe(1);
2017-08-17 22:00:37 +05:30
});
});
});