Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal: replace generator method with properties and types adjusts #27807

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 1 addition & 62 deletions generators/angular/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ 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';
import { clientFrameworkTypes } from '../../lib/jhipster/index.js';
import {
generateTypescriptTestEntity as generateTestEntity,
generateEntityClientEnumImports as getClientEnumImportsFormat,
getTypescriptKeyType as getTSKeyType,
generateTestEntityId as getTestEntityId,
generateTestEntityPrimaryKey as getTestEntityPrimaryKey,
} from '../client/support/index.js';
import { generateEntityClientEnumImports as getClientEnumImportsFormat } from '../client/support/index.js';
import { createNeedleCallback, mutateData } from '../base/support/index.js';
import { writeEslintClientRootConfigFile } from '../javascript/generators/eslint/support/tasks.js';
import type { PostWritingEntitiesTaskParam } from '../../lib/types/application/tasks.js';
Expand All @@ -44,7 +38,6 @@ import {
addItemToAdminMenu,
addRoute,
addToEntitiesMenu,
buildAngularFormPath as angularFormPath,
isTranslatedAngularFile,
translateAngularFilesTransform,
} from './support/index.js';
Expand Down Expand Up @@ -440,60 +433,6 @@ export default class AngularGenerator extends BaseApplicationGenerator {
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);
}

