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

Make stuff happening when root frame is rendered unit testable

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2022-05-19 14:35:03 +03:00
parent db60d1dace
commit eee8fb49e7
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
9 changed files with 117 additions and 5 deletions

View File

@ -7,7 +7,7 @@ import type { Channel } from "./channel-injection-token";
export type SendToAgnosticChannel = <TChannel extends Channel<unknown>>(
channel: TChannel,
message: TChannel["_messageTemplate"]
message?: TChannel["_messageTemplate"]
) => void;
export const sendToAgnosticChannelInjectionToken =

View File

@ -0,0 +1,21 @@
/**
* 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 type { Channel } from "../channel/channel-injection-token";
import { channelInjectionToken } from "../channel/channel-injection-token";
export type RootFrameRenderedChannel = Channel<void>;
const rootFrameRenderedChannelInjectable = getInjectable({
id: "root-frame-rendered-channel",
instantiate: (): RootFrameRenderedChannel => ({
id: "root-frame-rendered",
}),
injectionToken: channelInjectionToken,
});
export default rootFrameRenderedChannelInjectable;

View File

@ -17,6 +17,8 @@ const catalogSyncToRendererInjectable = getInjectable({
startCatalogSyncToRenderer(catalogEntityRegistry),
);
},
causesSideEffects: true,
});
export default catalogSyncToRendererInjectable;

View File

@ -82,6 +82,8 @@ import publishIsConfiguredInjectable from "./update-app/publish-is-configured.in
import checkForPlatformUpdatesInjectable from "./update-app/check-for-platform-updates/check-for-platform-updates.injectable";
import setUpdateOnQuitInjectable from "./electron-app/features/set-update-on-quit.injectable";
import downloadPlatformUpdateInjectable from "./update-app/download-platform-update/download-platform-update.injectable";
import startCatalogSyncInjectable from "./catalog-sync-to-renderer/start-catalog-sync.injectable";
import startKubeConfigSyncInjectable from "./start-main-application/runnables/kube-config-sync/start-kube-config-sync.injectable";
export function getDiForUnitTesting(opts: GetDiForUnitTestingOptions = {}) {
const {
@ -190,6 +192,8 @@ const overrideRunnablesHavingSideEffects = (di: DiContainer) => {
setupSystemCaInjectable,
setupListenerForCurrentClusterFrameInjectable,
setupRunnablesAfterWindowIsOpenedInjectable,
startCatalogSyncInjectable,
startKubeConfigSyncInjectable,
].forEach((injectable) => {
di.override(injectable, () => ({ run: () => {} }));
});

View File

@ -25,6 +25,8 @@ const startKubeConfigSyncInjectable = getInjectable({
};
},
causesSideEffects: true,
injectionToken: afterRootFrameIsReadyInjectionToken,
});

View File

@ -0,0 +1,35 @@
/**
* 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 { channelListenerInjectionToken } from "../../../../common/channel/channel-listener-injection-token";
import rootFrameRenderedChannelInjectable from "../../../../common/root-frame-rendered-channel/root-frame-rendered-channel.injectable";
import { runManyFor } from "../../../../common/runnable/run-many-for";
import { afterRootFrameIsReadyInjectionToken } from "../../runnable-tokens/after-root-frame-is-ready-injection-token";
const rootFrameRenderedChannelListenerInjectable = getInjectable({
id: "root-frame-rendered-channel-listener",
instantiate: (di) => {
const channel = di.inject(rootFrameRenderedChannelInjectable);
const runMany = runManyFor(di);
const runRunnablesAfterRootFrameIsReady = runMany(
afterRootFrameIsReadyInjectionToken,
);
return {
channel,
handler: async () => {
await runRunnablesAfterRootFrameIsReady();
},
};
},
injectionToken: channelListenerInjectionToken,
});
export default rootFrameRenderedChannelListenerInjectable;

View File

@ -48,6 +48,7 @@ import type { TrayMenuItem } from "../../../main/tray/tray-menu-item/tray-menu-i
import electronTrayInjectable from "../../../main/tray/electron-tray/electron-tray.injectable";
import applicationWindowInjectable from "../../../main/start-main-application/lens-window/application-window/application-window.injectable";
import { Notifications } from "../notifications/notifications";
import notifyThatRootFrameIsRenderedInjectable from "../../frames/root-frame/notify-that-root-frame-is-rendered.injectable";
type Callback = (dis: DiContainers) => void | Promise<void>;
@ -89,6 +90,7 @@ interface DiContainers {
interface Environment {
renderSidebar: () => React.ReactNode;
beforeRender: () => void;
onAllowKubeResource: () => void;
}
@ -119,6 +121,12 @@ export const getApplicationBuilder = () => {
application: {
renderSidebar: () => null,
beforeRender: () => {
const nofifyThatRootFrameIsRendered = rendererDi.inject(notifyThatRootFrameIsRenderedInjectable);
nofifyThatRootFrameIsRendered();
},
onAllowKubeResource: () => {
throw new Error(
"Tried to allow kube resource when environment is not cluster frame.",
@ -128,6 +136,7 @@ export const getApplicationBuilder = () => {
clusterFrame: {
renderSidebar: () => <Sidebar />,
beforeRender: () => {},
onAllowKubeResource: () => {},
} as Environment,
};
@ -380,6 +389,8 @@ export const getApplicationBuilder = () => {
await callback(dis);
}
environment.beforeRender();
rendered = render(
<Router history={history}>
{environment.renderSidebar()}

View File

@ -0,0 +1,22 @@
/**
* 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 { sendToAgnosticChannelInjectionToken } from "../../../common/channel/send-to-agnostic-channel-injection-token";
import rootFrameRenderedChannelInjectable from "../../../common/root-frame-rendered-channel/root-frame-rendered-channel.injectable";
const notifyThatRootFrameIsRenderedInjectable = getInjectable({
id: "notify-that-root-frame-is-rendered",
instantiate: (di) => {
const sendToAgnosticChannel = di.inject(sendToAgnosticChannelInjectionToken);
const rootFrameRenderedChannel = di.inject(rootFrameRenderedChannelInjectable);
return () => {
sendToAgnosticChannel(rootFrameRenderedChannel);
};
},
});
export default notifyThatRootFrameIsRenderedInjectable;

View File

@ -11,17 +11,21 @@ import { ErrorBoundary } from "../../components/error-boundary";
import { Notifications } from "../../components/notifications";
import { ConfirmDialog } from "../../components/confirm-dialog";
import { CommandContainer } from "../../components/command-palette/command-container";
import { ipcRenderer } from "electron";
import { IpcRendererNavigationEvents } from "../../navigation/events";
import { withInjectables } from "@ogre-tools/injectable-react";
import notifyThatRootFrameIsRenderedInjectable from "./notify-that-root-frame-is-rendered.injectable";
injectSystemCAs();
interface Dependencies {
notifyThatRootFrameIsRendered: () => void;
}
@observer
export class RootFrame extends React.Component {
class NonInjectedRootFrame extends React.Component<Dependencies> {
static displayName = "RootFrame";
componentDidMount() {
ipcRenderer.send(IpcRendererNavigationEvents.LOADED);
this.props.notifyThatRootFrameIsRendered();
}
render() {
@ -37,3 +41,14 @@ export class RootFrame extends React.Component {
);
}
}
export const RootFrame = withInjectables<Dependencies>(
NonInjectedRootFrame,
{
getProps: (di, props) => ({
notifyThatRootFrameIsRendered: di.inject(notifyThatRootFrameIsRenderedInjectable),
...props,
}),
},
);