feat: enhance DTO mock generation with dynamic import paths and mock value checks

This commit is contained in:
2026-03-27 15:40:33 +01:00
parent e0446d4939
commit 16ef1ce3e3
3 changed files with 20 additions and 7 deletions

View File

@@ -247,6 +247,7 @@ export function generateCleanArchitecture(
const varsMap = Object.keys(rawProperties).map((k) => {
let tsType = mapSwaggerTypeToTs(rawProperties[k].type);
const isInlineObject = rawProperties[k].type === 'object' && !rawProperties[k].$ref;
if (rawProperties[k].$ref) {
tsType = rawProperties[k].$ref.split('/').pop() || 'unknown';
} else if (rawProperties[k].type === 'array' && rawProperties[k].items?.$ref) {
@@ -257,7 +258,8 @@ export function generateCleanArchitecture(
originalName: k,
dataType: tsType,
description: rawProperties[k].description || '',
required: requiredProps.includes(k)
required: requiredProps.includes(k),
hasMockValue: !isInlineObject
};
});
@@ -347,11 +349,18 @@ export function generateCleanArchitecture(
name: safePropertyName(k),
mockValue: resolveMockValue(k, rawProperties[k], 'dto', schemaName)
}));
const dtoMockImports = [...referencedTypes].filter(Boolean).map((name) => ({
classname: name,
classFilename: toCamelCase(name),
tagFilename: schemaTagMap[name] || 'shared'
}));
const dtoMockImports = [...referencedTypes].filter(Boolean).map((name) => {
const targetTag = schemaTagMap[name] || 'shared';
const targetFile = `${toCamelCase(name)}.dto.mock`;
const importPath =
targetTag === tagFilename ? `./${targetFile}` : `../${targetTag}/${targetFile}`;
return {
classname: name,
classFilename: toCamelCase(name),
tagFilename: targetTag,
importPath
};
});
const dtoMockViewData = {
tagFilename,

View File

@@ -1,7 +1,7 @@
{{#models}}
{{#model}}
{{#mockImports}}
import { mock{{classname}}Dto } from './{{classFilename}}.dto.mock';
import { mock{{classname}}Dto } from '{{{importPath}}}';
{{/mockImports}}
import { {{classname}}Dto } from './{{classFilename}}.dto';

View File

@@ -11,6 +11,7 @@ describe('{{classname}}', () => {
});
{{#vars}}
{{#hasMockValue}}
it('should allow setting {{name}}', () => {
const model = new {{classname}}();
const expected = mock{{classname}}Model();
@@ -19,13 +20,16 @@ describe('{{classname}}', () => {
expect(model.{{name}}).toBe(expected.{{name}});
});
{{/hasMockValue}}
{{/vars}}
it('should build a valid model from mock', () => {
const model = mock{{classname}}Model();
expect(model).toBeInstanceOf({{classname}});
{{#vars}}
{{#hasMockValue}}
expect(model.{{name}}).toBeDefined();
{{/hasMockValue}}
{{/vars}}
});
});