feat: enhance DTO mock generation with dynamic import paths and mock value checks
This commit is contained in:
@@ -247,6 +247,7 @@ export function generateCleanArchitecture(
|
|||||||
|
|
||||||
const varsMap = Object.keys(rawProperties).map((k) => {
|
const varsMap = Object.keys(rawProperties).map((k) => {
|
||||||
let tsType = mapSwaggerTypeToTs(rawProperties[k].type);
|
let tsType = mapSwaggerTypeToTs(rawProperties[k].type);
|
||||||
|
const isInlineObject = rawProperties[k].type === 'object' && !rawProperties[k].$ref;
|
||||||
if (rawProperties[k].$ref) {
|
if (rawProperties[k].$ref) {
|
||||||
tsType = rawProperties[k].$ref.split('/').pop() || 'unknown';
|
tsType = rawProperties[k].$ref.split('/').pop() || 'unknown';
|
||||||
} else if (rawProperties[k].type === 'array' && rawProperties[k].items?.$ref) {
|
} else if (rawProperties[k].type === 'array' && rawProperties[k].items?.$ref) {
|
||||||
@@ -257,7 +258,8 @@ export function generateCleanArchitecture(
|
|||||||
originalName: k,
|
originalName: k,
|
||||||
dataType: tsType,
|
dataType: tsType,
|
||||||
description: rawProperties[k].description || '',
|
description: rawProperties[k].description || '',
|
||||||
required: requiredProps.includes(k)
|
required: requiredProps.includes(k),
|
||||||
|
hasMockValue: !isInlineObject
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -347,11 +349,18 @@ export function generateCleanArchitecture(
|
|||||||
name: safePropertyName(k),
|
name: safePropertyName(k),
|
||||||
mockValue: resolveMockValue(k, rawProperties[k], 'dto', schemaName)
|
mockValue: resolveMockValue(k, rawProperties[k], 'dto', schemaName)
|
||||||
}));
|
}));
|
||||||
const dtoMockImports = [...referencedTypes].filter(Boolean).map((name) => ({
|
const dtoMockImports = [...referencedTypes].filter(Boolean).map((name) => {
|
||||||
classname: name,
|
const targetTag = schemaTagMap[name] || 'shared';
|
||||||
classFilename: toCamelCase(name),
|
const targetFile = `${toCamelCase(name)}.dto.mock`;
|
||||||
tagFilename: schemaTagMap[name] || 'shared'
|
const importPath =
|
||||||
}));
|
targetTag === tagFilename ? `./${targetFile}` : `../${targetTag}/${targetFile}`;
|
||||||
|
return {
|
||||||
|
classname: name,
|
||||||
|
classFilename: toCamelCase(name),
|
||||||
|
tagFilename: targetTag,
|
||||||
|
importPath
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
const dtoMockViewData = {
|
const dtoMockViewData = {
|
||||||
tagFilename,
|
tagFilename,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{{#models}}
|
{{#models}}
|
||||||
{{#model}}
|
{{#model}}
|
||||||
{{#mockImports}}
|
{{#mockImports}}
|
||||||
import { mock{{classname}}Dto } from './{{classFilename}}.dto.mock';
|
import { mock{{classname}}Dto } from '{{{importPath}}}';
|
||||||
{{/mockImports}}
|
{{/mockImports}}
|
||||||
import { {{classname}}Dto } from './{{classFilename}}.dto';
|
import { {{classname}}Dto } from './{{classFilename}}.dto';
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ describe('{{classname}}', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
{{#vars}}
|
{{#vars}}
|
||||||
|
{{#hasMockValue}}
|
||||||
it('should allow setting {{name}}', () => {
|
it('should allow setting {{name}}', () => {
|
||||||
const model = new {{classname}}();
|
const model = new {{classname}}();
|
||||||
const expected = mock{{classname}}Model();
|
const expected = mock{{classname}}Model();
|
||||||
@@ -19,13 +20,16 @@ describe('{{classname}}', () => {
|
|||||||
expect(model.{{name}}).toBe(expected.{{name}});
|
expect(model.{{name}}).toBe(expected.{{name}});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
{{/hasMockValue}}
|
||||||
{{/vars}}
|
{{/vars}}
|
||||||
it('should build a valid model from mock', () => {
|
it('should build a valid model from mock', () => {
|
||||||
const model = mock{{classname}}Model();
|
const model = mock{{classname}}Model();
|
||||||
|
|
||||||
expect(model).toBeInstanceOf({{classname}});
|
expect(model).toBeInstanceOf({{classname}});
|
||||||
{{#vars}}
|
{{#vars}}
|
||||||
|
{{#hasMockValue}}
|
||||||
expect(model.{{name}}).toBeDefined();
|
expect(model.{{name}}).toBeDefined();
|
||||||
|
{{/hasMockValue}}
|
||||||
{{/vars}}
|
{{/vars}}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user