debian-mirror-gitlab/spec/frontend/blob_edit/edit_blob_spec.js

82 lines
2.6 KiB
JavaScript
Raw Normal View History

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';
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';
import SourceEditor from '~/editor/source_editor';
2020-07-28 23:09:34 +05:30
2021-09-30 23:02:18 +05:30
jest.mock('~/editor/source_editor');
jest.mock('~/editor/extensions/source_editor_markdown_ext');
jest.mock('~/editor/extensions/source_editor_file_template_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';
2020-07-28 23:09:34 +05:30
describe('Blob Editing', () => {
2021-01-03 14:25:43 +05:30
const useMock = jest.fn();
const mockInstance = {
use: useMock,
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(),
};
2020-07-28 23:09:34 +05:30
beforeEach(() => {
2021-02-22 17:27:13 +05:30
setFixtures(`
<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(() => {
EditorMarkdownExtension.mockClear();
FileTemplateExtension.mockClear();
});
2020-07-28 23:09:34 +05:30
2021-03-08 18:12:59 +05:30
const editorInst = (isMarkdown) => {
2020-07-28 23:09:34 +05:30
return new EditBlob({
isMarkdown,
2021-10-27 15:23:28 +05:30
previewMarkdownPath: PREVIEW_MARKDOWN_PATH,
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();
};
2020-11-24 15:15:51 +05:30
it('loads FileTemplateExtension by default', async () => {
2020-07-28 23:09:34 +05:30
await initEditor();
2021-10-27 15:23:28 +05:30
expect(useMock).toHaveBeenCalledWith(expect.any(FileTemplateExtension));
2021-02-22 17:27:13 +05:30
expect(FileTemplateExtension).toHaveBeenCalledTimes(1);
2020-07-28 23:09:34 +05:30
});
2020-11-24 15:15:51 +05:30
describe('Markdown', () => {
it('does not load MarkdownExtension by default', async () => {
await initEditor();
2021-02-22 17:27:13 +05:30
expect(EditorMarkdownExtension).not.toHaveBeenCalled();
2020-11-24 15:15:51 +05:30
});
it('loads MarkdownExtension only for the markdown files', async () => {
await initEditor(true);
2021-10-27 15:23:28 +05:30
expect(useMock).toHaveBeenCalledWith(expect.any(EditorMarkdownExtension));
2021-02-22 17:27:13 +05:30
expect(EditorMarkdownExtension).toHaveBeenCalledTimes(1);
2021-10-27 15:23:28 +05:30
expect(EditorMarkdownExtension).toHaveBeenCalledWith({
instance: mockInstance,
previewMarkdownPath: PREVIEW_MARKDOWN_PATH,
});
2020-11-24 15:15:51 +05:30
});
2020-07-28 23:09:34 +05:30
});
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
});