2023-01-13 00:05:48 +05:30
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2022-07-16 23:28:13 +05:30
|
|
|
import { Emitter } from 'monaco-editor';
|
|
|
|
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
|
2021-01-03 14:25:43 +05:30
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
2020-07-28 23:09:34 +05:30
|
|
|
import EditBlob from '~/blob_edit/edit_blob';
|
2022-01-26 12:08:38 +05:30
|
|
|
import { SourceEditorExtension } from '~/editor/extensions/source_editor_extension_base';
|
2021-09-30 23:02:18 +05:30
|
|
|
import { FileTemplateExtension } from '~/editor/extensions/source_editor_file_template_ext';
|
|
|
|
import { EditorMarkdownExtension } from '~/editor/extensions/source_editor_markdown_ext';
|
2022-01-26 12:08:38 +05:30
|
|
|
import { EditorMarkdownPreviewExtension } from '~/editor/extensions/source_editor_markdown_livepreview_ext';
|
2022-07-16 23:28:13 +05:30
|
|
|
import { ToolbarExtension } from '~/editor/extensions/source_editor_toolbar_ext';
|
2021-09-30 23:02:18 +05:30
|
|
|
import SourceEditor from '~/editor/source_editor';
|
2023-01-13 00:05:48 +05:30
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
jest.mock('~/editor/source_editor');
|
2022-01-26 12:08:38 +05:30
|
|
|
jest.mock('~/editor/extensions/source_editor_extension_base');
|
2021-09-30 23:02:18 +05:30
|
|
|
jest.mock('~/editor/extensions/source_editor_file_template_ext');
|
2022-01-26 12:08:38 +05:30
|
|
|
jest.mock('~/editor/extensions/source_editor_markdown_ext');
|
|
|
|
jest.mock('~/editor/extensions/source_editor_markdown_livepreview_ext');
|
2022-07-16 23:28:13 +05:30
|
|
|
jest.mock('~/editor/extensions/source_editor_toolbar_ext');
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
const PREVIEW_MARKDOWN_PATH = '/foo/bar/preview_markdown';
|
2022-01-26 12:08:38 +05:30
|
|
|
const defaultExtensions = [
|
2023-05-27 22:25:52 +05:30
|
|
|
{ definition: ToolbarExtension },
|
2022-01-26 12:08:38 +05:30
|
|
|
{ definition: SourceEditorExtension },
|
|
|
|
{ definition: FileTemplateExtension },
|
|
|
|
];
|
|
|
|
const markdownExtensions = [
|
|
|
|
{ definition: EditorMarkdownExtension },
|
|
|
|
{
|
|
|
|
definition: EditorMarkdownPreviewExtension,
|
|
|
|
setupOptions: { previewMarkdownPath: PREVIEW_MARKDOWN_PATH },
|
|
|
|
},
|
|
|
|
];
|
2021-10-27 15:23:28 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
describe('Blob Editing', () => {
|
2022-07-16 23:28:13 +05:30
|
|
|
let blobInstance;
|
2023-01-13 00:05:48 +05:30
|
|
|
let mock;
|
2022-07-16 23:28:13 +05:30
|
|
|
const useMock = jest.fn(() => markdownExtensions);
|
|
|
|
const unuseMock = jest.fn();
|
|
|
|
const emitter = new Emitter();
|
2021-01-03 14:25:43 +05:30
|
|
|
const mockInstance = {
|
|
|
|
use: useMock,
|
2022-07-16 23:28:13 +05:30
|
|
|
unuse: unuseMock,
|
2021-02-22 17:27:13 +05:30
|
|
|
setValue: jest.fn(),
|
|
|
|
getValue: jest.fn().mockReturnValue('test value'),
|
2021-01-03 14:25:43 +05:30
|
|
|
focus: jest.fn(),
|
2022-07-16 23:28:13 +05:30
|
|
|
onDidChangeModelLanguage: emitter.event,
|
2021-01-03 14:25:43 +05:30
|
|
|
};
|
2020-07-28 23:09:34 +05:30
|
|
|
beforeEach(() => {
|
2023-01-13 00:05:48 +05:30
|
|
|
mock = new MockAdapter(axios);
|
2022-07-16 23:28:13 +05:30
|
|
|
setHTMLFixture(`
|
2023-01-13 00:05:48 +05:30
|
|
|
<div class="js-edit-mode-pane"></div>
|
|
|
|
<div class="js-edit-mode"><a href="#write">Write</a><a href="#preview">Preview</a></div>
|
2021-02-22 17:27:13 +05:30
|
|
|
<form class="js-edit-blob-form">
|
|
|
|
<div id="file_path"></div>
|
|
|
|
<div id="editor"></div>
|
|
|
|
<textarea id="file-content"></textarea>
|
|
|
|
</form>
|
|
|
|
`);
|
2021-09-30 23:02:18 +05:30
|
|
|
jest.spyOn(SourceEditor.prototype, 'createInstance').mockReturnValue(mockInstance);
|
2020-07-28 23:09:34 +05:30
|
|
|
});
|
2021-02-22 17:27:13 +05:30
|
|
|
afterEach(() => {
|
2023-01-13 00:05:48 +05:30
|
|
|
mock.restore();
|
2022-07-16 23:28:13 +05:30
|
|
|
jest.clearAllMocks();
|
|
|
|
unuseMock.mockClear();
|
|
|
|
useMock.mockClear();
|
|
|
|
resetHTMLFixture();
|
2021-02-22 17:27:13 +05:30
|
|
|
});
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
const editorInst = (isMarkdown) => {
|
2022-07-16 23:28:13 +05:30
|
|
|
blobInstance = new EditBlob({
|
2020-07-28 23:09:34 +05:30
|
|
|
isMarkdown,
|
2021-10-27 15:23:28 +05:30
|
|
|
previewMarkdownPath: PREVIEW_MARKDOWN_PATH,
|
2020-07-28 23:09:34 +05:30
|
|
|
});
|
2022-07-16 23:28:13 +05:30
|
|
|
return blobInstance;
|
2020-07-28 23:09:34 +05:30
|
|
|
};
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
const initEditor = async (isMarkdown = false) => {
|
|
|
|
editorInst(isMarkdown);
|
|
|
|
await waitForPromises();
|
|
|
|
};
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
it('loads SourceEditorExtension and FileTemplateExtension by default', async () => {
|
2020-07-28 23:09:34 +05:30
|
|
|
await initEditor();
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(useMock).toHaveBeenCalledWith(defaultExtensions);
|
2020-07-28 23:09:34 +05:30
|
|
|
});
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
describe('Markdown', () => {
|
2022-01-26 12:08:38 +05:30
|
|
|
it('does not load MarkdownExtensions by default', async () => {
|
2020-11-24 15:15:51 +05:30
|
|
|
await initEditor();
|
2021-02-22 17:27:13 +05:30
|
|
|
expect(EditorMarkdownExtension).not.toHaveBeenCalled();
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(EditorMarkdownPreviewExtension).not.toHaveBeenCalled();
|
2020-11-24 15:15:51 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('loads MarkdownExtension only for the markdown files', async () => {
|
|
|
|
await initEditor(true);
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(useMock).toHaveBeenCalledTimes(2);
|
|
|
|
expect(useMock.mock.calls[1]).toEqual([markdownExtensions]);
|
2020-11-24 15:15:51 +05:30
|
|
|
});
|
2022-07-16 23:28:13 +05:30
|
|
|
|
|
|
|
it('correctly handles switching from markdown and un-uses markdown extensions', async () => {
|
|
|
|
await initEditor(true);
|
|
|
|
expect(unuseMock).not.toHaveBeenCalled();
|
|
|
|
await emitter.fire({ newLanguage: 'plaintext', oldLanguage: 'markdown' });
|
|
|
|
expect(unuseMock).toHaveBeenCalledWith(markdownExtensions);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('correctly handles switching from non-markdown to markdown extensions', async () => {
|
|
|
|
const mdSpy = jest.fn();
|
|
|
|
await initEditor();
|
|
|
|
blobInstance.fetchMarkdownExtension = mdSpy;
|
|
|
|
expect(mdSpy).not.toHaveBeenCalled();
|
|
|
|
await emitter.fire({ newLanguage: 'markdown', oldLanguage: 'plaintext' });
|
|
|
|
expect(mdSpy).toHaveBeenCalled();
|
|
|
|
});
|
2020-07-28 23:09:34 +05:30
|
|
|
});
|
2021-02-22 17:27:13 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
describe('correctly handles toggling the live-preview panel for different file types', () => {
|
|
|
|
it.each`
|
|
|
|
desc | isMarkdown | isPreviewOpened | tabToClick | shouldOpenPreview | shouldClosePreview | expectedDesc
|
|
|
|
${'not markdown with preview closed'} | ${false} | ${false} | ${'#write'} | ${false} | ${false} | ${'not toggle preview'}
|
|
|
|
${'not markdown with preview closed'} | ${false} | ${false} | ${'#preview'} | ${false} | ${false} | ${'not toggle preview'}
|
|
|
|
${'markdown with preview closed'} | ${true} | ${false} | ${'#write'} | ${false} | ${false} | ${'not toggle preview'}
|
|
|
|
${'markdown with preview closed'} | ${true} | ${false} | ${'#preview'} | ${true} | ${false} | ${'open preview'}
|
|
|
|
${'markdown with preview opened'} | ${true} | ${true} | ${'#write'} | ${false} | ${true} | ${'close preview'}
|
|
|
|
${'markdown with preview opened'} | ${true} | ${true} | ${'#preview'} | ${false} | ${false} | ${'not toggle preview'}
|
|
|
|
`(
|
|
|
|
'when $desc, clicking $tabToClick should $expectedDesc',
|
|
|
|
async ({
|
|
|
|
isMarkdown,
|
|
|
|
isPreviewOpened,
|
|
|
|
tabToClick,
|
|
|
|
shouldOpenPreview,
|
|
|
|
shouldClosePreview,
|
|
|
|
}) => {
|
|
|
|
const fire = jest.fn();
|
|
|
|
SourceEditor.prototype.createInstance = jest.fn().mockReturnValue({
|
|
|
|
...mockInstance,
|
|
|
|
markdownPreview: {
|
|
|
|
eventEmitter: {
|
|
|
|
fire,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
await initEditor(isMarkdown);
|
|
|
|
blobInstance.markdownLivePreviewOpened = isPreviewOpened;
|
|
|
|
const elToClick = document.querySelector(`a[href='${tabToClick}']`);
|
|
|
|
elToClick.dispatchEvent(new Event('click'));
|
|
|
|
|
|
|
|
if (shouldOpenPreview || shouldClosePreview) {
|
|
|
|
expect(fire).toHaveBeenCalled();
|
|
|
|
} else {
|
|
|
|
expect(fire).not.toHaveBeenCalled();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
it('adds trailing newline to the blob content on submit', async () => {
|
|
|
|
const form = document.querySelector('.js-edit-blob-form');
|
|
|
|
const fileContentEl = document.getElementById('file-content');
|
|
|
|
|
|
|
|
await initEditor();
|
|
|
|
|
|
|
|
form.dispatchEvent(new Event('submit'));
|
|
|
|
|
|
|
|
expect(fileContentEl.value).toBe('test value\n');
|
|
|
|
});
|
2020-07-28 23:09:34 +05:30
|
|
|
});
|