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

185 lines
4.5 KiB
JavaScript
Raw Normal View History

2017-04-13 16:50:40 +00:00
/* eslint-disable no-new */
import BlobViewer from '~/blob/viewer/index';
describe('Blob viewer', () => {
let blob;
preloadFixtures('snippets/show.html.raw');
2017-04-13 16:50:40 +00:00
beforeEach(() => {
loadFixtures('snippets/show.html.raw');
2017-04-13 16:50:40 +00:00
$('#modal-upload-blob').remove();
blob = new BlobViewer();
2017-04-13 16:50:40 +00:00
spyOn($, 'ajax').and.callFake(() => {
const d = $.Deferred();
d.resolve({
html: '<div>testing</div>',
});
return d.promise();
});
});
afterEach(() => {
location.hash = '';
});
it('loads source file after switching views', (done) => {
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
2017-04-13 16:50:40 +00:00
setTimeout(() => {
expect($.ajax).toHaveBeenCalled();
expect(
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]')
2017-04-13 16:50:40 +00:00
.classList.contains('hidden'),
).toBeFalsy();
done();
});
});
it('loads source file when line number is in hash', (done) => {
location.hash = '#L1';
new BlobViewer();
setTimeout(() => {
expect($.ajax).toHaveBeenCalled();
expect(
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]')
2017-04-13 16:50:40 +00: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 16:50:40 +00:00
setTimeout(resolve);
});
asyncClick()
.then(() => {
expect($.ajax).toHaveBeenCalled();
return asyncClick();
})
.then(() => {
expect($.ajax.calls.count()).toBe(1);
expect(
document.querySelector('.blob-viewer[data-type="simple"]').getAttribute('data-loaded'),
).toBe('true');
2017-04-26 09:09:07 +00:00
done();
})
.catch(() => {
fail();
2017-04-13 16:50:40 +00:00
done();
});
});
describe('copy blob button', () => {
let copyButton;
beforeEach(() => {
copyButton = document.querySelector('.js-copy-blob-source-btn');
});
2017-04-13 16:50:40 +00:00
it('disabled on load', () => {
expect(
copyButton.classList.contains('disabled'),
2017-04-13 16:50:40 +00:00
).toBeTruthy();
});
it('has tooltip when disabled', () => {
expect(
copyButton.getAttribute('data-original-title'),
2017-04-26 09:09:07 +00:00
).toBe('Switch to the source to copy it to the clipboard');
2017-04-13 16:50:40 +00: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 16:50:40 +00:00
it('enables after switching to simple view', (done) => {
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
2017-04-13 16:50:40 +00:00
setTimeout(() => {
expect($.ajax).toHaveBeenCalled();
expect(
copyButton.classList.contains('disabled'),
2017-04-13 16:50:40 +00: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 16:50:40 +00:00
setTimeout(() => {
expect($.ajax).toHaveBeenCalled();
expect(
copyButton.getAttribute('data-original-title'),
2017-04-26 09:09:07 +00:00
).toBe('Copy source to clipboard');
2017-04-13 16:50:40 +00: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();
expect(simpleBtn.blur).toHaveBeenCalled();
});
it('sends AJAX request when switching to simple view', () => {
blob.switchToViewer('simple');
expect($.ajax).toHaveBeenCalled();
});
it('does not send AJAX request when switching to rich view', () => {
blob.switchToViewer('simple');
blob.switchToViewer('rich');
expect($.ajax.calls.count()).toBe(1);
});
});
2017-04-13 16:50:40 +00:00
});