Skip to content

Commit dc75414

Browse files
simonljusCode-Hex
authored andcommitted
add test to verify interface with no typename
1 parent 24cc8b1 commit dc75414

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

tests/myzod.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,34 @@ describe('myzod', () => {
505505
expect(result.content).not.toContain('export function UserSchema(): myzod.Type<User> {');
506506
});
507507

508+
it('generate if withInterfaceType true', async () => {
509+
const schema = buildSchema(/* GraphQL */ `
510+
interface Book {
511+
title: String
512+
}
513+
`);
514+
const result = await plugin(
515+
schema,
516+
[],
517+
{
518+
schema: 'myzod',
519+
withInterfaceType: true,
520+
},
521+
{}
522+
);
523+
const wantContains = [
524+
'export function BookSchema(): myzod.Type<Book> {',
525+
'title: myzod.string().optional().nullable()',
526+
];
527+
const wantNotContains = ["__typename: myzod.literal('Book')"];
528+
for (const wantContain of wantContains) {
529+
expect(result.content).toContain(wantContain);
530+
}
531+
for (const wantNotContain of wantNotContains) {
532+
expect(result.content).not.toContain(wantNotContain);
533+
}
534+
});
535+
508536
it('generate interface type contains interface type', async () => {
509537
const schema = buildSchema(/* GraphQL */ `
510538
interface Book {

tests/yup.spec.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,34 @@ describe('yup', () => {
412412
expect(result.content).not.toContain('export function UserSchema(): yup.ObjectSchema<User> {');
413413
});
414414

415+
it('generate if withInterfaceType true', async () => {
416+
const schema = buildSchema(/* GraphQL */ `
417+
interface Book {
418+
title: String
419+
}
420+
`);
421+
const result = await plugin(
422+
schema,
423+
[],
424+
{
425+
schema: 'yup',
426+
withInterfaceType: true,
427+
},
428+
{}
429+
);
430+
const wantContains = [
431+
'export function BookSchema(): yup.ObjectSchema<Book> {',
432+
'title: yup.string().defined().nullable().optional()',
433+
];
434+
const wantNotContains = ["__typename: yup.string<'Book'>().optional()"];
435+
for (const wantContain of wantContains) {
436+
expect(result.content).toContain(wantContain);
437+
}
438+
for (const wantNotContain of wantNotContains) {
439+
expect(result.content).not.toContain(wantNotContain);
440+
}
441+
});
442+
415443
it('generate interface type contains interface type', async () => {
416444
const schema = buildSchema(/* GraphQL */ `
417445
interface Book {
@@ -445,11 +473,11 @@ describe('yup', () => {
445473

446474
'export function BookSchema(): yup.ObjectSchema<Book> {',
447475
'author: AuthorSchema().nullable().optional(),',
448-
'title: yup.string().defined().nonNullable()',
476+
'title: yup.string().defined().nullable().optional()',
449477

450478
'export function Book2Schema(): yup.ObjectSchema<Book2> {',
451479
'author: AuthorSchema().nonNullable(),',
452-
'title: yup.string().defined().nullable().optional()',
480+
'title: yup.string().defined().nonNullable()',
453481
];
454482
for (const wantContain of wantContains) {
455483
expect(result.content).toContain(wantContain);

tests/zod.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,34 @@ describe('zod', () => {
608608
expect(result.content).not.toContain('export function UserSchema(): z.ZodObject<Properties<User>>');
609609
});
610610

611+
it('generate if withInterfaceType true', async () => {
612+
const schema = buildSchema(/* GraphQL */ `
613+
interface Book {
614+
title: String
615+
}
616+
`);
617+
const result = await plugin(
618+
schema,
619+
[],
620+
{
621+
schema: 'zod',
622+
withInterfaceType: true,
623+
},
624+
{}
625+
);
626+
const wantContains = [
627+
'export function BookSchema(): z.ZodObject<Properties<Book>> {',
628+
'title: z.string().nullish()',
629+
];
630+
const wantNotContains = ["__typename: z.literal('Book')"];
631+
for (const wantContain of wantContains) {
632+
expect(result.content).toContain(wantContain);
633+
}
634+
for (const wantNotContain of wantNotContains) {
635+
expect(result.content).not.toContain(wantNotContain);
636+
}
637+
});
638+
611639
it('generate interface type contains interface type', async () => {
612640
const schema = buildSchema(/* GraphQL */ `
613641
interface Book {

0 commit comments

Comments
 (0)