diff --git a/src/extensions/as-legacy-globals-for-extension-api/as-legacy-global-object-for-extension-api-with-modifications.test.ts b/src/extensions/as-legacy-globals-for-extension-api/as-legacy-global-object-for-extension-api-with-modifications.test.ts new file mode 100644 index 0000000000..3e3cbb6ff1 --- /dev/null +++ b/src/extensions/as-legacy-globals-for-extension-api/as-legacy-global-object-for-extension-api-with-modifications.test.ts @@ -0,0 +1,80 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { + createContainer, + DiContainer, + getInjectable, + Injectable, +} from "@ogre-tools/injectable"; +import { setLegacyGlobalDiForExtensionApi } from "./legacy-global-di-for-extension-api"; +import { asLegacyGlobalObjectForExtensionApiWithModifications } from "./as-legacy-global-object-for-extension-api-with-modifications"; + +describe("asLegacyGlobalObjectForExtensionApiWithModifications", () => { + describe("given legacy global object", () => { + let di: DiContainer; + let someInjectable: Injectable<{ someProperty: string }, unknown, void>; + let actual: { someProperty: string } & { + someModificationProperty: string; + }; + + beforeEach(() => { + di = createContainer(); + + jest.spyOn(di, "inject"); + + setLegacyGlobalDiForExtensionApi(di); + + someInjectable = getInjectable({ + id: "some-injectable", + instantiate: () => ({ + someProperty: "some-property-value", + }), + }); + + di.register(someInjectable); + + actual = asLegacyGlobalObjectForExtensionApiWithModifications( + someInjectable, + { someModificationProperty: "some-modification-value" }, + ); + }); + + it("when not accessed, does not inject yet", () => { + expect(di.inject).not.toHaveBeenCalled(); + }); + + describe("when a property of global is accessed, ", () => { + let actualPropertyValue: string; + + beforeEach(() => { + actualPropertyValue = actual.someProperty; + }); + + it("injects the injectable for global", () => { + expect(di.inject).toHaveBeenCalledWith(someInjectable, undefined); + }); + + it("global has property of injectable", () => { + expect(actualPropertyValue).toBe("some-property-value"); + }); + }); + + describe("when a property of modification is accessed, ", () => { + let actualModificationValue: string; + + beforeEach(() => { + actualModificationValue = actual.someModificationProperty; + }); + + it("injects the injectable for global", () => { + expect(di.inject).toHaveBeenCalledWith(someInjectable, undefined); + }); + + it("global has property of modification", () => { + expect(actualModificationValue).toBe("some-modification-value"); + }); + }); + }); +}); diff --git a/src/extensions/as-legacy-globals-for-extension-api/as-legacy-global-object-for-extension-api-with-modifications.ts b/src/extensions/as-legacy-globals-for-extension-api/as-legacy-global-object-for-extension-api-with-modifications.ts index e8b36021c4..cc9b5917d6 100644 --- a/src/extensions/as-legacy-globals-for-extension-api/as-legacy-global-object-for-extension-api-with-modifications.ts +++ b/src/extensions/as-legacy-globals-for-extension-api/as-legacy-global-object-for-extension-api-with-modifications.ts @@ -13,4 +13,7 @@ export const asLegacyGlobalObjectForExtensionApiWithModifications = < injectable: Injectable, modificationObject: ModificationObject, ) => - Object.assign(modificationObject, asLegacyGlobalForExtensionApi(injectable)); + Object.assign( + asLegacyGlobalForExtensionApi(injectable), + modificationObject, + ); diff --git a/src/extensions/as-legacy-globals-for-extension-api/as-legacy-global-object-for-extension-api.ts b/src/extensions/as-legacy-globals-for-extension-api/as-legacy-global-object-for-extension-api.ts index 3d65e0ea0f..270bb6091e 100644 --- a/src/extensions/as-legacy-globals-for-extension-api/as-legacy-global-object-for-extension-api.ts +++ b/src/extensions/as-legacy-globals-for-extension-api/as-legacy-global-object-for-extension-api.ts @@ -13,7 +13,7 @@ export const asLegacyGlobalForExtensionApi = (( {}, { - apply(target, thisArg, argArray) { + apply(target: any, thisArg, argArray) { const fn = getLegacyGlobalDiForExtensionApi().inject( injectable, instantiationParameter, @@ -32,7 +32,7 @@ export const asLegacyGlobalForExtensionApi = (( instantiationParameter, ); - const propertyValue = instance[propertyName]; + const propertyValue = instance[propertyName] ?? target[propertyName]; if (typeof propertyValue === "function") { return function (...args: any[]) {