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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

190 lines
5.1 KiB
JavaScript
Raw Normal View History

2017-04-13 16:50:40 +00:00
/* eslint-disable no-new */
2018-01-26 09:19:03 +00:00
import MockAdapter from 'axios-mock-adapter';
import $ from 'jquery';
import { loadHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import { setTestTimeout } from 'helpers/timeout';
import { BlobViewer } from '~/blob/viewer/index';
2018-01-26 09:19:03 +00:00
import axios from '~/lib/utils/axios_utils';
2017-04-13 16:50:40 +00:00
const execImmediately = (callback) => {
callback();
};
describe('Blob viewer', () => {
let blob;
2018-01-26 09:19:03 +00:00
let mock;
const jQueryMock = {
tooltip: jest.fn(),
};
setTestTimeout(2000);
2017-04-13 16:50:40 +00:00
beforeEach(() => {
window.gon.features = { refactorBlobViewer: false }; // This file is based on the old (non-refactored) blob viewer
jest.spyOn(window, 'requestIdleCallback').mockImplementation(execImmediately);
$.fn.extend(jQueryMock);
2018-01-26 09:19:03 +00:00
mock = new MockAdapter(axios);
loadHTMLFixture('blob/show_readme.html');
2017-04-13 16:50:40 +00:00
$('#modal-upload-blob').remove();
mock.onGet(/blob\/.+\/README\.md/).reply(200, {
2018-01-26 09:19:03 +00:00
html: '<div>testing</div>',
2017-04-13 16:50:40 +00:00
});
2018-01-26 09:19:03 +00:00
blob = new BlobViewer();
2017-04-13 16:50:40 +00:00
});
afterEach(() => {
2018-01-26 09:19:03 +00:00
mock.restore();
window.location.hash = '';
resetHTMLFixture();
2017-04-13 16:50:40 +00:00
});
it('loads source file after switching views', async () => {
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
2017-04-13 16:50:40 +00:00
await axios.waitForAll();
2017-04-13 16:50:40 +00:00
expect(
document
.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]')
.classList.contains('hidden'),
).toBeFalsy();
2017-04-13 16:50:40 +00:00
});
it('loads source file when line number is in hash', async () => {
window.location.hash = '#L1';
2017-04-13 16:50:40 +00:00
new BlobViewer();
await axios.waitForAll();
2017-04-13 16:50:40 +00:00
expect(
document
.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]')
.classList.contains('hidden'),
).toBeFalsy();
2017-04-13 16:50:40 +00:00
});
it('doesnt reload file if already loaded', () => {
const asyncClick = async () => {
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
await axios.waitForAll();
};
return asyncClick()
2018-01-26 09:19:03 +00:00
.then(() => asyncClick())
2017-04-13 16:50:40 +00:00
.then(() => {
expect(document.querySelector('.blob-viewer[data-type="simple"]').dataset.loaded).toBe(
'true',
);
2017-04-13 16:50:40 +00:00
});
});
describe('copy blob button', () => {
let copyButton;
let copyButtonTooltip;
beforeEach(() => {
copyButton = document.querySelector('.js-copy-blob-source-btn');
copyButtonTooltip = document.querySelector('.js-copy-blob-source-btn-tooltip');
});
2017-04-13 16:50:40 +00:00
it('disabled on load', () => {
2018-10-17 07:13:26 +00:00
expect(copyButton.classList.contains('disabled')).toBeTruthy();
2017-04-13 16:50:40 +00:00
});
it('has tooltip when disabled', () => {
expect(copyButtonTooltip.getAttribute('title')).toBe(
'Switch to the source to copy the file contents',
);
2017-04-13 16:50:40 +00:00
});
it('is blurred when clicked and disabled', () => {
jest.spyOn(copyButton, 'blur').mockImplementation(() => {});
copyButton.click();
expect(copyButton.blur).toHaveBeenCalled();
});
it('is not blurred when clicked and not disabled', () => {
jest.spyOn(copyButton, 'blur').mockImplementation(() => {});
copyButton.classList.remove('disabled');
copyButton.click();
expect(copyButton.blur).not.toHaveBeenCalled();
});
it('enables after switching to simple view', async () => {
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
2017-04-13 16:50:40 +00:00
await axios.waitForAll();
2017-04-13 16:50:40 +00:00
expect(copyButton.classList.contains('disabled')).toBeFalsy();
2017-04-13 16:50:40 +00:00
});
it('updates tooltip after switching to simple view', async () => {
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
2017-04-13 16:50:40 +00:00
await axios.waitForAll();
2017-04-13 16:50:40 +00:00
expect(copyButtonTooltip.getAttribute('title')).toBe('Copy file contents');
2017-04-13 16:50:40 +00: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"]');
jest.spyOn(simpleBtn, 'blur').mockImplementation(() => {});
blob.switchToViewer('simple');
expect(simpleBtn.classList.contains('selected')).toBeTruthy();
2018-10-09 18:03:09 +00:00
expect(simpleBtn.blur).toHaveBeenCalled();
});
it('makes request for initial view', () => {
expect(mock.history).toMatchObject({
get: [{ url: expect.stringMatching(/README\.md\?.*viewer=rich/) }],
});
});
describe.each`
views
${['simple']}
${['simple', 'rich']}
`('when view switches to $views', ({ views }) => {
beforeEach(async () => {
views.forEach((view) => blob.switchToViewer(view));
await axios.waitForAll();
});
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-04-13 16:50:40 +00:00
});