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

Improve CatalogEntityRegistry types & add tests (#2495)

* improve CatalogEntityRegistry types & add tests

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* fix

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* fix

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen 2021-04-12 09:50:01 +03:00 committed by GitHub
parent 9f065a209f
commit 4504a76e6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 78 additions and 14 deletions

View File

@ -0,0 +1,63 @@
import { observable, reaction } from "mobx";
import { WebLink } from "../catalog-entities";
import { CatalogEntityRegistry } from "../catalog-entity-registry";
describe("CatalogEntityRegistry", () => {
let registry: CatalogEntityRegistry;
const entity = new WebLink({
apiVersion: "entity.k8slens.dev/v1alpha1",
kind: "WebLink",
metadata: {
uid: "test",
name: "test-link",
source: "test",
labels: {}
},
spec: {
url: "https://k8slens.dev"
},
status: {
phase: "ok"
}
});
beforeEach(() => {
registry = new CatalogEntityRegistry();
});
describe("addSource", () => {
it ("allows to add an observable source", () => {
const source = observable.array([]);
registry.addSource("test", source);
expect(registry.items.length).toEqual(0);
source.push(entity);
expect(registry.items.length).toEqual(1);
});
it ("added source change triggers reaction", (done) => {
const source = observable.array([]);
registry.addSource("test", source);
reaction(() => registry.items, () => {
done();
});
source.push(entity);
});
});
describe("removeSource", () => {
it ("removes source", () => {
const source = observable.array([]);
registry.addSource("test", source);
source.push(entity);
registry.removeSource("test");
expect(registry.items.length).toEqual(0);
});
});
});

View File

@ -1,5 +1,5 @@
import { observable } from "mobx"; import { observable } from "mobx";
import { CatalogCategory, CatalogEntity, CatalogEntityMetadata, CatalogEntityStatus } from "../catalog-entity"; import { CatalogCategory, CatalogEntity, CatalogEntityData, CatalogEntityMetadata, CatalogEntityStatus } from "../catalog-entity";
import { catalogCategoryRegistry } from "../catalog-category-registry"; import { catalogCategoryRegistry } from "../catalog-category-registry";
export interface WebLinkStatus extends CatalogEntityStatus { export interface WebLinkStatus extends CatalogEntityStatus {
@ -17,6 +17,12 @@ export class WebLink implements CatalogEntity {
@observable public status: WebLinkStatus; @observable public status: WebLinkStatus;
@observable public spec: WebLinkSpec; @observable public spec: WebLinkSpec;
constructor(data: CatalogEntityData) {
this.metadata = data.metadata;
this.status = data.status as WebLinkStatus;
this.spec = data.spec as WebLinkSpec;
}
getId() { getId() {
return this.metadata.uid; return this.metadata.uid;
} }

View File

@ -1,10 +1,10 @@
import { action, computed, observable } from "mobx"; import { action, computed, observable, IObservableArray } from "mobx";
import { CatalogEntity } from "./catalog-entity"; import { CatalogEntity } from "./catalog-entity";
export class CatalogEntityRegistry { export class CatalogEntityRegistry {
protected sources = observable.map<string, CatalogEntity[]>([], { deep: true }); protected sources = observable.map<string, IObservableArray<CatalogEntity>>([], { deep: true });
@action addSource(id: string, source: CatalogEntity[]) { @action addSource(id: string, source: IObservableArray<CatalogEntity>) {
this.sources.set(id, source); this.sources.set(id, source);
} }
@ -12,14 +12,8 @@ export class CatalogEntityRegistry {
this.sources.delete(id); this.sources.delete(id);
} }
@computed get items() { @computed get items(): CatalogEntity[] {
const catalogItems: CatalogEntity[] = []; return Array.from(this.sources.values()).flat();
for (const items of this.sources.values()) {
items.forEach((item) => catalogItems.push(item));
}
return catalogItems;
} }
getItemsForApiKind<T extends CatalogEntity>(apiVersion: string, kind: string): T[] { getItemsForApiKind<T extends CatalogEntity>(apiVersion: string, kind: string): T[] {

View File

@ -4,6 +4,7 @@ import { WindowManager } from "../main/window-manager";
import { getExtensionPageUrl } from "./registries/page-registry"; import { getExtensionPageUrl } from "./registries/page-registry";
import { catalogEntityRegistry } from "../common/catalog-entity-registry"; import { catalogEntityRegistry } from "../common/catalog-entity-registry";
import { CatalogEntity } from "../common/catalog-entity"; import { CatalogEntity } from "../common/catalog-entity";
import { IObservableArray } from "mobx";
export class LensMainExtension extends LensExtension { export class LensMainExtension extends LensExtension {
appMenus: MenuRegistration[] = []; appMenus: MenuRegistration[] = [];
@ -19,7 +20,7 @@ export class LensMainExtension extends LensExtension {
await windowManager.navigate(pageUrl, frameId); await windowManager.navigate(pageUrl, frameId);
} }
addCatalogSource(id: string, source: CatalogEntity[]) { addCatalogSource(id: string, source: IObservableArray<CatalogEntity>) {
catalogEntityRegistry.addSource(`${this.name}:${id}`, source); catalogEntityRegistry.addSource(`${this.name}:${id}`, source);
} }

View File

@ -14,7 +14,7 @@ import { catalogEntityRegistry } from "../common/catalog-entity-registry";
const clusterOwnerRef = "ClusterManager"; const clusterOwnerRef = "ClusterManager";
export class ClusterManager extends Singleton { export class ClusterManager extends Singleton {
@observable.deep catalogSource: CatalogEntity[] = []; catalogSource = observable.array<CatalogEntity>([]);
constructor(public readonly port: number) { constructor(public readonly port: number) {
super(); super();