Skip to content

Commit

Permalink
Added test for enums with defaults (#1149)
Browse files Browse the repository at this point in the history
* Added test for enums with defaults

* Fix style

* Fix test.only

Co-authored-by: Colin McDonnell <colinmcd@alum.mit.edu>
  • Loading branch information
ElianCordoba and Colin McDonnell committed May 20, 2022
1 parent b57a25c commit 51e93e7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
21 changes: 21 additions & 0 deletions deno/lib/__tests__/default.test.ts
Expand Up @@ -106,3 +106,24 @@ test("chained defaults", () => {
test("factory", () => {
z.ZodDefault.create(z.string()).parse(undefined);
});

test("native enum", () => {
enum Fruits {
apple = "apple",
orange = "orange",
}

const schema = z.object({
fruit: z.nativeEnum(Fruits).default(Fruits.apple),
});

expect(schema.parse({})).toEqual({ fruit: Fruits.apple });
});

test("enum", () => {
const schema = z.object({
fruit: z.enum(["apple", "orange"]).default("apple"),
});

expect(schema.parse({})).toEqual({ fruit: "apple" });
});
20 changes: 20 additions & 0 deletions deno/lib/declarations.d.ts
@@ -0,0 +1,20 @@
interface Body<T> {
readonly body: ReadableStream<Uint8Array> | null;
readonly bodyUsed: boolean;
arrayBuffer(): Promise<ArrayBuffer>;
blob(): Promise<Blob>;
formData(): Promise<FormData>;
json(): Promise<T>;
text(): Promise<string>;
}

interface Response<T> extends Body<T> {
readonly headers: Headers;
readonly ok: boolean;
readonly redirected: boolean;
readonly status: number;
readonly statusText: string;
readonly type: ResponseType;
readonly url: string;
clone(): Response<T>;
}
21 changes: 21 additions & 0 deletions src/__tests__/default.test.ts
Expand Up @@ -105,3 +105,24 @@ test("chained defaults", () => {
test("factory", () => {
z.ZodDefault.create(z.string()).parse(undefined);
});

test("native enum", () => {
enum Fruits {
apple = "apple",
orange = "orange",
}

const schema = z.object({
fruit: z.nativeEnum(Fruits).default(Fruits.apple),
});

expect(schema.parse({})).toEqual({ fruit: Fruits.apple });
});

test("enum", () => {
const schema = z.object({
fruit: z.enum(["apple", "orange"]).default("apple"),
});

expect(schema.parse({})).toEqual({ fruit: "apple" });
});

0 comments on commit 51e93e7

Please sign in to comment.