Skip to content

Commit

Permalink
test: additional unit-tests for object (#1729)
Browse files Browse the repository at this point in the history
* test: add unit-tests covering object extend method

* test: add explicit testing of shape method

* Fix lint

---------

Co-authored-by: Colin McDonnell <colinmcd@alum.mit.edu>
Co-authored-by: Colin McDonnell <colinmcd94@gmail.com>
  • Loading branch information
3 people committed Feb 8, 2023
1 parent 1e5b29e commit 767ab1b
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
51 changes: 51 additions & 0 deletions deno/lib/__tests__/object.test.ts
Expand Up @@ -29,6 +29,18 @@ test("unknown throw", () => {
expect(() => Test.parse(asdf)).toThrow();
});

test("shape() should return schema of particular key", () => {
const f1Schema = Test.shape.f1;
const f2Schema = Test.shape.f2;
const f3Schema = Test.shape.f3;
const f4Schema = Test.shape.f4;

expect(f1Schema).toBeInstanceOf(z.ZodNumber);
expect(f2Schema).toBeInstanceOf(z.ZodOptional);
expect(f3Schema).toBeInstanceOf(z.ZodNullable);
expect(f4Schema).toBeInstanceOf(z.ZodArray);
});

test("correct parsing", () => {
Test.parse({
f1: 12,
Expand Down Expand Up @@ -389,3 +401,42 @@ test("unknownkeys merging", () => {
util.assertEqual<mergedSchema["_def"]["catchall"], z.ZodString>(true);
expect(mergedSchema._def.catchall instanceof z.ZodString).toEqual(true);
});

const personToExtend = z.object({
firstName: z.string(),
lastName: z.string(),
});

test("extend() should return schema with new key", () => {
const PersonWithNickname = personToExtend.extend({ nickName: z.string() });
type PersonWithNickname = z.infer<typeof PersonWithNickname>;

const expected = { firstName: "f", nickName: "n", lastName: "l" };
const actual = PersonWithNickname.parse(expected);

expect(actual).toEqual(expected);
util.assertEqual<
keyof PersonWithNickname,
"firstName" | "lastName" | "nickName"
>(true);
util.assertEqual<
PersonWithNickname,
{ firstName: string; lastName: string; nickName: string }
>(true);
});

test("extend() should have power to override existing key", () => {
const PersonWithNumberAsLastName = personToExtend.extend({
lastName: z.number(),
});
type PersonWithNumberAsLastName = z.infer<typeof PersonWithNumberAsLastName>;

const expected = { firstName: "f", lastName: 42 };
const actual = PersonWithNumberAsLastName.parse(expected);

expect(actual).toEqual(expected);
util.assertEqual<
PersonWithNumberAsLastName,
{ firstName: string; lastName: number }
>(true);
});
51 changes: 51 additions & 0 deletions src/__tests__/object.test.ts
Expand Up @@ -28,6 +28,18 @@ test("unknown throw", () => {
expect(() => Test.parse(asdf)).toThrow();
});

test("shape() should return schema of particular key", () => {
const f1Schema = Test.shape.f1;
const f2Schema = Test.shape.f2;
const f3Schema = Test.shape.f3;
const f4Schema = Test.shape.f4;

expect(f1Schema).toBeInstanceOf(z.ZodNumber);
expect(f2Schema).toBeInstanceOf(z.ZodOptional);
expect(f3Schema).toBeInstanceOf(z.ZodNullable);
expect(f4Schema).toBeInstanceOf(z.ZodArray);
});

test("correct parsing", () => {
Test.parse({
f1: 12,
Expand Down Expand Up @@ -388,3 +400,42 @@ test("unknownkeys merging", () => {
util.assertEqual<mergedSchema["_def"]["catchall"], z.ZodString>(true);
expect(mergedSchema._def.catchall instanceof z.ZodString).toEqual(true);
});

const personToExtend = z.object({
firstName: z.string(),
lastName: z.string(),
});

test("extend() should return schema with new key", () => {
const PersonWithNickname = personToExtend.extend({ nickName: z.string() });
type PersonWithNickname = z.infer<typeof PersonWithNickname>;

const expected = { firstName: "f", nickName: "n", lastName: "l" };
const actual = PersonWithNickname.parse(expected);

expect(actual).toEqual(expected);
util.assertEqual<
keyof PersonWithNickname,
"firstName" | "lastName" | "nickName"
>(true);
util.assertEqual<
PersonWithNickname,
{ firstName: string; lastName: string; nickName: string }
>(true);
});

test("extend() should have power to override existing key", () => {
const PersonWithNumberAsLastName = personToExtend.extend({
lastName: z.number(),
});
type PersonWithNumberAsLastName = z.infer<typeof PersonWithNumberAsLastName>;

const expected = { firstName: "f", lastName: 42 };
const actual = PersonWithNumberAsLastName.parse(expected);

expect(actual).toEqual(expected);
util.assertEqual<
PersonWithNumberAsLastName,
{ firstName: string; lastName: number }
>(true);
});

0 comments on commit 767ab1b

Please sign in to comment.