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

125 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-04-13 16:50:40 +00:00
/* eslint-disable no-new */
import BlobViewer from '~/blob/viewer/index';
2017-04-26 09:09:07 +00:00
fdescribe('Blob viewer', () => {
2017-04-13 16:50:40 +00:00
preloadFixtures('blob/show.html.raw');
beforeEach(() => {
loadFixtures('blob/show.html.raw');
$('#modal-upload-blob').remove();
new BlobViewer();
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', () => {
it('disabled on load', () => {
expect(
document.querySelector('.js-copy-blob-source-btn').classList.contains('disabled'),
).toBeTruthy();
});
it('has tooltip when disabled', () => {
expect(
document.querySelector('.js-copy-blob-source-btn').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('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(
document.querySelector('.js-copy-blob-source-btn').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();
2017-04-13 16:50:40 +00:00
setTimeout(() => {
expect($.ajax).toHaveBeenCalled();
expect(
document.querySelector('.js-copy-blob-source-btn').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();
});
});
});
});