mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Fix Catalog displaying wrong number of items per category (#5427)
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
d6107e9585
commit
a61a455fad
@ -0,0 +1,163 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import type { CatalogCategoryMetadata, CatalogCategorySpec } from "../../../../common/catalog";
|
||||
import { CatalogEntity, categoryVersion } from "../../../../common/catalog";
|
||||
import { CatalogCategory } from "../../../api/catalog-entity";
|
||||
import { noop } from "../../../utils";
|
||||
import type { CatalogEntityStore } from "../catalog-entity-store/catalog-entity.store";
|
||||
import { catalogEntityStore } from "../catalog-entity-store/catalog-entity.store";
|
||||
|
||||
class TestEntityOne extends CatalogEntity {
|
||||
public static readonly apiVersion: string = "entity.k8slens.dev/v1alpha1";
|
||||
public static readonly kind: string = "TestEntityOne";
|
||||
|
||||
public readonly apiVersion = TestEntityOne.apiVersion;
|
||||
public readonly kind = TestEntityOne.kind;
|
||||
}
|
||||
|
||||
class TestEntityTwo extends CatalogEntity {
|
||||
public static readonly apiVersion: string = "entity.k8slens.dev/v1alpha1";
|
||||
public static readonly kind: string = "TestEntityTwo";
|
||||
|
||||
public readonly apiVersion = TestEntityTwo.apiVersion;
|
||||
public readonly kind = TestEntityTwo.kind;
|
||||
}
|
||||
|
||||
class TestCategoryOne extends CatalogCategory {
|
||||
apiVersion = "catalog.k8slens.dev/v1alpha1";
|
||||
kind = "CatalogCategory";
|
||||
metadata: CatalogCategoryMetadata = {
|
||||
icon: "dash",
|
||||
name: "test-one",
|
||||
};
|
||||
spec: CatalogCategorySpec = {
|
||||
group: "entity.k8slens.dev",
|
||||
versions: [
|
||||
categoryVersion("v1alpha1", TestEntityOne),
|
||||
],
|
||||
names: {
|
||||
kind: "KubernetesCluster",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
class TestCategoryTwo extends CatalogCategory {
|
||||
apiVersion = "catalog.k8slens.dev/v1alpha1";
|
||||
kind = "CatalogCategory";
|
||||
metadata: CatalogCategoryMetadata = {
|
||||
icon: "dash",
|
||||
name: "test-two",
|
||||
};
|
||||
spec: CatalogCategorySpec = {
|
||||
group: "entity.k8slens.dev",
|
||||
versions: [
|
||||
categoryVersion("v1alpha1", TestEntityTwo),
|
||||
],
|
||||
names: {
|
||||
kind: "KubernetesCluster",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe("CatalogEntityStore", () => {
|
||||
describe("getTotalCount", () => {
|
||||
let store: CatalogEntityStore;
|
||||
let testCategoryOne: TestCategoryOne;
|
||||
let testCategoryTwo: TestCategoryTwo;
|
||||
|
||||
beforeEach(() => {
|
||||
const entityItems = [
|
||||
new TestEntityOne({
|
||||
metadata: {
|
||||
labels: {},
|
||||
name: "my-test-one",
|
||||
uid: "1",
|
||||
},
|
||||
spec: {},
|
||||
status: {
|
||||
phase: "unknown",
|
||||
},
|
||||
}),
|
||||
new TestEntityOne({
|
||||
metadata: {
|
||||
labels: {},
|
||||
name: "my-test-two",
|
||||
uid: "2",
|
||||
},
|
||||
spec: {},
|
||||
status: {
|
||||
phase: "unknown",
|
||||
},
|
||||
}),
|
||||
new TestEntityTwo({
|
||||
metadata: {
|
||||
labels: {},
|
||||
name: "my-test-three",
|
||||
uid: "3",
|
||||
},
|
||||
spec: {},
|
||||
status: {
|
||||
phase: "unknown",
|
||||
},
|
||||
}),
|
||||
new TestEntityTwo({
|
||||
metadata: {
|
||||
labels: {},
|
||||
name: "my-test-four",
|
||||
uid: "4",
|
||||
},
|
||||
spec: {},
|
||||
status: {
|
||||
phase: "unknown",
|
||||
},
|
||||
}),
|
||||
new TestEntityTwo({
|
||||
metadata: {
|
||||
labels: {},
|
||||
name: "my-test-five",
|
||||
uid: "5",
|
||||
},
|
||||
spec: {},
|
||||
status: {
|
||||
phase: "unknown",
|
||||
},
|
||||
}),
|
||||
];
|
||||
|
||||
testCategoryOne = new TestCategoryOne();
|
||||
testCategoryTwo = new TestCategoryTwo();
|
||||
store = catalogEntityStore({
|
||||
catalogRegistry: {
|
||||
items: [
|
||||
testCategoryOne,
|
||||
testCategoryTwo,
|
||||
],
|
||||
},
|
||||
entityRegistry: {
|
||||
onRun: noop,
|
||||
filteredItems: entityItems,
|
||||
getItemsForCategory: <T extends CatalogEntity>(category: CatalogCategory): T[] => {
|
||||
return entityItems.filter(item => category.spec.versions.some(version => item instanceof version.entityClass)) as T[];
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("given no active category, returns count of all kinds", () => {
|
||||
expect(store.getTotalCount()).toBe(5);
|
||||
});
|
||||
|
||||
it("given active category is TestCategoryOne, only returns count for those declared kinds", () => {
|
||||
store.activeCategory.set(testCategoryOne);
|
||||
expect(store.getTotalCount()).toBe(2);
|
||||
});
|
||||
|
||||
it("given active category is TestCategoryTwo, only returns count for those declared kinds", () => {
|
||||
store.activeCategory.set(testCategoryTwo);
|
||||
expect(store.getTotalCount()).toBe(3);
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -12,9 +12,12 @@ import type { Disposer } from "../../../../common/utils";
|
||||
import { disposer } from "../../../../common/utils";
|
||||
import type { ItemListStore } from "../../item-object-list";
|
||||
|
||||
type EntityRegistry = Pick<CatalogEntityRegistry, "getItemsForCategory" | "filteredItems" | "onRun">;
|
||||
type CatalogRegistry = Pick<CatalogCategoryRegistry, "items">;
|
||||
|
||||
interface Dependencies {
|
||||
entityRegistry: CatalogEntityRegistry;
|
||||
catalogRegistry: CatalogCategoryRegistry;
|
||||
entityRegistry: EntityRegistry;
|
||||
catalogRegistry: CatalogRegistry;
|
||||
}
|
||||
|
||||
export type CatalogEntityStore = ItemListStore<CatalogEntity, false> & {
|
||||
@ -71,7 +74,7 @@ export function catalogEntityStore({
|
||||
),
|
||||
onRun: entity => entityRegistry.onRun(entity),
|
||||
failedLoading: false,
|
||||
getTotalCount: () => entityRegistry.filteredItems.length,
|
||||
getTotalCount: () => entities.get().length,
|
||||
isLoaded: true,
|
||||
isSelected: (item) => item.getId() === selectedItemId.get(),
|
||||
isSelectedAll: () => false,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user