Commit 6e41c79c authored by Denys Mishunov's avatar Denys Mishunov

Source Editor Extension module

While refactoring the Source Editor architecture it has been
decided to split the monolythic Source Editor core into different
modules. EditroExtension is one of them

Changelog: changed
parent 366d16cd
...@@ -20,6 +20,10 @@ export const EDITOR_TYPE_DIFF = 'vs.editor.IDiffEditor'; ...@@ -20,6 +20,10 @@ export const EDITOR_TYPE_DIFF = 'vs.editor.IDiffEditor';
export const EDITOR_CODE_INSTANCE_FN = 'createInstance'; export const EDITOR_CODE_INSTANCE_FN = 'createInstance';
export const EDITOR_DIFF_INSTANCE_FN = 'createDiffInstance'; export const EDITOR_DIFF_INSTANCE_FN = 'createDiffInstance';
export const EDITOR_EXTENSION_DEFINITION_ERROR = __(
'Extension definition should be either a class or a function',
);
// //
// EXTENSIONS' CONSTANTS // EXTENSIONS' CONSTANTS
// //
......
import { EDITOR_EXTENSION_DEFINITION_ERROR } from './constants';
export default class EditorExtension {
constructor({ definition, setupOptions } = {}) {
if (typeof definition !== 'function') {
throw new Error(EDITOR_EXTENSION_DEFINITION_ERROR);
}
this.name = definition.name; // both class- and fn-based extensions have a name
this.setupOptions = setupOptions;
// eslint-disable-next-line new-cap
this.obj = new definition();
}
getApi() {
return this.obj.provides();
}
}
...@@ -14083,6 +14083,9 @@ msgstr "" ...@@ -14083,6 +14083,9 @@ msgstr ""
msgid "Exported requirements" msgid "Exported requirements"
msgstr "" msgstr ""
msgid "Extension definition should be either a class or a function"
msgstr ""
msgid "External Classification Policy Authorization" msgid "External Classification Policy Authorization"
msgstr "" msgstr ""
......
import EditorExtension from '~/editor/source_editor_extension';
import { EDITOR_EXTENSION_DEFINITION_ERROR } from '~/editor/constants';
class MyClassExtension {
// eslint-disable-next-line class-methods-use-this
provides() {
return {
shared: () => 'extension',
classExtMethod: () => 'class own method',
};
}
}
function MyFnExtension() {
return {
fnExtMethod: () => 'fn own method',
provides: () => {
return {
shared: () => 'extension',
};
},
};
}
const MyConstExt = () => {
return {
provides: () => {
return {
shared: () => 'extension',
constExtMethod: () => 'const own method',
};
},
};
};
describe('Editor Extension', () => {
const dummyObj = { foo: 'bar' };
it.each`
definition | setupOptions
${undefined} | ${undefined}
${undefined} | ${{}}
${undefined} | ${dummyObj}
${{}} | ${dummyObj}
${dummyObj} | ${dummyObj}
`(
'throws when definition = $definition and setupOptions = $setupOptions',
({ definition, setupOptions }) => {
const constructExtension = () => new EditorExtension({ definition, setupOptions });
expect(constructExtension).toThrowError(EDITOR_EXTENSION_DEFINITION_ERROR);
},
);
it.each`
definition | setupOptions | expectedName
${MyClassExtension} | ${undefined} | ${'MyClassExtension'}
${MyClassExtension} | ${{}} | ${'MyClassExtension'}
${MyClassExtension} | ${dummyObj} | ${'MyClassExtension'}
${MyFnExtension} | ${undefined} | ${'MyFnExtension'}
${MyFnExtension} | ${{}} | ${'MyFnExtension'}
${MyFnExtension} | ${dummyObj} | ${'MyFnExtension'}
${MyConstExt} | ${undefined} | ${'MyConstExt'}
${MyConstExt} | ${{}} | ${'MyConstExt'}
${MyConstExt} | ${dummyObj} | ${'MyConstExt'}
`(
'correctly creates extension for definition = $definition and setupOptions = $setupOptions',
({ definition, setupOptions, expectedName }) => {
const extension = new EditorExtension({ definition, setupOptions });
// eslint-disable-next-line new-cap
const constructedDefinition = new definition();
expect(extension).toEqual(
expect.objectContaining({
name: expectedName,
setupOptions,
}),
);
expect(extension.obj.constructor.prototype).toBe(constructedDefinition.constructor.prototype);
},
);
describe('getApi', () => {
it.each`
definition | expectedKeys
${MyClassExtension} | ${['shared', 'classExtMethod']}
${MyFnExtension} | ${['shared']}
${MyConstExt} | ${['shared', 'constExtMethod']}
`('correctly returns API for $definition', ({ definition, expectedKeys }) => {
const extension = new EditorExtension({ definition });
const expectedApi = {};
expectedKeys.forEach((key) => {
expectedApi[key] = expect.any(Function);
});
expect(extension.getApi()).toEqual(expect.objectContaining(expectedApi));
});
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment