1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

fix sidebar-and-tab-navigation-for-core.test.tsx

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-04-21 08:43:12 -04:00
parent 27ec0573a3
commit e4f08a9eb7
6 changed files with 62 additions and 48 deletions

View File

@ -108,7 +108,6 @@ describe("cluster - sidebar and tab navigation for core", () => {
const sidebarStorage = rendererDi.inject(sidebarStorageInjectable);
await sidebarStorage.whenReady;
console.log({ ...sidebarStorage.get() });
});
rendered = await applicationBuilder.render();

View File

@ -34,9 +34,9 @@ import { getAbsolutePathFake } from "../common/test-utils/get-absolute-path-fake
import joinPathsInjectable from "../common/path/join-paths.injectable";
import { joinPathsFake } from "../common/test-utils/join-paths-fake";
import hotbarStoreInjectable from "../common/hotbars/store.injectable";
import type { GetDiForUnitTestingOptions } from "../test-utils/get-dis-for-unit-testing";
export interface GetDiOptions {
doGeneralOverrides?: boolean;
export interface GetMainDiForUnitTestingOptions extends GetDiForUnitTestingOptions {
overrideHotbarStore?: boolean;
overrideUserStore?: boolean;
overrideExtensionsStore?: boolean;
@ -44,7 +44,7 @@ export interface GetDiOptions {
overrideFileSystemProvisionerStore?: boolean;
}
export function getDiForUnitTesting(opts: GetDiOptions = {}) {
export function getDiForUnitTesting(opts: GetMainDiForUnitTestingOptions = {}) {
const {
doGeneralOverrides = false,
} = opts;

View File

@ -72,6 +72,7 @@ interface Environment {
export const getApplicationBuilder = () => {
const { rendererDi, mainDi, runSetups } = getDisForUnitTesting({
doGeneralOverrides: true,
overrideCreateStorage: false,
});
const dis = { rendererDi, mainDi };

View File

@ -39,10 +39,19 @@ import createStorageInjectable from "./utils/create-storage/create-storage.injec
import { observable, toJS } from "mobx";
import type { Draft } from "immer";
import { produce, isDraft } from "immer";
import type { GetDiForUnitTestingOptions } from "../test-utils/get-dis-for-unit-testing";
export const getDiForUnitTesting = (
{ doGeneralOverrides } = { doGeneralOverrides: false },
) => {
export interface GetRendererDiForUnitTestingOptions extends GetDiForUnitTestingOptions {
overrideCreateStorage?: boolean;
}
export const getDiForUnitTesting = (opts: GetRendererDiForUnitTestingOptions = {}) => {
const {
doGeneralOverrides = false,
} = opts;
const {
overrideCreateStorage = doGeneralOverrides,
} = opts;
const di = createContainer();
setLegacyGlobalDiForExtensionApi(di, Environments.renderer);
@ -64,40 +73,6 @@ export const getDiForUnitTesting = (
di.override(isLinuxInjectable, () => false);
di.override(terminalSpawningPoolInjectable, () => document.createElement("div"));
di.override(createStorageInjectable, () => function <MockT>(key: string, defaultValue: MockT) {
const srcValue = observable.box(defaultValue);
return {
get: () => srcValue.get(),
isDefaultValue: val => isEqual(val, defaultValue),
merge: (value: Partial<MockT> | ((draft: Draft<MockT>) => void | Partial<MockT>)) => {
const nextValue = produce(toJS(srcValue.get()), (draft) => {
if (typeof value == "function") {
const newValue = value(draft);
// merge returned plain objects from `value-as-callback` usage
// otherwise `draft` can be just modified inside a callback without returning any value (void)
if (newValue && !isDraft(newValue)) {
Object.assign(draft, newValue);
}
} else if (isPlainObject(value)) {
Object.assign(draft, value);
}
return draft;
});
srcValue.set(nextValue);
},
reset: () => srcValue.set(defaultValue),
set: (val: MockT) => srcValue.set(val),
get value() {
return srcValue.get();
},
whenReady: Promise.resolve(),
};
});
di.override(hostedClusterIdInjectable, () => undefined);
di.override(getAbsolutePathInjectable, () => getAbsolutePathFake);
@ -138,6 +113,43 @@ export const getDiForUnitTesting = (
}));
}
if (overrideCreateStorage) {
di.override(createStorageInjectable, () => function <MockT>(key: string, defaultValue: MockT) {
const srcValue = observable.box(defaultValue);
return {
get: () => srcValue.get(),
isDefaultValue: val => isEqual(val, defaultValue),
merge: (value: Partial<MockT> | ((draft: Draft<MockT>) => void | Partial<MockT>)) => {
const nextValue = produce(toJS(srcValue.get()), (draft) => {
if (typeof value == "function") {
const newValue = value(draft);
// merge returned plain objects from `value-as-callback` usage
// otherwise `draft` can be just modified inside a callback without returning any value (void)
if (newValue && !isDraft(newValue)) {
Object.assign(draft, newValue);
}
} else if (isPlainObject(value)) {
Object.assign(draft, value);
}
return draft;
});
srcValue.set(nextValue);
},
reset: () => srcValue.set(defaultValue),
set: (val: MockT) => srcValue.set(val),
get value() {
return srcValue.get();
},
whenReady: Promise.resolve(),
};
});
}
return di;
};

View File

@ -39,16 +39,12 @@ export const createStorage = ({
}: Dependencies): CreateStorage => (key, defaultValue) => {
const { logPrefix } = StorageHelper;
console.log("createStorage", { key, defaultValue });
if (!storage.initialized) {
storage.initialized = true;
(async () => {
const filePath = getAbsolutePath(directoryForLensLocalStorage, `${hostedClusterId || "app"}.json`);
console.log("reading from", filePath);
try {
storage.data = (await readJsonFile(filePath)) as JsonObject;
} catch {

View File

@ -2,13 +2,19 @@
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { GetRendererDiForUnitTestingOptions } from "../renderer/getDiForUnitTesting";
import { getDiForUnitTesting as getRendererDi } from "../renderer/getDiForUnitTesting";
import type { GetMainDiForUnitTestingOptions } from "../main/getDiForUnitTesting";
import { getDiForUnitTesting as getMainDi } from "../main/getDiForUnitTesting";
import { overrideIpcBridge } from "./override-ipc-bridge";
export const getDisForUnitTesting = ({ doGeneralOverrides } = { doGeneralOverrides: false }) => {
const rendererDi = getRendererDi({ doGeneralOverrides });
const mainDi = getMainDi({ doGeneralOverrides });
export interface GetDiForUnitTestingOptions {
doGeneralOverrides?: boolean;
}
export const getDisForUnitTesting = (opts?: GetMainDiForUnitTestingOptions & GetRendererDiForUnitTestingOptions) => {
const rendererDi = getRendererDi(opts);
const mainDi = getMainDi(opts);
overrideIpcBridge({ rendererDi, mainDi });