From 4948d952850d295d85f11af29d909ad63418a74f Mon Sep 17 00:00:00 2001 From: Charlie Mordant Date: Wed, 30 Oct 2024 21:29:37 +0100 Subject: [PATCH] generator-specific entity --- generators/angular/generator.ts | 102 ++++++------------ ...er-management-detail.component.spec.ts.ejs | 2 +- .../user-management.component.spec.ts.ejs | 2 +- .../list/user-management.component.ts.ejs | 2 +- .../user-management.service.spec.ts.ejs | 2 +- ...er-management-update.component.spec.ts.ejs | 2 +- .../user-management.model.ts.ejs | 2 +- .../_entityFolder_/_entityFile_.model.ts.ejs | 2 +- .../_entityFile_.test-samples.ts.ejs | 10 +- ...yFile_-delete-dialog.component.spec.ts.ejs | 2 +- .../_entityFile_-detail.component.spec.ts.ejs | 4 +- .../list/_entityFile_.component.spec.ts.ejs | 6 +- ...yFile_-routing-resolve.service.spec.ts.ejs | 2 +- .../service/_entityFile_.service.spec.ts.ejs | 14 +-- .../_entityFile_-update.component.html.ejs | 6 +- .../_entityFile_-update.component.spec.ts.ejs | 22 ++-- .../_entityFile_-update.component.ts.ejs | 2 +- .../app/entities/entity-navbar-items.ts.ejs | 2 +- generators/angular/types.d.ts | 53 ++++++++- lib/types/application/entity.d.ts | 8 +- 20 files changed, 126 insertions(+), 121 deletions(-) diff --git a/generators/angular/generator.ts b/generators/angular/generator.ts index cf13b51d9b05..fca505231598 100644 --- a/generators/angular/generator.ts +++ b/generators/angular/generator.ts @@ -18,7 +18,6 @@ */ import chalk from 'chalk'; import { isFileStateModified } from 'mem-fs-editor/state'; -import type { Entity } from '../../lib/types/application/entity.js'; import BaseApplicationGenerator from '../base-application/index.js'; import { GENERATOR_ANGULAR, GENERATOR_CLIENT, GENERATOR_LANGUAGES } from '../generator-list.js'; import { defaultLanguage } from '../languages/support/index.js'; @@ -47,17 +46,15 @@ import { isTranslatedAngularFile, translateAngularFilesTransform, } from './support/index.js'; -import type { AngularApplication } from './types.js'; +import type { AngularApplication, AngularEntity } from './types.js'; const { ANGULAR } = clientFrameworkTypes; export default class AngularGenerator extends BaseApplicationGenerator< - Entity, + AngularEntity, AngularApplication, - DefaultTaskTypes + DefaultTaskTypes > { - localEntities?: any[]; - async beforeQueue() { if (!this.fromBlueprint) { await this.composeWithBlueprints(); @@ -92,6 +89,12 @@ export default class AngularGenerator extends BaseApplicationGenerator< get preparing() { return this.asPreparingTaskGroup({ + prepareApplication({ application }) { + // @ts-ignore + application.getTypescriptKeyType = primaryKey => { + return getTSKeyType(primaryKey); + }; + }, applicationDefauts({ application, applicationDefaults }) { applicationDefaults({ __override__: true, @@ -99,7 +102,6 @@ export default class AngularGenerator extends BaseApplicationGenerator< webappEnumerationsDir: app => `${app.clientSrcDir}app/entities/enumerations/`, angularLocaleId: app => app.nativeLanguageDefinition.angularLocale ?? defaultLanguage.angularLocale, }); - application.addPrettierExtensions?.(['html', 'css', 'scss']); }, addNeedles({ source, application }) { @@ -114,7 +116,6 @@ export default class AngularGenerator extends BaseApplicationGenerator< const editCallback = addToEntitiesMenu(param); this.editFile(filePath, { ignoreNonExisting }, editCallback); }; - source.addAdminRoute = (args: Omit[0], 'needle'>) => this.editFile( `${application.srcMainWebapp}app/admin/admin.routes.ts`, @@ -175,6 +176,26 @@ export default class AngularGenerator extends BaseApplicationGenerator< ...(entity.entityReadAuthority?.split(',') ?? []), ]), }); + + entity.generateEntityClientEnumImports = fields => { + return getClientEnumImportsFormat(fields, ANGULAR); + }; + + entity.generateTestEntityPrimaryKey = (primaryKey, index) => { + return getTestEntityPrimaryKey(primaryKey, index); + }; + + entity.generateTypescriptTestEntity = (references, additionalFields) => { + return generateTestEntity(references, additionalFields); + }; + + entity.buildAngularFormPath = (reference, prefix = []) => { + return angularFormPath(reference, prefix); + }; + + entity.generateTestEntityId = (primaryKey, index = 0, wrapped = true) => { + return getTestEntityId(primaryKey, index, wrapped); + }; }, }); } @@ -220,9 +241,9 @@ export default class AngularGenerator extends BaseApplicationGenerator< get default() { return this.asDefaultTaskGroup({ - loadEntities() { + loadEntities({ application }) { const entities = this.sharedData.getEntities().map(({ entity }) => entity); - this.localEntities = entities.filter(entity => !entity.builtIn && !entity.skipClient); + application.frontendEntities = entities.filter(entity => !entity.builtIn && !entity.skipClient); }, queueTranslateTransform({ control, application }) { const { enableTranslation, jhiPrefix } = application; @@ -324,65 +345,4 @@ export default class AngularGenerator extends BaseApplicationGenerator< get [BaseApplicationGenerator.END]() { return this.delegateTasksToBlueprint(() => this.end); } - /** - * Returns the typescript import section of enums referenced by all fields of the entity. - * @param fields returns the import of enums that are referenced by the fields - * @returns {typeImports:Map} the fields that potentially contains some enum types - */ - generateEntityClientEnumImports(fields) { - return getClientEnumImportsFormat(fields, ANGULAR); - } - - /** - * Get the typescript type of a non-composite primary key - * @param primaryKey the primary key of the entity - * @returns {string} the typescript type. - */ - getTypescriptKeyType(primaryKey) { - return getTSKeyType(primaryKey); - } - - /** - * generates a value for a primary key type - * @param primaryKey the primary key attribute (or its type) of the entity - * @param index an index to add salt to the value - * @param wrapped if the value should be within quotes - * @returns {string|number|string} - */ - generateTestEntityId(primaryKey, index = 0, wrapped = true) { - return getTestEntityId(primaryKey, index, wrapped); - } - /** - * @private - * Generate a test entity, for the PK references (when the PK is a composite, derived key) - * - * @param {any} primaryKey - primary key definition. - * @param {number} [index] - index of the primary key sample, pass undefined for a random key. - */ - generateTestEntityPrimaryKey(primaryKey, index) { - return getTestEntityPrimaryKey(primaryKey, index); - } - - /** - * @private - * Generate a test entity instance with faked values. - * - * @param {any} references - references to other entities. - * @param {any} additionalFields - additional fields to add to the entity or with default values that overrides generated values. - */ - generateTypescriptTestEntity(references, additionalFields) { - return generateTestEntity(references, additionalFields); - } - - /** - * @private - * Create a angular form path getter method of reference. - * - * @param {object} reference - * @param {string[]} prefix - * @return {string} - */ - buildAngularFormPath(reference, prefix = []) { - return angularFormPath(reference, prefix); - } } diff --git a/generators/angular/templates/src/main/webapp/app/admin/user-management/detail/user-management-detail.component.spec.ts.ejs b/generators/angular/templates/src/main/webapp/app/admin/user-management/detail/user-management-detail.component.spec.ts.ejs index d38cefed6d44..7be36602f72d 100644 --- a/generators/angular/templates/src/main/webapp/app/admin/user-management/detail/user-management-detail.component.spec.ts.ejs +++ b/generators/angular/templates/src/main/webapp/app/admin/user-management/detail/user-management-detail.component.spec.ts.ejs @@ -17,7 +17,7 @@ limitations under the License. -%> <%_ -const tsKeyId = this.generateTestEntityId(user.primaryKey.type); +const tsKeyId = generateTestEntityId(user.primaryKey.type); _%> import { TestBed } from '@angular/core/testing'; import { provideRouter, withComponentInputBinding } from '@angular/router'; diff --git a/generators/angular/templates/src/main/webapp/app/admin/user-management/list/user-management.component.spec.ts.ejs b/generators/angular/templates/src/main/webapp/app/admin/user-management/list/user-management.component.spec.ts.ejs index 8863770d6d44..0479ef1018ca 100644 --- a/generators/angular/templates/src/main/webapp/app/admin/user-management/list/user-management.component.spec.ts.ejs +++ b/generators/angular/templates/src/main/webapp/app/admin/user-management/list/user-management.component.spec.ts.ejs @@ -17,7 +17,7 @@ limitations under the License. -%> <%_ -const tsKeyId = this.generateTestEntityId(user.primaryKey.type); +const tsKeyId = generateTestEntityId(user.primaryKey.type); _%> jest.mock('app/core/auth/account.service'); diff --git a/generators/angular/templates/src/main/webapp/app/admin/user-management/list/user-management.component.ts.ejs b/generators/angular/templates/src/main/webapp/app/admin/user-management/list/user-management.component.ts.ejs index 9092829a518c..796ebbe2e4fb 100644 --- a/generators/angular/templates/src/main/webapp/app/admin/user-management/list/user-management.component.ts.ejs +++ b/generators/angular/templates/src/main/webapp/app/admin/user-management/list/user-management.component.ts.ejs @@ -83,7 +83,7 @@ export default class UserManagementComponent implements OnInit { this.userService.update({ ...user, activated: isActivated }).subscribe(() => this.loadAll()); } - trackIdentity(item: User): <%= this.getTypescriptKeyType(user.primaryKey.type) %> { + trackIdentity(item: User): <%= getTypescriptKeyType(user.primaryKey.type) %> { return item.id!; } diff --git a/generators/angular/templates/src/main/webapp/app/admin/user-management/service/user-management.service.spec.ts.ejs b/generators/angular/templates/src/main/webapp/app/admin/user-management/service/user-management.service.spec.ts.ejs index e38e09e9f7ed..42c35ca88599 100644 --- a/generators/angular/templates/src/main/webapp/app/admin/user-management/service/user-management.service.spec.ts.ejs +++ b/generators/angular/templates/src/main/webapp/app/admin/user-management/service/user-management.service.spec.ts.ejs @@ -17,7 +17,7 @@ limitations under the License. -%> <%_ -const tsKeyId = this.generateTestEntityId(user.primaryKey.type); +const tsKeyId = generateTestEntityId(user.primaryKey.type); _%> import { TestBed } from '@angular/core/testing'; import { provideHttpClient, HttpErrorResponse } from '@angular/common/http'; diff --git a/generators/angular/templates/src/main/webapp/app/admin/user-management/update/user-management-update.component.spec.ts.ejs b/generators/angular/templates/src/main/webapp/app/admin/user-management/update/user-management-update.component.spec.ts.ejs index fd0d0d60dcbe..24010855ef33 100644 --- a/generators/angular/templates/src/main/webapp/app/admin/user-management/update/user-management-update.component.spec.ts.ejs +++ b/generators/angular/templates/src/main/webapp/app/admin/user-management/update/user-management-update.component.spec.ts.ejs @@ -17,7 +17,7 @@ limitations under the License. -%> <%_ -const tsKeyId = this.generateTestEntityId(user.primaryKey.type); +const tsKeyId = generateTestEntityId(user.primaryKey.type); _%> import { ComponentFixture, TestBed, waitForAsync, inject, fakeAsync, tick } from '@angular/core/testing'; import { provideHttpClient } from '@angular/common/http'; diff --git a/generators/angular/templates/src/main/webapp/app/admin/user-management/user-management.model.ts.ejs b/generators/angular/templates/src/main/webapp/app/admin/user-management/user-management.model.ts.ejs index b8f770592bc7..0e240498cd6f 100644 --- a/generators/angular/templates/src/main/webapp/app/admin/user-management/user-management.model.ts.ejs +++ b/generators/angular/templates/src/main/webapp/app/admin/user-management/user-management.model.ts.ejs @@ -17,7 +17,7 @@ limitations under the License. -%> <%_ - const idType = this.getTypescriptKeyType(user.primaryKey.type); + const idType = getTypescriptKeyType(user.primaryKey.type); _%> export interface IUser { id: <%= idType %> | null; diff --git a/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/_entityFile_.model.ts.ejs b/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/_entityFile_.model.ts.ejs index 7e70b178d928..a7d61dba6b5d 100644 --- a/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/_entityFile_.model.ts.ejs +++ b/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/_entityFile_.model.ts.ejs @@ -17,7 +17,7 @@ limitations under the License. -%> <% -const enumImports = this.generateEntityClientEnumImports(fields); +const enumImports = generateEntityClientEnumImports(fields); %> <%_ if (anyFieldIsDateDerived) { _%> import dayjs from 'dayjs/esm'; diff --git a/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/_entityFile_.test-samples.ts.ejs b/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/_entityFile_.test-samples.ts.ejs index 35a4648e5014..c9f6b0aebe8e 100644 --- a/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/_entityFile_.test-samples.ts.ejs +++ b/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/_entityFile_.test-samples.ts.ejs @@ -17,7 +17,7 @@ limitations under the License. -%> <%_ - const enumImports = this.generateEntityClientEnumImports(fields); + const enumImports = generateEntityClientEnumImports(fields); _%> <%_ if (anyFieldIsDateDerived) { _%> import dayjs from 'dayjs/esm'; @@ -29,14 +29,14 @@ import { <%- importedType %> } from '<%- importedPath %>'; import { I<%= entityAngularName %><% if (!readOnly && primaryKey) { %>, New<%= entityAngularName %><% } %> } from './<%= entityFileName %>.model'; -export const sampleWithRequiredData: I<%= entityAngularName %> = <%- this.generateTypescriptTestEntity(fields.filter(field => field.id || field.fieldValidationRequired).map(field => field.reference)) %>; +export const sampleWithRequiredData: I<%= entityAngularName %> = <%- generateTypescriptTestEntity(fields.filter(field => field.id || field.fieldValidationRequired).map(field => field.reference)) %>; -export const sampleWithPartialData: I<%= entityAngularName %> = <%- this.generateTypescriptTestEntity(fields.filter(field => field.id || field.fieldValidationRequired || (!field.transient && faker.datatype.boolean())).map(field => field.reference)) %>; +export const sampleWithPartialData: I<%= entityAngularName %> = <%- generateTypescriptTestEntity(fields.filter(field => field.id || field.fieldValidationRequired || (!field.transient && faker.datatype.boolean())).map(field => field.reference)) %>; -export const sampleWithFullData: I<%= entityAngularName %> = <%- this.generateTypescriptTestEntity(fields.filter(field => !field.transient).map(field => field.reference)) %>; +export const sampleWithFullData: I<%= entityAngularName %> = <%- generateTypescriptTestEntity(fields.filter(field => !field.transient).map(field => field.reference)) %>; <%_ if (!readOnly && primaryKey) { _%> -export const sampleWithNewData: New<%= entityAngularName %> = <%- this.generateTypescriptTestEntity(fields.filter(field => !field.id && field.fieldValidationRequired).map(field => field.reference), { [primaryKey.name]: null }) %>; +export const sampleWithNewData: New<%= entityAngularName %> = <%- generateTypescriptTestEntity(fields.filter(field => !field.id && field.fieldValidationRequired).map(field => field.reference), { [primaryKey.name]: null }) %>; Object.freeze(sampleWithNewData); <%_ } _%> diff --git a/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/delete/_entityFile_-delete-dialog.component.spec.ts.ejs b/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/delete/_entityFile_-delete-dialog.component.spec.ts.ejs index c7d8822262b7..754e951c9210 100644 --- a/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/delete/_entityFile_-delete-dialog.component.spec.ts.ejs +++ b/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/delete/_entityFile_-delete-dialog.component.spec.ts.ejs @@ -17,7 +17,7 @@ limitations under the License. -%> <%_ -const tsKeyId = this.generateTestEntityId(primaryKey.type); +const tsKeyId = generateTestEntityId(primaryKey.type); _%> jest.mock('@ng-bootstrap/ng-bootstrap'); diff --git a/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/detail/_entityFile_-detail.component.spec.ts.ejs b/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/detail/_entityFile_-detail.component.spec.ts.ejs index c595a3049e79..7bf9d5cc0d2c 100644 --- a/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/detail/_entityFile_-detail.component.spec.ts.ejs +++ b/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/detail/_entityFile_-detail.component.spec.ts.ejs @@ -17,8 +17,8 @@ limitations under the License. -%> <%_ -const tsKeyId = this.generateTestEntityId(primaryKey.type); -const testEntity = this.generateTestEntityPrimaryKey(primaryKey, 0); +const tsKeyId = generateTestEntityId(primaryKey.type); +const testEntity = generateTestEntityPrimaryKey(primaryKey, 0); _%> import { ComponentFixture, TestBed } from '@angular/core/testing'; import { provideRouter, withComponentInputBinding } from '@angular/router'; diff --git a/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/list/_entityFile_.component.spec.ts.ejs b/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/list/_entityFile_.component.spec.ts.ejs index d1095c54ac79..c71240c0bbbf 100644 --- a/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/list/_entityFile_.component.spec.ts.ejs +++ b/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/list/_entityFile_.component.spec.ts.ejs @@ -19,8 +19,8 @@ <%_ const entityArrayOptionalChainSymbol = '?.'; const order = 'desc'; -const testEntityPrimaryKey = this.generateTestEntityPrimaryKey(primaryKey, 0); -const testEntityPrimaryKey2 = this.generateTestEntityPrimaryKey(primaryKey, 1); +const testEntityPrimaryKey = generateTestEntityPrimaryKey(primaryKey, 0); +const testEntityPrimaryKey2 = generateTestEntityPrimaryKey(primaryKey, 1); _%> import { @@ -136,7 +136,7 @@ describe('<%= entityAngularName %> Management Component', () => { describe('track<%= primaryKey.nameCapitalized %>', () => { it('Should forward to <%= entityInstance %>Service', () => { - const entity = <%- this.generateTestEntityPrimaryKey(primaryKey, 0) %>; + const entity = <%- generateTestEntityPrimaryKey(primaryKey, 0) %>; jest.spyOn(service, 'get<%= entityAngularName %>Identifier'); const <%= primaryKey.name %> = comp.track<%= primaryKey.nameCapitalized %>(entity); expect(service.get<%= entityAngularName %>Identifier).toHaveBeenCalledWith(entity); diff --git a/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/route/_entityFile_-routing-resolve.service.spec.ts.ejs b/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/route/_entityFile_-routing-resolve.service.spec.ts.ejs index 3539949394ed..37e98944218c 100644 --- a/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/route/_entityFile_-routing-resolve.service.spec.ts.ejs +++ b/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/route/_entityFile_-routing-resolve.service.spec.ts.ejs @@ -17,7 +17,7 @@ limitations under the License. -%> <%_ -const tsKeyId = this.generateTestEntityId(primaryKey.type); +const tsKeyId = generateTestEntityId(primaryKey.type); _%> import { TestBed } from '@angular/core/testing'; import { provideHttpClient, HttpResponse } from '@angular/common/http'; diff --git a/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/service/_entityFile_.service.spec.ts.ejs b/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/service/_entityFile_.service.spec.ts.ejs index a0174c0dcc18..12b7f96417ff 100644 --- a/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/service/_entityFile_.service.spec.ts.ejs +++ b/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/service/_entityFile_.service.spec.ts.ejs @@ -17,8 +17,8 @@ limitations under the License. -%> <%_ -const tsKeyId = this.generateTestEntityId(primaryKey.type); -const enumImports = this.generateEntityClientEnumImports(fields); +const tsKeyId = generateTestEntityId(primaryKey.type); +const enumImports = generateEntityClientEnumImports(fields); _%> import { TestBed } from '@angular/core/testing'; import { provideHttpClientTesting, HttpTestingController } from '@angular/common/http/testing'; @@ -219,7 +219,7 @@ describe('<%= entityAngularName %> Service', () => { }); it('Should return false if one entity is null', () => { - const entity1 = <%- this.generateTestEntityPrimaryKey(primaryKey, 0) %>; + const entity1 = <%- generateTestEntityPrimaryKey(primaryKey, 0) %>; const entity2 = null; const compareResult1 = service.compare<%= entityAngularName %>(entity1, entity2); @@ -230,8 +230,8 @@ describe('<%= entityAngularName %> Service', () => { }); it('Should return false if primaryKey differs', () => { - const entity1 = <%- this.generateTestEntityPrimaryKey(primaryKey, 0) %>; - const entity2 = <%- this.generateTestEntityPrimaryKey(primaryKey, 1) %>; + const entity1 = <%- generateTestEntityPrimaryKey(primaryKey, 0) %>; + const entity2 = <%- generateTestEntityPrimaryKey(primaryKey, 1) %>; const compareResult1 = service.compare<%= entityAngularName %>(entity1, entity2); const compareResult2 = service.compare<%= entityAngularName %>(entity2, entity1); @@ -241,8 +241,8 @@ describe('<%= entityAngularName %> Service', () => { }); it('Should return false if primaryKey matches', () => { - const entity1 = <%- this.generateTestEntityPrimaryKey(primaryKey, 0) %>; - const entity2 = <%- this.generateTestEntityPrimaryKey(primaryKey, 0) %>; + const entity1 = <%- generateTestEntityPrimaryKey(primaryKey, 0) %>; + const entity2 = <%- generateTestEntityPrimaryKey(primaryKey, 0) %>; const compareResult1 = service.compare<%= entityAngularName %>(entity1, entity2); const compareResult2 = service.compare<%= entityAngularName %>(entity2, entity1); diff --git a/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/update/_entityFile_-update.component.html.ejs b/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/update/_entityFile_-update.component.html.ejs index f67cbff56ef9..09ca85c2fd51 100644 --- a/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/update/_entityFile_-update.component.html.ejs +++ b/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/update/_entityFile_-update.component.html.ejs @@ -175,7 +175,7 @@ _%> } <%_ } else { _%> - @if (editForm.get([<%- this.buildAngularFormPath(relationship.reference) %>])!.value == null) { + @if (editForm.get([<%- buildAngularFormPath(relationship.reference) %>])!.value == null) { } <%_ } _%> @@ -196,10 +196,10 @@ _%> <%_ } _%> <%_ if (relationship.relationshipValidate && relationship.persistableRelationship) { _%> - @if (editForm.get([<%- this.buildAngularFormPath(relationship.reference) %>])!.invalid && (editForm.get([<%- this.buildAngularFormPath(relationship.reference) %>])!.dirty || editForm.get([<%- this.buildAngularFormPath(relationship.reference) %>])!.touched)) { + @if (editForm.get([<%- buildAngularFormPath(relationship.reference) %>])!.invalid && (editForm.get([<%- buildAngularFormPath(relationship.reference) %>])!.dirty || editForm.get([<%- buildAngularFormPath(relationship.reference) %>])!.touched)) {
<%_ if (relationshipRequired) { _%> - @if (editForm.get([<%- this.buildAngularFormPath(relationship.reference) %>])?.errors?.required) { + @if (editForm.get([<%- buildAngularFormPath(relationship.reference) %>])?.errors?.required) { __jhiTranslateTag__('entity.validation.required') } <%_ } _%> diff --git a/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/update/_entityFile_-update.component.spec.ts.ejs b/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/update/_entityFile_-update.component.spec.ts.ejs index 6ca8f2982896..fd6e32f33b35 100644 --- a/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/update/_entityFile_-update.component.spec.ts.ejs +++ b/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/update/_entityFile_-update.component.spec.ts.ejs @@ -17,13 +17,13 @@ limitations under the License. -%> <%_ -const tsKeyId = this.generateTestEntityId(primaryKey.type); +const tsKeyId = generateTestEntityId(primaryKey.type); const allRelationshipsByEntityNeedingOptions = Object .values(relationshipsByOtherEntity) .map(relationships => relationships.filter(rel => rel.persistableRelationship && !rel.otherEntity.embedded)) .filter(relationships => relationships.length > 0); -const testEntityPrimaryKey0 = this.generateTestEntityPrimaryKey(primaryKey, 0); -const testEntityPrimaryKey1 = this.generateTestEntityPrimaryKey(primaryKey, 1); +const testEntityPrimaryKey0 = generateTestEntityPrimaryKey(primaryKey, 0); +const testEntityPrimaryKey1 = generateTestEntityPrimaryKey(primaryKey, 1); _%> import { ComponentFixture, TestBed } from '@angular/core/testing'; import { provideHttpClient, HttpResponse } from '@angular/common/http'; @@ -95,14 +95,14 @@ describe('<%= entityAngularName %> Management Update Component', () => { <%_ for (const relationship of relationshipsWithCustomSharedOptions) { _%> <%_ const reference = relationship.reference _%> <%_ if (relationship.collection) { _%> - const <%= reference.name %> : I<%= otherEntity.entityAngularName %>[] = [<%- this.generateTestEntityPrimaryKey(otherEntity.primaryKey) %>]; + const <%= reference.name %> : I<%= otherEntity.entityAngularName %>[] = [<%- generateTestEntityPrimaryKey(otherEntity.primaryKey) %>]; <%_ } else { _%> - const <%= reference.name %> : I<%= otherEntity.entityAngularName %> = <%- this.generateTestEntityPrimaryKey(otherEntity.primaryKey) %>; + const <%= reference.name %> : I<%= otherEntity.entityAngularName %> = <%- generateTestEntityPrimaryKey(otherEntity.primaryKey) %>; <%_ } _%> <%= entityInstance %>.<%= reference.name %> = <%= reference.name %>; <%_ } _%> - const <%= otherEntity.entityInstance %>Collection: I<%= otherEntity.entityAngularName %>[] = [<%- this.generateTestEntityPrimaryKey(otherEntity.primaryKey) %>]; + const <%= otherEntity.entityInstance %>Collection: I<%= otherEntity.entityAngularName %>[] = [<%- generateTestEntityPrimaryKey(otherEntity.primaryKey) %>]; jest.spyOn(<%= otherEntity.entityInstance %>Service, 'query').mockReturnValue(of(new HttpResponse({ body: <%= otherEntity.entityInstance %>Collection }))); const additional<%= otherEntity.entityAngularNamePlural %> = [ <%_ for (const relationship of relationshipsWithCustomSharedOptions) { _%> @@ -130,10 +130,10 @@ describe('<%= entityAngularName %> Management Update Component', () => { <%_ const reference = relationship.reference _%> it('Should call <%= reference.name %> query and add missing value', () => { const <%= entityInstance %> : I<%= entityAngularName %> = <%- testEntityPrimaryKey1 %>; - const <%= reference.name %> : I<%= otherEntity.entityAngularName %> = <%- this.generateTestEntityPrimaryKey(otherEntity.primaryKey) %>; + const <%= reference.name %> : I<%= otherEntity.entityAngularName %> = <%- generateTestEntityPrimaryKey(otherEntity.primaryKey) %>; <%= entityInstance %>.<%= reference.name %> = <%= reference.name %>; - const <%= reference.name %>Collection: I<%= otherEntity.entityAngularName %>[] = [<%- this.generateTestEntityPrimaryKey(otherEntity.primaryKey) %>]; + const <%= reference.name %>Collection: I<%= otherEntity.entityAngularName %>[] = [<%- generateTestEntityPrimaryKey(otherEntity.primaryKey) %>]; jest.spyOn(<%= otherEntity.entityInstance %>Service, 'query').mockReturnValue(of(new HttpResponse({ body: <%= reference.name %>Collection }))); const expectedCollection: I<%= otherEntity.entityAngularName %>[] = [<%= reference.name %>, ...<%= reference.name %>Collection]; jest.spyOn(<%= otherEntity.entityInstance %>Service, 'add<%= otherEntity.entityAngularName %>ToCollectionIfMissing').mockReturnValue(expectedCollection); @@ -153,7 +153,7 @@ describe('<%= entityAngularName %> Management Update Component', () => { <%_ for (const relationshipsByEntityNeedingOptions of allRelationshipsByEntityNeedingOptions) { _%> <%_ for (const relationship of relationshipsByEntityNeedingOptions) { _%> <%_ const otherEntity = relationship.otherEntity _%> - const <%= relationship.relationshipName %>: I<%= otherEntity.entityAngularName %> = <%- this.generateTestEntityPrimaryKey(otherEntity.primaryKey) %>; + const <%= relationship.relationshipName %>: I<%= otherEntity.entityAngularName %> = <%- generateTestEntityPrimaryKey(otherEntity.primaryKey) %>; <%= entityInstance %>.<%= relationship.reference.name %> = <%= relationship.collection ? `[${relationship.relationshipName}]` : relationship.relationshipName %>; <%_ } _%> <%_ } _%> @@ -254,8 +254,8 @@ _%> _%> describe('compare<%= otherEntity.entityAngularName %>', () => { it('Should forward to <%= otherEntity.entityInstance %>Service', () => { - const entity = <%- this.generateTestEntityPrimaryKey(otherEntity.primaryKey, 0) %>; - const entity2 = <%- this.generateTestEntityPrimaryKey(otherEntity.primaryKey, 1) %>; + const entity = <%- generateTestEntityPrimaryKey(otherEntity.primaryKey, 0) %>; + const entity2 = <%- generateTestEntityPrimaryKey(otherEntity.primaryKey, 1) %>; jest.spyOn(<%= otherEntity.entityInstance %>Service, 'compare<%= otherEntity.entityAngularName %>'); comp.compare<%= otherEntity.entityAngularName %>(entity, entity2); expect(<%= otherEntity.entityInstance %>Service.compare<%= otherEntity.entityAngularName %>).toHaveBeenCalledWith(entity, entity2); diff --git a/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/update/_entityFile_-update.component.ts.ejs b/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/update/_entityFile_-update.component.ts.ejs index 3c217954d618..42969d9bd4d9 100644 --- a/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/update/_entityFile_-update.component.ts.ejs +++ b/generators/angular/templates/src/main/webapp/app/entities/_entityFolder_/update/_entityFile_-update.component.ts.ejs @@ -49,7 +49,7 @@ import { <%= uniqueRel.otherEntityAngularName %>Service } from 'app/entities/<%= } } _%> -<%_ const enumImports = this.generateEntityClientEnumImports(fields); _%> +<%_ const enumImports = generateEntityClientEnumImports(fields); _%> <%_ enumImports.forEach( (importedPath, importedType) => { _%> import { <%- importedType %> } from '<%- importedPath %>'; <%_ }); _%> diff --git a/generators/angular/templates/src/main/webapp/app/entities/entity-navbar-items.ts.ejs b/generators/angular/templates/src/main/webapp/app/entities/entity-navbar-items.ts.ejs index 6ec007d1a2f8..79ea8475b812 100644 --- a/generators/angular/templates/src/main/webapp/app/entities/entity-navbar-items.ts.ejs +++ b/generators/angular/templates/src/main/webapp/app/entities/entity-navbar-items.ts.ejs @@ -19,7 +19,7 @@ import NavbarItem from 'app/layouts/navbar/navbar-item.model'; export const EntityNavbarItems: NavbarItem[] = [ -<%_ for (const entity of this.localEntities) { _%> +<%_ for (const entity of frontendEntities) { _%> { name: '<%= entity.entityAngularName %>', route: '/<%= entity.entityPage %>', diff --git a/generators/angular/types.d.ts b/generators/angular/types.d.ts index b88b53057932..8a3632f76433 100644 --- a/generators/angular/types.d.ts +++ b/generators/angular/types.d.ts @@ -1,6 +1,57 @@ import type { Entity } from '../../lib/types/application/entity.js'; import type { ApplicationType } from '../../lib/types/application/application.js'; +export interface AngularEntity extends Entity { + /** + * generates a value for a primary key type + * @param primaryKey the primary key attribute (or its type) of the entity + * @param index an index to add salt to the value + * @param wrapped if the value should be within quotes + * @returns {string|number|string} + */ + generateTestEntityId: (primaryKey: any, index?: number, wrapped?: boolean) => any; + /** + * @private + * Create a angular form path getter method of reference. + * + * @param {object} reference + * @param {string[]} prefix + * @return {string} + */ + buildAngularFormPath: (reference: any, prefix?: never[]) => string; + /** + * @private + * Generate a test entity instance with faked values. + * + * @param {any} references - references to other entities. + * @param {any} additionalFields - additional fields to add to the entity or with default values that overrides generated values. + */ + generateTypescriptTestEntity: (references: any, additionalFields: any) => string; + /** + * @private + * Generate a test entity, for the PK references (when the PK is a composite, derived key) + * + * @param {any} primaryKey - primary key definition. + * @param {number} [index] - index of the primary key sample, pass undefined for a random key. + */ + generateTestEntityPrimaryKey: (primaryKey: any, index: any) => string; + /** + * Returns the typescript import section of enums referenced by all fields of the entity. + * @param fields returns the import of enums that are referenced by the fields + * @returns {typeImports:Map} the fields that potentially contains some enum types + */ + generateEntityClientEnumImports: (fields: any) => Map; + entityAngularAuthorities?: string; + entityAngularReadAuthorities?: string; +} + export type AngularApplication = { angularLocaleId: string; -} & ApplicationType; + frontendEntities: any[]; + /** + * Get the typescript type of a non-composite primary key + * @param primaryKey the primary key of the entity + * @returns {string} the typescript type. + */ + getTypescriptKeyType: (primaryKey: any) => 'string' | 'number'; +} & ApplicationType; diff --git a/lib/types/application/entity.d.ts b/lib/types/application/entity.d.ts index fb468af44492..7f8de58cea9c 100644 --- a/lib/types/application/entity.d.ts +++ b/lib/types/application/entity.d.ts @@ -27,11 +27,6 @@ import type { FakerWithRandexp } from '../../../generators/base/support/faker.ts import type { Field } from './field.js'; import type { Relationship } from './relationship.js'; -type AngularEntity = { - entityAngularAuthorities?: string; - entityAngularReadAuthorities?: string; -}; - export type PrimaryKey = { name: string; fields: F[]; @@ -45,8 +40,7 @@ export type PrimaryKey = { export interface Entity extends Omit>, 'relationships'>, - SpringEntity, - AngularEntity { + SpringEntity { changelogDateForRecent: any; relationships: (IsNever extends true ? Relationship> : R)[]; otherRelationships: (IsNever extends true ? Relationship> : R)[];