forked from colinhacks/zod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenum.test.ts
33 lines (28 loc) · 938 Bytes
/
enum.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// @ts-ignore TS6133
import { expect, test } from "@jest/globals";
import { util } from "../helpers/util";
import * as z from "../index";
test("create enum", () => {
const MyEnum = z.enum(["Red", "Green", "Blue"]);
expect(MyEnum.Values.Red).toEqual("Red");
expect(MyEnum.Enum.Red).toEqual("Red");
expect(MyEnum.enum.Red).toEqual("Red");
});
test("infer enum", () => {
const MyEnum = z.enum(["Red", "Green", "Blue"]);
type MyEnum = z.infer<typeof MyEnum>;
const t1: util.AssertEqual<MyEnum, "Red" | "Green" | "Blue"> = true;
[t1];
});
test("get options", () => {
expect(z.enum(["tuna", "trout"]).options).toEqual(["tuna", "trout"]);
});
test("readonly enum", () => {
const HTTP_SUCCESS = ["200", "201"] as const;
const arg = z.enum(HTTP_SUCCESS);
type arg = z.infer<typeof arg>;
const f1: util.AssertEqual<arg, "200" | "201"> = true;
f1;
arg.parse("201");
expect(() => arg.parse("202")).toThrow();
});