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

Fix tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-08-04 12:36:05 -04:00
parent dd40bfee99
commit 3d9d930b47
4 changed files with 33 additions and 10 deletions

View File

@ -3,14 +3,23 @@
* 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 type { AppEvent } from "../app-event-bus/event-bus"; import type { AppEvent, AppEventBus } from "../app-event-bus/event-bus";
import { appEventBus } from "../app-event-bus/event-bus";
import { assert, Console } from "console"; import { assert, Console } from "console";
import { stdout, stderr } from "process"; import { stdout, stderr } from "process";
import { getDiForUnitTesting } from "../../main/getDiForUnitTesting";
import appEventBusInjectable from "../app-event-bus/app-event-bus.injectable";
console = new Console(stdout, stderr); console = new Console(stdout, stderr);
describe("event bus tests", () => { describe("event bus tests", () => {
let appEventBus: AppEventBus;
beforeEach(() => {
const di = getDiForUnitTesting();
appEventBus = di.inject(appEventBusInjectable);
});
describe("emit", () => { describe("emit", () => {
it("emits an event", () => { it("emits an event", () => {
let event: AppEvent | undefined; let event: AppEvent | undefined;

View File

@ -4,6 +4,7 @@
*/ */
import { asLegacyGlobalForExtensionApi } from "../../extensions/as-legacy-globals-for-extension-api/as-legacy-global-object-for-extension-api"; import { asLegacyGlobalForExtensionApi } from "../../extensions/as-legacy-globals-for-extension-api/as-legacy-global-object-for-extension-api";
import type { EventEmitter } from "../event-emitter";
import appEventBusInjectable from "./app-event-bus.injectable"; import appEventBusInjectable from "./app-event-bus.injectable";
export interface AppEvent { export interface AppEvent {
@ -13,6 +14,8 @@ export interface AppEvent {
params?: Record<string, any>; params?: Record<string, any>;
} }
export type AppEventBus = EventEmitter<[AppEvent]>;
/** /**
* @deprecated Switch to using appEventBusInjectable instead * @deprecated Switch to using appEventBusInjectable instead
*/ */

View File

@ -13,7 +13,7 @@ import { observer } from "mobx-react";
import path from "path"; import path from "path";
import React from "react"; import React from "react";
import * as uuid from "uuid"; import * as uuid from "uuid";
import { appEventBus } from "../../../common/app-event-bus/event-bus"; import type { AppEventBus } from "../../../common/app-event-bus/event-bus";
import { loadConfigFromString, splitConfig } from "../../../common/kube-helpers"; import { loadConfigFromString, splitConfig } from "../../../common/kube-helpers";
import { docsUrl } from "../../../common/vars"; import { docsUrl } from "../../../common/vars";
import { isDefined, iter } from "../../utils"; import { isDefined, iter } from "../../utils";
@ -25,6 +25,7 @@ import { withInjectables } from "@ogre-tools/injectable-react";
import getCustomKubeConfigDirectoryInjectable from "../../../common/app-paths/get-custom-kube-config-directory/get-custom-kube-config-directory.injectable"; import getCustomKubeConfigDirectoryInjectable from "../../../common/app-paths/get-custom-kube-config-directory/get-custom-kube-config-directory.injectable";
import type { NavigateToCatalog } from "../../../common/front-end-routing/routes/catalog/navigate-to-catalog.injectable"; import type { NavigateToCatalog } from "../../../common/front-end-routing/routes/catalog/navigate-to-catalog.injectable";
import navigateToCatalogInjectable from "../../../common/front-end-routing/routes/catalog/navigate-to-catalog.injectable"; import navigateToCatalogInjectable from "../../../common/front-end-routing/routes/catalog/navigate-to-catalog.injectable";
import appEventBusInjectable from "../../../common/app-event-bus/app-event-bus.injectable";
interface Option { interface Option {
config: KubeConfig; config: KubeConfig;
@ -34,6 +35,7 @@ interface Option {
interface Dependencies { interface Dependencies {
getCustomKubeConfigDirectory: (directoryName: string) => string; getCustomKubeConfigDirectory: (directoryName: string) => string;
navigateToCatalog: NavigateToCatalog; navigateToCatalog: NavigateToCatalog;
appEventBus: AppEventBus;
} }
function getContexts(config: KubeConfig): Map<string, Option> { function getContexts(config: KubeConfig): Map<string, Option> {
@ -59,7 +61,7 @@ class NonInjectedAddCluster extends React.Component<Dependencies> {
} }
componentDidMount() { componentDidMount() {
appEventBus.emit({ name: "cluster-add", action: "start" }); this.props.appEventBus.emit({ name: "cluster-add", action: "start" });
} }
@computed get allErrors(): string[] { @computed get allErrors(): string[] {
@ -85,7 +87,7 @@ class NonInjectedAddCluster extends React.Component<Dependencies> {
addClusters = action(async () => { addClusters = action(async () => {
this.isWaiting = true; this.isWaiting = true;
appEventBus.emit({ name: "cluster-add", action: "click" }); this.props.appEventBus.emit({ name: "cluster-add", action: "click" });
try { try {
const absPath = this.props.getCustomKubeConfigDirectory(uuid.v4()); const absPath = this.props.getCustomKubeConfigDirectory(uuid.v4());
@ -155,10 +157,8 @@ class NonInjectedAddCluster extends React.Component<Dependencies> {
export const AddCluster = withInjectables<Dependencies>(NonInjectedAddCluster, { export const AddCluster = withInjectables<Dependencies>(NonInjectedAddCluster, {
getProps: (di) => ({ getProps: (di) => ({
getCustomKubeConfigDirectory: di.inject( getCustomKubeConfigDirectory: di.inject(getCustomKubeConfigDirectoryInjectable),
getCustomKubeConfigDirectoryInjectable,
),
navigateToCatalog: di.inject(navigateToCatalogInjectable), navigateToCatalog: di.inject(navigateToCatalogInjectable),
appEventBus: di.inject(appEventBusInjectable),
}), }),
}); });

View File

@ -3,11 +3,14 @@
* 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 React from "react"; import React from "react";
import { render, screen } from "@testing-library/react"; import { screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event"; import userEvent from "@testing-library/user-event";
import type { CatalogCategorySpec } from "../../../../common/catalog"; import type { CatalogCategorySpec } from "../../../../common/catalog";
import { CatalogCategory } from "../../../../common/catalog"; import { CatalogCategory } from "../../../../common/catalog";
import { CatalogAddButton } from "../catalog-add-button"; import { CatalogAddButton } from "../catalog-add-button";
import type { DiRender } from "../../test-utils/renderFor";
import { renderFor } from "../../test-utils/renderFor";
import { getDiForUnitTesting } from "../../../getDiForUnitTesting";
class TestCatalogCategory extends CatalogCategory { class TestCatalogCategory extends CatalogCategory {
public readonly apiVersion = "catalog.k8slens.dev/v1alpha1"; public readonly apiVersion = "catalog.k8slens.dev/v1alpha1";
@ -26,6 +29,14 @@ class TestCatalogCategory extends CatalogCategory {
} }
describe("CatalogAddButton", () => { describe("CatalogAddButton", () => {
let render: DiRender;
beforeEach(() => {
const di = getDiForUnitTesting({ doGeneralOverrides: true });
render = renderFor(di);
});
it("opens Add menu", async () => { it("opens Add menu", async () => {
const category = new TestCatalogCategory(); const category = new TestCatalogCategory();