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

Remove need to override lensLocalStoragePath in tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2023-01-05 11:21:53 -05:00
parent 36caecb1c6
commit 5b505e339d
11 changed files with 75 additions and 80 deletions

View File

@ -7,7 +7,6 @@ import { getInjectable } from "@ogre-tools/injectable";
import React from "react"; import React from "react";
import type { RenderResult } from "@testing-library/react"; import type { RenderResult } from "@testing-library/react";
import { fireEvent } from "@testing-library/react"; import { fireEvent } from "@testing-library/react";
import directoryForLensLocalStorageInjectable from "../../common/directory-for-lens-local-storage/directory-for-lens-local-storage.injectable";
import { routeSpecificComponentInjectionToken } from "../../renderer/routes/route-specific-component-injection-token"; import { routeSpecificComponentInjectionToken } from "../../renderer/routes/route-specific-component-injection-token";
import type { SidebarItemRegistration } from "../../renderer/components/layout/sidebar-items.injectable"; import type { SidebarItemRegistration } from "../../renderer/components/layout/sidebar-items.injectable";
import { sidebarItemsInjectionToken } from "../../renderer/components/layout/sidebar-items.injectable"; import { sidebarItemsInjectionToken } from "../../renderer/components/layout/sidebar-items.injectable";
@ -38,11 +37,6 @@ describe("cluster - sidebar and tab navigation for core", () => {
builder.beforeWindowStart((windowDi) => { builder.beforeWindowStart((windowDi) => {
windowDi.override(storageSaveDelayInjectable, () => 250); windowDi.override(storageSaveDelayInjectable, () => 250);
windowDi.override(
directoryForLensLocalStorageInjectable,
() => "/some-directory-for-lens-local-storage",
);
}); });
}); });
@ -96,7 +90,7 @@ describe("cluster - sidebar and tab navigation for core", () => {
const writeJsonFile = windowDi.inject(writeJsonFileInjectable); const writeJsonFile = windowDi.inject(writeJsonFileInjectable);
await writeJsonFile( await writeJsonFile(
"/some-directory-for-lens-local-storage/some-cluster-id.json", "/some-directory-for-app-data/some-product-name/lens-local-storage/some-cluster-id.json",
{ {
sidebar: { sidebar: {
expanded: { "some-parent-id": true }, expanded: { "some-parent-id": true },
@ -132,7 +126,7 @@ describe("cluster - sidebar and tab navigation for core", () => {
const writeJsonFileFake = windowDi.inject(writeJsonFileInjectable); const writeJsonFileFake = windowDi.inject(writeJsonFileInjectable);
await writeJsonFileFake( await writeJsonFileFake(
"/some-directory-for-lens-local-storage/some-hosted-cluster-id.json", "/some-directory-for-app-data/some-product-name/lens-local-storage/some-hosted-cluster-id.json",
{ {
sidebar: { sidebar: {
expanded: { "some-unknown-parent-id": true }, expanded: { "some-unknown-parent-id": true },
@ -162,7 +156,7 @@ describe("cluster - sidebar and tab navigation for core", () => {
const writeJsonFileFake = windowDi.inject(writeJsonFileInjectable); const writeJsonFileFake = windowDi.inject(writeJsonFileInjectable);
await writeJsonFileFake( await writeJsonFileFake(
"/some-directory-for-lens-local-storage/some-hosted-cluster-id.json", "/some-directory-for-app-data/some-product-name/lens-local-storage/some-hosted-cluster-id.json",
{ {
someThingButSidebar: {}, someThingButSidebar: {},
}, },
@ -268,7 +262,7 @@ describe("cluster - sidebar and tab navigation for core", () => {
const pathExistsFake = windowDi.inject(pathExistsInjectable); const pathExistsFake = windowDi.inject(pathExistsInjectable);
const actual = await pathExistsFake( const actual = await pathExistsFake(
"/some-directory-for-lens-local-storage/some-hosted-cluster-id.json", "/some-directory-for-app-data/some-product-name/lens-local-storage/some-hosted-cluster-id.json",
); );
expect(actual).toBe(false); expect(actual).toBe(false);
@ -282,7 +276,7 @@ describe("cluster - sidebar and tab navigation for core", () => {
await flushPromises(); await flushPromises();
const actual = await readJsonFileFake( const actual = await readJsonFileFake(
"/some-directory-for-lens-local-storage/some-cluster-id.json", "/some-directory-for-app-data/some-product-name/lens-local-storage/some-cluster-id.json",
); );
expect(actual).toEqual({ expect(actual).toEqual({

View File

@ -3,15 +3,29 @@
* Licensed under MIT License. See LICENSE in root directory for more information. * Licensed under MIT License. See LICENSE in root directory for more information.
*/ */
import { getInjectable } from "@ogre-tools/injectable"; import { getInjectable } from "@ogre-tools/injectable";
import type { PathName } from "../../../common/app-paths/app-path-names";
import electronAppInjectable from "../../electron-app/electron-app.injectable"; import electronAppInjectable from "../../electron-app/electron-app.injectable";
import { getElectronAppPath } from "./get-electron-app-path";
export type GetElectronAppPath = (name: PathName | "currentApp") => string;
const getElectronAppPathInjectable = getInjectable({ const getElectronAppPathInjectable = getInjectable({
id: "get-electron-app-path", id: "get-electron-app-path",
instantiate: (di) => getElectronAppPath({ instantiate: (di): GetElectronAppPath => {
app: di.inject(electronAppInjectable), const electronApp = di.inject(electronAppInjectable);
}),
return (name) => {
try {
if (name === "currentApp") {
return electronApp.getAppPath();
}
return electronApp.getPath(name);
} catch (e) {
return "";
}
};
},
}); });
export default getElectronAppPathInjectable; export default getElectronAppPathInjectable;

View File

@ -1,28 +0,0 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { App } from "electron";
import type { PathName } from "../../../common/app-paths/app-path-names";
interface Dependencies {
app: App;
}
export type GetElectronAppPath = (name: PathName | "currentApp") => string;
export const getElectronAppPath = ({
app,
}: Dependencies): GetElectronAppPath => (
(name) => {
try {
if (name === "currentApp") {
return app.getAppPath();
}
return app.getPath(name);
} catch (e) {
return "";
}
}
);

View File

@ -6,11 +6,16 @@ import { getInjectable } from "@ogre-tools/injectable";
import type { PathName } from "../../../common/app-paths/app-path-names"; import type { PathName } from "../../../common/app-paths/app-path-names";
import electronAppInjectable from "../../electron-app/electron-app.injectable"; import electronAppInjectable from "../../electron-app/electron-app.injectable";
export type SetElectronAppPath = (name: PathName, path: string) => void;
const setElectronAppPathInjectable = getInjectable({ const setElectronAppPathInjectable = getInjectable({
id: "set-electron-app-path", id: "set-electron-app-path",
instantiate: (di) => (name: PathName, path: string) : void => instantiate: (di): SetElectronAppPath => {
di.inject(electronAppInjectable).setPath(name, path), const electronApp = di.inject(electronAppInjectable);
return (name, path) => electronApp.setPath(name, path);
},
}); });
export default setElectronAppPathInjectable; export default setElectronAppPathInjectable;

View File

@ -21,7 +21,7 @@ const setupAppPathsInjectable = getInjectable({
instantiate: (di) => { instantiate: (di) => {
const setElectronAppPath = di.inject(setElectronAppPathInjectable); const setElectronAppPath = di.inject(setElectronAppPathInjectable);
const appName = di.inject(appNameInjectable); const appName = di.inject(appNameInjectable);
const getAppPath = di.inject(getElectronAppPathInjectable); const getElectronAppPath = di.inject(getElectronAppPathInjectable);
const appPathsState = di.inject(appPathsStateInjectable); const appPathsState = di.inject(appPathsStateInjectable);
const directoryForIntegrationTesting = di.inject(directoryForIntegrationTestingInjectable); const directoryForIntegrationTesting = di.inject(directoryForIntegrationTestingInjectable);
const joinPaths = di.inject(joinPathsInjectable); const joinPaths = di.inject(joinPathsInjectable);
@ -33,13 +33,13 @@ const setupAppPathsInjectable = getInjectable({
setElectronAppPath("appData", directoryForIntegrationTesting); setElectronAppPath("appData", directoryForIntegrationTesting);
} }
const appDataPath = getAppPath("appData"); const appDataPath = getElectronAppPath("appData");
setElectronAppPath("userData", joinPaths(appDataPath, appName)); setElectronAppPath("userData", joinPaths(appDataPath, appName));
const appPaths = pipeline( const appPaths = pipeline(
pathNames, pathNames,
map(name => [name, getAppPath(name)]), map(name => [name, getElectronAppPath(name)]),
fromPairs, fromPairs,
) as AppPaths; ) as AppPaths;

View File

@ -3,21 +3,32 @@
* Licensed under MIT License. See LICENSE in root directory for more information. * Licensed under MIT License. See LICENSE in root directory for more information.
*/ */
import { kebabCase } from "lodash";
import { getGlobalOverride } from "../../common/test-utils/get-global-override"; import { getGlobalOverride } from "../../common/test-utils/get-global-override";
import electronAppInjectable from "./electron-app.injectable"; import electronAppInjectable from "./electron-app.injectable";
export default getGlobalOverride(electronAppInjectable, () => { export default getGlobalOverride(electronAppInjectable, () => {
const commandLineArgs: string[] = [];
const chromiumArgs = new Map<string, string | undefined>();
const appPaths = new Map<string, string>();
const app = ({ const app = ({
getVersion: () => "6.0.0", getVersion: () => "6.0.0",
setLoginItemSettings: () => { }, setLoginItemSettings: () => { },
on: () => app, on: () => app,
whenReady: async () => {},
getPath: (name) => appPaths.get(name) ?? `/some-directory-for-${kebabCase(name)}`,
setPath: (name, value) => appPaths.set(name, value),
getAppPath: () => "/some-path-to-the-applcation-binary",
focus: () => {},
commandLine: { commandLine: {
appendArgument: () => {}, appendArgument: (value) => commandLineArgs.push(value),
appendSwitch: () => {}, appendSwitch: (key, value) => chromiumArgs.set(key, value),
getSwitchValue: () => "", getSwitchValue: (key) => chromiumArgs.get(key),
hasSwitch: () => false, hasSwitch: (key) => chromiumArgs.has(key),
removeSwitch: () => {}, removeSwitch: (key) => chromiumArgs.delete(key),
}, },
exit: () => {},
} as Partial<Electron.App> as Electron.App); } as Partial<Electron.App> as Electron.App);
return app; return app;

View File

@ -15,8 +15,6 @@ const focusApplicationInjectable = getInjectable({
electronApp.focus({ steal: true }); electronApp.focus({ steal: true });
}; };
}, },
causesSideEffects: true,
}); });
export default focusApplicationInjectable; export default focusApplicationInjectable;

View File

@ -0,0 +1,16 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import EventEmitter from "events";
import { getGlobalOverride } from "../../../common/test-utils/get-global-override";
import nativeThemeInjectable from "./native-theme.injectable";
export default getGlobalOverride(nativeThemeInjectable, () => Object.assign(new EventEmitter(), {
shouldUseDarkColors: true,
inForcedColorsMode: true,
shouldUseHighContrastColors: false,
shouldUseInvertedColorScheme: false,
themeSource: "dark" as const,
}));

View File

@ -3,7 +3,7 @@
* Licensed under MIT License. See LICENSE in root directory for more information. * Licensed under MIT License. See LICENSE in root directory for more information.
*/ */
import { kebabCase, chunk } from "lodash/fp"; import { chunk } from "lodash/fp";
import type { DiContainer, Injectable } from "@ogre-tools/injectable"; import type { DiContainer, Injectable } from "@ogre-tools/injectable";
import { createContainer, isInjectable, getInjectable } from "@ogre-tools/injectable"; import { createContainer, isInjectable, getInjectable } from "@ogre-tools/injectable";
import { Environments, setLegacyGlobalDiForExtensionApi } from "../extensions/as-legacy-globals-for-extension-api/legacy-global-di-for-extension-api"; import { Environments, setLegacyGlobalDiForExtensionApi } from "../extensions/as-legacy-globals-for-extension-api/legacy-global-di-for-extension-api";
@ -17,13 +17,9 @@ import setupLensProxyInjectable from "./start-main-application/runnables/setup-l
import setupSyncingOfWeblinksInjectable from "./start-main-application/runnables/setup-syncing-of-weblinks.injectable"; import setupSyncingOfWeblinksInjectable from "./start-main-application/runnables/setup-syncing-of-weblinks.injectable";
import stopServicesAndExitAppInjectable from "./stop-services-and-exit-app.injectable"; import stopServicesAndExitAppInjectable from "./stop-services-and-exit-app.injectable";
import setupDeepLinkingInjectable from "./electron-app/runnables/setup-deep-linking.injectable"; import setupDeepLinkingInjectable from "./electron-app/runnables/setup-deep-linking.injectable";
import exitAppInjectable from "./electron-app/features/exit-app.injectable";
import getCommandLineSwitchInjectable from "./electron-app/features/get-command-line-switch.injectable";
import requestSingleInstanceLockInjectable from "./electron-app/features/request-single-instance-lock.injectable"; import requestSingleInstanceLockInjectable from "./electron-app/features/request-single-instance-lock.injectable";
import disableHardwareAccelerationInjectable from "./electron-app/features/disable-hardware-acceleration.injectable"; import disableHardwareAccelerationInjectable from "./electron-app/features/disable-hardware-acceleration.injectable";
import shouldStartHiddenInjectable from "./electron-app/features/should-start-hidden.injectable"; import shouldStartHiddenInjectable from "./electron-app/features/should-start-hidden.injectable";
import getElectronAppPathInjectable from "./app-paths/get-electron-app-path/get-electron-app-path.injectable";
import setElectronAppPathInjectable from "./app-paths/set-electron-app-path/set-electron-app-path.injectable";
import setupMainWindowVisibilityAfterActivationInjectable from "./electron-app/runnables/setup-main-window-visibility-after-activation.injectable"; import setupMainWindowVisibilityAfterActivationInjectable from "./electron-app/runnables/setup-main-window-visibility-after-activation.injectable";
import setupDeviceShutdownInjectable from "./electron-app/runnables/setup-device-shutdown.injectable"; import setupDeviceShutdownInjectable from "./electron-app/runnables/setup-device-shutdown.injectable";
import setupApplicationNameInjectable from "./electron-app/runnables/setup-application-name.injectable"; import setupApplicationNameInjectable from "./electron-app/runnables/setup-application-name.injectable";
@ -32,10 +28,7 @@ import showMessagePopupInjectable from "./electron-app/features/show-message-pop
import clusterFramesInjectable from "../common/cluster-frames.injectable"; import clusterFramesInjectable from "../common/cluster-frames.injectable";
import type { ClusterFrameInfo } from "../common/cluster-frames"; import type { ClusterFrameInfo } from "../common/cluster-frames";
import { observable, runInAction } from "mobx"; import { observable, runInAction } from "mobx";
import waitForElectronToBeReadyInjectable from "./electron-app/features/wait-for-electron-to-be-ready.injectable";
import broadcastMessageInjectable from "../common/ipc/broadcast-message.injectable"; import broadcastMessageInjectable from "../common/ipc/broadcast-message.injectable";
import getElectronThemeInjectable from "./electron-app/features/get-electron-theme.injectable";
import syncThemeFromOperatingSystemInjectable from "./electron-app/features/sync-theme-from-operating-system.injectable";
import platformInjectable from "../common/vars/platform.injectable"; import platformInjectable from "../common/vars/platform.injectable";
import electronQuitAndInstallUpdateInjectable from "./electron-app/features/electron-quit-and-install-update.injectable"; import electronQuitAndInstallUpdateInjectable from "./electron-app/features/electron-quit-and-install-update.injectable";
import electronUpdaterIsActiveInjectable from "./electron-app/features/electron-updater-is-active.injectable"; import electronUpdaterIsActiveInjectable from "./electron-app/features/electron-updater-is-active.injectable";
@ -46,7 +39,6 @@ import normalizedPlatformArchitectureInjectable from "../common/vars/normalized-
import waitUntilBundledExtensionsAreLoadedInjectable from "./start-main-application/lens-window/application-window/wait-until-bundled-extensions-are-loaded.injectable"; import waitUntilBundledExtensionsAreLoadedInjectable from "./start-main-application/lens-window/application-window/wait-until-bundled-extensions-are-loaded.injectable";
import { registerMobX } from "@ogre-tools/injectable-extension-for-mobx"; import { registerMobX } from "@ogre-tools/injectable-extension-for-mobx";
import electronInjectable from "./utils/resolve-system-proxy/electron.injectable"; import electronInjectable from "./utils/resolve-system-proxy/electron.injectable";
import focusApplicationInjectable from "./electron-app/features/focus-application.injectable";
import kubectlDownloadingNormalizedArchInjectable from "./kubectl/normalized-arch.injectable"; import kubectlDownloadingNormalizedArchInjectable from "./kubectl/normalized-arch.injectable";
import initializeClusterManagerInjectable from "./cluster/initialize-manager.injectable"; import initializeClusterManagerInjectable from "./cluster/initialize-manager.injectable";
import type { GlobalOverride } from "../common/test-utils/get-global-override"; import type { GlobalOverride } from "../common/test-utils/get-global-override";
@ -163,24 +155,11 @@ const overrideElectronFeatures = (di: DiContainer) => {
})); }));
}); });
di.override(exitAppInjectable, () => () => {});
di.override(getCommandLineSwitchInjectable, () => () => "irrelevant");
di.override(requestSingleInstanceLockInjectable, () => () => true); di.override(requestSingleInstanceLockInjectable, () => () => true);
di.override(disableHardwareAccelerationInjectable, () => () => {}); di.override(disableHardwareAccelerationInjectable, () => () => {});
di.override(shouldStartHiddenInjectable, () => false); di.override(shouldStartHiddenInjectable, () => false);
di.override(showMessagePopupInjectable, () => () => {}); di.override(showMessagePopupInjectable, () => () => {});
di.override(waitForElectronToBeReadyInjectable, () => () => Promise.resolve());
di.override(getElectronThemeInjectable, () => () => "dark");
di.override(syncThemeFromOperatingSystemInjectable, () => ({ start: () => {}, stop: () => {} }));
di.override(electronQuitAndInstallUpdateInjectable, () => () => {}); di.override(electronQuitAndInstallUpdateInjectable, () => () => {});
di.override(setUpdateOnQuitInjectable, () => () => {}); di.override(setUpdateOnQuitInjectable, () => () => {});
di.override(focusApplicationInjectable, () => () => {});
di.override(
getElectronAppPathInjectable,
() => (name: string) => `/some-electron-app-path-for-${kebabCase(name)}`,
);
di.override(setElectronAppPathInjectable, () => () => {});
di.override(electronUpdaterIsActiveInjectable, () => false); di.override(electronUpdaterIsActiveInjectable, () => false);
}; };

View File

@ -20,7 +20,6 @@ import startTopbarStateSyncInjectable from "./components/layout/top-bar/start-st
import { registerMobX } from "@ogre-tools/injectable-extension-for-mobx"; import { registerMobX } from "@ogre-tools/injectable-extension-for-mobx";
import watchHistoryStateInjectable from "./remote-helpers/watch-history-state.injectable"; import watchHistoryStateInjectable from "./remote-helpers/watch-history-state.injectable";
import legacyOnChannelListenInjectable from "./ipc/legacy-channel-listen.injectable"; import legacyOnChannelListenInjectable from "./ipc/legacy-channel-listen.injectable";
import storageSaveDelayInjectable from "./utils/create-storage/storage-save-delay.injectable";
import environmentVariablesInjectable from "../common/utils/environment-variables.injectable"; import environmentVariablesInjectable from "../common/utils/environment-variables.injectable";
import type { GlobalOverride } from "../common/test-utils/get-global-override"; import type { GlobalOverride } from "../common/test-utils/get-global-override";
import applicationInformationInjectable from "../common/vars/application-information-injectable"; import applicationInformationInjectable from "../common/vars/application-information-injectable";
@ -83,8 +82,6 @@ export const getDiForUnitTesting = (
di.override(legacyOnChannelListenInjectable, () => () => noop); di.override(legacyOnChannelListenInjectable, () => () => noop);
di.override(storageSaveDelayInjectable, () => 0);
di.override(requestAnimationFrameInjectable, () => (callback) => callback()); di.override(requestAnimationFrameInjectable, () => (callback) => callback());
di.override(lensResourcesDirInjectable, () => "/irrelevant"); di.override(lensResourcesDirInjectable, () => "/irrelevant");
di.override(environmentVariablesInjectable, () => ({})); di.override(environmentVariablesInjectable, () => ({}));

View File

@ -0,0 +1,9 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getGlobalOverride } from "../../../common/test-utils/get-global-override";
import storageSaveDelayInjectable from "./storage-save-delay.injectable";
export default getGlobalOverride(storageSaveDelayInjectable, () => 0);