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

185 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;
2017-09-10 17:25:29 +05:30
preloadFixtures('snippets/show.html.raw');
2017-08-17 22:00:37 +05:30
beforeEach(() => {
2018-03-17 18:26:18 +05:30
mock = new MockAdapter(axios);
2017-09-10 17:25:29 +05:30
loadFixtures('snippets/show.html.raw');
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
});
it('loads source file after switching views', (done) => {
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
setTimeout(() => {
expect(
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]')
.classList.contains('hidden'),
).toBeFalsy();
done();
});
});
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(
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]')
.classList.contains('hidden'),
).toBeFalsy();
done();
});
});
it('doesnt reload file if already loaded', (done) => {
const asyncClick = () => new Promise((resolve) => {
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
setTimeout(resolve);
});
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', () => {
expect(
copyButton.classList.contains('disabled'),
).toBeTruthy();
});
it('has tooltip when disabled', () => {
expect(
copyButton.getAttribute('data-original-title'),
).toBe('Switch to the source to copy it to the clipboard');
});
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();
});
it('enables after switching to simple view', (done) => {
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
setTimeout(() => {
expect(
copyButton.classList.contains('disabled'),
).toBeFalsy();
done();
});
});
it('updates tooltip after switching to simple view', (done) => {
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
setTimeout(() => {
expect(
copyButton.getAttribute('data-original-title'),
).toBe('Copy source to clipboard');
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');
expect(
simpleBtn.classList.contains('active'),
).toBeTruthy();
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
});
});
});