UNPKG

@cuba-platform/front-generator

Version:
134 lines 6.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const entities_generation_1 = require("../../../../generators/sdk/model/entities-generation"); const test_commons_1 = require("../../../test-commons"); const ts_helpers_1 = require("../../../../common/ts-helpers"); const import_utils_1 = require("../../../../generators/sdk/import-utils"); const entityModel = require('../../../fixtures/entity-model.json'); const projectModel = require('../../../fixtures/project-model--scr.json'); describe('generate TS entity', function () { it('should create entity class', function () { const enMap = fillEntitiesMap([ 'com.company.mpg.entity.Garage', 'com.company.mpg.entity.TechnicalCertificate', ], ['com.haulmont.cuba.core.entity.FileDescriptor']); const enumsMap = new Map(); enumsMap.set('com.company.mpg.entity.CarType', { name: { text: 'CarType' } }); enumsMap.set('com.company.mpg.entity.EcoRank', { name: { text: 'EcoRank' } }); const classTsNode = (0, entities_generation_1.createEntityClass)({ entity: entityModel, entitiesMap: enMap, enumsMap, isBaseProjectEntity: false }); let content = (0, ts_helpers_1.renderTSNodes)([classTsNode.classDeclaration]); let expected = '' + `export class Car { static NAME = "mpg$Car"; manufacturer?: string | null; model?: string | null; regNumber?: string | null; purchaseDate?: any | null; wheelOnRight?: boolean | null; carType?: CarType | null; ecoRank?: EcoRank | null; garage?: Garage | null; maxPassengers?: number | null; price?: any | null; mileage?: any | null; technicalCertificate?: TechnicalCertificate | null; photo?: FileDescriptor | null; }`; (0, test_commons_1.assertContent)(content, expected); const includes = (0, import_utils_1.createIncludes)(classTsNode.importInfos, undefined); content = (0, ts_helpers_1.renderTSNodes)(includes); expected = '' + `import { CarType, EcoRank } from "../enums/enums"; import { Garage } from "./mpg$Garage"; import { TechnicalCertificate } from "./mpg$TechnicalCertificate"; import { FileDescriptor } from "./sys$FileDescriptor";`; (0, test_commons_1.assertContent)(content, expected); }); it('should create an Integer ID entity class', () => { const integerIdEntityModel = projectModel.entities.find((e) => e.name === 'scr_IntegerIdTestEntity'); const classTsNode = (0, entities_generation_1.createEntityClass)({ entity: integerIdEntityModel, entitiesMap: new Map(), enumsMap: new Map(), isBaseProjectEntity: false }); let content = (0, ts_helpers_1.renderTSNodes)([classTsNode.classDeclaration]); let expected = `export class IntegerIdTestEntity { static NAME = "scr_IntegerIdTestEntity"; description?: string | null; createTs?: any | null; createdBy?: string | null; updateTs?: any | null; updatedBy?: string | null; deleteTs?: any | null; deletedBy?: string | null; version?: number | null; datatypesTestEntity3?: any | null; datatypesTestEntities?: any | null; }`; (0, test_commons_1.assertContent)(content, expected); }); it('should create a String ID entity class', () => { const stringIdEntityModel = projectModel.entities.find((e) => e.name === 'scr_StringIdTestEntity'); const classTsNode = (0, entities_generation_1.createEntityClass)({ entity: stringIdEntityModel, entitiesMap: new Map(), enumsMap: new Map(), isBaseProjectEntity: false }); let content = (0, ts_helpers_1.renderTSNodes)([classTsNode.classDeclaration]); let expected = `export class StringIdTestEntity { static NAME = "scr_StringIdTestEntity"; id?: string; description?: string | null; productCode?: string | null; createTs?: any | null; createdBy?: string | null; updateTs?: any | null; updatedBy?: string | null; deleteTs?: any | null; deletedBy?: string | null; version?: number | null; datatypesTestEntity?: any | null; datatypesTestEntity3?: any | null; }`; (0, test_commons_1.assertContent)(content, expected); }); it('should create a class for a String ID entity that has an attribute named `id` ' + 'but the actual ID attribute has a different name', () => { const weirdStringIdEntityModel = projectModel.entities.find((e) => e.name === 'scr_WeirdStringIdTestEntity'); const classTsNode = (0, entities_generation_1.createEntityClass)({ entity: weirdStringIdEntityModel, entitiesMap: new Map(), enumsMap: new Map(), isBaseProjectEntity: false }); let content = (0, ts_helpers_1.renderTSNodes)([classTsNode.classDeclaration]); let expected = `export class WeirdStringIdTestEntity { static NAME = "scr_WeirdStringIdTestEntity"; id?: string; description?: string | null; createTs?: any | null; createdBy?: string | null; updateTs?: any | null; updatedBy?: string | null; deleteTs?: any | null; deletedBy?: string | null; version?: number | null; datatypesTestEntity3?: any | null; }`; (0, test_commons_1.assertContent)(content, expected); }); }); function fillEntitiesMap(classNames, baseClassNames) { const entitiesMap = new Map(); classNames.forEach(en => entitiesMap.set(en, (0, test_commons_1.createTestProjectEntityInfo)(en))); baseClassNames.forEach(en => entitiesMap.set(en, (0, test_commons_1.createTestProjectEntityInfo)(en, true))); return entitiesMap; } //# sourceMappingURL=entities-generation.test.js.map