2017-04-13 12:50:40 -04:00
|
|
|
/* eslint-disable no-new */
|
2018-03-09 15:18:59 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
2017-04-26 05:19:09 -04:00
|
|
|
describe('Blob viewer', () => {
|
|
|
|
let blob;
|
2018-01-26 04:19:03 -05:00
|
|
|
let mock;
|
|
|
|
|
2017-07-17 12:55:50 -04:00
|
|
|
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);
|
|
|
|
|
2017-07-17 12:55:50 -04:00
|
|
|
loadFixtures('snippets/show.html.raw');
|
2017-04-13 12:50:40 -04:00
|
|
|
$('#modal-upload-blob').remove();
|
|
|
|
|
2017-04-26 05:19:09 -04:00
|
|
|
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();
|
2018-06-15 11:58:27 -04:00
|
|
|
window.location.hash = '';
|
2017-04-13 12:50:40 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('loads source file after switching views', (done) => {
|
2017-04-21 14:33:48 -04:00
|
|
|
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
|
2017-04-13 12:50:40 -04:00
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(
|
2017-04-21 14:33:48 -04:00
|
|
|
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) => {
|
2018-06-15 11:58:27 -04:00
|
|
|
window.location.hash = '#L1';
|
2017-04-13 12:50:40 -04:00
|
|
|
|
|
|
|
new BlobViewer();
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(
|
2017-04-21 14:33:48 -04:00
|
|
|
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) => {
|
2017-04-21 14:33:48 -04:00
|
|
|
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', () => {
|
2017-05-18 16:53:15 -04:00
|
|
|
let copyButton;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
copyButton = document.querySelector('.js-copy-blob-source-btn');
|
|
|
|
});
|
|
|
|
|
2017-04-13 12:50:40 -04:00
|
|
|
it('disabled on load', () => {
|
|
|
|
expect(
|
2017-05-18 16:53:15 -04:00
|
|
|
copyButton.classList.contains('disabled'),
|
2017-04-13 12:50:40 -04:00
|
|
|
).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('has tooltip when disabled', () => {
|
|
|
|
expect(
|
2017-05-18 16:53:15 -04:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2017-05-18 16:53:15 -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) => {
|
2017-04-21 14:33:48 -04:00
|
|
|
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
|
2017-04-13 12:50:40 -04:00
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(
|
2017-05-18 16:53:15 -04:00
|
|
|
copyButton.classList.contains('disabled'),
|
2017-04-13 12:50:40 -04:00
|
|
|
).toBeFalsy();
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('updates tooltip after switching to simple view', (done) => {
|
2017-04-21 14:33:48 -04:00
|
|
|
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
|
2017-04-13 12:50:40 -04:00
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(
|
2017-05-18 16:53:15 -04:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-04-26 05:19:09 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
2017-04-26 05:19: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();
|
2017-04-26 05:19:09 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
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-26 05:19:09 -04:00
|
|
|
});
|
|
|
|
});
|
2017-04-13 12:50:40 -04:00
|
|
|
});
|