Skip to content

Commit 0dc8325

Browse files
nicolacticNicolay
andauthored
fix(mock): makes resolveMockValue only return one factory import per ref-property (#3614)
* fix(mock): make resolveMockValue only return one factory import per ref-property * test(mock): cleans up unnecessary properties and assertions, drops looping N times --------- Co-authored-by: Nicolay <nicolay.mohebi@itverket.no>
1 parent 76de240 commit 0dc8325

2 files changed

Lines changed: 58 additions & 3 deletions

File tree

packages/mock/src/faker/index.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type {
22
ClientMockBuilder,
33
FakerMockOptions,
4+
GeneratorImport,
45
GeneratorOptions,
56
GeneratorSchema,
67
GeneratorVerbOptions,
@@ -18,6 +19,7 @@ import {
1819
generateFakerForSchemas,
1920
generateFakerImports,
2021
} from './index';
22+
import { resolveMockValue } from './resolvers';
2123

2224
const mockVerbOptions = {
2325
operationId: 'getUser',
@@ -305,3 +307,55 @@ describe('generateFakerForSchemas strict mock types (#3525)', () => {
305307
);
306308
});
307309
});
310+
311+
describe('resolveMockValue returns one factory import per ref-property (#3606)', () => {
312+
// Delegation to a `get<X>Mock` factory requires output.schemas to be set and
313+
// the $ref to point at a components schema.
314+
const context = createTestContextSpec({
315+
output: {
316+
schemas: 'model',
317+
mock: {
318+
indexMockFiles: false,
319+
generators: [{ type: OutputMockType.FAKER, schemas: true }],
320+
},
321+
},
322+
spec: {
323+
components: {
324+
schemas: {
325+
LeafDTO: { type: 'object', properties: { id: { type: 'string' } } },
326+
},
327+
},
328+
},
329+
});
330+
331+
const resolveLeafRef = (imports: GeneratorImport[]) =>
332+
resolveMockValue({
333+
schema: { $ref: '#/components/schemas/LeafDTO' },
334+
operationId: 'getUser',
335+
tags: [],
336+
context,
337+
imports,
338+
existingReferencedProperties: [],
339+
splitMockImplementations: [],
340+
});
341+
342+
it('returns only its own factory import', () => {
343+
// Shared imports array given to both calls. The first call can't reveal the
344+
// bug since imports is empty.
345+
const imports: GeneratorImport[] = [];
346+
347+
const first = resolveLeafRef(imports);
348+
expect(first.imports).toHaveLength(1);
349+
expect(first.imports[0]).toMatchObject({
350+
name: 'getLeafDTOMock',
351+
schemaFactory: true,
352+
});
353+
354+
const second = resolveLeafRef(imports);
355+
expect(second.imports).toHaveLength(1);
356+
expect(second.imports[0]).toMatchObject({
357+
name: 'getLeafDTOMock',
358+
schemaFactory: true,
359+
});
360+
});
361+
});

packages/mock/src/faker/resolvers/value.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,11 +356,12 @@ export function resolveMockValue({
356356

357357
if (canDelegate) {
358358
const factoryName = `get${pascal(name)}Mock`;
359-
imports.push({
359+
const factoryImport: GeneratorImport = {
360360
name: factoryName,
361361
values: true,
362362
schemaFactory: true,
363-
});
363+
};
364+
364365
// For object-like refs the historical inline output is `{ ...body }`
365366
// so the spread form keeps callers (combineSchemasMock, object
366367
// properties) working without other changes. For everything else
@@ -385,7 +386,7 @@ export function resolveMockValue({
385386
Boolean(newSchema.nullable),
386387
mockOptions?.nonNullable,
387388
),
388-
imports,
389+
imports: [factoryImport],
389390
name: newSchema.name,
390391
type: getType(newSchema),
391392
nullWrapped: Boolean(newSchema.nullable) && !mockOptions?.nonNullable,

0 commit comments

Comments
 (0)