Ensure that CatalogEntity.getName() and CatalogEntity.getId() are always used (#4763)

This commit is contained in:
Sebastian Malton authored and GitHub committed 2022-01-27 14:42:19 -05:00
1 parent 1cac3ca74c
commit 8d8491a035
17 files changed
+98 -166

No files matched your search

+28 -11
View File
@@ -4,45 +4,61 @@
*/
import { anyObject } from "jest-mock-extended";
import { merge } from "lodash";
import mockFs from "mock-fs";
import logger from "../../main/logger";
import type { CatalogEntity, CatalogEntityData, CatalogEntityKindData } from "../catalog";
import { HotbarStore } from "../hotbar-store";
import { getDiForUnitTesting } from "../../main/getDiForUnitTesting";
import directoryForUserDataInjectable
from "../app-paths/directory-for-user-data/directory-for-user-data.injectable";
import directoryForUserDataInjectable from "../app-paths/directory-for-user-data/directory-for-user-data.injectable";
jest.mock("../../main/catalog/catalog-entity-registry", () => ({
catalogEntityRegistry: {
items: [
{
getMockCatalogEntity({
apiVersion: "v1",
kind: "Cluster",
status: {
phase: "Running",
},
metadata: {
uid: "1dfa26e2ebab15780a3547e9c7fa785c",
name: "mycluster",
source: "local",
labels: {},
},
}),
getMockCatalogEntity({
apiVersion: "v1",
kind: "Cluster",
status: {
phase: "Running",
},
},
{
metadata: {
uid: "55b42c3c7ba3b04193416cda405269a5",
name: "my_shiny_cluster",
source: "remote",
labels: {},
},
}),
getMockCatalogEntity({
apiVersion: "v1",
kind: "Cluster",
status: {
phase: "Running",
},
},
{
metadata: {
uid: "catalog-entity",
name: "Catalog",
source: "app",
labels: {},
},
},
}),
],
},
}));
function getMockCatalogEntity(data: Partial<CatalogEntityData> & CatalogEntityKindData): CatalogEntity {
return merge(data, {
return {
getName: jest.fn(() => data.metadata?.name),
getId: jest.fn(() => data.metadata?.uid),
getSource: jest.fn(() => data.metadata?.source ?? "unknown"),
@@ -52,7 +68,8 @@ function getMockCatalogEntity(data: Partial<CatalogEntityData> & CatalogEntityKi
metadata: {},
spec: {},
status: {},
}) as CatalogEntity;
...data,
} as CatalogEntity;
}
const testCluster = getMockCatalogEntity({
@@ -67,22 +67,22 @@ export class KubernetesCluster extends CatalogEntity<KubernetesClusterMetadata,
async connect(): Promise<void> {
if (app) {
await ClusterStore.getInstance().getById(this.metadata.uid)?.activate();
await ClusterStore.getInstance().getById(this.getId())?.activate();
} else {
await requestClusterActivation(this.metadata.uid, false);
await requestClusterActivation(this.getId(), false);
}
}
async disconnect(): Promise<void> {
if (app) {
ClusterStore.getInstance().getById(this.metadata.uid)?.disconnect();
ClusterStore.getInstance().getById(this.getId())?.disconnect();
} else {
await requestClusterDisconnection(this.metadata.uid, false);
await requestClusterDisconnection(this.getId(), false);
}
}
async onRun(context: CatalogEntityActionContext) {
context.navigate(`/cluster/${this.metadata.uid}`);
context.navigate(`/cluster/${this.getId()}`);
}
onDetailsOpen(): void {
@@ -100,7 +100,7 @@ export class KubernetesCluster extends CatalogEntity<KubernetesClusterMetadata,
icon: "settings",
onClick: () => broadcastMessage(
IpcRendererNavigationEvents.NAVIGATE_IN_APP,
`/entity/${this.metadata.uid}/settings`,
`/entity/${this.getId()}/settings`,
),
});
}
@@ -111,14 +111,14 @@ export class KubernetesCluster extends CatalogEntity<KubernetesClusterMetadata,
context.menuItems.push({
title: "Disconnect",
icon: "link_off",
onClick: () => requestClusterDisconnection(this.metadata.uid),
onClick: () => requestClusterDisconnection(this.getId()),
});
break;
case LensKubernetesClusterStatus.DISCONNECTED:
context.menuItems.push({
title: "Connect",
icon: "link",
onClick: () => context.navigate(`/cluster/${this.metadata.uid}`),
onClick: () => context.navigate(`/cluster/${this.getId()}`),
});
break;
}
+2 -2
View File
@@ -38,9 +38,9 @@ export class WebLink extends CatalogEntity<CatalogEntityMetadata, WebLinkStatus,
context.menuItems.push({
title: "Delete",
icon: "delete",
onClick: async () => WeblinkStore.getInstance().removeById(this.metadata.uid),
onClick: async () => WeblinkStore.getInstance().removeById(this.getId()),
confirm: {
message: `Remove Web Link "${this.metadata.name}" from ${productName}?`,
message: `Remove Web Link "${this.getName()}" from ${productName}?`,
},
});
}
+17 -4
View File
@@ -315,11 +315,24 @@ export abstract class CatalogEntity<
@observable status: Status;
@observable spec: Spec;
constructor(data: CatalogEntityData<Metadata, Status, Spec>) {
constructor({ metadata, status, spec }: CatalogEntityData<Metadata, Status, Spec>) {
makeObservable(this);
this.metadata = data.metadata;
this.status = data.status;
this.spec = data.spec;
if (!metadata || typeof metadata !== "object") {
throw new TypeError("CatalogEntity's metadata must be a defined object");
}
if (!status || typeof status !== "object") {
throw new TypeError("CatalogEntity's status must be a defined object");
}
if (!spec || typeof spec !== "object") {
throw new TypeError("CatalogEntity's spec must be a defined object");
}
this.metadata = metadata;
this.status = status;
this.spec = spec;
}
/**
+18 -15
View File
@@ -154,28 +154,28 @@ export class HotbarStore extends BaseStore<HotbarStoreModel> {
@action
addToHotbar(item: CatalogEntity, cellIndex?: number) {
const hotbar = this.getActive();
const uid = item.metadata?.uid;
const name = item.metadata?.name;
const uid = item.getId();
const name = item.getName();
if (typeof uid !== "string") {
throw new TypeError("CatalogEntity.metadata.uid must be a string");
throw new TypeError("CatalogEntity's ID must be a string");
}
if (typeof name !== "string") {
throw new TypeError("CatalogEntity.metadata.name must be a string");
throw new TypeError("CatalogEntity's NAME must be a string");
}
const newItem = { entity: {
uid,
name,
source: item.metadata.source,
}};
if (this.isAddedToActive(item)) {
return;
}
const entity = {
uid,
name,
source: item.metadata.source,
};
const newItem = { entity };
if (cellIndex === undefined) {
// Add item to empty cell
const emptyCellIndex = hotbar.items.indexOf(null);
@@ -278,11 +278,14 @@ export class HotbarStore extends BaseStore<HotbarStoreModel> {
}
/**
* Checks if entity already pinned to hotbar
* @returns boolean
* Checks if entity already pinned to the active hotbar
*/
isAddedToActive(entity: CatalogEntity) {
return !!this.getActive().items.find(item => item?.entity.uid === entity.metadata.uid);
isAddedToActive(entity: CatalogEntity | null | undefined): boolean {
if (!entity) {
return false;
}
return this.getActive().items.findIndex(item => item?.entity.uid === entity.getId()) >= 0;
}
getDisplayLabel(hotbar: Hotbar): string {