diff options
Diffstat (limited to 'spec/frontend/editor')
-rw-r--r-- | spec/frontend/editor/editor_ci_schema_ext_spec.js | 2 | ||||
-rw-r--r-- | spec/frontend/editor/editor_lite_spec.js | 298 |
2 files changed, 219 insertions, 81 deletions
diff --git a/spec/frontend/editor/editor_ci_schema_ext_spec.js b/spec/frontend/editor/editor_ci_schema_ext_spec.js index 9dd88aad7e6..17a9ae7335f 100644 --- a/spec/frontend/editor/editor_ci_schema_ext_spec.js +++ b/spec/frontend/editor/editor_ci_schema_ext_spec.js @@ -1,8 +1,8 @@ import { languages } from 'monaco-editor'; import { TEST_HOST } from 'helpers/test_constants'; +import { EXTENSION_CI_SCHEMA_FILE_NAME_MATCH } from '~/editor/constants'; import EditorLite from '~/editor/editor_lite'; import { CiSchemaExtension } from '~/editor/extensions/editor_ci_schema_ext'; -import { EXTENSION_CI_SCHEMA_FILE_NAME_MATCH } from '~/editor/constants'; describe('~/editor/editor_ci_config_ext', () => { const defaultBlobPath = '.gitlab-ci.yml'; diff --git a/spec/frontend/editor/editor_lite_spec.js b/spec/frontend/editor/editor_lite_spec.js index c3099997287..815457e012f 100644 --- a/spec/frontend/editor/editor_lite_spec.js +++ b/spec/frontend/editor/editor_lite_spec.js @@ -1,14 +1,21 @@ /* eslint-disable max-classes-per-file */ -import { editor as monacoEditor, languages as monacoLanguages, Uri } from 'monaco-editor'; +import { editor as monacoEditor, languages as monacoLanguages } from 'monaco-editor'; import waitForPromises from 'helpers/wait_for_promises'; -import Editor from '~/editor/editor_lite'; +import { + EDITOR_LITE_INSTANCE_ERROR_NO_EL, + URI_PREFIX, + EDITOR_READY_EVENT, +} from '~/editor/constants'; +import EditorLite from '~/editor/editor_lite'; import { EditorLiteExtension } from '~/editor/extensions/editor_lite_extension_base'; import { DEFAULT_THEME, themes } from '~/ide/lib/themes'; -import { EDITOR_LITE_INSTANCE_ERROR_NO_EL, URI_PREFIX } from '~/editor/constants'; +import { joinPaths } from '~/lib/utils/url_utility'; describe('Base editor', () => { let editorEl; let editor; + let defaultArguments; + const blobOriginalContent = 'Foo Foo'; const blobContent = 'Foo Bar'; const blobPath = 'test.md'; const blobGlobalId = 'snippet_777'; @@ -17,15 +24,19 @@ describe('Base editor', () => { beforeEach(() => { setFixtures('<div id="editor" data-editor-loading></div>'); editorEl = document.getElementById('editor'); - editor = new Editor(); + defaultArguments = { el: editorEl, blobPath, blobContent, blobGlobalId }; + editor = new EditorLite(); }); afterEach(() => { editor.dispose(); editorEl.remove(); + monacoEditor.getModels().forEach((model) => { + model.dispose(); + }); }); - const createUri = (...paths) => Uri.file([URI_PREFIX, ...paths].join('/')); + const uriFilePath = joinPaths('/', URI_PREFIX, blobGlobalId, blobPath); it('initializes Editor with basic properties', () => { expect(editor).toBeDefined(); @@ -38,76 +49,192 @@ describe('Base editor', () => { expect(editorEl.dataset.editorLoading).toBeUndefined(); }); - describe('instance of the Editor', () => { + describe('instance of the Editor Lite', () => { let modelSpy; let instanceSpy; - let setModel; - let dispose; + const setModel = jest.fn(); + const dispose = jest.fn(); + const mockModelReturn = (res = fakeModel) => { + modelSpy = jest.spyOn(monacoEditor, 'createModel').mockImplementation(() => res); + }; + const mockDecorateInstance = (decorations = {}) => { + jest.spyOn(EditorLite, 'convertMonacoToELInstance').mockImplementation((inst) => { + return Object.assign(inst, decorations); + }); + }; beforeEach(() => { - setModel = jest.fn(); - dispose = jest.fn(); - modelSpy = jest.spyOn(monacoEditor, 'createModel').mockImplementation(() => fakeModel); - instanceSpy = jest.spyOn(monacoEditor, 'create').mockImplementation(() => ({ - setModel, - dispose, - onDidDispose: jest.fn(), - })); + modelSpy = jest.spyOn(monacoEditor, 'createModel'); }); - it('throws an error if no dom element is supplied', () => { - expect(() => { - editor.createInstance(); - }).toThrow(EDITOR_LITE_INSTANCE_ERROR_NO_EL); + describe('instance of the Code Editor', () => { + beforeEach(() => { + instanceSpy = jest.spyOn(monacoEditor, 'create'); + }); - expect(modelSpy).not.toHaveBeenCalled(); - expect(instanceSpy).not.toHaveBeenCalled(); - expect(setModel).not.toHaveBeenCalled(); - }); + it('throws an error if no dom element is supplied', () => { + mockDecorateInstance(); + expect(() => { + editor.createInstance(); + }).toThrow(EDITOR_LITE_INSTANCE_ERROR_NO_EL); - it('creates model to be supplied to Monaco editor', () => { - editor.createInstance({ el: editorEl, blobPath, blobContent, blobGlobalId: '' }); + expect(modelSpy).not.toHaveBeenCalled(); + expect(instanceSpy).not.toHaveBeenCalled(); + expect(EditorLite.convertMonacoToELInstance).not.toHaveBeenCalled(); + }); - expect(modelSpy).toHaveBeenCalledWith(blobContent, undefined, createUri(blobPath)); - expect(setModel).toHaveBeenCalledWith(fakeModel); - }); + it('creates model to be supplied to Monaco editor', () => { + mockModelReturn(); + mockDecorateInstance({ + setModel, + }); + editor.createInstance(defaultArguments); - it('initializes the instance on a supplied DOM node', () => { - editor.createInstance({ el: editorEl }); + expect(modelSpy).toHaveBeenCalledWith( + blobContent, + undefined, + expect.objectContaining({ + path: uriFilePath, + }), + ); + expect(setModel).toHaveBeenCalledWith(fakeModel); + }); - expect(editor.editorEl).not.toBe(null); - expect(instanceSpy).toHaveBeenCalledWith(editorEl, expect.anything()); - }); + it('does not create a model automatically if model is passed as `null`', () => { + mockDecorateInstance({ + setModel, + }); + editor.createInstance({ ...defaultArguments, model: null }); + expect(modelSpy).not.toHaveBeenCalled(); + expect(setModel).not.toHaveBeenCalled(); + }); - it('with blobGlobalId, creates model with id in uri', () => { - editor.createInstance({ el: editorEl, blobPath, blobContent, blobGlobalId }); + it('initializes the instance on a supplied DOM node', () => { + editor.createInstance({ el: editorEl }); - expect(modelSpy).toHaveBeenCalledWith( - blobContent, - undefined, - createUri(blobGlobalId, blobPath), - ); - }); + expect(editor.editorEl).not.toBe(null); + expect(instanceSpy).toHaveBeenCalledWith(editorEl, expect.anything()); + }); - it('initializes instance with passed properties', () => { - const instanceOptions = { - foo: 'bar', - }; - editor.createInstance({ - el: editorEl, - ...instanceOptions, + 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', () => { + mockDecorateInstance({ + dispose, + }); + editor.createInstance(defaultArguments); + + expect(dispose).not.toHaveBeenCalled(); + + editor.dispose(); + + expect(dispose).toHaveBeenCalled(); + }); + + it("removes the disposed instance from the global editor's storage and disposes the associated model", () => { + mockModelReturn(); + mockDecorateInstance({ + setModel, + }); + const instance = editor.createInstance(defaultArguments); + + expect(editor.instances).toHaveLength(1); + expect(fakeModel.dispose).not.toHaveBeenCalled(); + + instance.dispose(); + + expect(editor.instances).toHaveLength(0); + expect(fakeModel.dispose).toHaveBeenCalled(); }); - expect(instanceSpy).toHaveBeenCalledWith(editorEl, expect.objectContaining(instanceOptions)); }); - it('disposes instance when the editor is disposed', () => { - editor.createInstance({ el: editorEl, blobPath, blobContent, blobGlobalId }); + describe('instance of the Diff Editor', () => { + beforeEach(() => { + instanceSpy = jest.spyOn(monacoEditor, 'createDiffEditor'); + }); - expect(dispose).not.toHaveBeenCalled(); + 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, + }), + ); + }); - editor.dispose(); + 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); + }); - expect(dispose).toHaveBeenCalled(); + it('correctly disposes the diff editor model', () => { + const modifiedModel = fakeModel; + const originalModel = { ...fakeModel }; + mockDecorateInstance({ + getModel: jest.fn().mockReturnValue({ + original: originalModel, + modified: modifiedModel, + }), + }); + + const instance = editor.createDiffInstance({ ...defaultArguments, blobOriginalContent }); + + expect(editor.instances).toHaveLength(1); + expect(originalModel.dispose).not.toHaveBeenCalled(); + expect(modifiedModel.dispose).not.toHaveBeenCalled(); + + instance.dispose(); + + expect(editor.instances).toHaveLength(0); + expect(originalModel.dispose).toHaveBeenCalled(); + expect(modifiedModel.dispose).toHaveBeenCalled(); + }); }); }); @@ -127,16 +254,14 @@ describe('Base editor', () => { editorEl2 = document.getElementById('editor2'); inst1Args = { el: editorEl1, - blobGlobalId, }; inst2Args = { el: editorEl2, blobContent, blobPath, - blobGlobalId, }; - editor = new Editor(); + editor = new EditorLite(); instanceSpy = jest.spyOn(monacoEditor, 'create'); }); @@ -166,8 +291,20 @@ describe('Base editor', () => { expect(model1).not.toEqual(model2); }); + 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); + }); + it('shares global editor options among all instances', () => { - editor = new Editor({ + editor = new EditorLite({ readOnly: true, }); @@ -179,7 +316,7 @@ describe('Base editor', () => { }); it('allows overriding editor options on the instance level', () => { - editor = new Editor({ + editor = new EditorLite({ readOnly: true, }); inst1 = editor.createInstance({ @@ -200,6 +337,7 @@ describe('Base editor', () => { expect(monacoEditor.getModels()).toHaveLength(2); inst1.dispose(); + expect(inst1.getModel()).toBe(null); expect(inst2.getModel()).not.toBe(null); expect(editor.instances).toHaveLength(1); @@ -402,19 +540,20 @@ describe('Base editor', () => { el: editorEl, blobPath, blobContent, - blobGlobalId, extensions, }); }; beforeEach(() => { - editorExtensionSpy = jest.spyOn(Editor, 'pushToImportsArray').mockImplementation((arr) => { - arr.push( - Promise.resolve({ - default: {}, - }), - ); - }); + editorExtensionSpy = jest + .spyOn(EditorLite, 'pushToImportsArray') + .mockImplementation((arr) => { + arr.push( + Promise.resolve({ + default: {}, + }), + ); + }); }); it.each([undefined, [], [''], ''])( @@ -446,15 +585,20 @@ describe('Base editor', () => { expect(editorExtensionSpy).toHaveBeenCalledWith(expect.any(Array), expectation); }); - it('emits editor-ready event after all extensions were applied', async () => { + it('emits EDITOR_READY_EVENT event after all extensions were applied', async () => { const calls = []; const eventSpy = jest.fn().mockImplementation(() => { calls.push('event'); }); - const useSpy = jest.spyOn(editor, 'use').mockImplementation(() => { + const useSpy = jest.fn().mockImplementation(() => { calls.push('use'); }); - editorEl.addEventListener('editor-ready', eventSpy); + jest.spyOn(EditorLite, 'convertMonacoToELInstance').mockImplementation((inst) => { + const decoratedInstance = inst; + decoratedInstance.use = useSpy; + return decoratedInstance; + }); + editorEl.addEventListener(EDITOR_READY_EVENT, eventSpy); instance = instanceConstructor('foo, bar'); await waitForPromises(); expect(useSpy.mock.calls).toHaveLength(2); @@ -487,12 +631,6 @@ describe('Base editor', () => { expect(inst1.alpha()).toEqual(alphaRes); expect(inst2.alpha()).toEqual(alphaRes); }); - - it('extends specific instance if it has been passed', () => { - editor.use(AlphaExt, inst2); - expect(inst1.alpha).toBeUndefined(); - expect(inst2.alpha()).toEqual(alphaRes); - }); }); }); @@ -526,7 +664,7 @@ describe('Base editor', () => { it('sets default syntax highlighting theme', () => { const expectedTheme = themes.find((t) => t.name === DEFAULT_THEME); - editor = new Editor(); + editor = new EditorLite(); expect(themeDefineSpy).toHaveBeenCalledWith(DEFAULT_THEME, expectedTheme.data); expect(themeSetSpy).toHaveBeenCalledWith(DEFAULT_THEME); @@ -538,7 +676,7 @@ describe('Base editor', () => { expect(expectedTheme.name).not.toBe(DEFAULT_THEME); window.gon.user_color_scheme = expectedTheme.name; - editor = new Editor(); + editor = new EditorLite(); expect(themeDefineSpy).toHaveBeenCalledWith(expectedTheme.name, expectedTheme.data); expect(themeSetSpy).toHaveBeenCalledWith(expectedTheme.name); @@ -549,7 +687,7 @@ describe('Base editor', () => { const nonExistentTheme = { name }; window.gon.user_color_scheme = nonExistentTheme.name; - editor = new Editor(); + editor = new EditorLite(); expect(themeDefineSpy).not.toHaveBeenCalled(); expect(themeSetSpy).toHaveBeenCalledWith(DEFAULT_THEME); |