From e4f08a9eb783d3e4ee0a47325726340d22853452 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Thu, 21 Apr 2022 08:43:12 -0400 Subject: [PATCH] fix sidebar-and-tab-navigation-for-core.test.tsx Signed-off-by: Sebastian Malton --- ...debar-and-tab-navigation-for-core.test.tsx | 1 - src/main/getDiForUnitTesting.ts | 6 +- .../test-utils/get-application-builder.tsx | 1 + src/renderer/getDiForUnitTesting.tsx | 86 +++++++++++-------- .../utils/create-storage/create-storage.ts | 4 - src/test-utils/get-dis-for-unit-testing.ts | 12 ++- 6 files changed, 62 insertions(+), 48 deletions(-) diff --git a/src/behaviours/cluster/sidebar-and-tab-navigation-for-core.test.tsx b/src/behaviours/cluster/sidebar-and-tab-navigation-for-core.test.tsx index 2889ba17fc..cd96149816 100644 --- a/src/behaviours/cluster/sidebar-and-tab-navigation-for-core.test.tsx +++ b/src/behaviours/cluster/sidebar-and-tab-navigation-for-core.test.tsx @@ -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(); diff --git a/src/main/getDiForUnitTesting.ts b/src/main/getDiForUnitTesting.ts index d949d8f970..a22480a673 100644 --- a/src/main/getDiForUnitTesting.ts +++ b/src/main/getDiForUnitTesting.ts @@ -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; diff --git a/src/renderer/components/test-utils/get-application-builder.tsx b/src/renderer/components/test-utils/get-application-builder.tsx index 6151e48b55..43ce9831c5 100644 --- a/src/renderer/components/test-utils/get-application-builder.tsx +++ b/src/renderer/components/test-utils/get-application-builder.tsx @@ -72,6 +72,7 @@ interface Environment { export const getApplicationBuilder = () => { const { rendererDi, mainDi, runSetups } = getDisForUnitTesting({ doGeneralOverrides: true, + overrideCreateStorage: false, }); const dis = { rendererDi, mainDi }; diff --git a/src/renderer/getDiForUnitTesting.tsx b/src/renderer/getDiForUnitTesting.tsx index c6ad222c63..9d38b20a13 100644 --- a/src/renderer/getDiForUnitTesting.tsx +++ b/src/renderer/getDiForUnitTesting.tsx @@ -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 (key: string, defaultValue: MockT) { - const srcValue = observable.box(defaultValue); - - return { - get: () => srcValue.get(), - isDefaultValue: val => isEqual(val, defaultValue), - merge: (value: Partial | ((draft: Draft) => void | Partial)) => { - 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 (key: string, defaultValue: MockT) { + const srcValue = observable.box(defaultValue); + + return { + get: () => srcValue.get(), + isDefaultValue: val => isEqual(val, defaultValue), + merge: (value: Partial | ((draft: Draft) => void | Partial)) => { + 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; }; diff --git a/src/renderer/utils/create-storage/create-storage.ts b/src/renderer/utils/create-storage/create-storage.ts index f9c7150769..0bc3de06e4 100755 --- a/src/renderer/utils/create-storage/create-storage.ts +++ b/src/renderer/utils/create-storage/create-storage.ts @@ -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 { diff --git a/src/test-utils/get-dis-for-unit-testing.ts b/src/test-utils/get-dis-for-unit-testing.ts index 976117bdf9..9a41cf97cd 100644 --- a/src/test-utils/get-dis-for-unit-testing.ts +++ b/src/test-utils/get-dis-for-unit-testing.ts @@ -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 });