Skip to content

Commit

Permalink
fix: entity select respects toStringAttributes (#1425)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSlimvReal authored Sep 9, 2022
1 parent c3a2519 commit 735799c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { mockEntityMapper } from "../../../entity/mock-entity-mapper-service";
import { User } from "../../../user/user";
import { Child } from "../../../../child-dev-project/children/model/child";
import { FlexLayoutModule } from "@angular/flex-layout";
import { Subscription } from "rxjs";
import { firstValueFrom, Subscription } from "rxjs";
import { EntitySchemaService } from "../../../entity/schema/entity-schema.service";
import {
EntityRegistry,
Expand Down Expand Up @@ -176,6 +176,28 @@ describe("EntitySelectComponent", () => {
component.formControl.setValue("Abc");
});

it("should use the configurable toStringAttributes for comparing values", async () => {
class Person extends Entity {
static toStringAttributes = ["firstname", "lastname"];

firstname: string;
lastname: string;
}

const p1 = Object.assign(new Person(), { firstname: "Aa", lastname: "bb" });
const p2 = Object.assign(new Person(), { firstname: "Aa", lastname: "cc" });
component.allEntities = [p1, p2];
component.loading.next(false);

let res = firstValueFrom(component.filteredEntities);
component.formControl.setValue("Aa");
await expectAsync(res).toBeResolvedTo([p1, p2]);

res = firstValueFrom(component.filteredEntities);
component.formControl.setValue("Aa b");
await expectAsync(res).toBeResolvedTo([p1]);
});

it("should add an unselected entity to the filtered entities array", (done) => {
// TODO this is still throwing object unsubscribe error
component.allEntities = testUsers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class EntitySelectComponent<E extends Entity> implements OnChanges {
);
});
}

/** Underlying data-array */
selectedEntities: E[] = [];
/**
Expand Down Expand Up @@ -157,7 +158,7 @@ export class EntitySelectComponent<E extends Entity> implements OnChanges {
* <br> Per default, this filters for the name. If the entity
* has no name, this filters for the entity's id.
*/
@Input() accessor: (e: Entity) => string = (e) => e["name"] || e.getId();
@Input() accessor: (e: Entity) => string = (e) => e.toString();

@Input() additionalFilter: (e: E) => boolean = (_) => true;

Expand Down

0 comments on commit 735799c

Please sign in to comment.