1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/common/initializable-state/create.test.ts
2022-09-15 17:20:58 -04:00

78 lines
2.3 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 { DiContainer, Injectable } from "@ogre-tools/injectable";
import { runInAction } from "mobx";
import { getDiForUnitTesting } from "../../main/getDiForUnitTesting";
import type { InitializableState } from "./create";
import { createInitializableState } from "./create";
describe("InitializableState tests", () => {
let di: DiContainer;
beforeEach(() => {
di = getDiForUnitTesting({ doGeneralOverrides: true });
});
describe("when created", () => {
let stateInjectable: Injectable<InitializableState<number>, unknown, void>;
let initMock: AsyncFnMock<() => number>;
beforeEach(() => {
initMock = asyncFn();
stateInjectable = createInitializableState({
id: "my-state",
init: initMock,
});
runInAction(() => {
di.register(stateInjectable);
});
});
describe("when injected", () => {
let state: InitializableState<number>;
beforeEach(() => {
state = di.inject(stateInjectable);
});
it("when get is called, throw", () => {
expect(() => state.get()).toThrowError("InitializableState(my-state) has not been initialized yet");
});
describe("when init is called", () => {
beforeEach(() => {
state.init();
});
it("should call provided initialization function", () => {
expect(initMock).toBeCalled();
});
it("when get is called, throw", () => {
expect(() => state.get()).toThrowError("InitializableState(my-state) has not finished initializing");
});
describe("when initialization resolves", () => {
beforeEach(async () => {
await initMock.resolve(42);
});
it("when get is called, returns value", () => {
expect(state.get()).toBe(42);
});
it("when init is called again, throws", async () => {
await expect(() => state.init()).rejects.toThrow("Cannot initialize InitializableState(my-state) more than once");
});
});
});
});
});
});