Files
lens/src/features/cluster/kube-object-details/extension-api/disable-kube-object-detail-items-when-cluster-is-not-relevant.test.tsx
T
Sebastian Malton 83d2b12cca Refactor kube detail components and add KubeEvents automatically to all custom resources (#6468)
* Support Events on CustomResourece details panel

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Remove unnecessary check

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Remove legacy KubeObjectMeta use from non-metrics details

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Change orderNumber of default KubeMetaDetails

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Remove last use of legacy global getActiveClusterEntity

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Refactor ResourceMetrics to use IAsyncComputed

- Introduce first use for namespace metrics

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Introduce metrics details item for Ingress

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Remove legacyStore for nodes

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Remove legacyStore for ingresses

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Replace NodeMetrics in details with injectable

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Change namespace metrics details to use more injectables

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Change ingress metrics details to use more injectables

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Change PersistentVolumeClaim metrics to be injectable

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Change DaemonSet metrics to be injectable

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Change Deployment metrics to be injectable

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Change Job metrics to be injectable

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Convert Pod metrics to be injectable

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Make ReplicaSet metrics details injectable

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Make StatefulSet metrics component injectable

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Remove dead code

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Introduce fix for metrics components visible on all details

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Use the new and correct enabled check

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Remove legacy global for daemonSetStore

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Switch components to be static to help with React performance

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Remove legacy store of CronJobs

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Make DeploymentReplicaSets injectable to fix build error

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix up remove dead code

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Allow use of ResourceMetrics without IAsyncComputed

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix metrics not updating correctly

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Update snapshots because moving KubeObjectMeta out of CustomResourceDetails

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Update more snapshots

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix test failures due to newer dep versions

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix type errors from new asyncComputed

Signed-off-by: Sebastian Malton <sebastian@malton.name>

Signed-off-by: Sebastian Malton <sebastian@malton.name>
2022-12-15 10:14:00 -05:00

167 lines
5.4 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { AsyncFnMock } from "@async-fn/jest";
import asyncFn from "@async-fn/jest";
import type { RenderResult } from "@testing-library/react";
import type { ApplicationBuilder } from "../../../../renderer/components/test-utils/get-application-builder";
import type { KubernetesCluster } from "../../../../common/catalog-entities";
import { getApplicationBuilder } from "../../../../renderer/components/test-utils/get-application-builder";
import { getInjectable } from "@ogre-tools/injectable";
import { frontEndRouteInjectionToken } from "../../../../common/front-end-routing/front-end-route-injection-token";
import { computed, runInAction } from "mobx";
import React from "react";
import { navigateToRouteInjectionToken } from "../../../../common/front-end-routing/navigate-to-route-injection-token";
import { routeSpecificComponentInjectionToken } from "../../../../renderer/routes/route-specific-component-injection-token";
import { KubeObject } from "../../../../common/k8s-api/kube-object";
import extensionShouldBeEnabledForClusterFrameInjectable from "../../../../renderer/extension-loader/extension-should-be-enabled-for-cluster-frame.injectable";
import apiManagerInjectable from "../../../../common/k8s-api/api-manager/manager.injectable";
import { KubeObjectDetails } from "../../../../renderer/components/kube-object-details";
import type { ApiManager } from "../../../../common/k8s-api/api-manager";
import type { KubeObjectStore } from "../../../../common/k8s-api/kube-object.store";
describe("disable kube object detail items when cluster is not relevant", () => {
let builder: ApplicationBuilder;
let rendered: RenderResult;
let isEnabledForClusterMock: AsyncFnMock<
(cluster: KubernetesCluster) => Promise<boolean>
>;
beforeEach(async () => {
builder = getApplicationBuilder();
builder.setEnvironmentToClusterFrame();
builder.beforeWindowStart((windowDi) => {
windowDi.override(apiManagerInjectable, () => ({
getStore: () => ({
loadFromPath: async () => getKubeObjectStub("some-kind", "some-api-version"),
}) as Partial<KubeObjectStore> as KubeObjectStore,
}) as Partial<ApiManager> as ApiManager);
windowDi.unoverride(extensionShouldBeEnabledForClusterFrameInjectable);
runInAction(() => {
windowDi.register(testRouteInjectable, testRouteComponentInjectable);
});
});
isEnabledForClusterMock = asyncFn();
const testExtension = {
id: "test-extension-id",
name: "test-extension",
rendererOptions: {
isEnabledForCluster: isEnabledForClusterMock,
kubeObjectDetailItems: [
{
kind: "some-kind",
apiVersions: ["some-api-version"],
components: {
Details: () => (
<div data-testid="some-kube-object-detail-item">
Some detail
</div>
),
},
},
],
},
};
rendered = await builder.render();
const windowDi = builder.applicationWindow.only.di;
const navigateToRoute = windowDi.inject(navigateToRouteInjectionToken);
const testRoute = windowDi.inject(testRouteInjectable);
navigateToRoute(testRoute);
builder.extensions.enable(testExtension);
});
describe("given not yet known if extension should be enabled for the cluster", () => {
it("renders", () => {
expect(rendered.baseElement).toMatchSnapshot();
});
it("does not show the kube object detail item", () => {
const actual = rendered.queryByTestId("some-kube-object-detail-item");
expect(actual).not.toBeInTheDocument();
});
});
describe("given extension shouldn't be enabled for the cluster", () => {
beforeEach(async () => {
await isEnabledForClusterMock.resolve(false);
});
it("renders", () => {
expect(rendered.baseElement).toMatchSnapshot();
});
it("does not show the kube object detail item", () => {
const actual = rendered.queryByTestId("some-kube-object-detail-item");
expect(actual).not.toBeInTheDocument();
});
});
describe("given extension should be enabled for the cluster", () => {
beforeEach(async () => {
await isEnabledForClusterMock.resolve(true);
});
it("renders", () => {
expect(rendered.baseElement).toMatchSnapshot();
});
it("shows the kube object detail item", () => {
const actual = rendered.getByTestId("some-kube-object-detail-item");
expect(actual).toBeInTheDocument();
});
});
});
const testRouteInjectable = getInjectable({
id: "test-route",
instantiate: () => ({
path: "/test-route",
clusterFrame: true,
isEnabled: computed(() => true),
}),
injectionToken: frontEndRouteInjectionToken,
});
const testRouteComponentInjectable = getInjectable({
id: "test-route-component",
instantiate: (di) => ({
route: di.inject(testRouteInjectable),
Component: () => <KubeObjectDetails />,
}),
injectionToken: routeSpecificComponentInjectionToken,
});
const getKubeObjectStub = (kind: string, apiVersion: string) =>
KubeObject.create({
apiVersion,
kind,
metadata: {
uid: "some-uid",
name: "some-name",
resourceVersion: "some-resource-version",
namespace: "some-namespace",
selfLink: "",
},
});