debian-mirror-gitlab/spec/frontend/editor/source_editor_spec.js

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

436 lines
14 KiB
JavaScript
Raw Normal View History

2021-03-11 19:13:27 +05:30
import { editor as monacoEditor, languages as monacoLanguages } from 'monaco-editor';
2022-07-16 23:28:13 +05:30
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
2021-03-11 19:13:27 +05:30
import {
2021-09-30 23:02:18 +05:30
SOURCE_EDITOR_INSTANCE_ERROR_NO_EL,
2021-03-11 19:13:27 +05:30
URI_PREFIX,
EDITOR_READY_EVENT,
} from '~/editor/constants';
2021-09-30 23:02:18 +05:30
import SourceEditor from '~/editor/source_editor';
2020-04-08 14:13:33 +05:30
import { DEFAULT_THEME, themes } from '~/ide/lib/themes';
2021-03-11 19:13:27 +05:30
import { joinPaths } from '~/lib/utils/url_utility';
2020-10-24 23:57:45 +05:30
2022-01-26 12:08:38 +05:30
jest.mock('~/helpers/startup_css_helper', () => {
return {
waitForCSSLoaded: jest.fn().mockImplementation((cb) => {
// We have to artificially put the callback's execution
// to the end of the current call stack to be able to
// test that the callback is called after waitForCSSLoaded.
// setTimeout with 0 delay does exactly that.
// Otherwise we might end up with false positive results
setTimeout(() => {
cb.apply();
}, 0);
}),
};
});
2020-03-13 15:44:24 +05:30
describe('Base editor', () => {
let editorEl;
let editor;
2021-03-11 19:13:27 +05:30
let defaultArguments;
const blobOriginalContent = 'Foo Foo';
2020-03-13 15:44:24 +05:30
const blobContent = 'Foo Bar';
const blobPath = 'test.md';
2020-10-24 23:57:45 +05:30
const blobGlobalId = 'snippet_777';
2020-03-13 15:44:24 +05:30
beforeEach(() => {
2022-07-16 23:28:13 +05:30
setHTMLFixture('<div id="editor" data-editor-loading></div>');
2020-03-13 15:44:24 +05:30
editorEl = document.getElementById('editor');
2021-03-11 19:13:27 +05:30
defaultArguments = { el: editorEl, blobPath, blobContent, blobGlobalId };
2021-09-30 23:02:18 +05:30
editor = new SourceEditor();
2020-03-13 15:44:24 +05:30
});
afterEach(() => {
editor.dispose();
editorEl.remove();
2021-03-11 19:13:27 +05:30
monacoEditor.getModels().forEach((model) => {
model.dispose();
});
2022-07-16 23:28:13 +05:30
resetHTMLFixture();
2020-03-13 15:44:24 +05:30
});
2021-03-11 19:13:27 +05:30
const uriFilePath = joinPaths('/', URI_PREFIX, blobGlobalId, blobPath);
2020-10-24 23:57:45 +05:30
2020-03-13 15:44:24 +05:30
it('initializes Editor with basic properties', () => {
expect(editor).toBeDefined();
2020-11-24 15:15:51 +05:30
expect(editor.instances).toEqual([]);
2020-03-13 15:44:24 +05:30
});
it('removes `editor-loading` data attribute from the target DOM element', () => {
editor.createInstance({ el: editorEl });
expect(editorEl.dataset.editorLoading).toBeUndefined();
});
2021-09-30 23:02:18 +05:30
describe('instance of the Source Editor', () => {
2020-03-13 15:44:24 +05:30
let modelSpy;
let instanceSpy;
beforeEach(() => {
2021-03-11 19:13:27 +05:30
modelSpy = jest.spyOn(monacoEditor, 'createModel');
2020-03-13 15:44:24 +05:30
});
2021-03-11 19:13:27 +05:30
describe('instance of the Code Editor', () => {
beforeEach(() => {
instanceSpy = jest.spyOn(monacoEditor, 'create');
});
2020-03-13 15:44:24 +05:30
2021-03-11 19:13:27 +05:30
it('throws an error if no dom element is supplied', () => {
2022-01-26 12:08:38 +05:30
const create = () => {
2021-03-11 19:13:27 +05:30
editor.createInstance();
2022-01-26 12:08:38 +05:30
};
expect(create).toThrow(SOURCE_EDITOR_INSTANCE_ERROR_NO_EL);
2020-03-13 15:44:24 +05:30
2021-03-11 19:13:27 +05:30
expect(modelSpy).not.toHaveBeenCalled();
expect(instanceSpy).not.toHaveBeenCalled();
});
2020-03-13 15:44:24 +05:30
2022-01-26 12:08:38 +05:30
it('creates model and attaches it to the instance', () => {
jest.spyOn(monacoEditor, 'createModel');
const instance = editor.createInstance(defaultArguments);
2020-03-13 15:44:24 +05:30
2022-01-26 12:08:38 +05:30
expect(monacoEditor.createModel).toHaveBeenCalledWith(
2021-03-11 19:13:27 +05:30
blobContent,
undefined,
expect.objectContaining({
path: uriFilePath,
}),
);
2022-01-26 12:08:38 +05:30
expect(instance.getModel().getValue()).toEqual(defaultArguments.blobContent);
2021-03-11 19:13:27 +05:30
});
2020-03-13 15:44:24 +05:30
2021-03-11 19:13:27 +05:30
it('does not create a model automatically if model is passed as `null`', () => {
2022-01-26 12:08:38 +05:30
const instance = editor.createInstance({ ...defaultArguments, model: null });
expect(instance.getModel()).toBeNull();
2021-03-11 19:13:27 +05:30
});
2020-10-24 23:57:45 +05:30
2021-03-11 19:13:27 +05:30
it('initializes the instance on a supplied DOM node', () => {
editor.createInstance({ el: editorEl });
2020-10-24 23:57:45 +05:30
2022-01-26 12:08:38 +05:30
expect(editor.editorEl).not.toBeNull();
2021-03-11 19:13:27 +05:30
expect(instanceSpy).toHaveBeenCalledWith(editorEl, expect.anything());
});
2020-11-24 15:15:51 +05:30
2021-03-11 19:13:27 +05:30
it('with blobGlobalId, creates model with the id in uri', () => {
editor.createInstance(defaultArguments);
expect(modelSpy).toHaveBeenCalledWith(
blobContent,
undefined,
expect.objectContaining({
path: uriFilePath,
}),
);
});
it('initializes instance with passed properties', () => {
const instanceOptions = {
foo: 'bar',
};
editor.createInstance({
el: editorEl,
...instanceOptions,
});
expect(instanceSpy).toHaveBeenCalledWith(
editorEl,
expect.objectContaining(instanceOptions),
);
});
it('disposes instance when the global editor is disposed', () => {
2022-01-26 12:08:38 +05:30
const instance = editor.createInstance(defaultArguments);
instance.dispose = jest.fn();
2021-03-11 19:13:27 +05:30
2022-01-26 12:08:38 +05:30
expect(instance.dispose).not.toHaveBeenCalled();
2021-03-11 19:13:27 +05:30
editor.dispose();
2022-01-26 12:08:38 +05:30
expect(instance.dispose).toHaveBeenCalled();
2021-03-11 19:13:27 +05:30
});
it("removes the disposed instance from the global editor's storage and disposes the associated model", () => {
const instance = editor.createInstance(defaultArguments);
expect(editor.instances).toHaveLength(1);
2022-01-26 12:08:38 +05:30
expect(instance.getModel()).not.toBeNull();
2021-03-11 19:13:27 +05:30
instance.dispose();
expect(editor.instances).toHaveLength(0);
2022-01-26 12:08:38 +05:30
expect(instance.getModel()).toBeNull();
});
it('resets the layout in waitForCSSLoaded callback', async () => {
const layoutSpy = jest.fn();
jest.spyOn(monacoEditor, 'create').mockReturnValue({
layout: layoutSpy,
setModel: jest.fn(),
onDidDispose: jest.fn(),
dispose: jest.fn(),
});
editor.createInstance(defaultArguments);
expect(layoutSpy).not.toHaveBeenCalled();
// We're waiting for the waitForCSSLoaded mock to kick in
await jest.runOnlyPendingTimers();
expect(layoutSpy).toHaveBeenCalled();
2020-11-24 15:15:51 +05:30
});
});
2021-03-11 19:13:27 +05:30
describe('instance of the Diff Editor', () => {
beforeEach(() => {
instanceSpy = jest.spyOn(monacoEditor, 'createDiffEditor');
});
2020-11-24 15:15:51 +05:30
2021-03-11 19:13:27 +05:30
it('Diff Editor goes through the normal path of Code Editor just with the flag ON', () => {
const spy = jest.spyOn(editor, 'createInstance').mockImplementation(() => {});
editor.createDiffInstance();
expect(spy).toHaveBeenCalledWith(
expect.objectContaining({
isDiff: true,
}),
);
});
2020-11-24 15:15:51 +05:30
2021-03-11 19:13:27 +05:30
it('initializes the instance on a supplied DOM node', () => {
const wrongInstanceSpy = jest.spyOn(monacoEditor, 'create').mockImplementation(() => ({}));
editor.createDiffInstance({ ...defaultArguments, blobOriginalContent });
expect(editor.editorEl).not.toBe(null);
expect(wrongInstanceSpy).not.toHaveBeenCalled();
expect(instanceSpy).toHaveBeenCalledWith(editorEl, expect.anything());
});
it('creates correct model for the Diff Editor', () => {
const instance = editor.createDiffInstance({ ...defaultArguments, blobOriginalContent });
const getDiffModelValue = (model) => instance.getModel()[model].getValue();
expect(modelSpy).toHaveBeenCalledTimes(2);
expect(modelSpy.mock.calls[0]).toEqual([
blobContent,
undefined,
expect.objectContaining({
path: uriFilePath,
}),
]);
expect(modelSpy.mock.calls[1]).toEqual([blobOriginalContent, 'markdown']);
expect(getDiffModelValue('original')).toBe(blobOriginalContent);
expect(getDiffModelValue('modified')).toBe(blobContent);
});
2020-11-24 15:15:51 +05:30
2021-03-11 19:13:27 +05:30
it('correctly disposes the diff editor model', () => {
const instance = editor.createDiffInstance({ ...defaultArguments, blobOriginalContent });
expect(editor.instances).toHaveLength(1);
2022-01-26 12:08:38 +05:30
expect(instance.getOriginalEditor().getModel()).not.toBeNull();
expect(instance.getModifiedEditor().getModel()).not.toBeNull();
2021-03-11 19:13:27 +05:30
instance.dispose();
expect(editor.instances).toHaveLength(0);
2022-01-26 12:08:38 +05:30
expect(instance.getOriginalEditor().getModel()).toBeNull();
expect(instance.getModifiedEditor().getModel()).toBeNull();
2021-03-11 19:13:27 +05:30
});
2020-11-24 15:15:51 +05:30
});
});
describe('multiple instances', () => {
let instanceSpy;
let inst1Args;
let inst2Args;
let editorEl1;
let editorEl2;
let inst1;
let inst2;
2021-09-30 23:02:18 +05:30
const readOnlyIndex = '78'; // readOnly option has the internal index of 78 in the editor's options
2020-11-24 15:15:51 +05:30
beforeEach(() => {
2022-07-16 23:28:13 +05:30
setHTMLFixture('<div id="editor1"></div><div id="editor2"></div>');
2020-11-24 15:15:51 +05:30
editorEl1 = document.getElementById('editor1');
editorEl2 = document.getElementById('editor2');
inst1Args = {
el: editorEl1,
};
inst2Args = {
el: editorEl2,
blobContent,
blobPath,
};
2021-09-30 23:02:18 +05:30
editor = new SourceEditor();
2020-11-24 15:15:51 +05:30
instanceSpy = jest.spyOn(monacoEditor, 'create');
});
afterEach(() => {
editor.dispose();
2022-07-16 23:28:13 +05:30
resetHTMLFixture();
2020-11-24 15:15:51 +05:30
});
it('can initialize several instances of the same editor', () => {
editor.createInstance(inst1Args);
expect(editor.instances).toHaveLength(1);
editor.createInstance(inst2Args);
expect(instanceSpy).toHaveBeenCalledTimes(2);
expect(editor.instances).toHaveLength(2);
});
it('sets independent models on independent instances', () => {
inst1 = editor.createInstance(inst1Args);
inst2 = editor.createInstance(inst2Args);
const model1 = inst1.getModel();
const model2 = inst2.getModel();
expect(model1).toBeDefined();
expect(model2).toBeDefined();
expect(model1).not.toEqual(model2);
});
2021-03-11 19:13:27 +05:30
it('does not create a new model if a model for the path & globalId combo already exists', () => {
const modelSpy = jest.spyOn(monacoEditor, 'createModel');
inst1 = editor.createInstance({ ...inst2Args, blobGlobalId });
inst2 = editor.createInstance({ ...inst2Args, el: editorEl1, blobGlobalId });
const model1 = inst1.getModel();
const model2 = inst2.getModel();
expect(modelSpy).toHaveBeenCalledTimes(1);
expect(model1).toBe(model2);
});
2020-11-24 15:15:51 +05:30
it('shares global editor options among all instances', () => {
2021-09-30 23:02:18 +05:30
editor = new SourceEditor({
2020-11-24 15:15:51 +05:30
readOnly: true,
});
inst1 = editor.createInstance(inst1Args);
expect(inst1.getOption(readOnlyIndex)).toBe(true);
inst2 = editor.createInstance(inst2Args);
expect(inst2.getOption(readOnlyIndex)).toBe(true);
});
it('allows overriding editor options on the instance level', () => {
2021-09-30 23:02:18 +05:30
editor = new SourceEditor({
2020-11-24 15:15:51 +05:30
readOnly: true,
});
inst1 = editor.createInstance({
...inst1Args,
readOnly: false,
});
expect(inst1.getOption(readOnlyIndex)).toBe(false);
});
it('disposes instances and relevant models independently from each other', () => {
inst1 = editor.createInstance(inst1Args);
inst2 = editor.createInstance(inst2Args);
expect(inst1.getModel()).not.toBe(null);
expect(inst2.getModel()).not.toBe(null);
expect(editor.instances).toHaveLength(2);
expect(monacoEditor.getModels()).toHaveLength(2);
inst1.dispose();
2021-03-11 19:13:27 +05:30
2020-11-24 15:15:51 +05:30
expect(inst1.getModel()).toBe(null);
expect(inst2.getModel()).not.toBe(null);
expect(editor.instances).toHaveLength(1);
expect(monacoEditor.getModels()).toHaveLength(1);
});
2020-03-13 15:44:24 +05:30
});
describe('implementation', () => {
2020-11-24 15:15:51 +05:30
let instance;
2020-03-13 15:44:24 +05:30
it('correctly proxies value from the model', () => {
2022-03-02 08:16:31 +05:30
instance = editor.createInstance({ el: editorEl, blobPath, blobContent });
2020-11-24 15:15:51 +05:30
expect(instance.getValue()).toBe(blobContent);
2020-03-13 15:44:24 +05:30
});
2022-03-02 08:16:31 +05:30
it('emits the EDITOR_READY_EVENT event passing the instance after setting it up', () => {
2022-01-26 12:08:38 +05:30
jest.spyOn(monacoEditor, 'create').mockImplementation(() => {
return {
setModel: jest.fn(),
onDidDispose: jest.fn(),
layout: jest.fn(),
2022-03-02 08:16:31 +05:30
dispose: jest.fn(),
2021-01-03 14:25:43 +05:30
};
2020-11-24 15:15:51 +05:30
});
2022-03-02 08:16:31 +05:30
let passedInstance;
const eventSpy = jest.fn().mockImplementation((ev) => {
passedInstance = ev.detail.instance;
});
2022-01-26 12:08:38 +05:30
editorEl.addEventListener(EDITOR_READY_EVENT, eventSpy);
expect(eventSpy).not.toHaveBeenCalled();
2022-03-02 08:16:31 +05:30
instance = editor.createInstance({ el: editorEl });
2022-01-26 12:08:38 +05:30
expect(eventSpy).toHaveBeenCalled();
2022-03-02 08:16:31 +05:30
expect(passedInstance).toBe(instance);
2020-07-28 23:09:34 +05:30
});
});
2020-05-24 23:13:21 +05:30
describe('languages', () => {
it('registers custom languages defined with Monaco', () => {
expect(monacoLanguages.getLanguages()).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: 'vue',
}),
]),
);
});
});
2020-04-08 14:13:33 +05:30
describe('syntax highlighting theme', () => {
let themeDefineSpy;
let themeSetSpy;
let defaultScheme;
beforeEach(() => {
2020-05-24 23:13:21 +05:30
themeDefineSpy = jest.spyOn(monacoEditor, 'defineTheme').mockImplementation(() => {});
themeSetSpy = jest.spyOn(monacoEditor, 'setTheme').mockImplementation(() => {});
2020-04-08 14:13:33 +05:30
defaultScheme = window.gon.user_color_scheme;
});
afterEach(() => {
window.gon.user_color_scheme = defaultScheme;
});
it('sets default syntax highlighting theme', () => {
2021-03-08 18:12:59 +05:30
const expectedTheme = themes.find((t) => t.name === DEFAULT_THEME);
2020-04-08 14:13:33 +05:30
2021-09-30 23:02:18 +05:30
editor = new SourceEditor();
2020-04-08 14:13:33 +05:30
expect(themeDefineSpy).toHaveBeenCalledWith(DEFAULT_THEME, expectedTheme.data);
expect(themeSetSpy).toHaveBeenCalledWith(DEFAULT_THEME);
});
it('sets correct theme if it is set in users preferences', () => {
2021-03-08 18:12:59 +05:30
const expectedTheme = themes.find((t) => t.name !== DEFAULT_THEME);
2020-04-08 14:13:33 +05:30
expect(expectedTheme.name).not.toBe(DEFAULT_THEME);
window.gon.user_color_scheme = expectedTheme.name;
2021-09-30 23:02:18 +05:30
editor = new SourceEditor();
2020-04-08 14:13:33 +05:30
expect(themeDefineSpy).toHaveBeenCalledWith(expectedTheme.name, expectedTheme.data);
expect(themeSetSpy).toHaveBeenCalledWith(expectedTheme.name);
});
it('falls back to default theme if a selected one is not supported yet', () => {
const name = 'non-existent-theme';
const nonExistentTheme = { name };
window.gon.user_color_scheme = nonExistentTheme.name;
2021-09-30 23:02:18 +05:30
editor = new SourceEditor();
2020-04-08 14:13:33 +05:30
expect(themeDefineSpy).not.toHaveBeenCalled();
expect(themeSetSpy).toHaveBeenCalledWith(DEFAULT_THEME);
});
});
2020-03-13 15:44:24 +05:30
});