diff --git a/src/common/__tests__/hotbar-store.test.ts b/src/common/__tests__/hotbar-store.test.ts index f0e1b91907..267fbe3e8e 100644 --- a/src/common/__tests__/hotbar-store.test.ts +++ b/src/common/__tests__/hotbar-store.test.ts @@ -20,7 +20,6 @@ */ import mockFs from "mock-fs"; -import { catalogEntity } from "../../main/catalog-sources/general"; import { ClusterStore } from "../cluster-store"; import { HotbarStore } from "../hotbar-store"; @@ -41,7 +40,13 @@ jest.mock("../../main/catalog/catalog-entity-registry", () => ({ source: "remote" } }, - catalogEntity, + { + metadata: { + uid: "catalog-entity", + name: "Catalog", + source: "app" + }, + }, ] } })); diff --git a/src/common/user-store.ts b/src/common/user-store.ts index 61d2680818..c1e956b331 100644 --- a/src/common/user-store.ts +++ b/src/common/user-store.ts @@ -72,24 +72,6 @@ export class UserStore extends BaseStore { makeObservable(this); fileNameMigration(); this.load(); - - if (app) { - // track telemetry availability - reaction(() => this.allowTelemetry, allowed => { - appEventBus.emit({ name: "telemetry", action: allowed ? "enabled" : "disabled" }); - }); - - // open at system start-up - reaction(() => this.openAtLogin, openAtLogin => { - app.setLoginItemSettings({ - openAtLogin, - openAsHidden: true, - args: ["--hidden"] - }); - }, { - fireImmediately: true, - }); - } } @observable lastSeenAppVersion = "0.0.0"; @@ -130,6 +112,24 @@ export class UserStore extends BaseStore { return this.shell || process.env.SHELL || process.env.PTYSHELL; } + startMainReactions() { + // track telemetry availability + reaction(() => this.allowTelemetry, allowed => { + appEventBus.emit({ name: "telemetry", action: allowed ? "enabled" : "disabled" }); + }); + + // open at system start-up + reaction(() => this.openAtLogin, openAtLogin => { + app.setLoginItemSettings({ + openAtLogin, + openAsHidden: true, + args: ["--hidden"] + }); + }, { + fireImmediately: true, + }); + } + /** * Checks if a column (by ID) for a table (by ID) is configured to be hidden * @param tableId The ID of the table to be checked against diff --git a/src/extensions/__tests__/extension-discovery.test.ts b/src/extensions/__tests__/extension-discovery.test.ts index 347487e556..6db33acf19 100644 --- a/src/extensions/__tests__/extension-discovery.test.ts +++ b/src/extensions/__tests__/extension-discovery.test.ts @@ -39,6 +39,12 @@ jest.mock("../extension-installer", () => ({ installPackage: jest.fn() } })); +jest.mock("electron", () => ({ + app: { + getPath: () => "tmp", + setLoginItemSettings: jest.fn(), + }, +})); console = new Console(process.stdout, process.stderr); // fix mockFS const mockedWatch = watch as jest.MockedFunction; diff --git a/src/main/__test__/context-handler.test.ts b/src/main/__test__/context-handler.test.ts index ad3b42f5d2..0ba3a2d028 100644 --- a/src/main/__test__/context-handler.test.ts +++ b/src/main/__test__/context-handler.test.ts @@ -22,6 +22,14 @@ import { UserStore } from "../../common/user-store"; import { ContextHandler } from "../context-handler"; import { PrometheusProvider, PrometheusProviderRegistry, PrometheusService } from "../prometheus"; +import mockFs from "mock-fs"; + +jest.mock("electron", () => ({ + app: { + getPath: () => "tmp", + setLoginItemSettings: jest.fn(), + }, +})); enum ServiceResult { Success, @@ -70,6 +78,10 @@ function getHandler() { describe("ContextHandler", () => { beforeEach(() => { + mockFs({ + "tmp": {} + }); + PrometheusProviderRegistry.createInstance(); UserStore.createInstance(); }); @@ -77,6 +89,7 @@ describe("ContextHandler", () => { afterEach(() => { PrometheusProviderRegistry.resetInstance(); UserStore.resetInstance(); + mockFs.restore(); }); describe("getPrometheusService", () => { diff --git a/src/main/__test__/kube-auth-proxy.test.ts b/src/main/__test__/kube-auth-proxy.test.ts index 5006859637..e0dd7f73c6 100644 --- a/src/main/__test__/kube-auth-proxy.test.ts +++ b/src/main/__test__/kube-auth-proxy.test.ts @@ -46,7 +46,8 @@ jest.mock("winston", () => ({ jest.mock("electron", () => ({ app: { - getPath: () => "/foo", + getPath: () => "tmp", + setLoginItemSettings: jest.fn(), }, })); @@ -77,8 +78,6 @@ const mockWaitUntilUsed = waitUntilUsed as jest.MockedFunction { beforeEach(() => { jest.clearAllMocks(); - UserStore.resetInstance(); - UserStore.createInstance(); const mockMinikubeConfig = { "minikube-config.yml": JSON.stringify({ @@ -102,13 +101,16 @@ describe("kube auth proxy tests", () => { }], kind: "Config", preferences: {}, - }) + }), + "tmp": {}, }; mockFs(mockMinikubeConfig); + UserStore.createInstance(); }); afterEach(() => { + UserStore.resetInstance(); mockFs.restore(); }); diff --git a/src/main/index.ts b/src/main/index.ts index abef1e44da..0599f820cb 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -138,7 +138,7 @@ app.on("ready", async () => { logger.info("💾 Loading stores"); - UserStore.createInstance(); + UserStore.createInstance().startMainReactions(); ClusterStore.createInstance().provideInitialFromMain(); HotbarStore.createInstance(); ExtensionsStore.createInstance(); diff --git a/src/main/protocol-handler/__test__/router.test.ts b/src/main/protocol-handler/__test__/router.test.ts index 42e9bda3c6..045e05c127 100644 --- a/src/main/protocol-handler/__test__/router.test.ts +++ b/src/main/protocol-handler/__test__/router.test.ts @@ -28,9 +28,17 @@ import { LensExtension } from "../../../extensions/main-api"; import { ExtensionLoader } from "../../../extensions/extension-loader"; import { ExtensionsStore } from "../../../extensions/extensions-store"; import { LensProtocolRouterMain } from "../router"; +import mockFs from "mock-fs"; jest.mock("../../../common/ipc"); +jest.mock("electron", () => ({ + app: { + getPath: () => "tmp", + setLoginItemSettings: jest.fn(), + }, +})); + function throwIfDefined(val: any): void { if (val != null) { throw val; @@ -39,6 +47,9 @@ function throwIfDefined(val: any): void { describe("protocol router tests", () => { beforeEach(() => { + mockFs({ + "tmp": {} + }); ExtensionsStore.createInstance(); ExtensionLoader.createInstance(); @@ -53,6 +64,7 @@ describe("protocol router tests", () => { ExtensionsStore.resetInstance(); ExtensionLoader.resetInstance(); LensProtocolRouterMain.resetInstance(); + mockFs.restore(); }); it("should throw on non-lens URLS", () => { diff --git a/src/migrations/user-store/file-name-migration.ts b/src/migrations/user-store/file-name-migration.ts index 122cd78102..fc7784d2c2 100644 --- a/src/migrations/user-store/file-name-migration.ts +++ b/src/migrations/user-store/file-name-migration.ts @@ -22,8 +22,14 @@ import fse from "fs-extra"; import { app, remote } from "electron"; import path from "path"; +import { isTestEnv } from "../../common/vars"; export function fileNameMigration() { + if (isTestEnv) { + // hack: to make unit testing easier just ignore the following code + return; + } + const userDataPath = (app || remote.app).getPath("userData"); const configJsonPath = path.join(userDataPath, "config.json"); const lensUserStoreJsonPath = path.join(userDataPath, "lens-user-store.json"); diff --git a/src/renderer/components/dock/__test__/log-resource-selector.test.tsx b/src/renderer/components/dock/__test__/log-resource-selector.test.tsx index 62566b868a..17ebb92d09 100644 --- a/src/renderer/components/dock/__test__/log-resource-selector.test.tsx +++ b/src/renderer/components/dock/__test__/log-resource-selector.test.tsx @@ -30,10 +30,11 @@ import type { LogTabData } from "../log-tab.store"; import { dockerPod, deploymentPod1 } from "./pod.mock"; import { ThemeStore } from "../../../theme.store"; import { UserStore } from "../../../../common/user-store"; +import mockFs from "mock-fs"; jest.mock("electron", () => ({ app: { - getPath: () => "/foo", + getPath: () => "tmp", }, })); @@ -71,6 +72,9 @@ const getFewPodsTabData = (): LogTabData => { describe("", () => { beforeEach(() => { + mockFs({ + "tmp": {} + }); UserStore.createInstance(); ThemeStore.createInstance(); }); @@ -78,6 +82,7 @@ describe("", () => { afterEach(() => { UserStore.resetInstance(); ThemeStore.resetInstance(); + mockFs.restore(); }); it("renders w/o errors", () => { diff --git a/src/renderer/components/hotbar/__tests__/hotbar-remove-command.test.tsx b/src/renderer/components/hotbar/__tests__/hotbar-remove-command.test.tsx index dfb75ffda3..119212753d 100644 --- a/src/renderer/components/hotbar/__tests__/hotbar-remove-command.test.tsx +++ b/src/renderer/components/hotbar/__tests__/hotbar-remove-command.test.tsx @@ -26,6 +26,14 @@ import React from "react"; import { ThemeStore } from "../../../theme.store"; import { UserStore } from "../../../../common/user-store"; import { Notifications } from "../../notifications"; +import mockFs from "mock-fs"; + +jest.mock("electron", () => ({ + app: { + getPath: () => "tmp", + setLoginItemSettings: jest.fn(), + }, +})); const mockHotbars: {[id: string]: any} = { "1": { @@ -48,6 +56,9 @@ jest.mock("../../../../common/hotbar-store", () => ({ describe("", () => { beforeEach(() => { + mockFs({ + "tmp": {} + }); UserStore.createInstance(); ThemeStore.createInstance(); }); @@ -55,11 +66,12 @@ describe("", () => { afterEach(() => { UserStore.resetInstance(); ThemeStore.resetInstance(); + mockFs.restore(); }); it("renders w/o errors", () => { const { container } = render(); - + expect(container).toBeInstanceOf(HTMLElement); }); @@ -73,4 +85,3 @@ describe("", () => { spy.mockRestore(); }); }); -