Commit fc3ced13 authored by Kushal Pandya's avatar Kushal Pandya

Merge branch '292943-fixed-extensions-reuse' into 'master'

Fix re-use of extensions between instances

See merge request gitlab-org/gitlab!77319
parents 69a452c8 6c29b2da
...@@ -50,6 +50,10 @@ const utils = { ...@@ -50,6 +50,10 @@ const utils = {
} }
return extensionsStore.get(extensionName); return extensionsStore.get(extensionName);
}, },
hasFullApiRegistered: (targetMethods, newMethods) => {
return newMethods.find((fn) => !targetMethods.includes(fn)) === undefined;
},
}; };
/** Class representing a Source Editor Instance */ /** Class representing a Source Editor Instance */
...@@ -132,7 +136,9 @@ export default class EditorInstance { ...@@ -132,7 +136,9 @@ export default class EditorInstance {
const existingExt = utils.getStoredExtension(extensionsStore, definition.extensionName); const existingExt = utils.getStoredExtension(extensionsStore, definition.extensionName);
if (existingExt) { if (existingExt) {
if (isEqual(extension.setupOptions, existingExt.setupOptions)) { if (isEqual(extension.setupOptions, existingExt.setupOptions)) {
return existingExt; if (utils.hasFullApiRegistered(this.extensionsAPI, Object.keys(existingExt.api))) {
return existingExt;
}
} }
this.unuseExtension(extensionsStore, existingExt); this.unuseExtension(extensionsStore, existingExt);
} }
......
...@@ -277,6 +277,17 @@ describe('Source Editor Instance', () => { ...@@ -277,6 +277,17 @@ describe('Source Editor Instance', () => {
expect(extensionStore.set).toHaveBeenCalledWith('SEClassExtension', extension1); expect(extensionStore.set).toHaveBeenCalledWith('SEClassExtension', extension1);
}); });
it('correctly registers methods from the existing extension on an instance', () => {
const seInstance2 = new SourceEditorInstance({}, extensionStore);
seInstance.use({ definition: SEClassExtension });
const val1 = seInstance.classExtMethod();
seInstance2.use({ definition: SEClassExtension });
expect(seInstance2.classExtMethod).toBeDefined();
expect(seInstance2.classExtMethod()).toBe(val1); // from helpers.js we know classExtMethod()returns a string. Hence `toBe`
});
it.each` it.each`
desc | currentSetupOptions | newSetupOptions | expectedCallTimes desc | currentSetupOptions | newSetupOptions | expectedCallTimes
${'updates'} | ${undefined} | ${defSetupOptions} | ${2} ${'updates'} | ${undefined} | ${defSetupOptions} | ${2}
......
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