2021-08-16 12:09:17 +00:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2021-08-17 15:10:19 +00:00
|
|
|
import { Range, Position, editor as monacoEditor } from 'monaco-editor';
|
2021-08-16 12:09:17 +00:00
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
|
|
|
import {
|
|
|
|
EXTENSION_MARKDOWN_PREVIEW_PANEL_CLASS,
|
|
|
|
EXTENSION_MARKDOWN_PREVIEW_ACTION_ID,
|
|
|
|
EXTENSION_MARKDOWN_PREVIEW_PANEL_WIDTH,
|
|
|
|
EXTENSION_MARKDOWN_PREVIEW_PANEL_PARENT_CLASS,
|
2021-08-17 15:10:19 +00:00
|
|
|
EXTENSION_MARKDOWN_PREVIEW_UPDATE_DELAY,
|
2021-08-16 12:09:17 +00:00
|
|
|
} from '~/editor/constants';
|
2021-06-18 21:10:06 +00:00
|
|
|
import { EditorMarkdownExtension } from '~/editor/extensions/source_editor_markdown_ext';
|
|
|
|
import SourceEditor from '~/editor/source_editor';
|
2021-08-16 12:09:17 +00:00
|
|
|
import createFlash from '~/flash';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import syntaxHighlight from '~/syntax_highlight';
|
|
|
|
|
|
|
|
jest.mock('~/syntax_highlight');
|
|
|
|
jest.mock('~/flash');
|
2020-07-01 18:08:50 +00:00
|
|
|
|
2021-06-18 21:10:06 +00:00
|
|
|
describe('Markdown Extension for Source Editor', () => {
|
2020-07-01 18:08:50 +00:00
|
|
|
let editor;
|
2020-08-26 09:10:16 +00:00
|
|
|
let instance;
|
2020-07-01 18:08:50 +00:00
|
|
|
let editorEl;
|
2021-08-16 12:09:17 +00:00
|
|
|
let panelSpy;
|
|
|
|
let mockAxios;
|
2021-08-24 15:10:36 +00:00
|
|
|
const previewMarkdownPath = '/gitlab/fooGroup/barProj/preview_markdown';
|
2020-07-01 18:08:50 +00:00
|
|
|
const firstLine = 'This is a';
|
|
|
|
const secondLine = 'multiline';
|
|
|
|
const thirdLine = 'string with some **markup**';
|
|
|
|
const text = `${firstLine}\n${secondLine}\n${thirdLine}`;
|
2021-08-17 15:10:19 +00:00
|
|
|
const plaintextPath = 'foo.txt';
|
|
|
|
const markdownPath = 'foo.md';
|
2021-08-16 12:09:17 +00:00
|
|
|
const responseData = '<div>FooBar</div>';
|
2020-07-01 18:08:50 +00:00
|
|
|
|
|
|
|
const setSelection = (startLineNumber = 1, startColumn = 1, endLineNumber = 1, endColumn = 1) => {
|
|
|
|
const selection = new Range(startLineNumber, startColumn, endLineNumber, endColumn);
|
2020-08-26 09:10:16 +00:00
|
|
|
instance.setSelection(selection);
|
2020-07-01 18:08:50 +00:00
|
|
|
};
|
|
|
|
const selectSecondString = () => setSelection(2, 1, 2, secondLine.length + 1); // select the whole second line
|
|
|
|
const selectSecondAndThirdLines = () => setSelection(2, 1, 3, thirdLine.length + 1); // select second and third lines
|
|
|
|
|
2020-08-26 09:10:16 +00:00
|
|
|
const selectionToString = () => instance.getSelection().toString();
|
|
|
|
const positionToString = () => instance.getPosition().toString();
|
2020-07-01 18:08:50 +00:00
|
|
|
|
2021-08-16 12:09:17 +00:00
|
|
|
const togglePreview = async () => {
|
|
|
|
instance.togglePreview();
|
|
|
|
await waitForPromises();
|
|
|
|
};
|
|
|
|
|
2020-07-01 18:08:50 +00:00
|
|
|
beforeEach(() => {
|
2021-08-16 12:09:17 +00:00
|
|
|
mockAxios = new MockAdapter(axios);
|
2020-07-01 18:08:50 +00:00
|
|
|
setFixtures('<div id="editor" data-editor-loading></div>');
|
|
|
|
editorEl = document.getElementById('editor');
|
2021-06-18 21:10:06 +00:00
|
|
|
editor = new SourceEditor();
|
2020-08-26 09:10:16 +00:00
|
|
|
instance = editor.createInstance({
|
2020-07-01 18:08:50 +00:00
|
|
|
el: editorEl,
|
2021-08-17 15:10:19 +00:00
|
|
|
blobPath: markdownPath,
|
2020-07-01 18:08:50 +00:00
|
|
|
blobContent: text,
|
|
|
|
});
|
2021-08-24 15:10:36 +00:00
|
|
|
editor.use(new EditorMarkdownExtension({ instance, previewMarkdownPath }));
|
2021-08-16 12:09:17 +00:00
|
|
|
panelSpy = jest.spyOn(EditorMarkdownExtension, 'togglePreviewPanel');
|
2020-07-01 18:08:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2020-09-04 06:09:04 +00:00
|
|
|
instance.dispose();
|
2020-07-01 18:08:50 +00:00
|
|
|
editorEl.remove();
|
2021-08-16 12:09:17 +00:00
|
|
|
mockAxios.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets up the instance', () => {
|
|
|
|
expect(instance.preview).toEqual({
|
|
|
|
el: undefined,
|
|
|
|
action: expect.any(Object),
|
|
|
|
shown: false,
|
2021-08-17 15:10:19 +00:00
|
|
|
modelChangeListener: undefined,
|
2021-08-16 12:09:17 +00:00
|
|
|
});
|
2021-08-24 15:10:36 +00:00
|
|
|
expect(instance.previewMarkdownPath).toBe(previewMarkdownPath);
|
2021-08-16 12:09:17 +00:00
|
|
|
});
|
|
|
|
|
2021-08-17 15:10:19 +00:00
|
|
|
describe('model language changes listener', () => {
|
|
|
|
let cleanupSpy;
|
|
|
|
let actionSpy;
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
cleanupSpy = jest.spyOn(instance, 'cleanup');
|
|
|
|
actionSpy = jest.spyOn(instance, 'setupPreviewAction');
|
|
|
|
await togglePreview();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('cleans up when switching away from markdown', () => {
|
|
|
|
expect(instance.cleanup).not.toHaveBeenCalled();
|
|
|
|
expect(instance.setupPreviewAction).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
instance.updateModelLanguage(plaintextPath);
|
|
|
|
|
|
|
|
expect(cleanupSpy).toHaveBeenCalled();
|
|
|
|
expect(actionSpy).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
oldLanguage | newLanguage | setupCalledTimes
|
|
|
|
${'plaintext'} | ${'markdown'} | ${1}
|
|
|
|
${'markdown'} | ${'markdown'} | ${0}
|
|
|
|
${'markdown'} | ${'plaintext'} | ${0}
|
|
|
|
${'markdown'} | ${undefined} | ${0}
|
|
|
|
${undefined} | ${'markdown'} | ${1}
|
|
|
|
`(
|
|
|
|
'correctly handles re-enabling of the action when switching from $oldLanguage to $newLanguage',
|
|
|
|
({ oldLanguage, newLanguage, setupCalledTimes } = {}) => {
|
|
|
|
expect(actionSpy).not.toHaveBeenCalled();
|
|
|
|
instance.updateModelLanguage(oldLanguage);
|
|
|
|
instance.updateModelLanguage(newLanguage);
|
|
|
|
expect(actionSpy).toHaveBeenCalledTimes(setupCalledTimes);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('model change listener', () => {
|
|
|
|
let cleanupSpy;
|
|
|
|
let actionSpy;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
cleanupSpy = jest.spyOn(instance, 'cleanup');
|
|
|
|
actionSpy = jest.spyOn(instance, 'setupPreviewAction');
|
|
|
|
instance.togglePreview();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not do anything if there is no model', () => {
|
|
|
|
instance.setModel(null);
|
|
|
|
|
|
|
|
expect(cleanupSpy).not.toHaveBeenCalled();
|
|
|
|
expect(actionSpy).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('cleans up the preview when the model changes', () => {
|
|
|
|
instance.setModel(monacoEditor.createModel('foo'));
|
|
|
|
expect(cleanupSpy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
language | setupCalledTimes
|
|
|
|
${'markdown'} | ${1}
|
|
|
|
${'plaintext'} | ${0}
|
|
|
|
${undefined} | ${0}
|
|
|
|
`(
|
|
|
|
'correctly handles actions when the new model is $language',
|
|
|
|
({ language, setupCalledTimes } = {}) => {
|
|
|
|
instance.setModel(monacoEditor.createModel('foo', language));
|
|
|
|
|
|
|
|
expect(actionSpy).toHaveBeenCalledTimes(setupCalledTimes);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-08-16 12:09:17 +00:00
|
|
|
describe('cleanup', () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
mockAxios.onPost().reply(200, { body: responseData });
|
|
|
|
await togglePreview();
|
|
|
|
});
|
|
|
|
|
2021-08-17 15:10:19 +00:00
|
|
|
it('disposes the modelChange listener and does not fetch preview on content changes', () => {
|
|
|
|
expect(instance.preview.modelChangeListener).toBeDefined();
|
|
|
|
jest.spyOn(instance, 'fetchPreview');
|
|
|
|
|
|
|
|
instance.cleanup();
|
|
|
|
instance.setValue('Foo Bar');
|
|
|
|
jest.advanceTimersByTime(EXTENSION_MARKDOWN_PREVIEW_UPDATE_DELAY);
|
|
|
|
|
|
|
|
expect(instance.fetchPreview).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2021-08-16 12:09:17 +00:00
|
|
|
it('removes the contextual menu action', () => {
|
|
|
|
expect(instance.getAction(EXTENSION_MARKDOWN_PREVIEW_ACTION_ID)).toBeDefined();
|
|
|
|
|
|
|
|
instance.cleanup();
|
|
|
|
|
|
|
|
expect(instance.getAction(EXTENSION_MARKDOWN_PREVIEW_ACTION_ID)).toBe(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('toggles the `shown` flag', () => {
|
|
|
|
expect(instance.preview.shown).toBe(true);
|
|
|
|
instance.cleanup();
|
|
|
|
expect(instance.preview.shown).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('toggles the panel only if the preview is visible', () => {
|
|
|
|
const { el: previewEl } = instance.preview;
|
|
|
|
const parentEl = previewEl.parentElement;
|
|
|
|
|
|
|
|
expect(previewEl).toBeVisible();
|
|
|
|
expect(parentEl.classList.contains(EXTENSION_MARKDOWN_PREVIEW_PANEL_PARENT_CLASS)).toBe(true);
|
|
|
|
|
|
|
|
instance.cleanup();
|
|
|
|
expect(previewEl).toBeHidden();
|
|
|
|
expect(parentEl.classList.contains(EXTENSION_MARKDOWN_PREVIEW_PANEL_PARENT_CLASS)).toBe(
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
|
|
|
|
instance.cleanup();
|
|
|
|
expect(previewEl).toBeHidden();
|
|
|
|
expect(parentEl.classList.contains(EXTENSION_MARKDOWN_PREVIEW_PANEL_PARENT_CLASS)).toBe(
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('toggles the layout only if the preview is visible', () => {
|
|
|
|
const { width } = instance.getLayoutInfo();
|
|
|
|
|
|
|
|
expect(instance.preview.shown).toBe(true);
|
|
|
|
|
|
|
|
instance.cleanup();
|
|
|
|
|
|
|
|
const { width: newWidth } = instance.getLayoutInfo();
|
|
|
|
expect(newWidth === width / EXTENSION_MARKDOWN_PREVIEW_PANEL_WIDTH).toBe(true);
|
|
|
|
|
|
|
|
instance.cleanup();
|
|
|
|
expect(newWidth === width / EXTENSION_MARKDOWN_PREVIEW_PANEL_WIDTH).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('fetchPreview', () => {
|
|
|
|
const fetchPreview = async () => {
|
|
|
|
instance.fetchPreview();
|
|
|
|
await waitForPromises();
|
|
|
|
};
|
|
|
|
|
2021-08-24 15:10:36 +00:00
|
|
|
let previewMarkdownSpy;
|
|
|
|
|
2021-08-16 12:09:17 +00:00
|
|
|
beforeEach(() => {
|
2021-08-24 15:10:36 +00:00
|
|
|
previewMarkdownSpy = jest.fn().mockImplementation(() => [200, { body: responseData }]);
|
|
|
|
mockAxios.onPost(previewMarkdownPath).replyOnce((req) => previewMarkdownSpy(req));
|
2021-08-16 12:09:17 +00:00
|
|
|
});
|
|
|
|
|
2021-08-24 15:10:36 +00:00
|
|
|
it('correctly fetches preview based on previewMarkdownPath', async () => {
|
2021-08-16 12:09:17 +00:00
|
|
|
await fetchPreview();
|
|
|
|
|
2021-08-24 15:10:36 +00:00
|
|
|
expect(previewMarkdownSpy).toHaveBeenCalledWith(
|
|
|
|
expect.objectContaining({ data: JSON.stringify({ text }) }),
|
|
|
|
);
|
2021-08-16 12:09:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('puts the fetched content into the preview DOM element', async () => {
|
|
|
|
instance.preview.el = editorEl.parentElement;
|
|
|
|
await fetchPreview();
|
|
|
|
expect(instance.preview.el.innerHTML).toEqual(responseData);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('applies syntax highlighting to the preview content', async () => {
|
|
|
|
instance.preview.el = editorEl.parentElement;
|
|
|
|
await fetchPreview();
|
|
|
|
expect(syntaxHighlight).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('catches the errors when fetching the preview', async () => {
|
|
|
|
mockAxios.onPost().reply(500);
|
|
|
|
|
|
|
|
await fetchPreview();
|
|
|
|
expect(createFlash).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setupPreviewAction', () => {
|
|
|
|
it('adds the contextual menu action', () => {
|
|
|
|
expect(instance.getAction(EXTENSION_MARKDOWN_PREVIEW_ACTION_ID)).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not set up action if one already exists', () => {
|
|
|
|
jest.spyOn(instance, 'addAction').mockImplementation();
|
|
|
|
|
|
|
|
instance.setupPreviewAction();
|
|
|
|
expect(instance.addAction).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('toggles preview when the action is triggered', () => {
|
|
|
|
jest.spyOn(instance, 'togglePreview').mockImplementation();
|
|
|
|
|
|
|
|
expect(instance.togglePreview).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
const action = instance.getAction(EXTENSION_MARKDOWN_PREVIEW_ACTION_ID);
|
|
|
|
action.run();
|
|
|
|
|
|
|
|
expect(instance.togglePreview).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('togglePreview', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mockAxios.onPost().reply(200, { body: responseData });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('toggles preview flag on instance', () => {
|
|
|
|
expect(instance.preview.shown).toBe(false);
|
|
|
|
|
|
|
|
instance.togglePreview();
|
|
|
|
expect(instance.preview.shown).toBe(true);
|
|
|
|
|
|
|
|
instance.togglePreview();
|
|
|
|
expect(instance.preview.shown).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('panel DOM element set up', () => {
|
|
|
|
it('sets up an element to contain the preview and stores it on instance', () => {
|
|
|
|
expect(instance.preview.el).toBeUndefined();
|
|
|
|
|
|
|
|
instance.togglePreview();
|
|
|
|
|
|
|
|
expect(instance.preview.el).toBeDefined();
|
|
|
|
expect(instance.preview.el.classList.contains(EXTENSION_MARKDOWN_PREVIEW_PANEL_CLASS)).toBe(
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('re-uses existing preview DOM element on repeated calls', () => {
|
|
|
|
instance.togglePreview();
|
|
|
|
const origPreviewEl = instance.preview.el;
|
|
|
|
instance.togglePreview();
|
|
|
|
|
|
|
|
expect(instance.preview.el).toBe(origPreviewEl);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('hides the preview DOM element by default', () => {
|
|
|
|
panelSpy.mockImplementation();
|
|
|
|
instance.togglePreview();
|
|
|
|
expect(instance.preview.el.style.display).toBe('none');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('preview layout setup', () => {
|
|
|
|
it('sets correct preview layout', () => {
|
|
|
|
jest.spyOn(instance, 'layout');
|
|
|
|
const { width, height } = instance.getLayoutInfo();
|
|
|
|
|
|
|
|
instance.togglePreview();
|
|
|
|
|
|
|
|
expect(instance.layout).toHaveBeenCalledWith({
|
|
|
|
width: width * EXTENSION_MARKDOWN_PREVIEW_PANEL_WIDTH,
|
|
|
|
height,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('preview panel', () => {
|
|
|
|
it('toggles preview CSS class on the editor', () => {
|
|
|
|
expect(editorEl.classList.contains(EXTENSION_MARKDOWN_PREVIEW_PANEL_PARENT_CLASS)).toBe(
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
instance.togglePreview();
|
|
|
|
expect(editorEl.classList.contains(EXTENSION_MARKDOWN_PREVIEW_PANEL_PARENT_CLASS)).toBe(
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
instance.togglePreview();
|
|
|
|
expect(editorEl.classList.contains(EXTENSION_MARKDOWN_PREVIEW_PANEL_PARENT_CLASS)).toBe(
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('toggles visibility of the preview DOM element', async () => {
|
|
|
|
await togglePreview();
|
|
|
|
expect(instance.preview.el.style.display).toBe('block');
|
|
|
|
await togglePreview();
|
|
|
|
expect(instance.preview.el.style.display).toBe('none');
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('hidden preview DOM element', () => {
|
|
|
|
it('listens to model changes and re-fetches preview', async () => {
|
|
|
|
expect(mockAxios.history.post).toHaveLength(0);
|
|
|
|
await togglePreview();
|
|
|
|
expect(mockAxios.history.post).toHaveLength(1);
|
|
|
|
|
|
|
|
instance.setValue('New Value');
|
|
|
|
await waitForPromises();
|
|
|
|
expect(mockAxios.history.post).toHaveLength(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('stores disposable listener for model changes', async () => {
|
2021-08-17 15:10:19 +00:00
|
|
|
expect(instance.preview.modelChangeListener).toBeUndefined();
|
2021-08-16 12:09:17 +00:00
|
|
|
await togglePreview();
|
2021-08-17 15:10:19 +00:00
|
|
|
expect(instance.preview.modelChangeListener).toBeDefined();
|
2021-08-16 12:09:17 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('already visible preview', () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await togglePreview();
|
|
|
|
mockAxios.resetHistory();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not re-fetch the preview', () => {
|
|
|
|
instance.togglePreview();
|
|
|
|
expect(mockAxios.history.post).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('disposes the model change event listener', () => {
|
|
|
|
const disposeSpy = jest.fn();
|
2021-08-17 15:10:19 +00:00
|
|
|
instance.preview.modelChangeListener = {
|
2021-08-16 12:09:17 +00:00
|
|
|
dispose: disposeSpy,
|
|
|
|
};
|
|
|
|
instance.togglePreview();
|
|
|
|
expect(disposeSpy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-07-01 18:08:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('getSelectedText', () => {
|
|
|
|
it('does not fail if there is no selection and returns the empty string', () => {
|
2020-08-26 09:10:16 +00:00
|
|
|
jest.spyOn(instance, 'getSelection');
|
|
|
|
const resText = instance.getSelectedText();
|
2020-07-01 18:08:50 +00:00
|
|
|
|
2020-08-26 09:10:16 +00:00
|
|
|
expect(instance.getSelection).toHaveBeenCalled();
|
2020-07-01 18:08:50 +00:00
|
|
|
expect(resText).toBe('');
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
description | selection | expectedString
|
|
|
|
${'same-line'} | ${[1, 1, 1, firstLine.length + 1]} | ${firstLine}
|
|
|
|
${'two-lines'} | ${[1, 1, 2, secondLine.length + 1]} | ${`${firstLine}\n${secondLine}`}
|
|
|
|
${'multi-lines'} | ${[1, 1, 3, thirdLine.length + 1]} | ${text}
|
|
|
|
`('correctly returns selected text for $description', ({ selection, expectedString }) => {
|
|
|
|
setSelection(...selection);
|
|
|
|
|
2020-08-26 09:10:16 +00:00
|
|
|
const resText = instance.getSelectedText();
|
2020-07-01 18:08:50 +00:00
|
|
|
|
|
|
|
expect(resText).toBe(expectedString);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('accepts selection object that serves as a source instead of current selection', () => {
|
|
|
|
selectSecondString();
|
|
|
|
const firstLineSelection = new Range(1, 1, 1, firstLine.length + 1);
|
|
|
|
|
2020-08-26 09:10:16 +00:00
|
|
|
const resText = instance.getSelectedText(firstLineSelection);
|
2020-07-01 18:08:50 +00:00
|
|
|
|
|
|
|
expect(resText).toBe(firstLine);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('replaceSelectedText', () => {
|
|
|
|
const expectedStr = 'foo';
|
|
|
|
|
|
|
|
it('replaces selected text with the supplied one', () => {
|
|
|
|
selectSecondString();
|
2020-08-26 09:10:16 +00:00
|
|
|
instance.replaceSelectedText(expectedStr);
|
2020-07-01 18:08:50 +00:00
|
|
|
|
2020-08-31 18:10:43 +00:00
|
|
|
expect(instance.getValue()).toBe(`${firstLine}\n${expectedStr}\n${thirdLine}`);
|
2020-07-01 18:08:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('prepends the supplied text if no text is selected', () => {
|
2020-08-26 09:10:16 +00:00
|
|
|
instance.replaceSelectedText(expectedStr);
|
2020-08-31 18:10:43 +00:00
|
|
|
expect(instance.getValue()).toBe(`${expectedStr}${firstLine}\n${secondLine}\n${thirdLine}`);
|
2020-07-01 18:08:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('replaces selection with empty string if no text is supplied', () => {
|
|
|
|
selectSecondString();
|
2020-08-26 09:10:16 +00:00
|
|
|
instance.replaceSelectedText();
|
2020-08-31 18:10:43 +00:00
|
|
|
expect(instance.getValue()).toBe(`${firstLine}\n\n${thirdLine}`);
|
2020-07-01 18:08:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('puts cursor at the end of the new string and collapses selection by default', () => {
|
|
|
|
selectSecondString();
|
2020-08-26 09:10:16 +00:00
|
|
|
instance.replaceSelectedText(expectedStr);
|
2020-07-01 18:08:50 +00:00
|
|
|
|
|
|
|
expect(positionToString()).toBe(`(2,${expectedStr.length + 1})`);
|
|
|
|
expect(selectionToString()).toBe(
|
|
|
|
`[2,${expectedStr.length + 1} -> 2,${expectedStr.length + 1}]`,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('puts cursor at the end of the new string and keeps selection if "select" is supplied', () => {
|
|
|
|
const select = 'url';
|
|
|
|
const complexReplacementString = `[${secondLine}](${select})`;
|
|
|
|
selectSecondString();
|
2020-08-26 09:10:16 +00:00
|
|
|
instance.replaceSelectedText(complexReplacementString, select);
|
2020-07-01 18:08:50 +00:00
|
|
|
|
|
|
|
expect(positionToString()).toBe(`(2,${complexReplacementString.length + 1})`);
|
|
|
|
expect(selectionToString()).toBe(`[2,1 -> 2,${complexReplacementString.length + 1}]`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('moveCursor', () => {
|
2020-12-23 21:10:24 +00:00
|
|
|
const setPosition = (endCol) => {
|
2020-07-01 18:08:50 +00:00
|
|
|
const currentPos = new Position(2, endCol);
|
2020-08-26 09:10:16 +00:00
|
|
|
instance.setPosition(currentPos);
|
2020-07-01 18:08:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
direction | condition | startColumn | shift | endPosition
|
|
|
|
${'left'} | ${'negative'} | ${secondLine.length + 1} | ${-1} | ${`(2,${secondLine.length})`}
|
|
|
|
${'left'} | ${'negative'} | ${secondLine.length} | ${secondLine.length * -1} | ${'(2,1)'}
|
|
|
|
${'right'} | ${'positive'} | ${1} | ${1} | ${'(2,2)'}
|
|
|
|
${'right'} | ${'positive'} | ${2} | ${secondLine.length} | ${`(2,${secondLine.length + 1})`}
|
|
|
|
${'up'} | ${'positive'} | ${1} | ${[0, -1]} | ${'(1,1)'}
|
|
|
|
${'top of file'} | ${'positive'} | ${1} | ${[0, -100]} | ${'(1,1)'}
|
|
|
|
${'down'} | ${'negative'} | ${1} | ${[0, 1]} | ${'(3,1)'}
|
|
|
|
${'end of file'} | ${'negative'} | ${1} | ${[0, 100]} | ${`(3,${thirdLine.length + 1})`}
|
|
|
|
${'end of line'} | ${'too large'} | ${1} | ${secondLine.length + 100} | ${`(2,${secondLine.length + 1})`}
|
|
|
|
${'start of line'} | ${'too low'} | ${1} | ${-100} | ${'(2,1)'}
|
|
|
|
`(
|
|
|
|
'moves cursor to the $direction if $condition supplied',
|
|
|
|
({ startColumn, shift, endPosition }) => {
|
|
|
|
setPosition(startColumn);
|
|
|
|
if (Array.isArray(shift)) {
|
2020-08-26 09:10:16 +00:00
|
|
|
instance.moveCursor(...shift);
|
2020-07-01 18:08:50 +00:00
|
|
|
} else {
|
2020-08-26 09:10:16 +00:00
|
|
|
instance.moveCursor(shift);
|
2020-07-01 18:08:50 +00:00
|
|
|
}
|
|
|
|
expect(positionToString()).toBe(endPosition);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('selectWithinSelection', () => {
|
|
|
|
it('scopes down current selection to supplied text', () => {
|
|
|
|
const selectedText = `${secondLine}\n${thirdLine}`;
|
|
|
|
const toSelect = 'string';
|
|
|
|
selectSecondAndThirdLines();
|
|
|
|
|
|
|
|
expect(selectionToString()).toBe(`[2,1 -> 3,${thirdLine.length + 1}]`);
|
|
|
|
|
2020-08-26 09:10:16 +00:00
|
|
|
instance.selectWithinSelection(toSelect, selectedText);
|
2020-07-01 18:08:50 +00:00
|
|
|
expect(selectionToString()).toBe(`[3,1 -> 3,${toSelect.length + 1}]`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not fail when only `toSelect` is supplied and fetches the text from selection', () => {
|
2020-08-26 09:10:16 +00:00
|
|
|
jest.spyOn(instance, 'getSelectedText');
|
2020-07-01 18:08:50 +00:00
|
|
|
const toSelect = 'string';
|
|
|
|
selectSecondAndThirdLines();
|
|
|
|
|
2020-08-26 09:10:16 +00:00
|
|
|
instance.selectWithinSelection(toSelect);
|
2020-07-01 18:08:50 +00:00
|
|
|
|
2020-08-26 09:10:16 +00:00
|
|
|
expect(instance.getSelectedText).toHaveBeenCalled();
|
2020-07-01 18:08:50 +00:00
|
|
|
expect(selectionToString()).toBe(`[3,1 -> 3,${toSelect.length + 1}]`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does nothing if no `toSelect` is supplied', () => {
|
|
|
|
selectSecondAndThirdLines();
|
|
|
|
const expectedPos = `(3,${thirdLine.length + 1})`;
|
|
|
|
const expectedSelection = `[2,1 -> 3,${thirdLine.length + 1}]`;
|
|
|
|
|
|
|
|
expect(positionToString()).toBe(expectedPos);
|
|
|
|
expect(selectionToString()).toBe(expectedSelection);
|
|
|
|
|
2020-08-26 09:10:16 +00:00
|
|
|
instance.selectWithinSelection();
|
2020-07-01 18:08:50 +00:00
|
|
|
|
|
|
|
expect(positionToString()).toBe(expectedPos);
|
|
|
|
expect(selectionToString()).toBe(expectedSelection);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does nothing if no selection is set in the editor', () => {
|
|
|
|
const expectedPos = '(1,1)';
|
|
|
|
const expectedSelection = '[1,1 -> 1,1]';
|
|
|
|
const toSelect = 'string';
|
|
|
|
|
|
|
|
expect(positionToString()).toBe(expectedPos);
|
|
|
|
expect(selectionToString()).toBe(expectedSelection);
|
|
|
|
|
2020-08-26 09:10:16 +00:00
|
|
|
instance.selectWithinSelection(toSelect);
|
2020-07-01 18:08:50 +00:00
|
|
|
|
|
|
|
expect(positionToString()).toBe(expectedPos);
|
|
|
|
expect(selectionToString()).toBe(expectedSelection);
|
|
|
|
|
2020-08-26 09:10:16 +00:00
|
|
|
instance.selectWithinSelection();
|
2020-07-01 18:08:50 +00:00
|
|
|
|
|
|
|
expect(positionToString()).toBe(expectedPos);
|
|
|
|
expect(selectionToString()).toBe(expectedSelection);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|