Skip to content

Commit 91477c2

Browse files
authored
fix: listElementCount sometimes undefined (#94)
Update the generator to make sure `listElementCount` is set to 1 when undefined The output was also different than before version `2.4.0` since `fieldName` changed when generating list items This PR makes sure the output of generated files is the same as before if we don't touch `listElementCount` option
1 parent d40b9e6 commit 91477c2

File tree

3 files changed

+40
-38
lines changed

3 files changed

+40
-38
lines changed

src/index.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ type Options<T = TypeNode> = {
2121
currentType: T;
2222
customScalars?: ScalarMap;
2323
transformUnderscore: boolean;
24-
listElementCount?: number;
25-
dynamicValues?: boolean;
24+
listElementCount: number;
25+
dynamicValues: boolean;
2626
};
2727

2828
const convertName = (value: string, fn: (v: string) => string, transformUnderscore: boolean): string => {
@@ -220,7 +220,7 @@ const generateMockValue = (opts: Options): string | number | boolean => {
220220
const listElements = Array.from({ length: opts.listElementCount }, (_, index) =>
221221
generateMockValue({
222222
...opts,
223-
fieldName: opts.fieldName + index,
223+
fieldName: opts.listElementCount === 1 ? opts.fieldName : `${opts.fieldName}${index}`,
224224
currentType: (opts.currentType as ListTypeNode).type,
225225
}),
226226
);
@@ -371,6 +371,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
371371
const typenamesConvention = config.typenames || 'pascal-case#pascalCase';
372372
const transformUnderscore = config.transformUnderscore ?? true;
373373
const listElementCount = config.listElementCount > 0 ? config.listElementCount : 1;
374+
const dynamicValues = !!config.dynamicValues;
374375
// List of types that are enums
375376
const types: TypeItem[] = [];
376377
const visitor: VisitorType = {
@@ -414,7 +415,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
414415
customScalars: config.scalars,
415416
transformUnderscore,
416417
listElementCount,
417-
dynamicValues: config.dynamicValues,
418+
dynamicValues,
418419
});
419420

420421
return ` ${fieldName}: overrides && overrides.hasOwnProperty('${fieldName}') ? overrides.${fieldName}! : ${value},`;
@@ -443,7 +444,8 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
443444
currentType: field.type,
444445
customScalars: config.scalars,
445446
transformUnderscore,
446-
dynamicValues: config.dynamicValues,
447+
listElementCount,
448+
dynamicValues,
447449
});
448450

449451
return ` ${field.name.value}: overrides && overrides.hasOwnProperty('${field.name.value}') ? overrides.${field.name.value}! : ${value},`;
@@ -541,11 +543,11 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
541543
.join('\n');
542544

