1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/packages/core/src/common/initializable-state/create.test.ts
Sebastian Malton eafd1efbc0 chore: Remove InitializableState for loosing competition
Signed-off-by: Sebastian Malton <sebastian@malton.name>
2023-04-18 08:16:40 -04:00

83 lines
2.8 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 { Runnable } from "@k8slens/run-many";
import { runManyFor } from "@k8slens/run-many";
import type { DiContainer, InjectionToken } from "@ogre-tools/injectable";
import { createContainer, getInjectionToken } from "@ogre-tools/injectable";
import type { ImplInitializableInjectionTokensArgs, Initializable } from "./create";
import { getInjectablesForInitializable, getInitializable } from "./create";
describe("InitializableTokens technical tests", () => {
let di: DiContainer;
let initializableToken: Initializable<number>;
let phase: InjectionToken<Runnable<void>, void>;
beforeEach(() => {
di = createContainer("irrelevant");
initializableToken = getInitializable("some-root-id");
phase = getInjectionToken({ id: "some-runnable-phase" });
});
it("throws given attempting to inject the state token", () => {
expect(() => di.inject(initializableToken.stateToken)).toThrowErrorMatchingInlineSnapshot(
`"Tried to inject non-registered injectable "irrelevant" -> "some-root-id-state-token"."`,
);
});
describe("given some implementation for initializableToken is registered", () => {
let mockInit: AsyncFnMock<ImplInitializableInjectionTokensArgs<number>["init"]>;
beforeEach(() => {
mockInit = asyncFn();
const { initializationInjectable, stateInjectable } = getInjectablesForInitializable({
init: mockInit,
phase,
token: initializableToken,
});
di.register(initializationInjectable, stateInjectable);
});
it("throws given attempting to inject the state token", () => {
expect(() => di.inject(initializableToken.stateToken)).toThrowErrorMatchingInlineSnapshot(
`"Tried to inject "some-root-id" before initialization was complete"`,
);
});
describe("given the phase is started to be run", () => {
let runManyPromise: Promise<void>;
beforeEach(() => {
runManyPromise = runManyFor(di)(phase)();
});
it("throws given attempting to inject the state token", () => {
expect(() => di.inject(initializableToken.stateToken)).toThrowErrorMatchingInlineSnapshot(
`"Tried to inject "some-root-id" before initialization was complete"`,
);
});
describe("when initialization is complete", () => {
beforeEach(async () => {
await mockInit.resolve(10);
});
it("initializes the state", () => {
expect(di.inject(initializableToken.stateToken)).toBe(10);
});
it("allows the runMany to complete", async () => {
await runManyPromise;
});
});
});
});
});