gitlab-org--gitlab-foss/spec/javascripts/blob/viewer/index_spec.js

186 lines
4.5 KiB
JavaScript
Raw Normal View History

2017-04-13 12:50:40 -04:00
/* eslint-disable no-new */
import $ from 'jquery';
2018-01-26 04:19:03 -05:00
import MockAdapter from 'axios-mock-adapter';
2017-04-13 12:50:40 -04:00
import BlobViewer from '~/blob/viewer/index';
2018-01-26 04:19:03 -05:00
import axios from '~/lib/utils/axios_utils';
2017-04-13 12:50:40 -04:00
describe('Blob viewer', () => {
let blob;
2018-01-26 04:19:03 -05:00
let mock;
preloadFixtures('snippets/show.html.raw');
2017-04-13 12:50:40 -04:00
beforeEach(() => {
2018-01-26 04:19:03 -05:00
mock = new MockAdapter(axios);
loadFixtures('snippets/show.html.raw');
2017-04-13 12:50:40 -04:00
$('#modal-upload-blob').remove();
blob = new BlobViewer();
2017-04-13 12:50:40 -04:00
2018-01-26 04:19:03 -05:00
mock.onGet('http://test.host/snippets/1.json?viewer=rich').reply(200, {
html: '<div>testing</div>',
});
2017-04-13 12:50:40 -04:00
2018-01-26 04:19:03 -05:00
mock.onGet('http://test.host/snippets/1.json?viewer=simple').reply(200, {
html: '<div>testing</div>',
2017-04-13 12:50:40 -04:00
});
2018-01-26 04:19:03 -05:00
spyOn(axios, 'get').and.callThrough();
2017-04-13 12:50:40 -04:00
});
afterEach(() => {
2018-01-26 04:19:03 -05:00
mock.restore();
window.location.hash = '';
2017-04-13 12:50:40 -04:00
});
it('loads source file after switching views', (done) => {
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
2017-04-13 12:50:40 -04:00
setTimeout(() => {
expect(
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]')
2017-04-13 12:50:40 -04:00
.classList.contains('hidden'),
).toBeFalsy();
done();
});
});
it('loads source file when line number is in hash', (done) => {
window.location.hash = '#L1';
2017-04-13 12:50:40 -04:00
new BlobViewer();
setTimeout(() => {
expect(
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]')
2017-04-13 12:50:40 -04:00
.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();
2017-04-13 12:50:40 -04:00
setTimeout(resolve);
});
asyncClick()
2018-01-26 04:19:03 -05:00
.then(() => asyncClick())
2017-04-13 12:50:40 -04:00
.then(() => {
expect(
document.querySelector('.blob-viewer[data-type="simple"]').getAttribute('data-loaded'),
).toBe('true');
2017-04-26 05:09:07 -04:00
done();
})
.catch(() => {
fail();
2017-04-13 12:50:40 -04:00
done();
});
});
describe('copy blob button', () => {
let copyButton;
beforeEach(() => {
copyButton = document.querySelector('.js-copy-blob-source-btn');
});
2017-04-13 12:50:40 -04:00
it('disabled on load', () => {
expect(
copyButton.classList.contains('disabled'),
2017-04-13 12:50:40 -04:00
).toBeTruthy();
});
it('has tooltip when disabled', () => {
expect(
copyButton.getAttribute('data-original-title'),
2017-04-26 05:09:07 -04:00
).toBe('Switch to the source to copy it to the clipboard');
2017-04-13 12:50:40 -04:00
});
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();
});
2017-04-13 12:50:40 -04:00
it('enables after switching to simple view', (done) => {
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
2017-04-13 12:50:40 -04:00
setTimeout(() => {
expect(
copyButton.classList.contains('disabled'),
2017-04-13 12:50:40 -04:00
).toBeFalsy();
done();
});
});
it('updates tooltip after switching to simple view', (done) => {
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
2017-04-13 12:50:40 -04:00
setTimeout(() => {
expect(
copyButton.getAttribute('data-original-title'),
2017-04-26 05:09:07 -04:00
).toBe('Copy source to clipboard');
2017-04-13 12:50:40 -04:00
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();
2018-10-09 14:03:09 -04:00
expect(simpleBtn.blur).toHaveBeenCalled();
});
it('sends AJAX request when switching to simple view', () => {
blob.switchToViewer('simple');
2018-01-26 04:19:03 -05:00
expect(axios.get).toHaveBeenCalled();
});
it('does not send AJAX request when switching to rich view', () => {
blob.switchToViewer('simple');
blob.switchToViewer('rich');
2018-01-26 04:19:03 -05:00
expect(axios.get.calls.count()).toBe(1);
});
});
2017-04-13 12:50:40 -04:00
});