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

Implement json data generation for more generic tests. #13860

Merged
merged 3 commits into from
Mar 2, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
-%>
<%_
const tsKeyId = generateTestEntityId(user.primaryKey.type);
const testEntityPrimaryKey0 = generateTestEntityPrimaryKey(user.primaryKey, 0);
const testEntityPrimaryKey1 = generateTestEntityPrimaryKey(user.primaryKey, 1);
_%>
import { TestBed } from '@angular/core/testing';
import { HttpErrorResponse } from '@angular/common/http';
Expand Down Expand Up @@ -72,50 +74,50 @@ describe('Service Tests', () => {

describe('addUserToCollectionIfMissing', () => {
it('should add a User to an empty array', () => {
const user: IUser = <%- generateTestEntityPrimaryKey(user.primaryKey, 0) %>;
const user: IUser = <%- testEntityPrimaryKey0 %>;
expectedResult = service.addUserToCollectionIfMissing([], user);
expect(expectedResult).toHaveLength(1);
expect(expectedResult).toContain(user);
});

it('should not add a User to an array that contains it', () => {
const user: IUser = <%- generateTestEntityPrimaryKey(user.primaryKey, 0) %>;
const user: IUser = <%- testEntityPrimaryKey0 %>;
const userCollection: IUser[] = [
{
...user,
},
<%- generateTestEntityPrimaryKey(user.primaryKey, 1) %>,
<%- testEntityPrimaryKey1 %>,
];
expectedResult = service.addUserToCollectionIfMissing(userCollection, user);
expect(expectedResult).toHaveLength(2);
});

it("should add a User to an array that doesn't contain it", () => {
const user: IUser = <%- generateTestEntityPrimaryKey(user.primaryKey, 0) %>;
const userCollection: IUser[] = [<%- generateTestEntityPrimaryKey(user.primaryKey, 1) %>];
const user: IUser = <%- testEntityPrimaryKey0 %>;
const userCollection: IUser[] = [<%- testEntityPrimaryKey1 %>];
expectedResult = service.addUserToCollectionIfMissing(userCollection, user);
expect(expectedResult).toHaveLength(2);
expect(expectedResult).toContain(user);
});

it('should add only unique User to an array', () => {
const userArray: IUser[] = [<%- generateTestEntityPrimaryKey(user.primaryKey, 0) %>, <%- generateTestEntityPrimaryKey(user.primaryKey, 1) %>, <%- generateTestEntityPrimaryKey(user.primaryKey) %>];
const userCollection: IUser[] = [<%- generateTestEntityPrimaryKey(user.primaryKey, 1) %>];
const userArray: IUser[] = [<%- testEntityPrimaryKey0 %>, <%- testEntityPrimaryKey1 %>, <%- generateTestEntityPrimaryKey(user.primaryKey) %>];
const userCollection: IUser[] = [<%- testEntityPrimaryKey1 %>];
expectedResult = service.addUserToCollectionIfMissing(userCollection, ...userArray);
expect(expectedResult).toHaveLength(3);
});

it("should accept varargs", () => {
const user: IUser = <%- generateTestEntityPrimaryKey(user.primaryKey, 0) %>;
const user2: IUser = <%- generateTestEntityPrimaryKey(user.primaryKey, 1) %>;
const user: IUser = <%- testEntityPrimaryKey0 %>;
const user2: IUser = <%- testEntityPrimaryKey1 %>;
expectedResult = service.addUserToCollectionIfMissing([], user, user2);
expect(expectedResult).toHaveLength(2);
expect(expectedResult).toContain(user);
expect(expectedResult).toContain(user2);
});

it("should accept null and undefined values", () => {
const user: IUser = <%- generateTestEntityPrimaryKey(user.primaryKey, 0) %>;
const user: IUser = <%- testEntityPrimaryKey0 %>;
expectedResult = service.addUserToCollectionIfMissing([], null, user, undefined);
expect(expectedResult).toHaveLength(1);
expect(expectedResult).toContain(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<%_ for (relationship of relationships) {
if (relationship.relationshipType === 'many-to-one' || (relationship.relationshipType === 'one-to-one' && relationship.ownerSide === true
&& (relationship.id == null || relationship.id === false))) {
relationship.otherEntity.idFields.forEach(idField => {
relationship.otherEntity.primaryKey.ownFields.forEach(idField => {
const uniqueConstraintName = relationship.relationshipType === 'one-to-one' ? getUXConstraintName(entity.entityTableName, relationship.columnName + '_' + idField.columnName, entity.prodDatabaseType) : null;
_%>
<column name="<%= relationship.columnName %>_<%= idField.columnName %>" type="<%= idField.columnType %>">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
let baseColumnNames;
let referencedColumnNames;
if (relationshipType === 'one-to-one' && ownerSide && relationship.id === true) {
baseColumnNames = relationship.otherEntity.idFields.map(field => getColumnName(field.columnName)).join(',');
referencedColumnNames = relationship.otherEntity.idFields.map(field => getColumnName(field.columnName)).join(',');
baseColumnNames = relationship.otherEntity.primaryKey.ownFields.map(field => getColumnName(field.columnName)).join(',');
referencedColumnNames = relationship.otherEntity.primaryKey.ownFields.map(field => getColumnName(field.columnName)).join(',');
} else if (relationship.otherEntity) {
baseColumnNames = relationship.otherEntity.idFields.map(field => getColumnName(relationshipName + '_' + field.columnName)).join(',');
referencedColumnNames = relationship.otherEntity.idFields.map(field => getColumnName(field.columnName)).join(',');
baseColumnNames = relationship.otherEntity.primaryKey.ownFields.map(field => getColumnName(relationshipName + '_' + field.columnName)).join(',');
referencedColumnNames = relationship.otherEntity.primaryKey.ownFields.map(field => getColumnName(field.columnName)).join(',');
} %>
<addForeignKeyConstraint baseColumnNames="<%= baseColumnNames %>"
baseTableName="<%= entity.entityTableName %>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<<%= jhiPrefixDashed %>-alert></<%= jhiPrefixDashed %>-alert>

<dl class="row-md jh-entity-details">
<%_ for (const field of fields) {
<%_ for (const field of fields.filter(field => !field.hidden)) {
const fieldName = field.fieldName;
const fieldType = field.fieldType;
const fieldTypeBlobContent = field.fieldTypeBlobContent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
-%>
<%_
const tsKeyId = generateTestEntityId(primaryKey.type);
const testEntity = generateTestEntityPrimaryKey(primaryKey, 0);
_%>
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ActivatedRoute } from '@angular/router';
import { of } from 'rxjs';

import { <%= entityAngularName %> } from '../<%= entityFileName %>.model';
<%_ if (fieldsContainBlob) { _%>
import { DataUtils } from 'app/core/util/data-util.service';
<%_ } _%>
Expand All @@ -44,7 +44,7 @@ describe('Component Tests', () => {
providers: [
{
provide: ActivatedRoute,
useValue: { data: of({ <%= entityInstance %>: new <%= entityAngularName %>(<%- tsKeyId %>) }) }
useValue: { data: of({ <%= entityInstance %>: <%- testEntity %> }) }
}
]
})
Expand All @@ -63,7 +63,7 @@ describe('Component Tests', () => {
comp.ngOnInit();

// THEN
expect(comp.<%= entityInstance %>).toEqual(jasmine.objectContaining({ <%- primaryKey.name %>: <%- tsKeyId %> }));
expect(comp.<%= entityInstance %>).toEqual(jasmine.objectContaining(<%- testEntity %>));
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ _%>
<table class="table table-striped" aria-describedby="page-heading">
<thead>
<tr<% if (pagination !== 'no') { %> <%= jhiPrefix %>Sort [(predicate)]="predicate" [(ascending)]="ascending" [callback]="<%= pagination !== 'infinite-scroll' ? 'loadPage.bind(this)' : 'reset.bind(this)'%>"<% } %>>
<%_ for (const field of fields) { _%>
<%_ for (const field of fields.filter(field => !field.hidden)) { _%>
<th scope="col"<% if (pagination !== 'no') { %> <%= jhiPrefix %>SortBy="<%= field.fieldName %>"<% } %>><span <%= jhiPrefix %>Translate="<%= field.fieldTranslationKey %>"><%= field.fieldNameHumanized %></span><% if (pagination !== 'no') { %> <fa-icon icon="sort"></fa-icon><% } %></th>
<%_ } _%>
<%_ for (const relationship of relationships) { _%>
Expand All @@ -97,9 +97,8 @@ _%>
<tbody<% if (pagination === 'infinite-scroll') { %> infinite-scroll (scrolled)="loadPage(page + 1)" [infiniteScrollDisabled]="page >= links['last']" [infiniteScrollDistance]="0"<% } %>>
<tr *ngFor="let <%= entityInstance %> of <%= entityInstancePlural %>; trackBy: track<%= primaryKey.nameCapitalized %>" data-cy="entityTable">
<%_
const keys = idFields.map(idField => `${entityInstance}.${idField.fieldName}`);
const routerLink = idFields.length === 0 ? '' : ` [routerLink]="['/${ entityUrl }', ${ keys.join(', ') }, 'view']"`;
for ([idx, field] of fields.entries()) {
const routerLink = ` [routerLink]="['/${ entityUrl }', ${entityInstance}.${primaryKey.name}, 'view']"`;
for (field of fields.filter(field => !field.hidden)) {
const fieldName = field.fieldName;
const fieldNameCapitalized = field.fieldNameCapitalized;
const fieldType = field.fieldType;
Expand Down Expand Up @@ -155,11 +154,11 @@ _%>
<%_ } else { _%>
<%_ if (relationshipType === 'many-to-many') { _%>
<span *ngFor="let <%= relationshipFieldName %> of <%= entityInstance %>.<%= relationshipFieldNamePlural %>; let last = last">
<a class="form-control-static" [routerLink]="['/<%= otherEntityStateName %>', <%= relationshipFieldName %>?.<%= relationship.otherEntity.primaryKey.name %>, 'view']">{{ <%= relationshipFieldName %>.<%= otherEntityField %> }}</a>{{ last ? '' : ', ' }}
<a class="form-control-static" [routerLink]="['/<%= otherEntityStateName %>', <%= relationshipFieldName %>.<%= relationship.otherEntity.primaryKey.name %>, 'view']">{{ <%= relationshipFieldName %>.<%= otherEntityField %> }}</a>{{ last ? '' : ', ' }}
</span>
<%_ } else { _%>
<div *ngIf="<%= entityInstance + "." + relationshipFieldName %>">
<a [routerLink]="['/<%= otherEntityStateName %>', <%= entityInstance + "." + relationshipFieldName + "?." + relationship.otherEntity.primaryKey.name %>, 'view']" >{{ <%= entityInstance + "." + relationshipFieldName + "?." + otherEntityField %> }}</a>
<a [routerLink]="['/<%= otherEntityStateName %>', <%= entityInstance %>.<%= relationshipFieldName %>?.<%= relationship.otherEntity.primaryKey.name %>, 'view']">{{ <%= entityInstance + "." + relationshipFieldName + "?." + otherEntityField %> }}</a>
</div>
<%_ } _%>
<%_ } _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
limitations under the License.
-%>
<%_
const tsKeyId = generateTestEntityId(primaryKey.type);
const entityArrayOptionalChainSymbol = pagination === 'infinite-scroll' ? '' : '?.';
const order = pagination === 'infinite-scroll' ? 'asc' : 'desc';
const testEntityPrimaryKey = generateTestEntityPrimaryKey(primaryKey, 0);
_%>
<%_ if (pagination === 'pagination' || searchEngine !== false) { _%>
jest.mock('@angular/router');
Expand All @@ -36,7 +36,6 @@ import { ActivatedRoute } from '@angular/router';
import { of } from 'rxjs';

import { <%= entityAngularName %>Service } from '../service/<%= entityFileName %>.service';
import { <%= entityAngularName %> } from '../<%= entityFileName %>.model';

import { <%= entityAngularName %>Component } from './<%= entityFileName %>.component';

Expand Down Expand Up @@ -92,7 +91,7 @@ describe('Component Tests', () => {
spyOn(service, 'query').and.returnValue(
of(
new HttpResponse({
body: [new <%= entityAngularName %>(<%- tsKeyId %>)],
body: [<%- testEntityPrimaryKey %>],
headers,
})
)
Expand All @@ -105,7 +104,7 @@ describe('Component Tests', () => {

// THEN
expect(service.query).toHaveBeenCalled();
expect(comp.<%= entityInstancePlural %><%= entityArrayOptionalChainSymbol %>[0]).toEqual(jasmine.objectContaining({ <%- primaryKey.name %>: <%- tsKeyId %> }));
expect(comp.<%= entityInstancePlural %><%= entityArrayOptionalChainSymbol %>[0]).toEqual(jasmine.objectContaining(<%- testEntityPrimaryKey %>));
});

<%_ if (pagination !== 'no') { _%>
Expand All @@ -115,15 +114,15 @@ describe('Component Tests', () => {

// THEN
expect(service.query).toHaveBeenCalled();
expect(comp.<%= entityInstancePlural %><%= entityArrayOptionalChainSymbol %>[0]).toEqual(jasmine.objectContaining({ <%- primaryKey.name %>: <%- tsKeyId %> }));
expect(comp.<%= entityInstancePlural %><%= entityArrayOptionalChainSymbol %>[0]).toEqual(jasmine.objectContaining(<%- testEntityPrimaryKey %>));
});

it('should calculate the sort attribute for an id', () => {
// WHEN
comp.ngOnInit();

// THEN
expect(service.query).toHaveBeenCalledWith(expect.objectContaining({ sort: ['<%- primaryKey.name %>,<%= order %>'] }));
expect(service.query).toHaveBeenCalledWith(expect.objectContaining({ sort: ['<%- primaryKey.fields.map(field => field.fieldName).join(',') %>,<%= order %>'] }));
});

it('should calculate the sort attribute for a non-id attribute', () => {
Expand All @@ -137,7 +136,7 @@ describe('Component Tests', () => {
comp.loadPage(1);

// THEN
expect(service.query).toHaveBeenLastCalledWith(expect.objectContaining({ sort: ['name,<%= order %>', '<%- primaryKey.name %>'] }));
expect(service.query).toHaveBeenLastCalledWith(expect.objectContaining({ sort: ['name,<%= order %>', '<%- primaryKey.fields.map(field => field.fieldName).join(',') %>'] }));
});

<%_ if (pagination === 'infinite-scroll') { _%>
Expand All @@ -149,7 +148,7 @@ describe('Component Tests', () => {
// THEN
expect(comp.page).toEqual(0);
expect(service.query).toHaveBeenCalledTimes(2);
expect(comp.<%= entityInstancePlural %>[0]).toEqual(jasmine.objectContaining({ <%- primaryKey.name %>: <%- tsKeyId %> }));
expect(comp.<%= entityInstancePlural %>[0]).toEqual(jasmine.objectContaining(<%- testEntityPrimaryKey %>));
});
<%_ } _%>
<%_ } _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class <%= entityAngularName %>Component implements OnInit {
<%_ } _%>

<%_ if (primaryKey) { _%>
track<%= primaryKey.nameCapitalized %>(index: number, item: I<%= entityAngularName %>): <%= tsKeyType %> {
track<%= primaryKey.nameCapitalized %>(index: number, item: I<%= entityAngularName %>): <%= primaryKey.tsType %> {
return item.<%= primaryKey.name %>!;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ describe('Service Tests', () => {
});

describe('resolve', () => {
it('should return existing I<%= entityAngularName %> for existing id', () => {
it('should return I<%= entityAngularName %> returned by find', () => {
// GIVEN
service.find = jest.fn(id => of(new HttpResponse({ body: new <%= entityAngularName %>(id) })));
mockActivatedRouteSnapshot.params = { id: <%- tsKeyId %> };
service.find = jest.fn(<%= primaryKey.name %> => of(new HttpResponse({ body: { <%= primaryKey.name %> } })));
mockActivatedRouteSnapshot.params = { <%= primaryKey.name %>: <%- tsKeyId %> };

// WHEN
routingResolveService.resolve(mockActivatedRouteSnapshot).subscribe(result => {
Expand All @@ -65,7 +65,7 @@ describe('Service Tests', () => {

// THEN
expect(service.find).toBeCalledWith(<%- tsKeyId %>);
expect(result<%= entityAngularName %>).toEqual(new <%= entityAngularName %>(<%- tsKeyId %>));
expect(result<%= entityAngularName %>).toEqual({ <%= primaryKey.name %>: <%- tsKeyId %> });
});

it('should return new I<%= entityAngularName %> if id is not provided', () => {
Expand All @@ -86,7 +86,7 @@ describe('Service Tests', () => {
it('should route to 404 page if data not found in server', () => {
// GIVEN
spyOn(service, 'find').and.returnValue(of(new HttpResponse({ body: null })));
mockActivatedRouteSnapshot.params = { id: <%- tsKeyId %> };
mockActivatedRouteSnapshot.params = { <%= primaryKey.name %>: <%- tsKeyId %> };

// WHEN
routingResolveService.resolve(mockActivatedRouteSnapshot).subscribe(result => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class <%= entityAngularName %>RoutingResolveService implements Resolve<I<
constructor(protected service: <%= entityAngularName %>Service, protected router: Router) {}

resolve(route: ActivatedRouteSnapshot): Observable<I<%= entityAngularName %>> | Observable<never> {
const id = route.params['id'];
const id = route.params['<%= primaryKey.name %>'];
if (id) {
return this.service.find(id).pipe(
mergeMap((<%= entityInstance %>: HttpResponse<<%= entityAngularName %>>) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const <%= entityInstance %>Route: Routes = [
canActivate: [UserRouteAccessService]
},
{
path: ':id/view',
path: ':<%= primaryKey.name %>/view',
component: <%= entityAngularName %>DetailComponent,
resolve: {
<%= entityInstance %>: <%= entityAngularName %>RoutingResolveService
Expand All @@ -56,7 +56,7 @@ const <%= entityInstance %>Route: Routes = [
canActivate: [UserRouteAccessService]
},
{
path: ':id/edit',
path: ':<%= primaryKey.name %>/edit',
component: <%= entityAngularName %>UpdateComponent,
resolve: {
<%= entityInstance %>: <%= entityAngularName %>RoutingResolveService
Expand Down
Loading