From 3d9d930b47c2876350d1a9d5dde6412e65b55780 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Thu, 4 Aug 2022 12:36:05 -0400 Subject: [PATCH] Fix tests Signed-off-by: Sebastian Malton --- src/common/__tests__/event-bus.test.ts | 13 +++++++++++-- src/common/app-event-bus/event-bus.ts | 3 +++ .../components/+add-cluster/add-cluster.tsx | 14 +++++++------- .../+catalog/__tests__/catalog-add-button.test.tsx | 13 ++++++++++++- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/common/__tests__/event-bus.test.ts b/src/common/__tests__/event-bus.test.ts index f90381eb44..9142ec2769 100644 --- a/src/common/__tests__/event-bus.test.ts +++ b/src/common/__tests__/event-bus.test.ts @@ -3,14 +3,23 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ -import type { AppEvent } from "../app-event-bus/event-bus"; -import { appEventBus } from "../app-event-bus/event-bus"; +import type { AppEvent, AppEventBus } from "../app-event-bus/event-bus"; import { assert, Console } from "console"; 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); describe("event bus tests", () => { + let appEventBus: AppEventBus; + + beforeEach(() => { + const di = getDiForUnitTesting(); + + appEventBus = di.inject(appEventBusInjectable); + }); + describe("emit", () => { it("emits an event", () => { let event: AppEvent | undefined; diff --git a/src/common/app-event-bus/event-bus.ts b/src/common/app-event-bus/event-bus.ts index e947b5a4a7..2f4d01b185 100644 --- a/src/common/app-event-bus/event-bus.ts +++ b/src/common/app-event-bus/event-bus.ts @@ -4,6 +4,7 @@ */ 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"; export interface AppEvent { @@ -13,6 +14,8 @@ export interface AppEvent { params?: Record; } +export type AppEventBus = EventEmitter<[AppEvent]>; + /** * @deprecated Switch to using appEventBusInjectable instead */ diff --git a/src/renderer/components/+add-cluster/add-cluster.tsx b/src/renderer/components/+add-cluster/add-cluster.tsx index 6630420ad6..9e76d99818 100644 --- a/src/renderer/components/+add-cluster/add-cluster.tsx +++ b/src/renderer/components/+add-cluster/add-cluster.tsx @@ -13,7 +13,7 @@ import { observer } from "mobx-react"; import path from "path"; import React from "react"; 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 { docsUrl } from "../../../common/vars"; 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 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 appEventBusInjectable from "../../../common/app-event-bus/app-event-bus.injectable"; interface Option { config: KubeConfig; @@ -34,6 +35,7 @@ interface Option { interface Dependencies { getCustomKubeConfigDirectory: (directoryName: string) => string; navigateToCatalog: NavigateToCatalog; + appEventBus: AppEventBus; } function getContexts(config: KubeConfig): Map { @@ -59,7 +61,7 @@ class NonInjectedAddCluster extends React.Component { } componentDidMount() { - appEventBus.emit({ name: "cluster-add", action: "start" }); + this.props.appEventBus.emit({ name: "cluster-add", action: "start" }); } @computed get allErrors(): string[] { @@ -85,7 +87,7 @@ class NonInjectedAddCluster extends React.Component { addClusters = action(async () => { this.isWaiting = true; - appEventBus.emit({ name: "cluster-add", action: "click" }); + this.props.appEventBus.emit({ name: "cluster-add", action: "click" }); try { const absPath = this.props.getCustomKubeConfigDirectory(uuid.v4()); @@ -155,10 +157,8 @@ class NonInjectedAddCluster extends React.Component { export const AddCluster = withInjectables(NonInjectedAddCluster, { getProps: (di) => ({ - getCustomKubeConfigDirectory: di.inject( - getCustomKubeConfigDirectoryInjectable, - ), - + getCustomKubeConfigDirectory: di.inject(getCustomKubeConfigDirectoryInjectable), navigateToCatalog: di.inject(navigateToCatalogInjectable), + appEventBus: di.inject(appEventBusInjectable), }), }); diff --git a/src/renderer/components/+catalog/__tests__/catalog-add-button.test.tsx b/src/renderer/components/+catalog/__tests__/catalog-add-button.test.tsx index 1f1fa1bfab..0594eb3d72 100644 --- a/src/renderer/components/+catalog/__tests__/catalog-add-button.test.tsx +++ b/src/renderer/components/+catalog/__tests__/catalog-add-button.test.tsx @@ -3,11 +3,14 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ 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 type { CatalogCategorySpec } from "../../../../common/catalog"; import { CatalogCategory } from "../../../../common/catalog"; 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 { public readonly apiVersion = "catalog.k8slens.dev/v1alpha1"; @@ -26,6 +29,14 @@ class TestCatalogCategory extends CatalogCategory { } describe("CatalogAddButton", () => { + let render: DiRender; + + beforeEach(() => { + const di = getDiForUnitTesting({ doGeneralOverrides: true }); + + render = renderFor(di); + }); + it("opens Add menu", async () => { const category = new TestCatalogCategory();