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

Fix unit tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-06-24 14:56:01 -04:00
parent 62b381e33b
commit 0e1911cc9d
10 changed files with 88 additions and 28 deletions

View File

@ -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"
},
},
]
}
}));

View File

@ -72,24 +72,6 @@ export class UserStore extends BaseStore<UserStoreModel> {
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<UserStoreModel> {
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

View File

@ -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<typeof watch>;

View File

@ -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", () => {

View File

@ -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<typeof waitUntilU
describe("kube auth proxy tests", () => {
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();
});

View File

@ -138,7 +138,7 @@ app.on("ready", async () => {
logger.info("💾 Loading stores");
UserStore.createInstance();
UserStore.createInstance().startMainReactions();
ClusterStore.createInstance().provideInitialFromMain();
HotbarStore.createInstance();
ExtensionsStore.createInstance();

View File

@ -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", () => {

View File

@ -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");

View File

@ -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("<LogResourceSelector />", () => {
beforeEach(() => {
mockFs({
"tmp": {}
});
UserStore.createInstance();
ThemeStore.createInstance();
});
@ -78,6 +82,7 @@ describe("<LogResourceSelector />", () => {
afterEach(() => {
UserStore.resetInstance();
ThemeStore.resetInstance();
mockFs.restore();
});
it("renders w/o errors", () => {

View File

@ -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("<HotbarRemoveCommand />", () => {
beforeEach(() => {
mockFs({
"tmp": {}
});
UserStore.createInstance();
ThemeStore.createInstance();
});
@ -55,11 +66,12 @@ describe("<HotbarRemoveCommand />", () => {
afterEach(() => {
UserStore.resetInstance();
ThemeStore.resetInstance();
mockFs.restore();
});
it("renders w/o errors", () => {
const { container } = render(<HotbarRemoveCommand/>);
expect(container).toBeInstanceOf(HTMLElement);
});
@ -73,4 +85,3 @@ describe("<HotbarRemoveCommand />", () => {
spy.mockRestore();
});
});