mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Add more tests
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
e37c3bde17
commit
85ee7d34f8
@ -3,18 +3,25 @@
|
|||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { when } from "mobx";
|
import type { ClusterContext } from "../cluster-context";
|
||||||
|
import type { KubeApi } from "../kube-api";
|
||||||
import { KubeObject } from "../kube-object";
|
import { KubeObject } from "../kube-object";
|
||||||
import type { KubeObjectStoreLoadingParams } from "../kube-object.store";
|
import type { KubeObjectStoreLoadingParams } from "../kube-object.store";
|
||||||
import { KubeObjectStore } from "../kube-object.store";
|
import { KubeObjectStore } from "../kube-object.store";
|
||||||
|
|
||||||
class FakeKubeObjectStore extends KubeObjectStore<KubeObject> {
|
class FakeKubeObjectStore extends KubeObjectStore<KubeObject> {
|
||||||
get contextReady() {
|
_context = {
|
||||||
return when(() => true);
|
allNamespaces: [],
|
||||||
|
contextNamespaces: [],
|
||||||
|
hasSelectedAll: false,
|
||||||
|
} as ClusterContext;
|
||||||
|
|
||||||
|
get context() {
|
||||||
|
return this._context;
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(private readonly _loadItems: (params: KubeObjectStoreLoadingParams) => KubeObject[]) {
|
constructor(private readonly _loadItems: (params: KubeObjectStoreLoadingParams) => KubeObject[], api: Partial<KubeApi<KubeObject>>) {
|
||||||
super();
|
super(api as KubeApi<KubeObject>);
|
||||||
}
|
}
|
||||||
|
|
||||||
async loadItems(params: KubeObjectStoreLoadingParams) {
|
async loadItems(params: KubeObjectStoreLoadingParams) {
|
||||||
@ -35,7 +42,9 @@ describe("KubeObjectStore", () => {
|
|||||||
namespace: "default",
|
namespace: "default",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const store = new FakeKubeObjectStore(loadItems);
|
const store = new FakeKubeObjectStore(loadItems, {
|
||||||
|
isNamespaced: true,
|
||||||
|
});
|
||||||
|
|
||||||
loadItems.mockImplementationOnce(() => [obj]);
|
loadItems.mockImplementationOnce(() => [obj]);
|
||||||
|
|
||||||
@ -53,4 +62,85 @@ describe("KubeObjectStore", () => {
|
|||||||
|
|
||||||
expect(store.items).not.toContain(obj);
|
expect(store.items).not.toContain(obj);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should not remove an object that is not returned, if it is in a different namespace", async () => {
|
||||||
|
const loadItems = jest.fn();
|
||||||
|
const objInDefaultNamespace = new KubeObject({
|
||||||
|
apiVersion: "v1",
|
||||||
|
kind: "Foo",
|
||||||
|
metadata: {
|
||||||
|
name: "some-obj-name",
|
||||||
|
resourceVersion: "1",
|
||||||
|
uid: "some-uid",
|
||||||
|
namespace: "default",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const objNotInDefaultNamespace = new KubeObject({
|
||||||
|
apiVersion: "v1",
|
||||||
|
kind: "Foo",
|
||||||
|
metadata: {
|
||||||
|
name: "some-obj-name",
|
||||||
|
resourceVersion: "1",
|
||||||
|
uid: "some-uid",
|
||||||
|
namespace: "not-default",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const store = new FakeKubeObjectStore(loadItems, {
|
||||||
|
isNamespaced: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
loadItems.mockImplementationOnce(() => [objInDefaultNamespace]);
|
||||||
|
|
||||||
|
await store.loadAll({
|
||||||
|
namespaces: ["default"],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(store.items).toContain(objInDefaultNamespace);
|
||||||
|
|
||||||
|
loadItems.mockImplementationOnce(() => [objNotInDefaultNamespace]);
|
||||||
|
|
||||||
|
await store.loadAll({
|
||||||
|
namespaces: ["not-default"],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(store.items).toContain(objInDefaultNamespace);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should remove all objects not returned if the api is cluster-scoped", async () => {
|
||||||
|
const loadItems = jest.fn();
|
||||||
|
const clusterScopedObject1 = new KubeObject({
|
||||||
|
apiVersion: "v1",
|
||||||
|
kind: "Foo",
|
||||||
|
metadata: {
|
||||||
|
name: "some-obj-name",
|
||||||
|
resourceVersion: "1",
|
||||||
|
uid: "some-uid",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const clusterScopedObject2 = new KubeObject({
|
||||||
|
apiVersion: "v1",
|
||||||
|
kind: "Foo",
|
||||||
|
metadata: {
|
||||||
|
name: "some-obj-name",
|
||||||
|
resourceVersion: "1",
|
||||||
|
uid: "some-uid",
|
||||||
|
namespace: "not-default",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const store = new FakeKubeObjectStore(loadItems, {
|
||||||
|
isNamespaced: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
loadItems.mockImplementationOnce(() => [clusterScopedObject1]);
|
||||||
|
|
||||||
|
await store.loadAll({});
|
||||||
|
|
||||||
|
expect(store.items).toContain(clusterScopedObject1);
|
||||||
|
|
||||||
|
loadItems.mockImplementationOnce(() => [clusterScopedObject2]);
|
||||||
|
|
||||||
|
await store.loadAll({});
|
||||||
|
|
||||||
|
expect(store.items).not.toContain(clusterScopedObject1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -71,7 +71,7 @@ export interface MergeItemsOptions {
|
|||||||
export abstract class KubeObjectStore<T extends KubeObject> extends ItemStore<T> {
|
export abstract class KubeObjectStore<T extends KubeObject> extends ItemStore<T> {
|
||||||
static defaultContext = observable.box<ClusterContext>(); // TODO: support multiple cluster contexts
|
static defaultContext = observable.box<ClusterContext>(); // TODO: support multiple cluster contexts
|
||||||
|
|
||||||
public api: KubeApi<T>;
|
public readonly api: KubeApi<T>;
|
||||||
public readonly limit?: number;
|
public readonly limit?: number;
|
||||||
public readonly bufferSize: number = 50000;
|
public readonly bufferSize: number = 50000;
|
||||||
@observable private loadedNamespaces?: string[];
|
@observable private loadedNamespaces?: string[];
|
||||||
@ -276,7 +276,7 @@ export abstract class KubeObjectStore<T extends KubeObject> extends ItemStore<T>
|
|||||||
let items = partialItems;
|
let items = partialItems;
|
||||||
|
|
||||||
// update existing items
|
// update existing items
|
||||||
if (merge) {
|
if (merge && this.api.isNamespaced) {
|
||||||
const ns = new Set(namespaces);
|
const ns = new Set(namespaces);
|
||||||
|
|
||||||
items = [
|
items = [
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user