From 231e01c933573b43463bc6cb5b6519fab76d8df5 Mon Sep 17 00:00:00 2001 From: Panu Horsmalahti Date: Wed, 23 Mar 2022 11:03:10 +0200 Subject: [PATCH] Emit AppEvent when opening Catalog and changing Catalog Category (#5071) * Emit AppEvent when opening Catalog and changing Catalog Category. Signed-off-by: Panu Horsmalahti * Add getName method to CatalogCategory Signed-off-by: Panu Horsmalahti --- src/common/__tests__/catalog-entity.test.ts | 30 +++++++++++++ src/common/catalog/catalog-entity.ts | 7 +++ .../components/+catalog/catalog.test.tsx | 44 +++++++++++++++++++ src/renderer/components/+catalog/catalog.tsx | 17 +++++++ 4 files changed, 98 insertions(+) create mode 100644 src/common/__tests__/catalog-entity.test.ts diff --git a/src/common/__tests__/catalog-entity.test.ts b/src/common/__tests__/catalog-entity.test.ts new file mode 100644 index 0000000000..3785ed1d49 --- /dev/null +++ b/src/common/__tests__/catalog-entity.test.ts @@ -0,0 +1,30 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import { CatalogCategory, CatalogCategorySpec } from "../catalog"; + +class TestCatalogCategory extends CatalogCategory { + public readonly apiVersion = "catalog.k8slens.dev/v1alpha1"; + public readonly kind = "CatalogCategory"; + public metadata = { + name: "Test Category", + icon: "", + }; + public spec: CatalogCategorySpec = { + group: "entity.k8slens.dev", + versions: [], + names: { + kind: "Test", + }, + }; +} + +describe("CatalogCategory", () => { + it("returns name", () => { + const category = new TestCatalogCategory(); + + expect(category.getName()).toEqual("Test Category"); + }); +}); diff --git a/src/common/catalog/catalog-entity.ts b/src/common/catalog/catalog-entity.ts index daff2e6017..2704bed2e0 100644 --- a/src/common/catalog/catalog-entity.ts +++ b/src/common/catalog/catalog-entity.ts @@ -173,6 +173,13 @@ export abstract class CatalogCategory extends (EventEmitter as new () => TypedEm return `${this.spec.group}/${this.spec.names.kind}`; } + /** + * Get the name of this category + */ + public getName(): string { + return this.metadata.name; + } + /** * Add a filter for menu items of catalogAddMenu * @param fn The function that should return a truthy value if that menu item should be displayed diff --git a/src/renderer/components/+catalog/catalog.test.tsx b/src/renderer/components/+catalog/catalog.test.tsx index 218bb60feb..5c221bb457 100644 --- a/src/renderer/components/+catalog/catalog.test.tsx +++ b/src/renderer/components/+catalog/catalog.test.tsx @@ -25,6 +25,8 @@ import { UserStore } from "../../../common/user-store"; import mockFs from "mock-fs"; import directoryForUserDataInjectable from "../../../common/app-paths/directory-for-user-data/directory-for-user-data.injectable"; +import type { AppEvent } from "../../../common/app-event-bus/event-bus"; +import appEventBusInjectable from "../../../common/app-event-bus/app-event-bus.injectable"; mockWindow(); jest.mock("electron", () => ({ @@ -102,6 +104,7 @@ describe("", () => { let di: DiContainer; let catalogEntityStore: CatalogEntityStore; let catalogEntityRegistry: CatalogEntityRegistry; + let emitEvent: (event: AppEvent) => void; let render: DiRender; beforeEach(async () => { @@ -125,6 +128,12 @@ describe("", () => { di.override(catalogEntityRegistryInjectable, () => catalogEntityRegistry); + emitEvent = jest.fn(); + + di.override(appEventBusInjectable, () => ({ + emit: emitEvent, + })); + catalogEntityStore = di.inject(catalogEntityStoreInjectable); }); @@ -320,4 +329,39 @@ describe("", () => { userEvent.click(screen.getByTestId("detail-panel-hot-bar-icon")); }); + + it("emits catalog open AppEvent", () => { + render( + , + ); + + expect(emitEvent).toHaveBeenCalledWith( { + action: "open", + name: "catalog", + }); + }); + + it("emits catalog change AppEvent when changing the category", () => { + render( + , + ); + + userEvent.click(screen.getByText("Web Links")); + + expect(emitEvent).toHaveBeenLastCalledWith({ + action: "change-category", + name: "catalog", + params: { + category: "Web Links", + }, + }); + }); }); diff --git a/src/renderer/components/+catalog/catalog.tsx b/src/renderer/components/+catalog/catalog.tsx index 359e538ac5..a3af640fc5 100644 --- a/src/renderer/components/+catalog/catalog.tsx +++ b/src/renderer/components/+catalog/catalog.tsx @@ -36,6 +36,8 @@ import getCategoryColumnsInjectable from "./get-category-columns.injectable"; import type { RegisteredCustomCategoryViewDecl } from "./custom-views.injectable"; import customCategoryViewsInjectable from "./custom-views.injectable"; import type { CustomCategoryViewComponents } from "./custom-views"; +import type { AppEvent } from "../../../common/app-event-bus/event-bus"; +import appEventBusInjectable from "../../../common/app-event-bus/app-event-bus.injectable"; export interface CatalogProps extends RouteComponentProps {} @@ -44,6 +46,7 @@ interface Dependencies { catalogEntityStore: CatalogEntityStore; getCategoryColumns: (params: GetCategoryColumnsParams) => CategoryColumns; customCategoryViews: IComputedValue>>; + emitEvent: (event: AppEvent) => void; } @observer @@ -104,6 +107,11 @@ class NonInjectedCatalog extends React.Component { }); } })); + + this.props.emitEvent({ + name: "catalog", + action: "open", + }); } addToHotbar(entity: CatalogEntity): void { @@ -146,6 +154,14 @@ class NonInjectedCatalog extends React.Component { onTabChange = action((tabId: string | null) => { const activeCategory = this.categories.find(category => category.getId() === tabId); + this.props.emitEvent({ + name: "catalog", + action: "change-category", + params: { + category: activeCategory ? activeCategory.getName() : "Browse", + }, + }); + if (activeCategory) { navigate(catalogURL({ params: { group: activeCategory.spec.group, kind: activeCategory.spec.names.kind }})); } else { @@ -311,6 +327,7 @@ export const Catalog = withInjectables( NonInjectedC catalogPreviousActiveTabStorage: di.inject(catalogPreviousActiveTabStorageInjectable), getCategoryColumns: di.inject(getCategoryColumnsInjectable), customCategoryViews: di.inject(customCategoryViewsInjectable), + emitEvent: di.inject(appEventBusInjectable).emit, ...props, }), });