Skip to content

Commit 9744479

Browse files
add coverage for faker
1 parent 1fdde47 commit 9744479

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed

tests/scalars/__snapshots__/spec.ts.snap

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,62 @@ export const aC = (overrides?: Partial<C>): C => {
176176
export const seedMocks = (seed: number) => faker.seed(seed);
177177
"
178178
`;
179+
180+
exports[`custom scalar generation using faker with different input/output configurations should generate distinct custom scalars for native and custom input/output types 1`] = `
181+
"
182+
export const anA = (overrides?: Partial<A>): A => {
183+
return {
184+
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 83,
185+
str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'Depereo nulla calco blanditiis cornu defetiscor.',
186+
obj: overrides && overrides.hasOwnProperty('obj') ? overrides.obj! : aB(),
187+
anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : '[email protected]',
188+
};
189+
};
190+
191+
export const aB = (overrides?: Partial<B>): B => {
192+
return {
193+
int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : -93,
194+
flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : -24.51,
195+
bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false,
196+
};
197+
};
198+
199+
export const aC = (overrides?: Partial<C>): C => {
200+
return {
201+
anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'vilicus',
202+
};
203+
};
204+
"
205+
`;
206+
207+
exports[`custom scalar generation using faker with different input/output configurations should generate distinct dynamic custom scalars for native and custom types 1`] = `
208+
"import { fakerEN as faker } from '@faker-js/faker';
209+
210+
faker.seed(0);
211+
212+
export const anA = (overrides?: Partial<A>): A => {
213+
return {
214+
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : faker['number']['int'](...[{\\"min\\":1,\\"max\\":100}]),
215+
str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : faker['lorem']['sentence'](),
216+
obj: overrides && overrides.hasOwnProperty('obj') ? overrides.obj! : aB(),
217+
anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : faker['internet']['email'](),
218+
};
219+
};
220+
221+
export const aB = (overrides?: Partial<B>): B => {
222+
return {
223+
int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : faker['number']['int'](...[{\\"min\\":-100,\\"max\\":0}]),
224+
flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : faker['number']['float'](...[{\\"min\\":-100,\\"max\\":0}]),
225+
bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false,
226+
};
227+
};
228+
229+
export const aC = (overrides?: Partial<C>): C => {
230+
return {
231+
anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : faker['lorem']['word'](),
232+
};
233+
};
234+
235+
export const seedMocks = (seed: number) => faker.seed(seed);
236+
"
237+
`;

tests/scalars/spec.ts

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,4 +320,127 @@ describe('custom scalar generation using faker', () => {
320320

321321
expect(result).toMatchSnapshot();
322322
});
323+
324+
describe('with different input/output configurations', () => {
325+
it('should generate distinct custom scalars for native and custom input/output types', async () => {
326+
const result = await plugin(testSchema, [], {
327+
generateLibrary: 'faker',
328+
scalars: {
329+
String: 'lorem.sentence',
330+
Float: {
331+
generator: 'number.float',
332+
arguments: [{ min: -100, max: 0, fractionDigits: 2 }],
333+
},
334+
ID: {
335+
generator: 'number.int',
336+
arguments: [{ min: 1, max: 100 }],
337+
},
338+
Boolean: 'false',
339+
Int: {
340+
generator: 'number.int',
341+
arguments: [{ min: -100, max: 0 }],
342+
},
343+
AnyObject: {
344+
input: 'lorem.word',
345+
output: 'internet.email',
346+
},
347+
},
348+
});
349+
350+
expect(result).toBeDefined();
351+
352+
// String
353+
expect(result).toContain(
354+
"str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : 'Depereo nulla calco blanditiis cornu defetiscor.',",
355+
);
356+
357+
// Float
358+
expect(result).toContain("flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : -24.51,");
359+
360+
// ID
361+
expect(result).toContain("id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 83,");
362+
363+
// Boolean
364+
expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false");
365+
366+
// Int
367+
expect(result).toContain("int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : -93,");
368+
369+
// AnyObject in type A (an email)
370+
expect(result).toContain(
371+
"anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : '[email protected]',",
372+
);
373+
374+
// AnyObject in input C (a string)
375+
expect(result).toContain(
376+
"anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : 'vilicus',",
377+
);
378+
379+
expect(result).toMatchSnapshot();
380+
});
381+
382+
it('should generate distinct dynamic custom scalars for native and custom types', async () => {
383+
const result = await plugin(testSchema, [], {
384+
generateLibrary: 'faker',
385+
dynamicValues: true,
386+
scalars: {
387+
String: 'lorem.sentence',
388+
Float: {
389+
generator: 'number.float',
390+
arguments: [{ min: -100, max: 0 }],
391+
},
392+
ID: {
393+
generator: 'number.int',
394+
arguments: [{ min: 1, max: 100 }],
395+
},
396+
Boolean: 'false',
397+
Int: {
398+
generator: 'number.int',
399+
arguments: [{ min: -100, max: 0 }],
400+
},
401+
AnyObject: {
402+
input: 'lorem.word',
403+
output: 'internet.email',
404+
},
405+
},
406+
});
407+
408+
expect(result).toBeDefined();
409+
410+
// String
411+
expect(result).toContain(
412+
"str: overrides && overrides.hasOwnProperty('str') ? overrides.str! : faker['lorem']['sentence'](),",
413+
);
414+
415+
// Float
416+
expect(result).toContain(
417+
"flt: overrides && overrides.hasOwnProperty('flt') ? overrides.flt! : faker['number']['float'](...[{\"min\":-100,\"max\":0}]),",
418+
);
419+
420+
// ID
421+
expect(result).toContain(
422+
"id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : faker['number']['int'](...[{\"min\":1,\"max\":100}]),",
423+
);
424+
425+
// Boolean
426+
expect(result).toContain("bool: overrides && overrides.hasOwnProperty('bool') ? overrides.bool! : false");
427+
428+
// Int
429+
expect(result).toContain(
430+
"int: overrides && overrides.hasOwnProperty('int') ? overrides.int! : faker['number']['int'](...[{\"min\":-100,\"max\":0}]),",
431+
);
432+
433+
// AnyObject in type A (an email)
434+
expect(result).toContain(
435+
"anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : faker['internet']['email'](),",
436+
);
437+
438+
// AnyObject in input C (a string)
439+
expect(result).toContain(
440+
"anyObject: overrides && overrides.hasOwnProperty('anyObject') ? overrides.anyObject! : faker['lorem']['word'](),",
441+
);
442+
443+
expect(result).toMatchSnapshot();
444+
});
445+
});
323446
});

0 commit comments

Comments
 (0)