Skip to content

Commit 6245c05

Browse files
authored
allow listElementCount to be 0 in config (#129)
1 parent 9671528 commit 6245c05

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
555555
const enumValuesConvention = config.enumValues || 'change-case-all#pascalCase';
556556
const typeNamesConvention = config.typeNames || 'change-case-all#pascalCase';
557557
const transformUnderscore = config.transformUnderscore ?? true;
558-
const listElementCount = config.listElementCount > 0 ? config.listElementCount : 1;
558+
const listElementCount = Math.max(0, config.listElementCount ?? 1);
559559
const dynamicValues = !!config.dynamicValues;
560560
const generateLibrary = config.generateLibrary || 'casual';
561561
const enumsAsTypes = config.enumsAsTypes ?? false;

tests/__snapshots__/typescript-mock-data.spec.ts.snap

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,6 +2444,85 @@ export const aQuery = (overrides?: Partial<Query>): Query => {
24442444
"
24452445
`;
24462446

2447+
exports[`should generate no list elements when listElementCount is 0 1`] = `
2448+
"import { Avatar, User, WithAvatar, CamelCaseThing, PrefixedResponse, AbcType, ListType, UpdateUserInput, Mutation, Query, AbcStatus, Status, PrefixedEnum } from './types/graphql';
2449+
2450+
export const anAvatar = (overrides?: Partial<Avatar>): Avatar => {
2451+
return {
2452+
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '0550ff93-dd31-49b4-8c38-ff1cb68bdc38',
2453+
url: overrides && overrides.hasOwnProperty('url') ? overrides.url! : 'aliquid',
2454+
};
2455+
};
2456+
2457+
export const aUser = (overrides?: Partial<User>): User => {
2458+
return {
2459+
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : 'a5756f00-41a6-422a-8a7d-d13ee6a63750',
2460+
creationDate: overrides && overrides.hasOwnProperty('creationDate') ? overrides.creationDate! : '1970-01-09T16:33:21.532Z',
2461+
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'libero',
2462+
avatar: overrides && overrides.hasOwnProperty('avatar') ? overrides.avatar! : anAvatar(),
2463+
status: overrides && overrides.hasOwnProperty('status') ? overrides.status! : Status.Online,
2464+
customStatus: overrides && overrides.hasOwnProperty('customStatus') ? overrides.customStatus! : AbcStatus.HasXyzStatus,
2465+
scalarValue: overrides && overrides.hasOwnProperty('scalarValue') ? overrides.scalarValue! : 'neque',
2466+
camelCaseThing: overrides && overrides.hasOwnProperty('camelCaseThing') ? overrides.camelCaseThing! : aCamelCaseThing(),
2467+
unionThing: overrides && overrides.hasOwnProperty('unionThing') ? overrides.unionThing! : anAvatar(),
2468+
prefixedEnum: overrides && overrides.hasOwnProperty('prefixedEnum') ? overrides.prefixedEnum! : PrefixedEnum.PrefixedValue,
2469+
};
2470+
};
2471+
2472+
export const aWithAvatar = (overrides?: Partial<WithAvatar>): WithAvatar => {
2473+
return {
2474+
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '89f515e7-31e0-461d-a230-c4c7f4dafc5c',
2475+
avatar: overrides && overrides.hasOwnProperty('avatar') ? overrides.avatar! : anAvatar(),
2476+
};
2477+
};
2478+
2479+
export const aCamelCaseThing = (overrides?: Partial<CamelCaseThing>): CamelCaseThing => {
2480+
return {
2481+
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '345b9cf9-00fa-4974-800f-aeee5ee7fd42',
2482+
};
2483+
};
2484+
2485+
export const aPrefixedResponse = (overrides?: Partial<PrefixedResponse>): PrefixedResponse => {
2486+
return {
2487+
ping: overrides && overrides.hasOwnProperty('ping') ? overrides.ping! : 'sunt',
2488+
};
2489+
};
2490+
2491+
export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {
2492+
return {
2493+
abc: overrides && overrides.hasOwnProperty('abc') ? overrides.abc! : 'sit',
2494+
};
2495+
};
2496+
2497+
export const aListType = (overrides?: Partial<ListType>): ListType => {
2498+
return {
2499+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : [],
2500+
};
2501+
};
2502+
2503+
export const anUpdateUserInput = (overrides?: Partial<UpdateUserInput>): UpdateUserInput => {
2504+
return {
2505+
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',
2506+
login: overrides && overrides.hasOwnProperty('login') ? overrides.login! : 'qui',
2507+
avatar: overrides && overrides.hasOwnProperty('avatar') ? overrides.avatar! : anAvatar(),
2508+
};
2509+
};
2510+
2511+
export const aMutation = (overrides?: Partial<Mutation>): Mutation => {
2512+
return {
2513+
updateUser: overrides && overrides.hasOwnProperty('updateUser') ? overrides.updateUser! : aUser(),
2514+
};
2515+
};
2516+
2517+
export const aQuery = (overrides?: Partial<Query>): Query => {
2518+
return {
2519+
user: overrides && overrides.hasOwnProperty('user') ? overrides.user! : aUser(),
2520+
prefixed_query: overrides && overrides.hasOwnProperty('prefixed_query') ? overrides.prefixed_query! : aPrefixedResponse(),
2521+
};
2522+
};
2523+
"
2524+
`;
2525+
24472526
exports[`should generate single list element 1`] = `
24482527
"import { Avatar, User, WithAvatar, CamelCaseThing, PrefixedResponse, AbcType, ListType, UpdateUserInput, Mutation, Query, AbcStatus, Status, PrefixedEnum } from './types/graphql';
24492528

tests/typescript-mock-data.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,19 @@ it('should generate multiple list elements', async () => {
481481
expect(result).toMatchSnapshot();
482482
});
483483

484+
it('should generate no list elements when listElementCount is 0', async () => {
485+
const result = await plugin(testSchema, [], {
486+
typesFile: './types/graphql.ts',
487+
listElementCount: 0,
488+
});
489+
490+
expect(result).toBeDefined();
491+
expect(result).toContain(
492+
"stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : []",
493+
);
494+
expect(result).toMatchSnapshot();
495+
});
496+
484497
it('should generate dynamic values in mocks', async () => {
485498
const result = await plugin(testSchema, [], { dynamicValues: true });
486499

0 commit comments

Comments
 (0)