Skip to content

Commit 959b95c

Browse files
committed
added test
1 parent 3d336dc commit 959b95c

File tree

1 file changed

+218
-0
lines changed

1 file changed

+218
-0
lines changed

tests/zod.spec.ts

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
import { buildSchema } from 'graphql';
2+
import { plugin } from '../src/index';
3+
4+
describe('zod', () => {
5+
test.each([
6+
[
7+
'non-null and defined',
8+
/* GraphQL */ `
9+
input PrimitiveInput {
10+
a: ID!
11+
b: String!
12+
c: Boolean!
13+
d: Int!
14+
e: Float!
15+
}
16+
`,
17+
[
18+
'export function PrimitiveInputSchema(): z.ZodSchema<PrimitiveInput>',
19+
'a: z.string()',
20+
'b: z.string()',
21+
'c: z.boolean()',
22+
'd: z.number()',
23+
'e: z.number()',
24+
],
25+
],
26+
[
27+
'nullish',
28+
/* GraphQL */ `
29+
input PrimitiveInput {
30+
a: ID
31+
b: String
32+
c: Boolean
33+
d: Int
34+
e: Float
35+
z: String! # no defined check
36+
}
37+
`,
38+
[
39+
'export function PrimitiveInputSchema(): z.ZodSchema<PrimitiveInput>',
40+
// alphabet order
41+
'a: z.string().nullish(),',
42+
'b: z.string().nullish(),',
43+
'c: z.boolean().nullish(),',
44+
'd: z.number().nullish(),',
45+
'e: z.number().nullish(),',
46+
],
47+
],
48+
[
49+
'array',
50+
/* GraphQL */ `
51+
input ArrayInput {
52+
a: [String]
53+
b: [String!]
54+
c: [String!]!
55+
d: [[String]]
56+
e: [[String]!]
57+
f: [[String]!]!
58+
}
59+
`,
60+
[
61+
'export function ArrayInputSchema(): z.ZodSchema<ArrayInput>',
62+
'a: z.array(z.string().nullable()).nullish(),',
63+
'b: z.array(z.string()).nullish(),',
64+
'c: z.array(z.string()),',
65+
'd: z.array(z.array(z.string().nullable()).nullish()).nullish(),',
66+
'e: z.array(z.array(z.string().nullable())).nullish(),',
67+
'f: z.array(z.array(z.string().nullable()))',
68+
],
69+
],
70+
[
71+
'ref input object',
72+
/* GraphQL */ `
73+
input AInput {
74+
b: BInput!
75+
}
76+
input BInput {
77+
c: CInput!
78+
}
79+
input CInput {
80+
a: AInput!
81+
}
82+
`,
83+
[
84+
'export function AInputSchema(): z.ZodSchema<AInput>',
85+
'b: z.lazy(() => BInputSchema())',
86+
'export function BInputSchema(): z.ZodSchema<BInput>',
87+
'c: z.lazy(() => CInputSchema())',
88+
'export function CInputSchema(): z.ZodSchema<CInput>',
89+
'a: z.lazy(() => AInputSchema())',
90+
],
91+
],
92+
[
93+
'nested input object',
94+
/* GraphQL */ `
95+
input NestedInput {
96+
child: NestedInput
97+
childrens: [NestedInput]
98+
}
99+
`,
100+
[
101+
'export function NestedInputSchema(): z.ZodSchema<NestedInput>',
102+
'child: z.lazy(() => NestedInputSchema().nullish()),',
103+
'childrens: z.array(z.lazy(() => NestedInputSchema().nullable())).nullish()',
104+
],
105+
],
106+
[
107+
'enum',
108+
/* GraphQL */ `
109+
enum PageType {
110+
PUBLIC
111+
BASIC_AUTH
112+
}
113+
input PageInput {
114+
pageType: PageType!
115+
}
116+
`,
117+
[
118+
'export const PageTypeSchema = z.nativeEnum(PageType)',
119+
'export function PageInputSchema(): z.ZodSchema<PageInput>',
120+
'pageType: PageTypeSchema',
121+
],
122+
],
123+
[
124+
'camelcase',
125+
/* GraphQL */ `
126+
input HTTPInput {
127+
method: HTTPMethod
128+
url: URL!
129+
}
130+
131+
enum HTTPMethod {
132+
GET
133+
POST
134+
}
135+
136+
scalar URL # unknown scalar, should be any (definedNonNullAnySchema)
137+
`,
138+
[
139+
'export function HttpInputSchema(): z.ZodSchema<HttpInput>',
140+
'export const HttpMethodSchema = z.nativeEnum(HttpMethod)',
141+
'method: HttpMethodSchema',
142+
'url: definedNonNullAnySchema',
143+
],
144+
],
145+
])('%s', async (_, textSchema, wantContains) => {
146+
const schema = buildSchema(textSchema);
147+
const result = await plugin(schema, [], { schema: 'zod' }, {});
148+
expect(result.prepend).toContain("import { z } from 'zod'");
149+
150+
for (const wantContain of wantContains) {
151+
expect(result.content).toContain(wantContain);
152+
}
153+
});
154+
155+
it('with scalars', async () => {
156+
const schema = buildSchema(/* GraphQL */ `
157+
input Say {
158+
phrase: Text!
159+
times: Count!
160+
}
161+
162+
scalar Count
163+
scalar Text
164+
`);
165+
const result = await plugin(
166+
schema,
167+
[],
168+
{
169+
schema: 'zod',
170+
scalars: {
171+
Text: 'string',
172+
Count: 'number',
173+
},
174+
},
175+
{}
176+
);
177+
expect(result.content).toContain('phrase: z.string()');
178+
expect(result.content).toContain('times: z.number()');
179+
});
180+
181+
it('with importFrom', async () => {
182+
const schema = buildSchema(/* GraphQL */ `
183+
input Say {
184+
phrase: String!
185+
}
186+
`);
187+
const result = await plugin(
188+
schema,
189+
[],
190+
{
191+
schema: 'zod',
192+
importFrom: './types',
193+
},
194+
{}
195+
);
196+
expect(result.prepend).toContain("import { Say } from './types'");
197+
expect(result.content).toContain('phrase: z.string()');
198+
});
199+
200+
it('with enumsAsTypes', async () => {
201+
const schema = buildSchema(/* GraphQL */ `
202+
enum PageType {
203+
PUBLIC
204+
BASIC_AUTH
205+
}
206+
`);
207+
const result = await plugin(
208+
schema,
209+
[],
210+
{
211+
schema: 'zod',
212+
enumsAsTypes: true,
213+
},
214+
{}
215+
);
216+
expect(result.content).toContain("export const PageTypeSchema = z.enum(['PUBLIC', 'BASIC_AUTH'])");
217+
});
218+
});

0 commit comments

Comments
 (0)