mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Fix a faulty "legacy-global-helper", and add unit tests to make sure it works
Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>
This commit is contained in:
parent
ca086350ad
commit
00712214ef
@ -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");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -13,4 +13,7 @@ export const asLegacyGlobalObjectForExtensionApiWithModifications = <
|
|||||||
injectable: Injectable<InjectableInstance, InjectionTokenInstance, void>,
|
injectable: Injectable<InjectableInstance, InjectionTokenInstance, void>,
|
||||||
modificationObject: ModificationObject,
|
modificationObject: ModificationObject,
|
||||||
) =>
|
) =>
|
||||||
Object.assign(modificationObject, asLegacyGlobalForExtensionApi(injectable));
|
Object.assign(
|
||||||
|
asLegacyGlobalForExtensionApi(injectable),
|
||||||
|
modificationObject,
|
||||||
|
);
|
||||||
|
|||||||
@ -13,7 +13,7 @@ export const asLegacyGlobalForExtensionApi = ((
|
|||||||
{},
|
{},
|
||||||
|
|
||||||
{
|
{
|
||||||
apply(target, thisArg, argArray) {
|
apply(target: any, thisArg, argArray) {
|
||||||
const fn = getLegacyGlobalDiForExtensionApi().inject(
|
const fn = getLegacyGlobalDiForExtensionApi().inject(
|
||||||
injectable,
|
injectable,
|
||||||
instantiationParameter,
|
instantiationParameter,
|
||||||
@ -32,7 +32,7 @@ export const asLegacyGlobalForExtensionApi = ((
|
|||||||
instantiationParameter,
|
instantiationParameter,
|
||||||
);
|
);
|
||||||
|
|
||||||
const propertyValue = instance[propertyName];
|
const propertyValue = instance[propertyName] ?? target[propertyName];
|
||||||
|
|
||||||
if (typeof propertyValue === "function") {
|
if (typeof propertyValue === "function") {
|
||||||
return function (...args: any[]) {
|
return function (...args: any[]) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user