Skip to content

Commit b2febdc

Browse files
committed
refactor: decompose it.each
1 parent afc1052 commit b2febdc

File tree

1 file changed

+193
-160
lines changed

1 file changed

+193
-160
lines changed

tests/yup.spec.ts

Lines changed: 193 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -4,176 +4,209 @@ import dedent from 'ts-dedent';
44
import { plugin } from '../src/index';
55

66
describe('yup', () => {
7-
it.each([
8-
[
9-
'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(): yup.ObjectSchema<PrimitiveInput>',
22-
'a: yup.string().defined()',
23-
'b: yup.string().defined()',
24-
'c: yup.boolean().defined()',
25-
'd: yup.number().defined()',
26-
'e: yup.number().defined()',
27-
],
28-
scalars: {
29-
ID: 'string',
30-
},
31-
},
32-
],
33-
[
34-
'optional',
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(): yup.ObjectSchema<PrimitiveInput>',
48-
// alphabet order
49-
'a: yup.string().defined().nullable().optional(),',
50-
'b: yup.string().defined().nullable().optional(),',
51-
'c: yup.boolean().defined().nullable().optional(),',
52-
'd: yup.number().defined().nullable().optional(),',
53-
'e: yup.number().defined().nullable().optional(),',
54-
],
55-
scalars: {
56-
ID: 'string',
57-
},
58-
},
59-
],
60-
[
61-
'array',
62-
{
63-
textSchema: /* GraphQL */ `
64-
input ArrayInput {
65-
a: [String]
66-
b: [String!]
67-
c: [String!]!
68-
d: [[String]]
69-
e: [[String]!]
70-
f: [[String]!]!
71-
}
72-
`,
73-
wantContains: [
74-
'export function ArrayInputSchema(): yup.ObjectSchema<ArrayInput>',
75-
'a: yup.array(yup.string().defined().nullable()).defined().nullable().optional(),',
76-
'b: yup.array(yup.string().defined().nonNullable()).defined().nullable().optional(),',
77-
'c: yup.array(yup.string().defined().nonNullable()).defined(),',
78-
'd: yup.array(yup.array(yup.string().defined().nullable()).defined().nullable()).defined().nullable().optional(),',
79-
'e: yup.array(yup.array(yup.string().defined().nullable()).defined()).defined().nullable().optional(),',
80-
'f: yup.array(yup.array(yup.string().defined().nullable()).defined()).defined()',
81-
],
82-
scalars: undefined,
83-
},
84-
],
85-
[
86-
'ref input object',
87-
{
88-
textSchema: /* GraphQL */ `
89-
input AInput {
90-
b: BInput!
91-
}
92-
input BInput {
93-
c: CInput!
94-
}
95-
input CInput {
96-
a: AInput!
97-
}
98-
`,
99-
wantContains: [
100-
'export function AInputSchema(): yup.ObjectSchema<AInput>',
101-
'b: yup.lazy(() => BInputSchema().nonNullable())',
102-
'export function BInputSchema(): yup.ObjectSchema<BInput>',
103-
'c: yup.lazy(() => CInputSchema().nonNullable())',
104-
'export function CInputSchema(): yup.ObjectSchema<CInput>',
105-
'a: yup.lazy(() => AInputSchema().nonNullable())',
106-
],
107-
scalars: undefined,
108-
},
109-
],
110-
[
111-
'nested input object',
7+
it('defined', async () => {
8+
const textSchema = /* GraphQL */ `
9+
input PrimitiveInput {
10+
a: ID!
11+
b: String!
12+
c: Boolean!
13+
d: Int!
14+
e: Float!
15+
}
16+
`;
17+
const wantContains = [
18+
'export function PrimitiveInputSchema(): yup.ObjectSchema<PrimitiveInput>',
19+
'a: yup.string().defined()',
20+
'b: yup.string().defined()',
21+
'c: yup.boolean().defined()',
22+
'd: yup.number().defined()',
23+
'e: yup.number().defined()',
24+
];
25+
const scalars = { ID: 'string' };
26+
27+
const schema = buildSchema(textSchema);
28+
const result = await plugin(schema, [], { scalars }, {});
29+
30+
expect(result.prepend).toContain('import * as yup from \'yup\'');
31+
32+
for (const wantContain of wantContains) {
33+
expect(result.content).toContain(wantContain);
34+
}
35+
});
36+
37+
it('optional', async () => {
38+
const textSchema = /* GraphQL */ `
39+
input PrimitiveInput {
40+
a: ID
41+
b: String
42+
c: Boolean
43+
d: Int
44+
e: Float
45+
z: String! # no defined check
46+
}
47+
`;
48+
const wantContains = [
49+
'export function PrimitiveInputSchema(): yup.ObjectSchema<PrimitiveInput>',
50+
'a: yup.string().defined().nullable().optional(),',
51+
'b: yup.string().defined().nullable().optional(),',
52+
'c: yup.boolean().defined().nullable().optional(),',
53+
'd: yup.number().defined().nullable().optional(),',
54+
'e: yup.number().defined().nullable().optional(),',
55+
];
56+
const scalars = { ID: 'string' };
57+
58+
const schema = buildSchema(textSchema);
59+
const result = await plugin(schema, [], { scalars }, {});
60+
61+
expect(result.prepend).toContain('import * as yup from \'yup\'');
62+
63+
for (const wantContain of wantContains) {
64+
expect(result.content).toContain(wantContain);
65+
}
66+
});
67+
68+
it('array', async () => {
69+
const textSchema = /* GraphQL */ `
70+
input ArrayInput {
71+
a: [String]
72+
b: [String!]
73+
c: [String!]!
74+
d: [[String]]
75+
e: [[String]!]
76+
f: [[String]!]!
77+
}
78+
`;
79+
const wantContains = [
80+
'export function ArrayInputSchema(): yup.ObjectSchema<ArrayInput>',
81+
'a: yup.array(yup.string().defined().nullable()).defined().nullable().optional(),',
82+
'b: yup.array(yup.string().defined().nonNullable()).defined().nullable().optional(),',
83+
'c: yup.array(yup.string().defined().nonNullable()).defined(),',
84+
'd: yup.array(yup.array(yup.string().defined().nullable()).defined().nullable()).defined().nullable().optional(),',
85+
'e: yup.array(yup.array(yup.string().defined().nullable()).defined()).defined().nullable().optional(),',
86+
'f: yup.array(yup.array(yup.string().defined().nullable()).defined()).defined()',
87+
];
88+
89+
const schema = buildSchema(textSchema);
90+
const result = await plugin(schema, [], {}, {});
91+
92+
expect(result.prepend).toContain('import * as yup from \'yup\'');
93+
94+
for (const wantContain of wantContains) {
95+
expect(result.content).toContain(wantContain);
96+
}
97+
});
98+
99+
it('ref input object', async () => {
100+
const textSchema = /* GraphQL */ `
101+
input AInput {
102+
b: BInput!
103+
}
104+
input BInput {
105+
c: CInput!
106+
}
107+
input CInput {
108+
a: AInput!
109+
}
110+
`;
111+
const wantContains = [
112+
'export function AInputSchema(): yup.ObjectSchema<AInput>',
113+
'b: yup.lazy(() => BInputSchema().nonNullable())',
114+
'export function BInputSchema(): yup.ObjectSchema<BInput>',
115+
'c: yup.lazy(() => CInputSchema().nonNullable())',
116+
'export function CInputSchema(): yup.ObjectSchema<CInput>',
117+
'a: yup.lazy(() => AInputSchema().nonNullable())',
118+
];
119+
120+
const schema = buildSchema(textSchema);
121+
const result = await plugin(schema, [], {}, {});
122+
123+
expect(result.prepend).toContain('import * as yup from \'yup\'');
124+
125+
for (const wantContain of wantContains) {
126+
expect(result.content).toContain(wantContain);
127+
}
128+
});
129+
130+
131+
it('nested input object', async () => {
132+
const schema = buildSchema(/* GraphQL */ `
133+
input NestedInput {
134+
child: NestedInput
135+
childrens: [NestedInput]
136+
}
137+
`);
138+
const result = await plugin(
139+
schema,
140+
[],
112141
{
113-
textSchema: /* GraphQL */ `
114-
input NestedInput {
115-
child: NestedInput
116-
childrens: [NestedInput]
117-
}
118-
`,
119-
wantContains: [
120-
'export function NestedInputSchema(): yup.ObjectSchema<NestedInput>',
121-
'child: yup.lazy(() => NestedInputSchema()).optional(),',
122-
'childrens: yup.array(yup.lazy(() => NestedInputSchema())).defined().nullable().optional()',
123-
],
124142
scalars: undefined,
125143
},
126-
],
127-
[
128-
'enum',
144+
{},
145+
);
146+
const wantContains = [
147+
'export function NestedInputSchema(): yup.ObjectSchema<NestedInput>',
148+
'child: yup.lazy(() => NestedInputSchema()).optional(),',
149+
'childrens: yup.array(yup.lazy(() => NestedInputSchema())).defined().nullable().optional()',
150+
]
151+
for (const wantContain of wantContains)
152+
expect(result.content).toContain(wantContain);
153+
});
154+
155+
it('enum', async () => {
156+
const schema = buildSchema(/* GraphQL */ `
157+
enum PageType {
158+
PUBLIC
159+
BASIC_AUTH
160+
}
161+
input PageInput {
162+
pageType: PageType!
163+
}
164+
`);
165+
const result = await plugin(
166+
schema,
167+
[],
129168
{
130-
textSchema: /* GraphQL */ `
131-
enum PageType {
132-
PUBLIC
133-
BASIC_AUTH
134-
}
135-
input PageInput {
136-
pageType: PageType!
137-
}
138-
`,
139-
wantContains: [
140-
'export const PageTypeSchema = yup.string<PageType>().oneOf(Object.values(PageType)).defined();',
141-
'export function PageInputSchema(): yup.ObjectSchema<PageInput>',
142-
'pageType: PageTypeSchema.nonNullable()',
143-
],
144169
scalars: undefined,
145170
},
146-
],
147-
[
148-
'camelcase',
149-
{
150-
textSchema: /* GraphQL */ `
151-
input HTTPInput {
152-
method: HTTPMethod
153-
url: URL!
154-
}
171+
{},
172+
);
173+
const wantContains = [
174+
'export const PageTypeSchema = yup.string<PageType>().oneOf(Object.values(PageType)).defined();',
175+
'export function PageInputSchema(): yup.ObjectSchema<PageInput>',
176+
'pageType: PageTypeSchema.nonNullable()',
177+
]
178+
for (const wantContain of wantContains)
179+
expect(result.content).toContain(wantContain);
180+
});
155181

156-
enum HTTPMethod {
157-
GET
158-
POST
159-
}
182+
it('camelcase', async () => {
183+
const schema = buildSchema(/* GraphQL */ `
184+
input HTTPInput {
185+
method: HTTPMethod
186+
url: URL!
187+
}
188+
189+
enum HTTPMethod {
190+
GET
191+
POST
192+
}
160193
161-
scalar URL # unknown scalar, should be any (yup.mixed())
162-
`,
163-
wantContains: [
164-
'export function HttpInputSchema(): yup.ObjectSchema<HttpInput>',
165-
'export const HttpMethodSchema = yup.string<HttpMethod>().oneOf(Object.values(HttpMethod)).defined();',
166-
'method: HttpMethodSchema.nullable().optional(),',
167-
'url: yup.mixed().nonNullable()',
168-
],
194+
scalar URL # unknown scalar, should be any (yup.mixed())
195+
`);
196+
const result = await plugin(
197+
schema,
198+
[],
199+
{
169200
scalars: undefined,
170201
},
171-
],
172-
])('%s', async (_, { textSchema, wantContains, scalars }) => {
173-
const schema = buildSchema(textSchema);
174-
const result = await plugin(schema, [], { scalars }, {});
175-
expect(result.prepend).toContain('import * as yup from \'yup\'');
176-
202+
{},
203+
);
204+
const wantContains = [
205+
'export function HttpInputSchema(): yup.ObjectSchema<HttpInput>',
206+
'export const HttpMethodSchema = yup.string<HttpMethod>().oneOf(Object.values(HttpMethod)).defined();',
207+
'method: HttpMethodSchema.nullable().optional(),',
208+
'url: yup.mixed().nonNullable()',
209+
]
177210
for (const wantContain of wantContains)
178211
expect(result.content).toContain(wantContain);
179212
});

0 commit comments

Comments
 (0)