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
Janne Savolainen 0d480917cf
Simplify test setup by removing option for global overrides (#7302)
* Remove option to doGeneralOverrides and do it always

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Override telemetry by default to optimize and simplify testing

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Simplify more usages of getDiForUnitTesting

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Fix code style

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

---------

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
2023-03-07 12:23:57 -05:00

78 lines
2.2 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();
});
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");
});
});
});
});
});
});