Skip to content

fix 373 #375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixed plugin config in some tests
  • Loading branch information
Code-Hex committed May 27, 2023
commit 657f8be9693ff0bd350a4c0303c2ecbccc8186e3
268 changes: 148 additions & 120 deletions tests/myzod.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,146 +5,171 @@ describe('myzod', () => {
test.each([
[
'non-null and defined',
/* GraphQL */ `
input PrimitiveInput {
a: ID!
b: String!
c: Boolean!
d: Int!
e: Float!
}
`,
[
'export function PrimitiveInputSchema(): myzod.Type<PrimitiveInput> {',
'a: myzod.string()',
'b: myzod.string()',
'c: myzod.boolean()',
'd: myzod.number()',
'e: myzod.number()',
],
{
textSchema: /* GraphQL */ `
input PrimitiveInput {
a: ID!
b: String!
c: Boolean!
d: Int!
e: Float!
}
`,
wantContains: [
'export function PrimitiveInputSchema(): myzod.Type<PrimitiveInput> {',
'a: myzod.string()',
'b: myzod.string()',
'c: myzod.boolean()',
'd: myzod.number()',
'e: myzod.number()',
],
scalars: {
ID: 'string',
},
},
],
[
'nullish',
/* GraphQL */ `
input PrimitiveInput {
a: ID
b: String
c: Boolean
d: Int
e: Float
z: String! # no defined check
}
`,
[
'export function PrimitiveInputSchema(): myzod.Type<PrimitiveInput> {',
// alphabet order
'a: myzod.string().optional().nullable(),',
'b: myzod.string().optional().nullable(),',
'c: myzod.boolean().optional().nullable(),',
'd: myzod.number().optional().nullable(),',
'e: myzod.number().optional().nullable(),',
],
{
textSchema: /* GraphQL */ `
input PrimitiveInput {
a: ID
b: String
c: Boolean
d: Int
e: Float
z: String! # no defined check
}
`,
wantContains: [
'export function PrimitiveInputSchema(): myzod.Type<PrimitiveInput> {',
// alphabet order
'a: myzod.string().optional().nullable(),',
'b: myzod.string().optional().nullable(),',
'c: myzod.boolean().optional().nullable(),',
'd: myzod.number().optional().nullable(),',
'e: myzod.number().optional().nullable(),',
],
scalars: {
ID: 'string',
},
},
],
[
'array',
/* GraphQL */ `
input ArrayInput {
a: [String]
b: [String!]
c: [String!]!
d: [[String]]
e: [[String]!]
f: [[String]!]!
}
`,
[
'export function ArrayInputSchema(): myzod.Type<ArrayInput> {',
'a: myzod.array(myzod.string().nullable()).optional().nullable(),',
'b: myzod.array(myzod.string()).optional().nullable(),',
'c: myzod.array(myzod.string()),',
'd: myzod.array(myzod.array(myzod.string().nullable()).optional().nullable()).optional().nullable(),',
'e: myzod.array(myzod.array(myzod.string().nullable())).optional().nullable(),',
'f: myzod.array(myzod.array(myzod.string().nullable()))',
],
{
textSchema: /* GraphQL */ `
input ArrayInput {
a: [String]
b: [String!]
c: [String!]!
d: [[String]]
e: [[String]!]
f: [[String]!]!
}
`,
wantContains: [
'export function ArrayInputSchema(): myzod.Type<ArrayInput> {',
'a: myzod.array(myzod.string().nullable()).optional().nullable(),',
'b: myzod.array(myzod.string()).optional().nullable(),',
'c: myzod.array(myzod.string()),',
'd: myzod.array(myzod.array(myzod.string().nullable()).optional().nullable()).optional().nullable(),',
'e: myzod.array(myzod.array(myzod.string().nullable())).optional().nullable(),',
'f: myzod.array(myzod.array(myzod.string().nullable()))',
],
scalars: undefined,
},
],
[
'ref input object',
/* GraphQL */ `
input AInput {
b: BInput!
}
input BInput {
c: CInput!
}
input CInput {
a: AInput!
}
`,
[
'export function AInputSchema(): myzod.Type<AInput> {',
'b: myzod.lazy(() => BInputSchema())',
'export function BInputSchema(): myzod.Type<BInput> {',
'c: myzod.lazy(() => CInputSchema())',
'export function CInputSchema(): myzod.Type<CInput> {',
'a: myzod.lazy(() => AInputSchema())',
],
{
textSchema: /* GraphQL */ `
input AInput {
b: BInput!
}
input BInput {
c: CInput!
}
input CInput {
a: AInput!
}
`,
wantContains: [
'export function AInputSchema(): myzod.Type<AInput> {',
'b: myzod.lazy(() => BInputSchema())',
'export function BInputSchema(): myzod.Type<BInput> {',
'c: myzod.lazy(() => CInputSchema())',
'export function CInputSchema(): myzod.Type<CInput> {',
'a: myzod.lazy(() => AInputSchema())',
],
scalars: undefined,
},
],
[
'nested input object',
/* GraphQL */ `
input NestedInput {
child: NestedInput
childrens: [NestedInput]
}
`,
[
'export function NestedInputSchema(): myzod.Type<NestedInput> {',
'child: myzod.lazy(() => NestedInputSchema().optional().nullable()),',
'childrens: myzod.array(myzod.lazy(() => NestedInputSchema().nullable())).optional().nullable()',
],
{
textSchema: /* GraphQL */ `
input NestedInput {
child: NestedInput
childrens: [NestedInput]
}
`,
wantContains: [
'export function NestedInputSchema(): myzod.Type<NestedInput> {',
'child: myzod.lazy(() => NestedInputSchema().optional().nullable()),',
'childrens: myzod.array(myzod.lazy(() => NestedInputSchema().nullable())).optional().nullable()',
],
scalars: undefined,
},
],
[
'enum',
/* GraphQL */ `
enum PageType {
PUBLIC
BASIC_AUTH
}
input PageInput {
pageType: PageType!
}
`,
[
'export const PageTypeSchema = myzod.enum(PageType)',
'export function PageInputSchema(): myzod.Type<PageInput> {',
'pageType: PageTypeSchema',
],
{
textSchema: /* GraphQL */ `
enum PageType {
PUBLIC
BASIC_AUTH
}
input PageInput {
pageType: PageType!
}
`,
wantContains: [
'export const PageTypeSchema = myzod.enum(PageType)',
'export function PageInputSchema(): myzod.Type<PageInput> {',
'pageType: PageTypeSchema',
],
scalars: undefined,
},
],
[
'camelcase',
/* GraphQL */ `
input HTTPInput {
method: HTTPMethod
url: URL!
}

enum HTTPMethod {
GET
POST
}

scalar URL # unknown scalar, should be any (definedNonNullAnySchema)
`,
[
'export function HttpInputSchema(): myzod.Type<HttpInput> {',
'export const HttpMethodSchema = myzod.enum(HttpMethod)',
'method: HttpMethodSchema',
'url: definedNonNullAnySchema',
],
{
textSchema: /* GraphQL */ `
input HTTPInput {
method: HTTPMethod
url: URL!
}

enum HTTPMethod {
GET
POST
}

scalar URL # unknown scalar, should be any (definedNonNullAnySchema)
`,
wantContains: [
'export function HttpInputSchema(): myzod.Type<HttpInput> {',
'export const HttpMethodSchema = myzod.enum(HttpMethod)',
'method: HttpMethodSchema',
'url: definedNonNullAnySchema',
],
scalars: undefined,
},
],
])('%s', async (_, textSchema, wantContains) => {
])('%s', async (_, { textSchema, wantContains, scalars }) => {
const schema = buildSchema(textSchema);
const result = await plugin(schema, [], { schema: 'myzod' }, {});
const result = await plugin(schema, [], { schema: 'myzod', scalars }, {});
expect(result.prepend).toContain("import * as myzod from 'myzod'");

for (const wantContain of wantContains) {
Expand Down Expand Up @@ -232,6 +257,9 @@ describe('myzod', () => {
{
schema: 'myzod',
notAllowEmptyString: true,
scalars: {
ID: 'string',
},
},
{}
);
Expand Down
Loading