import { expect, test } from "vitest"; import * as t from "../index"; test("converts to typescript", () => { expect(t.toTypescript(t.map(t.num, t.bool))).toEqual("Map"); }); test("can't convert to JSON Schema", () => { expect(() => { t.toJSONSchema("bye", t.map(t.num, t.bool)) }).toThrow(); }); test("accepts empty maps", () => { const check = t.map(t.num, t.str); check.assert(new Map()); }); test("accepts maps where keys and values match checks", () => { const check = t.map(t.num, t.str); const map = new Map(); map.set(1, "hi"); check.assert(map); }); test("rejects maps where keys mismatch", () => { const check = t.map(t.num, t.str); const map = new Map(); map.set("foo", "bar"); expect(() => { check.assert(map); }).toThrow(); }); test("rejects maps where values mismatch", () => { const check = t.map(t.num, t.str); const map = new Map(); map.set(0, 1); expect(() => { check.assert(map); }).toThrow(); }); test("rejects non-maps", () => { const check = t.map(t.any, t.any); expect(() => { check.assert(null); }).toThrow(); }); test("sliced nested objects", () => { const nested = t.subtype({ id: t.str }); const check = t.map(t.str, nested); const map = new Map>(); map.set("hello", { id: "world", name: "blarg", } as t.GetType); const result = check.slice(map); const data: any = result.get("hello"); expect(data["name"]).toBeUndefined(); }); test("nested slicing returns the property value that passed validation", () => { const check = t.map(t.str, t.subtype({ id: t.str })); let reads = 0; const value = Object.defineProperty({}, "id", { enumerable: true, get() { reads += 1; return reads === 1 ? "valid" : 5; }, }); expect(check.slice(new Map([[ "key", value ]])).get("key")).toEqual({ id: "valid" }); expect(reads).toBe(1); });