1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/packages/technical-features/application/electron-main/src/starting-of-electron-main-application.test.ts
Sebastian Malton a63f737adb
Change typing for after/before window/application start (#7342)
- Now the callback is provided with an object containing either
  `mainDi` or `windowDi` fields. This should help with confusion
  over which environment the `di` is for

Signed-off-by: Sebastian Malton <sebastian@malton.name>
2023-03-13 15:35:33 +02:00

90 lines
3.0 KiB
TypeScript

import { createContainer, DiContainer, getInjectable } from "@ogre-tools/injectable";
import { registerFeature } from "@k8slens/feature-core";
import { applicationFeatureForElectronMain } from "./feature";
import {
beforeApplicationIsLoadingInjectionToken,
startApplicationInjectionToken,
} from "@k8slens/application";
import asyncFn, { AsyncFnMock } from "@async-fn/jest";
import whenAppIsReadyInjectable from "./start-application/when-app-is-ready.injectable";
import * as timeSlots from "./start-application/time-slots";
describe("starting-of-electron-main-application", () => {
let di: DiContainer;
let beforeAnythingMock: jest.Mock;
let beforeElectronIsReadyMock: jest.Mock;
let beforeApplicationIsLoadingMock: AsyncFnMock<() => Promise<void>>;
let whenAppIsReadyMock: AsyncFnMock<() => Promise<void>>;
beforeEach(() => {
di = createContainer("irrelevant");
beforeAnythingMock = jest.fn();
beforeElectronIsReadyMock = jest.fn();
beforeApplicationIsLoadingMock = asyncFn();
whenAppIsReadyMock = asyncFn();
registerFeature(di, applicationFeatureForElectronMain);
const beforeAnythingIsLoadingInjectable = getInjectable({
id: "before-anything",
instantiate: () => ({ run: beforeAnythingMock }),
injectionToken: timeSlots.beforeAnythingInjectionToken,
});
const beforeElectronIsReadyIsLoadingInjectable = getInjectable({
id: "before-electron-is-ready",
instantiate: () => ({ run: beforeElectronIsReadyMock }),
injectionToken: timeSlots.beforeElectronIsReadyInjectionToken,
});
const beforeApplicationIsLoadingInjectable = getInjectable({
id: "before-application-is-loading",
instantiate: () => ({ run: beforeApplicationIsLoadingMock }),
injectionToken: beforeApplicationIsLoadingInjectionToken,
});
di.register(
beforeAnythingIsLoadingInjectable,
beforeElectronIsReadyIsLoadingInjectable,
beforeApplicationIsLoadingInjectable,
);
di.override(whenAppIsReadyInjectable, () => whenAppIsReadyMock);
});
describe("when application is started", () => {
beforeEach(() => {
const startApplication = di.inject(startApplicationInjectionToken);
void startApplication();
});
it("calls for synchronous runnables for before anything", () => {
expect(beforeAnythingMock).toHaveBeenCalled();
});
it("calls for synchronous runnables for before electron is ready", () => {
expect(beforeElectronIsReadyMock).toHaveBeenCalled();
});
it("calls to wait when electron is ready", () => {
expect(whenAppIsReadyMock).toHaveBeenCalled();
});
it("does not call runnables for before application is loading yet", () => {
expect(beforeApplicationIsLoadingMock).not.toHaveBeenCalled();
});
describe("when electron is ready", () => {
beforeEach(async () => {
await whenAppIsReadyMock.resolve();
});
it("calls runnables for before application is loading", () => {
expect(beforeApplicationIsLoadingMock).toHaveBeenCalled();
});
});
});
});