543545
let mockFile = '';
544-
if (config.dynamicValues) mockFile += "import casual from 'casual';\n";
546+
if (dynamicValues) mockFile += "import casual from 'casual';\n";
545547
mockFile += typesFileImport;
546-
if (config.dynamicValues) mockFile += '\ncasual.seed(0);\n';
548+
if (dynamicValues) mockFile += '\ncasual.seed(0);\n';
547549
mockFile += mockFns;
548-
if (config.dynamicValues) mockFile += '\n\nexport const seedMocks = (seed: number) => casual.seed(seed);';
550+
if (dynamicValues) mockFile += '\n\nexport const seedMocks = (seed: number) => casual.seed(seed);';
549551
mockFile += '\n';
550552
return mockFile;
551553
};

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

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export const mockAbcType = (overrides?: Partial<AbcType>): AbcType => {
5151

5252
export const mockListType = (overrides?: Partial<ListType>): ListType => {
5353
return {
54-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
54+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
5555
};
5656
};
5757

@@ -129,7 +129,7 @@ export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {
129129

130130
export const aListType = (overrides?: Partial<ListType>): ListType => {
131131
return {
132-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
132+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
133133
};
134134
};
135135

@@ -208,7 +208,7 @@ export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {
208208

209209
export const aListType = (overrides?: Partial<ListType>): ListType => {
210210
return {
211-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
211+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
212212
};
213213
};
214214

@@ -287,7 +287,7 @@ export const anAbcType = (overrides?: Partial<Api.AbcType>): Api.AbcType => {
287287

288288
export const aListType = (overrides?: Partial<Api.ListType>): Api.ListType => {
289289
return {
290-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
290+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
291291
};
292292
};
293293

@@ -365,7 +365,7 @@ export const anAbcType = (overrides?: Partial<Api.AbcType>): Api.AbcType => {
365365

366366
export const aListType = (overrides?: Partial<Api.ListType>): Api.ListType => {
367367
return {
368-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
368+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
369369
};
370370
};
371371

@@ -444,7 +444,7 @@ export const anAbcType = (overrides?: Partial<Api.AbcType>): Api.AbcType => {
444444

445445
export const aListType = (overrides?: Partial<Api.ListType>): Api.ListType => {
446446
return {
447-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
447+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
448448
};
449449
};
450450

@@ -522,7 +522,7 @@ export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {
522522

523523
export const aListType = (overrides?: Partial<ListType>): ListType => {
524524
return {
525-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
525+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
526526
};
527527
};
528528

@@ -600,7 +600,7 @@ export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {
600600

601601
export const aListType = (overrides?: Partial<ListType>): ListType => {
602602
return {
603-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
603+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
604604
};
605605
};
606606

@@ -678,7 +678,7 @@ export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {
678678

679679
export const aListType = (overrides?: Partial<ListType>): ListType => {
680680
return {
681-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
681+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
682682
};
683683
};
684684

@@ -756,7 +756,7 @@ export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {
756756

757757
export const aListType = (overrides?: Partial<ListType>): ListType => {
758758
return {
759-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
759+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
760760
};
761761
};
762762

@@ -834,7 +834,7 @@ export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {
834834

835835
export const aListType = (overrides?: Partial<ListType>): ListType => {
836836
return {
837-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
837+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
838838
};
839839
};
840840

@@ -995,7 +995,7 @@ export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {
995995

996996
export const aListType = (overrides?: Partial<ListType>): ListType => {
997997
return {
998-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
998+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
999999
};
10001000
};
10011001

@@ -1074,7 +1074,7 @@ export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {
10741074

10751075
export const aListType = (overrides?: Partial<ListType>): ListType => {
10761076
return {
1077-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
1077+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
10781078
};
10791079
};
10801080

@@ -1152,7 +1152,7 @@ export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {
11521152

11531153
export const aListType = (overrides?: Partial<ListType>): ListType => {
11541154
return {
1155-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
1155+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
11561156
};
11571157
};
11581158

@@ -1230,7 +1230,7 @@ export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {
12301230

12311231
export const aListType = (overrides?: Partial<ListType>): ListType => {
12321232
return {
1233-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
1233+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
12341234
};
12351235
};
12361236

@@ -1308,7 +1308,7 @@ export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {
13081308

13091309
export const aListType = (overrides?: Partial<ListType>): ListType => {
13101310
return {
1311-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
1311+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
13121312
};
13131313
};
13141314

@@ -1386,7 +1386,7 @@ export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {
13861386

13871387
export const aListType = (overrides?: Partial<ListType>): ListType => {
13881388
return {
1389-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
1389+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
13901390
};
13911391
};
13921392

@@ -1465,7 +1465,7 @@ export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {
14651465

14661466
export const aListType = (overrides?: Partial<ListType>): ListType => {
14671467
return {
1468-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
1468+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
14691469
};
14701470
};
14711471

@@ -1543,7 +1543,7 @@ export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {
15431543

15441544
export const aListType = (overrides?: Partial<ListType>): ListType => {
15451545
return {
1546-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
1546+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
15471547
};
15481548
};
15491549

@@ -1621,7 +1621,7 @@ export const anABCType = (overrides?: Partial<ABCType>): ABCType => {
16211621

16221622
export const aListType = (overrides?: Partial<ListType>): ListType => {
16231623
return {
1624-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
1624+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
16251625
};
16261626
};
16271627

@@ -1706,7 +1706,7 @@ export const anAbcType = (overrides?: Partial<AbcType>): { __typename: 'ABCType'
17061706
export const aListType = (overrides?: Partial<ListType>): { __typename: 'ListType' } & ListType => {
17071707
return {
17081708
__typename: 'ListType',
1709-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
1709+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
17101710
};
17111711
};
17121712

@@ -1786,7 +1786,7 @@ export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {
17861786

17871787
export const aListType = (overrides?: Partial<ListType>): ListType => {
17881788
return {
1789-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
1789+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
17901790
};
17911791
};
17921792

@@ -1864,7 +1864,7 @@ export const anABCTYPE = (overrides?: Partial<ABCTYPE>): ABCTYPE => {
18641864

18651865
export const aLISTTYPE = (overrides?: Partial<LISTTYPE>): LISTTYPE => {
18661866
return {
1867-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
1867+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
18681868
};
18691869
};
18701870

@@ -1943,7 +1943,7 @@ export const anABCTYPE = (overrides?: Partial<ABCTYPE>): ABCTYPE => {
19431943

19441944
export const aLISTTYPE = (overrides?: Partial<LISTTYPE>): LISTTYPE => {
19451945
return {
1946-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
1946+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
19471947
};
19481948
};
19491949

@@ -2101,7 +2101,7 @@ export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {
21012101

21022102
export const aListType = (overrides?: Partial<ListType>): ListType => {
21032103
return {
2104-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
2104+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
21052105
};
21062106
};
21072107

@@ -2180,7 +2180,7 @@ export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {
21802180

21812181
export const aListType = (overrides?: Partial<ListType>): ListType => {
21822182
return {
2183-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
2183+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
21842184
};
21852185
};
21862186

@@ -2259,7 +2259,7 @@ export const anAbcType = (overrides?: Partial<ApiAbcType>): ApiAbcType => {
22592259

22602260
export const aListType = (overrides?: Partial<ApiListType>): ApiListType => {
22612261
return {
2262-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
2262+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
22632263
};
22642264
};
22652265

@@ -2338,7 +2338,7 @@ export const anAbcType = (overrides?: Partial<AbcType>): AbcType => {
23382338

23392339
export const aListType = (overrides?: Partial<ListType>): ListType => {
23402340
return {
2341-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
2341+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
23422342
};
23432343
};
23442344

@@ -2423,7 +2423,7 @@ export const anAbcType = (overrides?: Partial<AbcType>, _relationshipsToOmit: Ar
24232423
export const aListType = (overrides?: Partial<ListType>, _relationshipsToOmit: Array<string> = []): ListType => {
24242424
const relationshipsToOmit = ([..._relationshipsToOmit, 'ListType']);
24252425
return {
2426-
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id'],
2426+
stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem'],
24272427
};
24282428
};
24292429

tests/typescript-mock-data.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ it('should generate single list element', async () => {
401401

402402
expect(result).toBeDefined();
403403
expect(result).toContain(
404-
"stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['id']",
404+
"stringList: overrides && overrides.hasOwnProperty('stringList') ? overrides.stringList! : ['voluptatem']",
405405
);
406406
expect(result).toMatchSnapshot();
407407
});

0 commit comments

Comments
 (0)