/**
* @private
* Add a new menu element, at the root of the menu.
Expand Down
1 change: 1 addition & 0 deletions generators/angular/support/path-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

/**
* @private
* @deprecated
* Create a angular form path getter method of reference.
*
* @param {object} reference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
limitations under the License.
-%>
<%_
const tsKeyId = this.generateTestEntityId(user.primaryKey.type);
const tsKeyId = user.primaryKey.tsSampleValues[0];
_%>
import { TestBed } from '@angular/core/testing';
import { provideRouter, withComponentInputBinding } from '@angular/router';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
limitations under the License.
-%>
<%_
const tsKeyId = this.generateTestEntityId(user.primaryKey.type);
const tsKeyId = user.primaryKey.tsSampleValues[0];
_%>
jest.mock('app/core/auth/account.service');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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): <%= user.primaryKey.tsType %> {
return item.id!;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
limitations under the License.
-%>
<%_
const tsKeyId = this.generateTestEntityId(user.primaryKey.type);
const tsKeyId = user.primaryKey.tsSampleValues[0];
_%>
import { TestBed } from '@angular/core/testing';
import { provideHttpClient, HttpErrorResponse } from '@angular/common/http';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
limitations under the License.
-%>
<%_
const tsKeyId = this.generateTestEntityId(user.primaryKey.type);
const tsKeyId = user.primaryKey.tsSampleValues[0];
_%>
import { ComponentFixture, TestBed, waitForAsync, inject, fakeAsync, tick } from '@angular/core/testing';
import { provideHttpClient } from '@angular/common/http';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
limitations under the License.
-%>
<%_
const idType = this.getTypescriptKeyType(user.primaryKey.type);
const idType = user.primaryKey.tsType;
_%>
export interface IUser {
id: <%= idType %> | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 %> = <%- tsSampleWithRequiredData %>;

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 %> = <%- tsSampleWithPartialData %>;

export const sampleWithFullData: I<%= entityAngularName %> = <%- this.generateTypescriptTestEntity(fields.filter(field => !field.transient).map(field => field.reference)) %>;
export const sampleWithFullData: I<%= entityAngularName %> = <%- tsSampleWithFullData %>;
<%_ 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 %> = <%- tsSampleWithNewData %>;

Object.freeze(sampleWithNewData);
<%_ } _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
limitations under the License.
-%>
<%_
const tsKeyId = this.generateTestEntityId(primaryKey.type);
const tsKeyId = primaryKey.tsSampleValues[0];
_%>
jest.mock('@ng-bootstrap/ng-bootstrap');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
limitations under the License.
-%>
<%_
const tsKeyId = this.generateTestEntityId(primaryKey.type);
const testEntity = this.generateTestEntityPrimaryKey(primaryKey, 0);
const tsKeyId = primaryKey.tsSampleValues[0];
const testEntity = tsPrimaryKeySamples[0];
_%>
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { provideRouter, withComponentInputBinding } from '@angular/router';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
<%_
const entityArrayOptionalChainSymbol = '?.';
const order = 'desc';
const testEntityPrimaryKey = this.generateTestEntityPrimaryKey(primaryKey, 0);
const testEntityPrimaryKey2 = this.generateTestEntityPrimaryKey(primaryKey, 1);
const testEntityPrimaryKey = tsPrimaryKeySamples[0];
const testEntityPrimaryKey2 = tsPrimaryKeySamples[1];
_%>

import {
Expand Down Expand Up @@ -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 = <%- tsPrimaryKeySamples[0] %>;
jest.spyOn(service, 'get<%= entityAngularName %>Identifier');
const <%= primaryKey.name %> = comp.track<%= primaryKey.nameCapitalized %>(entity);
expect(service.get<%= entityAngularName %>Identifier).toHaveBeenCalledWith(entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
limitations under the License.
-%>
<%_
const tsKeyId = this.generateTestEntityId(primaryKey.type);
const tsKeyId = primaryKey.tsSampleValues[0];
_%>
import { TestBed } from '@angular/core/testing';
import { provideHttpClient, HttpResponse } from '@angular/common/http';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
limitations under the License.
-%>
<%_
const tsKeyId = this.generateTestEntityId(primaryKey.type);
const tsKeyId = primaryKey.tsSampleValues[0];
const enumImports = this.generateEntityClientEnumImports(fields);
_%>
import { TestBed } from '@angular/core/testing';
Expand Down Expand Up @@ -219,7 +219,7 @@ describe('<%= entityAngularName %> Service', () => {
});

it('Should return false if one entity is null', () => {
const entity1 = <%- this.generateTestEntityPrimaryKey(primaryKey, 0) %>;
const entity1 = <%- tsPrimaryKeySamples[0] %>;
const entity2 = null;

const compareResult1 = service.compare<%= entityAngularName %>(entity1, entity2);
Expand All @@ -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 = <%- tsPrimaryKeySamples[0] %>;
const entity2 = <%- tsPrimaryKeySamples[1] %>;

const compareResult1 = service.compare<%= entityAngularName %>(entity1, entity2);
const compareResult2 = service.compare<%= entityAngularName %>(entity2, entity1);
Expand All @@ -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 = <%- tsPrimaryKeySamples[0] %>;
const entity2 = <%- tsPrimaryKeySamples[0] %>;

const compareResult1 = service.compare<%= entityAngularName %>(entity1, entity2);
const compareResult2 = service.compare<%= entityAngularName %>(entity2, entity1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ _%>
<option [ngValue]="null" selected></option>
}
<%_ } else { _%>
@if (editForm.get([<%- this.buildAngularFormPath(relationship.reference) %>])!.value == null) {
@if (editForm.get(['<%- relationship.propertyName %>'])!.value == null) {
<option [ngValue]="null" selected></option>
}
<%_ } _%>
Expand All @@ -196,10 +196,10 @@ _%>
</div>
<%_ } _%>
<%_ 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(['<%- relationship.propertyName %>'])!.invalid && (editForm.get(['<%- relationship.propertyName %>'])!.dirty || editForm.get(['<%- relationship.propertyName %>'])!.touched)) {
<div>
<%_ if (relationshipRequired) { _%>
@if (editForm.get([<%- this.buildAngularFormPath(relationship.reference) %>])?.errors?.required) {
@if (editForm.get(['<%- relationship.propertyName %>'])?.errors?.required) {
<small class="form-text text-danger">__jhiTranslateTag__('entity.validation.required')</small>
}
<%_ } _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
limitations under the License.
-%>
<%_
const tsKeyId = this.generateTestEntityId(primaryKey.type);
const tsKeyId = primaryKey.tsSampleValues[0];
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 = tsPrimaryKeySamples[0];
const testEntityPrimaryKey1 = tsPrimaryKeySamples[1];
_%>
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { provideHttpClient, HttpResponse } from '@angular/common/http';
Expand Down Expand Up @@ -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 %>[] = [<%- otherEntity.tsPrimaryKeySamples[0] %>];
<%_ } else { _%>
const <%= reference.name %> : I<%= otherEntity.entityAngularName %> = <%- this.generateTestEntityPrimaryKey(otherEntity.primaryKey) %>;
const <%= reference.name %> : I<%= otherEntity.entityAngularName %> = <%- otherEntity.tsPrimaryKeySamples[0] %>;
<%_ } _%>
<%= entityInstance %>.<%= reference.name %> = <%= reference.name %>;
<%_ } _%>

const <%= otherEntity.entityInstance %>Collection: I<%= otherEntity.entityAngularName %>[] = [<%- this.generateTestEntityPrimaryKey(otherEntity.primaryKey) %>];
const <%= otherEntity.entityInstance %>Collection: I<%= otherEntity.entityAngularName %>[] = [<%- otherEntity.tsPrimaryKeySamples[0] %>];
jest.spyOn(<%= otherEntity.entityInstance %>Service, 'query').mockReturnValue(of(new HttpResponse({ body: <%= otherEntity.entityInstance %>Collection })));
const additional<%= otherEntity.entityAngularNamePlural %> = [
<%_ for (const relationship of relationshipsWithCustomSharedOptions) { _%>
Expand Down Expand Up @@ -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 %> = <%- otherEntity.tsPrimaryKeySamples[0] %>;
<%= entityInstance %>.<%= reference.name %> = <%= reference.name %>;

const <%= reference.name %>Collection: I<%= otherEntity.entityAngularName %>[] = [<%- this.generateTestEntityPrimaryKey(otherEntity.primaryKey) %>];
const <%= reference.name %>Collection: I<%= otherEntity.entityAngularName %>[] = [<%- otherEntity.tsPrimaryKeySamples[0] %>];
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);
Expand All @@ -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 %> = <%- otherEntity.tsPrimaryKeySamples[0] %>;
<%= entityInstance %>.<%= relationship.reference.name %> = <%= relationship.collection ? `[${relationship.relationshipName}]` : relationship.relationshipName %>;
<%_ } _%>
<%_ } _%>
Expand All @@ -165,7 +165,7 @@ describe('<%= entityAngularName %> Management Update Component', () => {
<%_ const relationshipsWithCustomUniqueOptions = relationshipsByEntityNeedingOptions.filter(rel => rel.relationshipOneToOne && rel.otherRelationship); _%>
<%_ for (const relationship of relationshipsByEntityNeedingOptions) { _%>
<%_ const otherEntity = relationship.otherEntity _%>
expect(comp.<% if (!relationshipsWithCustomUniqueOptions.includes(relationship)) { %><%= otherEntity.entityInstancePlural %>Shared<% } else { %><%= relationship.relationshipFieldNamePlural %><% } %>Collection).toContain(<%= relationship.relationshipName %>);
expect(comp.<% if (!relationshipsWithCustomUniqueOptions.includes(relationship)) { %><%= otherEntity.entityInstancePlural %>Shared<% } else { %><%= relationship.relationshipFieldNamePlural %><% } %>Collection).toContainEqual(<%= relationship.relationshipName %>);
<%_ } _%>
<%_ } _%>
expect(comp.<%= entityInstance %>).toEqual(<%= entityInstance %>);
Expand Down Expand Up @@ -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 = <%- otherEntity.tsPrimaryKeySamples[0] %>;
const entity2 = <%- otherEntity.tsPrimaryKeySamples[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);
Expand Down
Loading
Loading