import type { enumValueToKey, getEnumKeys, getEnumValues } from "#app/utils/enums"; import type { EnumOrObject, NormalEnum, TSNumericEnum } from "#types/enum-types"; import type { ObjectValues } from "#types/type-helpers"; import { describe, expectTypeOf, it } from "vitest"; enum testEnumNum { testN1 = 1, testN2 = 2, } enum testEnumString { testS1 = "apple", testS2 = "banana", } const testObjNum = { testON1: 1, testON2: 2 } as const; const testObjString = { testOS1: "apple", testOS2: "banana" } as const; interface testObject { key_1: "1"; key_2: "2"; key_3: "3"; } describe("Enum Type Helpers", () => { describe("ObjectValues", () => { it("should produce a union of an object's values", () => { expectTypeOf>().toEqualTypeOf<"1" | "2" | "3">(); }); it("should go from enum object type to value type", () => { expectTypeOf>().toEqualTypeOf(); expectTypeOf>().branded.toEqualTypeOf<1 | 2>(); expectTypeOf>().toEqualTypeOf(); expectTypeOf>().toEqualTypeOf< testEnumString.testS1 | testEnumString.testS2 >(); expectTypeOf>().toExtend<"apple" | "banana">(); }); it("should produce union of const object values as type", () => { expectTypeOf>().toEqualTypeOf<1 | 2>(); expectTypeOf>().toEqualTypeOf<"apple" | "banana">(); }); }); describe("TSNumericEnum", () => { it("should match numeric enums", () => { expectTypeOf>().toEqualTypeOf(); }); it("should not match string enums or const objects", () => { expectTypeOf>().toBeNever(); expectTypeOf>().toBeNever(); expectTypeOf>().toBeNever(); }); }); describe("NormalEnum", () => { it("should match string enums or const objects", () => { expectTypeOf>().toEqualTypeOf(); expectTypeOf>().toEqualTypeOf(); expectTypeOf>().toEqualTypeOf(); }); it("should not match numeric enums", () => { expectTypeOf>().toBeNever(); }); }); describe("EnumOrObject", () => { it("should match any enum or const object", () => { expectTypeOf().toExtend(); expectTypeOf().toExtend(); expectTypeOf().toExtend(); expectTypeOf().toExtend(); }); it("should not match an enum value union w/o typeof", () => { expectTypeOf().not.toExtend(); expectTypeOf().not.toExtend(); }); it("should be equivalent to `TSNumericEnum | NormalEnum`", () => { expectTypeOf().toEqualTypeOf | NormalEnum>(); }); }); }); describe("Enum Functions", () => { describe("getEnumKeys", () => { it("should retrieve keys of numeric enum", () => { expectTypeOf>().returns.toEqualTypeOf<("testN1" | "testN2")[]>(); expectTypeOf>().returns.toEqualTypeOf<("testON1" | "testON2")[]>(); }); }); describe("getEnumValues", () => { it("should retrieve values of numeric enum", () => { expectTypeOf>().returns.branded.toEqualTypeOf<(1 | 2)[]>(); }); }); describe("enumValueToKey", () => { it("should retrieve values for a given key", () => { expectTypeOf< typeof enumValueToKey >().returns.toEqualTypeOf<"testS1">(); expectTypeOf>().returns.toEqualTypeOf< "testS1" | "testS2" >(); expectTypeOf>().returns.toEqualTypeOf<"testON1">(); expectTypeOf>().returns.toEqualTypeOf<"testON1" | "testON2">(); }); }); });