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:
parent
db60d1dace
commit
eee8fb49e7
@ -7,7 +7,7 @@ import type { Channel } from "./channel-injection-token";
|
|||||||
|
|
||||||
export type SendToAgnosticChannel = <TChannel extends Channel<unknown>>(
|
export type SendToAgnosticChannel = <TChannel extends Channel<unknown>>(
|
||||||
channel: TChannel,
|
channel: TChannel,
|
||||||
message: TChannel["_messageTemplate"]
|
message?: TChannel["_messageTemplate"]
|
||||||
) => void;
|
) => void;
|
||||||
|
|
||||||
export const sendToAgnosticChannelInjectionToken =
|
export const sendToAgnosticChannelInjectionToken =
|
||||||
|
|||||||
@ -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;
|
||||||
@ -17,6 +17,8 @@ const catalogSyncToRendererInjectable = getInjectable({
|
|||||||
startCatalogSyncToRenderer(catalogEntityRegistry),
|
startCatalogSyncToRenderer(catalogEntityRegistry),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
causesSideEffects: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
export default catalogSyncToRendererInjectable;
|
export default catalogSyncToRendererInjectable;
|
||||||
|
|||||||
@ -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 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 setUpdateOnQuitInjectable from "./electron-app/features/set-update-on-quit.injectable";
|
||||||
import downloadPlatformUpdateInjectable from "./update-app/download-platform-update/download-platform-update.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 = {}) {
|
export function getDiForUnitTesting(opts: GetDiForUnitTestingOptions = {}) {
|
||||||
const {
|
const {
|
||||||
@ -190,6 +192,8 @@ const overrideRunnablesHavingSideEffects = (di: DiContainer) => {
|
|||||||
setupSystemCaInjectable,
|
setupSystemCaInjectable,
|
||||||
setupListenerForCurrentClusterFrameInjectable,
|
setupListenerForCurrentClusterFrameInjectable,
|
||||||
setupRunnablesAfterWindowIsOpenedInjectable,
|
setupRunnablesAfterWindowIsOpenedInjectable,
|
||||||
|
startCatalogSyncInjectable,
|
||||||
|
startKubeConfigSyncInjectable,
|
||||||
].forEach((injectable) => {
|
].forEach((injectable) => {
|
||||||
di.override(injectable, () => ({ run: () => {} }));
|
di.override(injectable, () => ({ run: () => {} }));
|
||||||
});
|
});
|
||||||
|
|||||||
@ -25,6 +25,8 @@ const startKubeConfigSyncInjectable = getInjectable({
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
|
causesSideEffects: true,
|
||||||
|
|
||||||
injectionToken: afterRootFrameIsReadyInjectionToken,
|
injectionToken: afterRootFrameIsReadyInjectionToken,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -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;
|
||||||
@ -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 electronTrayInjectable from "../../../main/tray/electron-tray/electron-tray.injectable";
|
||||||
import applicationWindowInjectable from "../../../main/start-main-application/lens-window/application-window/application-window.injectable";
|
import applicationWindowInjectable from "../../../main/start-main-application/lens-window/application-window/application-window.injectable";
|
||||||
import { Notifications } from "../notifications/notifications";
|
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>;
|
type Callback = (dis: DiContainers) => void | Promise<void>;
|
||||||
|
|
||||||
@ -89,6 +90,7 @@ interface DiContainers {
|
|||||||
|
|
||||||
interface Environment {
|
interface Environment {
|
||||||
renderSidebar: () => React.ReactNode;
|
renderSidebar: () => React.ReactNode;
|
||||||
|
beforeRender: () => void;
|
||||||
onAllowKubeResource: () => void;
|
onAllowKubeResource: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,6 +121,12 @@ export const getApplicationBuilder = () => {
|
|||||||
application: {
|
application: {
|
||||||
renderSidebar: () => null,
|
renderSidebar: () => null,
|
||||||
|
|
||||||
|
beforeRender: () => {
|
||||||
|
const nofifyThatRootFrameIsRendered = rendererDi.inject(notifyThatRootFrameIsRenderedInjectable);
|
||||||
|
|
||||||
|
nofifyThatRootFrameIsRendered();
|
||||||
|
},
|
||||||
|
|
||||||
onAllowKubeResource: () => {
|
onAllowKubeResource: () => {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"Tried to allow kube resource when environment is not cluster frame.",
|
"Tried to allow kube resource when environment is not cluster frame.",
|
||||||
@ -128,6 +136,7 @@ export const getApplicationBuilder = () => {
|
|||||||
|
|
||||||
clusterFrame: {
|
clusterFrame: {
|
||||||
renderSidebar: () => <Sidebar />,
|
renderSidebar: () => <Sidebar />,
|
||||||
|
beforeRender: () => {},
|
||||||
onAllowKubeResource: () => {},
|
onAllowKubeResource: () => {},
|
||||||
} as Environment,
|
} as Environment,
|
||||||
};
|
};
|
||||||
@ -380,6 +389,8 @@ export const getApplicationBuilder = () => {
|
|||||||
await callback(dis);
|
await callback(dis);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
environment.beforeRender();
|
||||||
|
|
||||||
rendered = render(
|
rendered = render(
|
||||||
<Router history={history}>
|
<Router history={history}>
|
||||||
{environment.renderSidebar()}
|
{environment.renderSidebar()}
|
||||||
|
|||||||
@ -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;
|
||||||
@ -11,17 +11,21 @@ import { ErrorBoundary } from "../../components/error-boundary";
|
|||||||
import { Notifications } from "../../components/notifications";
|
import { Notifications } from "../../components/notifications";
|
||||||
import { ConfirmDialog } from "../../components/confirm-dialog";
|
import { ConfirmDialog } from "../../components/confirm-dialog";
|
||||||
import { CommandContainer } from "../../components/command-palette/command-container";
|
import { CommandContainer } from "../../components/command-palette/command-container";
|
||||||
import { ipcRenderer } from "electron";
|
import { withInjectables } from "@ogre-tools/injectable-react";
|
||||||
import { IpcRendererNavigationEvents } from "../../navigation/events";
|
import notifyThatRootFrameIsRenderedInjectable from "./notify-that-root-frame-is-rendered.injectable";
|
||||||
|
|
||||||
injectSystemCAs();
|
injectSystemCAs();
|
||||||
|
|
||||||
|
interface Dependencies {
|
||||||
|
notifyThatRootFrameIsRendered: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
@observer
|
@observer
|
||||||
export class RootFrame extends React.Component {
|
class NonInjectedRootFrame extends React.Component<Dependencies> {
|
||||||
static displayName = "RootFrame";
|
static displayName = "RootFrame";
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
ipcRenderer.send(IpcRendererNavigationEvents.LOADED);
|
this.props.notifyThatRootFrameIsRendered();
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
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,
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user