Skip to content

Commit 1fdde47

Browse files
Add example coverage to scalars/spec.ts
I.e. the very basic display of what this PR is meant to change. Follow up commits will cover the faker side of things, and possibly other config permutations.
1 parent e336c6c commit 1fdde47

File tree

2 files changed

+188
-4
lines changed

2 files changed

+188
-4
lines changed

tests/scalars/__snapshots__/spec.ts.snap

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`custom scalar generation for native and custom types using casual should generate custom scalars for native and custom types using casual 1`] = `
3+
exports[`Custom scalar generation using casual should generate custom scalars for native and custom types 1`] = `
44
"
55
export const anA = (overrides?: Partial<A>): A => {
66
return {
@@ -27,7 +27,7 @@ export const aC = (overrides?: Partial<C>): C => {
2727
"
2828
`;
2929
30-
exports[`custom scalar generation for native and custom types using casual should generate dynamic custom scalars for native and custom types using casual 1`] = `
30+
exports[`Custom scalar generation using casual should generate dynamic custom scalars for native and custom types 1`] = `
3131
"import casual from 'casual';
3232
3333
casual.seed(0);
@@ -59,7 +59,66 @@ export const seedMocks = (seed: number) => casual.seed(seed);
5959
"
6060
`;
6161
62-
exports[`custom scalar generation for native and custom types using faker should generate custom scalars for native and custom types using faker 1`] = `
62+
exports[`Custom scalar generation using casual with different input/output configurations should generate distinct custom scalars for native and custom input/output types 1`] = `
63+
"
64+
export const anA = (overrides?: Partial<A>): A => {
65+
return {
66+
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 82,
67+
str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'ea corrupti qui incidunt eius consequatur blanditiis',
68+
obj: overrides && overrides.hasOwnProperty('obj') ? overrides.obj! : aB(),
69+
anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : '[email protected]',
70+
};
71+
};
72+
73+
export const aB = (overrides?: Partial<B>): B => {
74+
return {
75+
int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : -93,
76+
flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : -24.509902694262564,
77+
bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false,
78+
};
79+
};
80+
81+
export const aC = (overrides?: Partial<C>): C => {
82+
return {
83+
anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'itaque distinctio iure molestias voluptas reprehenderit quos',
84+
};
85+
};
86+
"
87+
`;
88+
89+
exports[`Custom scalar generation using casual with different input/output configurations should generate distinct dynamic custom scalars for native and custom types 1`] = `
90+
"import casual from 'casual';
91+
92+
casual.seed(0);
93+
94+
export const anA = (overrides?: Partial<A>): A => {
95+
return {
96+
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : casual['integer'](...[1,100]),
97+
str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : casual['string'],
98+
obj: overrides && overrides.hasOwnProperty('obj') ? overrides.obj! : aB(),
99+
anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : casual['email'],
100+
};
101+
};
102+
103+
export const aB = (overrides?: Partial<B>): B => {
104+
return {
105+
int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : casual['integer'](...[-100,0]),
106+
flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : casual['double'](...[-100,0]),
107+
bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false,
108+
};
109+
};
110+
111+
export const aC = (overrides?: Partial<C>): C => {
112+
return {
113+
anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : casual['string'],
114+
};
115+
};
116+
117+
export const seedMocks = (seed: number) => casual.seed(seed);
118+
"
119+
`;
120+
121+
exports[`custom scalar generation using faker should generate custom scalars for native and custom types 1`] = `
63122
"
64123
export const anA = (overrides?: Partial<A>): A => {
65124
return {
@@ -86,7 +145,7 @@ export const aC = (overrides?: Partial<C>): C => {
86145
"
87146
`;
88147
89-
exports[`custom scalar generation for native and custom types using faker should generate dynamic custom scalars for native and custom types using faker 1`] = `
148+
exports[`custom scalar generation using faker should generate dynamic custom scalars for native and custom types 1`] = `
90149
"import { fakerEN as faker } from '@faker-js/faker';
91150
92151
faker.seed(0);

tests/scalars/spec.ts

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,131 @@ describe('Custom scalar generation using casual', () => {
9898

9999
expect(result).toMatchSnapshot();
100100
});
101+
102+
describe('with different input/output configurations', () => {
103+
it('should generate distinct custom scalars for native and custom input/output types', async () => {
104+
const result = await plugin(testSchema, [], {
105+
generateLibrary: 'casual',
106+
scalars: {
107+
String: 'string',
108+
Float: {
109+
generator: 'double',
110+
arguments: [-100, 0],
111+
},
112+
ID: {
113+
generator: 'integer',
114+
arguments: [1, 100],
115+
},
116+
Boolean: 'false',
117+
Int: {
118+
generator: 'integer',
119+
arguments: [-100, 0],
120+
},
121+
AnyObject: {
122+
input: 'string',
123+
output: 'email',
124+
},
125+
},
126+
});
127+
128+
expect(result).toBeDefined();
129+
130+
// String
131+
expect(result).toContain(
132+
"str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'ea corrupti qui incidunt eius consequatur blanditiis',",
133+
);
134+
135+
// Float
136+
expect(result).toContain(
137+
"flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : -24.509902694262564,",
138+
);
139+
140+
// ID
141+
expect(result).toContain("id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 82,");
142+
143+
// Boolean
144+
expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false");
145+
146+
// Int
147+
expect(result).toContain("int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : -93,");
148+
149+
// AnyObject in type A (an email)
150+
expect(result).toContain(
151+
"anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : '[email protected]',",
152+
);
153+
154+
// AnyObject in input C (a string)
155+
expect(result).toContain(
156+
"anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'itaque distinctio iure molestias voluptas reprehenderit quos',",
157+
);
158+
159+
expect(result).toMatchSnapshot();
160+
});
161+
162+
it('should generate distinct dynamic custom scalars for native and custom types', async () => {
163+
const result = await plugin(testSchema, [], {
164+
generateLibrary: 'casual',
165+
dynamicValues: true,
166+
scalars: {
167+
String: 'string',
168+
Float: {
169+
generator: 'double',
170+
arguments: [-100, 0],
171+
},
172+
ID: {
173+
generator: 'integer',
174+
arguments: [1, 100],
175+
},
176+
Boolean: 'false',
177+
Int: {
178+
generator: 'integer',
179+
arguments: [-100, 0],
180+
},
181+
AnyObject: {
182+
input: 'string',
183+
output: 'email',
184+
},
185+
},
186+
});
187+
188+
expect(result).toBeDefined();
189+
190+
// String
191+
expect(result).toContain(
192+
"str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : casual['string'],",
193+
);
194+
195+
// Float
196+
expect(result).toContain(
197+
"flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : casual['double'](...[-100,0]),",
198+
);
199+
200+
// ID
201+
expect(result).toContain(
202+
"id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : casual['integer'](...[1,100]),",
203+
);
204+
205+
// Boolean
206+
expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false");
207+
208+
// Int
209+
expect(result).toContain(
210+
"int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : casual['integer'](...[-100,0]),",
211+
);
212+
213+
// AnyObject in type A (an email)
214+
expect(result).toContain(
215+
"anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : casual['email'],",
216+
);
217+
218+
// AnyObject in input C (an string)
219+
expect(result).toContain(
220+
"anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : casual['string'],",
221+
);
222+
223+
expect(result).toMatchSnapshot();
224+
});
225+
});
101226
});
102227

103228
describe('custom scalar generation using faker', () => {

0 commit comments

Comments
 (0)