mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Add techincal tests
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
c6d1e77237
commit
2de7061ad0
77
src/common/initializable-state/create.test.ts
Normal file
77
src/common/initializable-state/create.test.ts
Normal file
@ -0,0 +1,77 @@
|
||||
/**
|
||||
* 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");
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -17,7 +17,9 @@ export interface InitializableState<T> {
|
||||
init: () => Promise<void>;
|
||||
}
|
||||
|
||||
type InitializableStateValue<T> = { set: false } | { set: true; value: T };
|
||||
type InitializableStateValue<T> =
|
||||
| { set: false }
|
||||
| { set: true; value: T } ;
|
||||
|
||||
export function createInitializableState<T>(args: CreateInitializableStateArgs<T>): Injectable<InitializableState<T>, unknown, void> {
|
||||
const { id, init, injectionToken } = args;
|
||||
@ -33,7 +35,7 @@ export function createInitializableState<T>(args: CreateInitializableStateArgs<T
|
||||
return {
|
||||
init: async () => {
|
||||
if (initCalled) {
|
||||
throw new Error(`Cannot initialized AsyncSyncBox ${id}) more than once`);
|
||||
throw new Error(`Cannot initialize InitializableState(${id}) more than once`);
|
||||
}
|
||||
|
||||
initCalled = true;
|
||||
@ -44,11 +46,11 @@ export function createInitializableState<T>(args: CreateInitializableStateArgs<T
|
||||
},
|
||||
get: () => {
|
||||
if (!initCalled) {
|
||||
throw new Error(`AsyncSyncBox(${id}) has not been initialized yet`);
|
||||
throw new Error(`InitializableState(${id}) has not been initialized yet`);
|
||||
}
|
||||
|
||||
if (!box.set) {
|
||||
throw new Error(`AsyncSyncBox(${id}) has not finished initializing`);
|
||||
if (box.set === false) {
|
||||
throw new Error(`InitializableState(${id}) has not finished initializing`);
|
||||
}
|
||||
|
||||
return box.value;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user