@@ -4,63 +4,68 @@ import { dedent } from 'ts-dedent';
44import { plugin } from '../src/index' ;
55
66describe ( 'zod' , ( ) => {
7- it . each ( [
8- [
9- 'non-null and defined' ,
10- {
11- textSchema : /* GraphQL */ `
12- input PrimitiveInput {
13- a: ID!
14- b: String!
15- c: Boolean!
16- d: Int!
17- e: Float!
18- }
19- ` ,
20- wantContains : [
21- 'export function PrimitiveInputSchema(): z.ZodObject<Properties<PrimitiveInput>>' ,
22- 'a: z.string()' ,
23- 'b: z.string()' ,
24- 'c: z.boolean()' ,
25- 'd: z.number()' ,
26- 'e: z.number()' ,
27- ] ,
28- scalars : {
29- ID : 'string' ,
30- } ,
31- } ,
32- ] ,
33- [
34- 'nullish' ,
35- {
36- textSchema : /* GraphQL */ `
37- input PrimitiveInput {
38- a: ID
39- b: String
40- c: Boolean
41- d: Int
42- e: Float
43- z: String! # no defined check
44- }
45- ` ,
46- wantContains : [
47- 'export function PrimitiveInputSchema(): z.ZodObject<Properties<PrimitiveInput>>' ,
48- // alphabet order
49- 'a: z.string().nullish(),' ,
50- 'b: z.string().nullish(),' ,
51- 'c: z.boolean().nullish(),' ,
52- 'd: z.number().nullish(),' ,
53- 'e: z.number().nullish(),' ,
54- ] ,
55- scalars : {
56- ID : 'string' ,
57- } ,
58- } ,
59- ] ,
60- [
61- 'array' ,
62- {
63- textSchema : /* GraphQL */ `
7+ it ( "non-null and defined" , async ( ) => {
8+ const schema = buildSchema ( /* GraphQL */ `
9+ input PrimitiveInput {
10+ a: ID!
11+ b: String!
12+ c: Boolean!
13+ d: Int!
14+ e: Float!
15+ }
16+ ` ) ;
17+ const scalars = {
18+ ID : 'string' ,
19+ }
20+ const result = await plugin ( schema , [ ] , { schema : 'zod' , scalars } , { } ) ;
21+ expect ( result . prepend ) . toContain ( 'import { z } from \'zod\'' ) ;
22+
23+ const wantContains = [
24+ 'export function PrimitiveInputSchema(): z.ZodObject<Properties<PrimitiveInput>>' ,
25+ 'a: z.string()' ,
26+ 'b: z.string()' ,
27+ 'c: z.boolean()' ,
28+ 'd: z.number()' ,
29+ 'e: z.number()' ,
30+ ]
31+
32+ for ( const wantContain of wantContains )
33+ expect ( result . content ) . toContain ( wantContain ) ;
34+ } )
35+
36+ it ( "nullish" , async ( ) => {
37+ const schema = buildSchema ( /* GraphQL */ `
38+ input PrimitiveInput {
39+ a: ID
40+ b: String
41+ c: Boolean
42+ d: Int
43+ e: Float
44+ z: String! # no defined check
45+ }
46+ ` ) ;
47+ const scalars = {
48+ ID : 'string' ,
49+ }
50+ const result = await plugin ( schema , [ ] , { schema : 'zod' , scalars } , { } ) ;
51+ expect ( result . prepend ) . toContain ( 'import { z } from \'zod\'' ) ;
52+
53+ const wantContains = [
54+ 'export function PrimitiveInputSchema(): z.ZodObject<Properties<PrimitiveInput>>' ,
55+ // alphabet order
56+ 'a: z.string().nullish(),' ,
57+ 'b: z.string().nullish(),' ,
58+ 'c: z.boolean().nullish(),' ,
59+ 'd: z.number().nullish(),' ,
60+ 'e: z.number().nullish(),' ,
61+ ]
62+
63+ for ( const wantContain of wantContains )
64+ expect ( result . content ) . toContain ( wantContain ) ;
65+ } )
66+
67+ it ( "array" , async ( ) => {
68+ const schema = buildSchema ( /* GraphQL */ `
6469 input ArrayInput {
6570 a: [String]
6671 b: [String!]
@@ -69,23 +74,27 @@ describe('zod', () => {
6974 e: [[String]!]
7075 f: [[String]!]!
7176 }
72- ` ,
73- wantContains : [
74- 'export function ArrayInputSchema(): z.ZodObject<Properties<ArrayInput>>' ,
75- 'a: z.array(z.string().nullable()).nullish(),' ,
76- 'b: z.array(z.string()).nullish(),' ,
77- 'c: z.array(z.string()),' ,
78- 'd: z.array(z.array(z.string().nullable()).nullish()).nullish(),' ,
79- 'e: z.array(z.array(z.string().nullable())).nullish(),' ,
80- 'f: z.array(z.array(z.string().nullable()))' ,
81- ] ,
82- scalars : undefined ,
83- } ,
84- ] ,
85- [
86- 'ref input object' ,
87- {
88- textSchema : /* GraphQL */ `
77+ ` ) ;
78+ const scalars = undefined
79+ const result = await plugin ( schema , [ ] , { schema : 'zod' , scalars } , { } ) ;
80+ expect ( result . prepend ) . toContain ( 'import { z } from \'zod\'' ) ;
81+
82+ const wantContains = [
83+ 'export function ArrayInputSchema(): z.ZodObject<Properties<ArrayInput>>' ,
84+ 'a: z.array(z.string().nullable()).nullish(),' ,
85+ 'b: z.array(z.string()).nullish(),' ,
86+ 'c: z.array(z.string()),' ,
87+ 'd: z.array(z.array(z.string().nullable()).nullish()).nullish(),' ,
88+ 'e: z.array(z.array(z.string().nullable())).nullish(),' ,
89+ 'f: z.array(z.array(z.string().nullable()))' ,
90+ ]
91+
92+ for ( const wantContain of wantContains )
93+ expect ( result . content ) . toContain ( wantContain ) ;
94+ } )
95+
96+ it ( "ref input object" , async ( ) => {
97+ const schema = buildSchema ( /* GraphQL */ `
8998 input AInput {
9099 b: BInput!
91100 }
@@ -95,59 +104,71 @@ describe('zod', () => {
95104 input CInput {
96105 a: AInput!
97106 }
98- ` ,
99- wantContains : [
100- 'export function AInputSchema(): z.ZodObject<Properties<AInput>>' ,
101- 'b: z.lazy(() => BInputSchema())' ,
102- 'export function BInputSchema(): z.ZodObject<Properties<BInput>>' ,
103- 'c: z.lazy(() => CInputSchema())' ,
104- 'export function CInputSchema(): z.ZodObject<Properties<CInput>>' ,
105- 'a: z.lazy(() => AInputSchema())' ,
106- ] ,
107- scalars : undefined ,
108- } ,
109- ] ,
110- [
111- 'nested input object' ,
112- {
113- textSchema : /* GraphQL */ `
107+ ` ) ;
108+ const scalars = undefined
109+ const result = await plugin ( schema , [ ] , { schema : 'zod' , scalars } , { } ) ;
110+ expect ( result . prepend ) . toContain ( 'import { z } from \'zod\'' ) ;
111+
112+ const wantContains = [
113+ 'export function AInputSchema(): z.ZodObject<Properties<AInput>>' ,
114+ 'b: z.lazy(() => BInputSchema())' ,
115+ 'export function BInputSchema(): z.ZodObject<Properties<BInput>>' ,
116+ 'c: z.lazy(() => CInputSchema())' ,
117+ 'export function CInputSchema(): z.ZodObject<Properties<CInput>>' ,
118+ 'a: z.lazy(() => AInputSchema())' ,
119+ ]
120+
121+ for ( const wantContain of wantContains )
122+ expect ( result . content ) . toContain ( wantContain ) ;
123+ } )
124+
125+ it ( 'nested input object' , async ( ) => {
126+ const schema = buildSchema ( /* GraphQL */ `
114127 input NestedInput {
115128 child: NestedInput
116129 childrens: [NestedInput]
117130 }
118- ` ,
119- wantContains : [
120- 'export function NestedInputSchema(): z.ZodObject<Properties<NestedInput>>' ,
121- 'child: z.lazy(() => NestedInputSchema().nullish()),' ,
122- 'childrens: z.array(z.lazy(() => NestedInputSchema().nullable())).nullish()' ,
123- ] ,
124- scalars : undefined ,
125- } ,
126- ] ,
127- [
128- 'enum' ,
129- {
130- textSchema : /* GraphQL */ `
131+ ` ) ;
132+ const scalars = undefined
133+ const result = await plugin ( schema , [ ] , { schema : 'zod' , scalars } , { } ) ;
134+ expect ( result . prepend ) . toContain ( 'import { z } from \'zod\'' ) ;
135+
136+ const wantContains = [
137+ 'export function NestedInputSchema(): z.ZodObject<Properties<NestedInput>>' ,
138+ 'child: z.lazy(() => NestedInputSchema().nullish()),' ,
139+ 'childrens: z.array(z.lazy(() => NestedInputSchema().nullable())).nullish()' ,
140+ ]
141+
142+ for ( const wantContain of wantContains )
143+ expect ( result . content ) . toContain ( wantContain ) ;
144+ } )
145+
146+ it ( 'enum' , async ( ) => {
147+ const schema = buildSchema ( /* GraphQL */ `
131148 enum PageType {
132149 PUBLIC
133150 BASIC_AUTH
134151 }
135152 input PageInput {
136153 pageType: PageType!
137154 }
138- ` ,
139- wantContains : [
140- 'export const PageTypeSchema = z.nativeEnum(PageType)' ,
141- 'export function PageInputSchema(): z.ZodObject<Properties<PageInput>>' ,
142- 'pageType: PageTypeSchema' ,
143- ] ,
144- scalars : undefined ,
145- } ,
146- ] ,
147- [
148- 'camelcase' ,
149- {
150- textSchema : /* GraphQL */ `
155+ ` ) ;
156+ const scalars = undefined
157+ const result = await plugin ( schema , [ ] , { schema : 'zod' , scalars } , { } ) ;
158+ expect ( result . prepend ) . toContain ( 'import { z } from \'zod\'' ) ;
159+
160+ const wantContains = [
161+ 'export const PageTypeSchema = z.nativeEnum(PageType)' ,
162+ 'export function PageInputSchema(): z.ZodObject<Properties<PageInput>>' ,
163+ 'pageType: PageTypeSchema' ,
164+ ]
165+
166+ for ( const wantContain of wantContains )
167+ expect ( result . content ) . toContain ( wantContain ) ;
168+ } )
169+
170+ it ( 'camelcase' , async ( ) => {
171+ const schema = buildSchema ( /* GraphQL */ `
151172 input HTTPInput {
152173 method: HTTPMethod
153174 url: URL!
@@ -159,24 +180,21 @@ describe('zod', () => {
159180 }
160181
161182 scalar URL # unknown scalar, should be any (definedNonNullAnySchema)
162- ` ,
163- wantContains : [
164- 'export function HttpInputSchema(): z.ZodObject<Properties<HttpInput>>' ,
165- 'export const HttpMethodSchema = z.nativeEnum(HttpMethod)' ,
166- 'method: HttpMethodSchema' ,
167- 'url: definedNonNullAnySchema' ,
168- ] ,
169- scalars : undefined ,
170- } ,
171- ] ,
172- ] ) ( '%s' , async ( _ , { textSchema, wantContains, scalars } ) => {
173- const schema = buildSchema ( textSchema ) ;
183+ ` ) ;
184+ const scalars = undefined
174185 const result = await plugin ( schema , [ ] , { schema : 'zod' , scalars } , { } ) ;
175186 expect ( result . prepend ) . toContain ( 'import { z } from \'zod\'' ) ;
176187
188+ const wantContains = [
189+ 'export function HttpInputSchema(): z.ZodObject<Properties<HttpInput>>' ,
190+ 'export const HttpMethodSchema = z.nativeEnum(HttpMethod)' ,
191+ 'method: HttpMethodSchema' ,
192+ 'url: definedNonNullAnySchema' ,
193+ ]
194+
177195 for ( const wantContain of wantContains )
178196 expect ( result . content ) . toContain ( wantContain ) ;
179- } ) ;
197+ } )
180198
181199 it ( 'with scalars' , async ( ) => {
182200 const schema = buildSchema ( /* GraphQL */ `
@@ -973,7 +991,7 @@ describe('zod', () => {
973991 author: Author
974992 title: String
975993 }
976-
994+
977995 interface Author {
978996 books: [Book]
979997 name: String
@@ -1007,19 +1025,19 @@ describe('zod', () => {
10071025 title: String!
10081026 author: Author!
10091027 }
1010-
1028+
10111029 type Textbook implements Book {
10121030 title: String!
10131031 author: Author!
10141032 courses: [String!]!
10151033 }
1016-
1034+
10171035 type ColoringBook implements Book {
10181036 title: String!
10191037 author: Author!
10201038 colors: [String!]!
10211039 }
1022-
1040+
10231041 type Author {
10241042 books: [Book!]
10251043 name: String
0 commit comments