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

187 lines
5.1 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
2018-03-17 18:26:18 +05:30
import MockAdapter from 'axios-mock-adapter';
2021-03-11 19:13:27 +05:30
import $ from 'jquery';
2021-01-03 14:25:43 +05:30
import { setTestTimeout } from 'helpers/timeout';
2021-10-27 15:23:28 +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
2021-09-30 23:02:18 +05:30
const execImmediately = (callback) => {
callback();
};
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);
2017-08-17 22:00:37 +05:30
beforeEach(() => {
2022-03-02 08:16:31 +05:30
window.gon.features = { refactorBlobViewer: false }; // This file is based on the old (non-refactored) blob viewer
2021-09-30 23:02:18 +05:30
jest.spyOn(window, 'requestIdleCallback').mockImplementation(execImmediately);
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();
2022-01-26 12:08:38 +05:30
mock.onGet(/blob\/.+\/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
});
2022-04-04 11:22:00 +05:30
it('loads source file after switching views', async () => {
2017-08-17 22:00:37 +05:30
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
2022-04-04 11:22:00 +05:30
await axios.waitForAll();
2017-08-17 22:00:37 +05:30
2022-04-04 11:22:00 +05:30
expect(
document
.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]')
.classList.contains('hidden'),
).toBeFalsy();
2017-08-17 22:00:37 +05:30
});
2022-04-04 11:22:00 +05:30
it('loads source file when line number is in hash', async () => {
2018-11-08 19:23:39 +05:30
window.location.hash = '#L1';
2017-08-17 22:00:37 +05:30
new BlobViewer();
2022-04-04 11:22:00 +05:30
await axios.waitForAll();
2017-08-17 22:00:37 +05:30
2022-04-04 11:22:00 +05:30
expect(
document
.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]')
.classList.contains('hidden'),
).toBeFalsy();
2017-08-17 22:00:37 +05:30
});
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;
2021-04-17 20:07:23 +05:30
let copyButtonTooltip;
2017-08-17 22:00:37 +05:30
beforeEach(() => {
copyButton = document.querySelector('.js-copy-blob-source-btn');
2021-04-17 20:07:23 +05:30
copyButtonTooltip = document.querySelector('.js-copy-blob-source-btn-tooltip');
2017-08-17 22:00:37 +05:30
});
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', () => {
2021-04-17 20:07:23 +05:30
expect(copyButtonTooltip.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();
});
2022-04-04 11:22:00 +05:30
it('enables after switching to simple view', async () => {
2017-08-17 22:00:37 +05:30
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
2022-04-04 11:22:00 +05:30
await axios.waitForAll();
2017-08-17 22:00:37 +05:30
2022-04-04 11:22:00 +05:30
expect(copyButton.classList.contains('disabled')).toBeFalsy();
2017-08-17 22:00:37 +05:30
});
2022-04-04 11:22:00 +05:30
it('updates tooltip after switching to simple view', async () => {
2017-08-17 22:00:37 +05:30
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
2022-04-04 11:22:00 +05:30
await axios.waitForAll();
2017-08-17 22:00:37 +05:30
2022-04-04 11:22:00 +05:30
expect(copyButtonTooltip.getAttribute('title')).toBe('Copy file contents');
2017-08-17 22:00:37 +05:30
});
});
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');
2021-02-22 17:27:13 +05:30
expect(simpleBtn.classList.contains('selected')).toBeTruthy();
2018-12-13 13:39:08 +05:30
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 () => {
2021-03-08 18:12:59 +05:30
views.forEach((view) => blob.switchToViewer(view));
2021-01-03 14:25:43 +05:30
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
});
});
});