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

189 lines
4.8 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';
2021-01-03 14:25:43 +05:30
import { setTestTimeout } from 'helpers/timeout';
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;
2020-04-08 14:13:33 +05:30
const jQueryMock = {
tooltip: jest.fn(),
};
2021-01-03 14:25:43 +05:30
setTestTimeout(2000);
preloadFixtures('blob/show_readme.html');
2017-08-17 22:00:37 +05:30
beforeEach(() => {
2020-04-08 14:13:33 +05:30
$.fn.extend(jQueryMock);
2018-03-17 18:26:18 +05:30
mock = new MockAdapter(axios);
2021-01-03 14:25:43 +05:30
loadFixtures('blob/show_readme.html');
2017-08-17 22:00:37 +05:30
$('#modal-upload-blob').remove();
2021-01-03 14:25:43 +05:30
mock.onGet(/blob\/master\/README\.md/).reply(200, {
2018-03-17 18:26:18 +05:30
html: '<div>testing</div>',
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
2021-01-03 14:25:43 +05:30
blob = new BlobViewer();
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
});
2018-12-13 13:39:08 +05:30
it('loads source file after switching views', done => {
2017-08-17 22:00:37 +05:30
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
2020-04-08 14:13:33 +05:30
setImmediate(() => {
2017-08-17 22:00:37 +05:30
expect(
2018-12-13 13:39:08 +05:30
document
.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]')
2017-08-17 22:00:37 +05:30
.classList.contains('hidden'),
).toBeFalsy();
done();
});
});
2018-12-13 13:39:08 +05:30
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();
2020-04-08 14:13:33 +05:30
setImmediate(() => {
2017-08-17 22:00:37 +05:30
expect(
2018-12-13 13:39:08 +05:30
document
.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]')
2017-08-17 22:00:37 +05:30
.classList.contains('hidden'),
).toBeFalsy();
done();
});
});
2020-04-08 14:13:33 +05:30
it('doesnt reload file if already loaded', () => {
2021-01-03 14:25:43 +05:30
const asyncClick = async () => {
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
2017-08-17 22:00:37 +05:30
2021-01-03 14:25:43 +05:30
await axios.waitForAll();
};
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
return 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');
});
});
describe('copy blob button', () => {
let copyButton;
beforeEach(() => {
copyButton = document.querySelector('.js-copy-blob-source-btn');
});
it('disabled on load', () => {
2018-12-13 13:39:08 +05:30
expect(copyButton.classList.contains('disabled')).toBeTruthy();
2017-08-17 22:00:37 +05:30
});
it('has tooltip when disabled', () => {
2020-04-08 14:13:33 +05:30
expect(copyButton.getAttribute('title')).toBe(
2019-12-21 20:55:43 +05:30
'Switch to the source to copy the file contents',
2018-12-13 13:39:08 +05:30
);
2017-08-17 22:00:37 +05:30
});
it('is blurred when clicked and disabled', () => {
2020-04-08 14:13:33 +05:30
jest.spyOn(copyButton, 'blur').mockImplementation(() => {});
2017-08-17 22:00:37 +05:30
copyButton.click();
expect(copyButton.blur).toHaveBeenCalled();
});
it('is not blurred when clicked and not disabled', () => {
2020-04-08 14:13:33 +05:30
jest.spyOn(copyButton, 'blur').mockImplementation(() => {});
2017-08-17 22:00:37 +05:30
copyButton.classList.remove('disabled');
copyButton.click();
expect(copyButton.blur).not.toHaveBeenCalled();
});
2018-12-13 13:39:08 +05:30
it('enables after switching to simple view', done => {
2017-08-17 22:00:37 +05:30
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
2020-04-08 14:13:33 +05:30
setImmediate(() => {
2018-12-13 13:39:08 +05:30
expect(copyButton.classList.contains('disabled')).toBeFalsy();
2017-08-17 22:00:37 +05:30
done();
});
});
2018-12-13 13:39:08 +05:30
it('updates tooltip after switching to simple view', done => {
2017-08-17 22:00:37 +05:30
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
2020-04-08 14:13:33 +05:30
setImmediate(() => {
expect(copyButton.getAttribute('title')).toBe('Copy file contents');
2017-08-17 22:00:37 +05:30
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"]');
2020-04-08 14:13:33 +05:30
jest.spyOn(simpleBtn, 'blur').mockImplementation(() => {});
2017-08-17 22:00:37 +05:30
blob.switchToViewer('simple');
2018-12-13 13:39:08 +05:30
expect(simpleBtn.classList.contains('active')).toBeTruthy();
2017-08-17 22:00:37 +05:30
expect(simpleBtn.blur).toHaveBeenCalled();
});
2021-01-03 14:25:43 +05:30
it('makes request for initial view', () => {
expect(mock.history).toMatchObject({
get: [{ url: expect.stringMatching(/README\.md\?.*viewer=rich/) }],
});
2017-08-17 22:00:37 +05:30
});
2021-01-03 14:25:43 +05:30
describe.each`
views
${['simple']}
${['simple', 'rich']}
`('when view switches to $views', ({ views }) => {
beforeEach(async () => {
views.forEach(view => blob.switchToViewer(view));
await axios.waitForAll();
});
2017-08-17 22:00:37 +05:30
2021-01-03 14:25:43 +05:30
it('sends 1 AJAX request for new view', async () => {
expect(mock.history).toMatchObject({
get: [
{ url: expect.stringMatching(/README\.md\?.*viewer=rich/) },
{ url: expect.stringMatching(/README\.md\?.*viewer=simple/) },
],
});
});
2017-08-17 22:00:37 +05:30
});
});
});