import type { EnumOrObject, EnumValues, NormalEnum, TSNumericEnum } from "#app/@types/enum-types"; import type { enumValueToKey, getEnumKeys, getEnumValues } from "#app/utils/enums"; 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; describe("Enum Type Helpers", () => { describe("EnumValues", () => { it("should go from enum object type to value type", () => { expectTypeOf>().toEqualTypeOf(); expectTypeOf>().branded.toEqualTypeOf<1 | 2>(); expectTypeOf>().toEqualTypeOf(); expectTypeOf>().toEqualTypeOf(); expectTypeOf>().toMatchTypeOf<"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().toMatchTypeOf(); expectTypeOf().toMatchTypeOf(); expectTypeOf().toMatchTypeOf(); expectTypeOf().toMatchTypeOf(); }); it("should not match an enum value union w/o typeof", () => { expectTypeOf().not.toMatchTypeOf(); expectTypeOf().not.toMatchTypeOf(); }); it("should be equivalent to `TSNumericEnum | NormalEnum`", () => { expectTypeOf().branded.toEqualTypeOf | NormalEnum>(); }); }); }); describe("Enum Functions", () => { describe("getEnumKeys", () => { it("should retrieve keys of numeric enum", () => { expectTypeOf>().returns.toEqualTypeOf<("testN1" | "testN2")[]>(); }); }); 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">(); }); }); });