mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Switch to running injection token based setups over legacy DI setups in unit tests
Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com> Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
parent
4d79d25b6e
commit
cf87bc14d9
@ -24,6 +24,7 @@ import directoryForUserDataInjectable from "../app-paths/directory-for-user-data
|
||||
import { getDiForUnitTesting } from "../../main/getDiForUnitTesting";
|
||||
import getConfigurationFileModelInjectable from "../get-configuration-file-model/get-configuration-file-model.injectable";
|
||||
import appVersionInjectable from "../get-configuration-file-model/app-version/app-version.injectable";
|
||||
import { runSetupables } from "../setupable-injection-token/run-setupables";
|
||||
|
||||
console = new Console(stdout, stderr);
|
||||
|
||||
@ -82,6 +83,8 @@ describe("cluster-store", () => {
|
||||
beforeEach(async () => {
|
||||
mainDi = getDiForUnitTesting({ doGeneralOverrides: true });
|
||||
|
||||
await runSetupables(mainDi);
|
||||
|
||||
mockFs();
|
||||
|
||||
mainDi.override(clusterStoreInjectable, (di) => ClusterStore.createInstance({ createCluster: di.inject(createClusterInjectionToken) }));
|
||||
@ -90,8 +93,6 @@ describe("cluster-store", () => {
|
||||
mainDi.permitSideEffects(getConfigurationFileModelInjectable);
|
||||
mainDi.permitSideEffects(appVersionInjectable);
|
||||
|
||||
await mainDi.runSetups();
|
||||
|
||||
createCluster = mainDi.inject(createClusterInjectionToken);
|
||||
});
|
||||
|
||||
|
||||
@ -14,6 +14,7 @@ import type { DiContainer } from "@ogre-tools/injectable";
|
||||
import hotbarStoreInjectable from "../hotbar-store.injectable";
|
||||
import { HotbarStore } from "../hotbar-store";
|
||||
import catalogCatalogEntityInjectable from "../catalog-entities/general-catalog-entities/implementations/catalog-catalog-entity.injectable";
|
||||
import { runSetupables } from "../setupable-injection-token/run-setupables";
|
||||
|
||||
jest.mock("../../main/catalog/catalog-entity-registry", () => ({
|
||||
catalogEntityRegistry: {
|
||||
@ -150,7 +151,7 @@ describe("HotbarStore", () => {
|
||||
beforeEach(async () => {
|
||||
mockFs();
|
||||
|
||||
await di.runSetups();
|
||||
await runSetupables(di);
|
||||
|
||||
hotbarStore = di.inject(hotbarStoreInjectable);
|
||||
});
|
||||
@ -381,7 +382,7 @@ describe("HotbarStore", () => {
|
||||
|
||||
mockFs(configurationToBeMigrated);
|
||||
|
||||
await di.runSetups();
|
||||
await runSetupables(di);
|
||||
|
||||
hotbarStore = di.inject(hotbarStoreInjectable);
|
||||
});
|
||||
|
||||
14
src/common/setupable-injection-token/run-setupables.ts
Normal file
14
src/common/setupable-injection-token/run-setupables.ts
Normal file
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import type { DiContainer } from "@ogre-tools/injectable";
|
||||
import { setupableInjectionToken } from "./setupable-injection-token";
|
||||
|
||||
export const runSetupables = async (di: DiContainer) => {
|
||||
await Promise.all(
|
||||
di
|
||||
.injectMany(setupableInjectionToken)
|
||||
.map((setupable) => setupable.doSetup()),
|
||||
);
|
||||
};
|
||||
@ -2,6 +2,7 @@
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { runSetupables } from "../../common/setupable-injection-token/run-setupables";
|
||||
|
||||
const logger = {
|
||||
silly: jest.fn(),
|
||||
@ -79,14 +80,14 @@ describe("create clusters", () => {
|
||||
}),
|
||||
});
|
||||
|
||||
await di.runSetups();
|
||||
|
||||
di.override(authorizationReviewInjectable, () => () => () => Promise.resolve(true));
|
||||
di.override(listNamespacesInjectable, () => () => () => Promise.resolve([ "default" ]));
|
||||
di.override(createContextHandlerInjectable, () => () => {
|
||||
throw new Error("you should never come here");
|
||||
});
|
||||
|
||||
await runSetupables(di);
|
||||
|
||||
createCluster = di.inject(createClusterInjectionToken);
|
||||
|
||||
jest.spyOn(Kubectl.prototype, "ensureKubectl").mockReturnValue(Promise.resolve(true));
|
||||
|
||||
@ -57,6 +57,7 @@ import path from "path";
|
||||
import spawnInjectable from "../child-process/spawn.injectable";
|
||||
import getConfigurationFileModelInjectable from "../../common/get-configuration-file-model/get-configuration-file-model.injectable";
|
||||
import appVersionInjectable from "../../common/get-configuration-file-model/app-version/app-version.injectable";
|
||||
import { runSetupables } from "../../common/setupable-injection-token/run-setupables";
|
||||
|
||||
console = new Console(stdout, stderr);
|
||||
|
||||
@ -106,7 +107,7 @@ describe("kube auth proxy tests", () => {
|
||||
|
||||
mockFs(mockMinikubeConfig);
|
||||
|
||||
await di.runSetups();
|
||||
await runSetupables(di);
|
||||
|
||||
createCluster = di.inject(createClusterInjectionToken);
|
||||
|
||||
|
||||
@ -2,6 +2,8 @@
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { runSetupables } from "../../common/setupable-injection-token/run-setupables";
|
||||
|
||||
const logger = {
|
||||
silly: jest.fn(),
|
||||
debug: jest.fn(),
|
||||
@ -49,7 +51,7 @@ console = new Console(process.stdout, process.stderr); // fix mockFS
|
||||
describe("kubeconfig manager tests", () => {
|
||||
let clusterFake: Cluster;
|
||||
let createKubeconfigManager: (cluster: Cluster) => KubeconfigManager;
|
||||
let di: DiContainer;
|
||||
let di: DiContainer;
|
||||
|
||||
beforeEach(async () => {
|
||||
di = getDiForUnitTesting({ doGeneralOverrides: true });
|
||||
@ -80,12 +82,12 @@ describe("kubeconfig manager tests", () => {
|
||||
}),
|
||||
});
|
||||
|
||||
await di.runSetups();
|
||||
|
||||
di.override(createContextHandlerInjectable, () => () => {
|
||||
throw new Error("you should never come here");
|
||||
});
|
||||
|
||||
await runSetupables(di);
|
||||
|
||||
const createCluster = di.inject(createClusterInjectionToken);
|
||||
|
||||
createKubeconfigManager = di.inject(createKubeconfigManagerInjectable);
|
||||
|
||||
@ -16,10 +16,9 @@ import { getDiForUnitTesting } from "../../getDiForUnitTesting";
|
||||
import { createClusterInjectionToken } from "../../../common/cluster/create-cluster-injection-token";
|
||||
import directoryForKubeConfigsInjectable from "../../../common/app-paths/directory-for-kube-configs/directory-for-kube-configs.injectable";
|
||||
import { ClusterStore } from "../../../common/cluster-store/cluster-store";
|
||||
import getConfigurationFileModelInjectable
|
||||
from "../../../common/get-configuration-file-model/get-configuration-file-model.injectable";
|
||||
import appVersionInjectable
|
||||
from "../../../common/get-configuration-file-model/app-version/app-version.injectable";
|
||||
import getConfigurationFileModelInjectable from "../../../common/get-configuration-file-model/get-configuration-file-model.injectable";
|
||||
import appVersionInjectable from "../../../common/get-configuration-file-model/app-version/app-version.injectable";
|
||||
import { runSetupables } from "../../../common/setupable-injection-token/run-setupables";
|
||||
|
||||
jest.mock("electron", () => ({
|
||||
app: {
|
||||
@ -45,17 +44,17 @@ describe("kubeconfig-sync.source tests", () => {
|
||||
|
||||
mockFs();
|
||||
|
||||
await di.runSetups();
|
||||
di.override(clusterStoreInjectable, () =>
|
||||
ClusterStore.createInstance({ createCluster: () => null }),
|
||||
);
|
||||
|
||||
await runSetupables(di);
|
||||
|
||||
computeDiff = computeDiffFor({
|
||||
directoryForKubeConfigs: di.inject(directoryForKubeConfigsInjectable),
|
||||
createCluster: di.inject(createClusterInjectionToken),
|
||||
});
|
||||
|
||||
di.override(clusterStoreInjectable, () =>
|
||||
ClusterStore.createInstance({ createCluster: () => null }),
|
||||
);
|
||||
|
||||
di.permitSideEffects(getConfigurationFileModelInjectable);
|
||||
di.permitSideEffects(appVersionInjectable);
|
||||
|
||||
|
||||
@ -18,6 +18,7 @@ import lensProtocolRouterMainInjectable from "../lens-protocol-router-main/lens-
|
||||
import extensionsStoreInjectable from "../../../extensions/extensions-store/extensions-store.injectable";
|
||||
import getConfigurationFileModelInjectable from "../../../common/get-configuration-file-model/get-configuration-file-model.injectable";
|
||||
import appVersionInjectable from "../../../common/get-configuration-file-model/app-version/app-version.injectable";
|
||||
import { runSetupables } from "../../../common/setupable-injection-token/run-setupables";
|
||||
|
||||
jest.mock("../../../common/ipc");
|
||||
|
||||
@ -46,7 +47,7 @@ describe("protocol router tests", () => {
|
||||
di.permitSideEffects(getConfigurationFileModelInjectable);
|
||||
di.permitSideEffects(appVersionInjectable);
|
||||
|
||||
await di.runSetups();
|
||||
await runSetupables(di);
|
||||
|
||||
extensionLoader = di.inject(extensionLoaderInjectable);
|
||||
|
||||
|
||||
@ -14,6 +14,7 @@ import asyncFn from "@async-fn/jest";
|
||||
import parseRequestInjectable from "./parse-request.injectable";
|
||||
import { contentTypes } from "./router-content-types";
|
||||
import mockFs from "mock-fs";
|
||||
import { runSetupables } from "../../common/setupable-injection-token/run-setupables";
|
||||
|
||||
describe("router", () => {
|
||||
let router: Router;
|
||||
@ -28,8 +29,6 @@ describe("router", () => {
|
||||
|
||||
di.override(parseRequestInjectable, () => () => Promise.resolve({ payload: "some-payload" }));
|
||||
|
||||
await di.runSetups();
|
||||
|
||||
const injectable = getInjectable({
|
||||
id: "some-route",
|
||||
|
||||
@ -44,6 +43,8 @@ describe("router", () => {
|
||||
|
||||
di.register(injectable);
|
||||
|
||||
await runSetupables(di);
|
||||
|
||||
router = di.inject(routerInjectable);
|
||||
});
|
||||
|
||||
|
||||
@ -5,9 +5,11 @@
|
||||
import { getDiForUnitTesting as getRendererDi } from "../renderer/getDiForUnitTesting";
|
||||
import { getDiForUnitTesting as getMainDi } from "../main/getDiForUnitTesting";
|
||||
import { overrideIpcBridge } from "./override-ipc-bridge";
|
||||
import { setupableInjectionToken } from "../common/setupable-injection-token/setupable-injection-token";
|
||||
import { runSetupables } from "../common/setupable-injection-token/run-setupables";
|
||||
|
||||
export const getDisForUnitTesting = ({ doGeneralOverrides } = { doGeneralOverrides: false }) => {
|
||||
export const getDisForUnitTesting = (
|
||||
{ doGeneralOverrides } = { doGeneralOverrides: false },
|
||||
) => {
|
||||
const rendererDi = getRendererDi({ doGeneralOverrides });
|
||||
const mainDi = getMainDi({ doGeneralOverrides });
|
||||
|
||||
@ -16,17 +18,8 @@ export const getDisForUnitTesting = ({ doGeneralOverrides } = { doGeneralOverrid
|
||||
return {
|
||||
rendererDi,
|
||||
mainDi,
|
||||
runSetups: () => {
|
||||
const setupPromises = [
|
||||
...rendererDi.injectMany(setupableInjectionToken),
|
||||
...mainDi.injectMany(setupableInjectionToken),
|
||||
].map(setupable => setupable.doSetup());
|
||||
|
||||
return Promise.all([
|
||||
rendererDi.runSetups(),
|
||||
mainDi.runSetups(),
|
||||
...setupPromises,
|
||||
]);
|
||||
},
|
||||
runSetups: () =>
|
||||
Promise.all([runSetupables(rendererDi), runSetupables(mainDi)]),
|
||||
};
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user