-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kludge "isEnabledForCluster" work again for cluster pages
Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
- Loading branch information
Showing
10 changed files
with
1,683 additions
and
7 deletions.
There are no files selected for viewing
1,424 changes: 1,424 additions & 0 deletions
1,424
...ension-api/__snapshots__/disable-cluster-pages-when-cluster-is-not-relevant.test.tsx.snap
Large diffs are not rendered by default.
Oops, something went wrong.
106 changes: 106 additions & 0 deletions
106
...aviours/cluster/extension-api/disable-cluster-pages-when-cluster-is-not-relevant.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** | ||
* 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 { getApplicationBuilder } from "../../../renderer/components/test-utils/get-application-builder"; | ||
import type { TestExtensionRenderer } from "../../../renderer/components/test-utils/get-extension-fake"; | ||
import { getExtensionFakeFor } from "../../../renderer/components/test-utils/get-extension-fake"; | ||
import type { KubernetesCluster } from "../../../common/catalog-entities"; | ||
import React from "react"; | ||
import extensionShouldBeEnabledForClusterFrameInjectable from "../../../renderer/extension-loader/extension-should-be-enabled-for-cluster-frame.injectable"; | ||
|
||
describe("disable-cluster-pages-when-cluster-is-not-relevant", () => { | ||
let builder: ApplicationBuilder; | ||
let rendered: RenderResult; | ||
let rendererTestExtension: TestExtensionRenderer; | ||
let isEnabledForClusterMock: AsyncFnMock<(cluster: KubernetesCluster) => Promise<boolean>>; | ||
|
||
beforeEach(async () => { | ||
builder = getApplicationBuilder(); | ||
|
||
builder.setEnvironmentToClusterFrame(); | ||
|
||
builder.dis.rendererDi.unoverride(extensionShouldBeEnabledForClusterFrameInjectable); | ||
|
||
const getExtensionFake = getExtensionFakeFor(builder); | ||
|
||
isEnabledForClusterMock = asyncFn(); | ||
|
||
const testExtension = getExtensionFake({ | ||
id: "test-extension-id", | ||
name: "test-extension", | ||
|
||
rendererOptions: { | ||
isEnabledForCluster: isEnabledForClusterMock, | ||
|
||
clusterPages: [{ | ||
components: { | ||
Page: () => <div data-testid="some-test-page">Some page</div>, | ||
}, | ||
}], | ||
}, | ||
}); | ||
|
||
rendererTestExtension = testExtension.renderer; | ||
|
||
rendered = await builder.render(); | ||
|
||
builder.extensions.enable(testExtension); | ||
}); | ||
|
||
describe("given not yet known if extension should be enabled for the cluster, when navigating", () => { | ||
beforeEach(() => { | ||
rendererTestExtension.navigate(); | ||
}); | ||
|
||
it("renders", () => { | ||
expect(rendered.baseElement).toMatchSnapshot(); | ||
}); | ||
|
||
it("does not show the page", () => { | ||
const actual = rendered.queryByTestId("some-test-page"); | ||
|
||
expect(actual).not.toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
describe("given extension shouldn't be enabled for the cluster, when navigating", () => { | ||
beforeEach(async () => { | ||
await isEnabledForClusterMock.resolve(false); | ||
|
||
rendererTestExtension.navigate(); | ||
}); | ||
|
||
it("renders", () => { | ||
expect(rendered.baseElement).toMatchSnapshot(); | ||
}); | ||
|
||
it("does not show the page", () => { | ||
const actual = rendered.queryByTestId("some-test-page"); | ||
|
||
expect(actual).not.toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
describe("given extension should be enabled for the cluster, when navigating", () => { | ||
beforeEach(async () => { | ||
await isEnabledForClusterMock.resolve(true); | ||
|
||
rendererTestExtension.navigate(); | ||
}); | ||
|
||
it("renders", () => { | ||
expect(rendered.baseElement).toMatchSnapshot(); | ||
}); | ||
|
||
it("shows the page", () => { | ||
const actual = rendered.getByTestId("some-test-page"); | ||
|
||
expect(actual).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
30 changes: 30 additions & 0 deletions
30
src/extensions/extension-loader/extension-is-enabled-for-cluster.injectable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Copyright (c) OpenLens Authors. All rights reserved. | ||
* Licensed under MIT License. See LICENSE in root directory for more information. | ||
*/ | ||
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable"; | ||
import type { KubernetesCluster } from "../../common/catalog-entities"; | ||
import type { LensRendererExtension } from "../lens-renderer-extension"; | ||
|
||
interface ExtensionIsEnabledForCluster { | ||
extension: LensRendererExtension; | ||
cluster: KubernetesCluster; | ||
} | ||
|
||
const extensionIsEnabledForClusterInjectable = getInjectable({ | ||
id: "extension-is-enabled-for-cluster", | ||
|
||
instantiate: async ( | ||
di, | ||
{ extension, cluster }: ExtensionIsEnabledForCluster, | ||
) => (await extension.isEnabledForCluster(cluster)) as boolean, | ||
|
||
lifecycle: lifecycleEnum.keyedSingleton({ | ||
getInstanceKey: ( | ||
di, | ||
{ extension, cluster }: ExtensionIsEnabledForCluster, | ||
) => `${extension.sanitizedExtensionId}-${cluster.getId()}`, | ||
}), | ||
}); | ||
|
||
export default extensionIsEnabledForClusterInjectable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/renderer/cluster-frame-context/active-kubernetes-cluster.injectable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Copyright (c) OpenLens Authors. All rights reserved. | ||
* Licensed under MIT License. See LICENSE in root directory for more information. | ||
*/ | ||
import { getInjectable } from "@ogre-tools/injectable"; | ||
import catalogEntityRegistryInjectable from "../api/catalog/entity/registry.injectable"; | ||
import { computed } from "mobx"; | ||
import { isKubernetesCluster } from "../../common/catalog-entities"; | ||
|
||
const activeKubernetesClusterInjectable = getInjectable({ | ||
id: "active-kubernetes-cluster", | ||
|
||
instantiate: (di) => { | ||
const catalogEntityRegistry = di.inject(catalogEntityRegistryInjectable); | ||
|
||
return computed(() => { | ||
const activeEntity = catalogEntityRegistry.activeEntity; | ||
|
||
if (!isKubernetesCluster(activeEntity)) { | ||
return null; | ||
} | ||
|
||
return activeEntity; | ||
}); | ||
}, | ||
}); | ||
|
||
export default activeKubernetesClusterInjectable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/renderer/extension-loader/extension-should-be-enabled-for-cluster-frame.injectable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Copyright (c) OpenLens Authors. All rights reserved. | ||
* Licensed under MIT License. See LICENSE in root directory for more information. | ||
*/ | ||
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable"; | ||
import { asyncComputed } from "@ogre-tools/injectable-react"; | ||
import type { LensRendererExtension } from "../../extensions/lens-renderer-extension"; | ||
import type { KubernetesCluster } from "../../common/catalog-entities"; | ||
import extensionIsEnabledForClusterInjectable from "../../extensions/extension-loader/extension-is-enabled-for-cluster.injectable"; | ||
import activeKubernetesClusterInjectable from "../cluster-frame-context/active-kubernetes-cluster.injectable"; | ||
import { untracked } from "mobx"; | ||
|
||
const extensionShouldBeEnabledForClusterFrameInjectable = getInjectable({ | ||
id: "extension-should-be-enabled-for-cluster-frame", | ||
|
||
instantiate: (di, extension: LensRendererExtension) => { | ||
const activeKubernetesCluster = di.inject(activeKubernetesClusterInjectable); | ||
|
||
const getExtensionIsEnabledForCluster = ( | ||
extension: LensRendererExtension, | ||
cluster: KubernetesCluster, | ||
) => | ||
untracked(() => di.inject(extensionIsEnabledForClusterInjectable, { extension, cluster })); | ||
|
||
return asyncComputed( | ||
async () => { | ||
const cluster = activeKubernetesCluster.get(); | ||
|
||
if (!cluster) { | ||
return false; | ||
} | ||
|
||
return getExtensionIsEnabledForCluster( | ||
extension, | ||
cluster, | ||
); | ||
}, | ||
|
||
false, | ||
); | ||
}, | ||
|
||
lifecycle: lifecycleEnum.keyedSingleton({ | ||
getInstanceKey: (di, extension: LensRendererExtension) => | ||
extension.sanitizedExtensionId, | ||
}), | ||
}); | ||
|
||
export default extensionShouldBeEnabledForClusterFrameInjectable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters