Skip to content

Commit

Permalink
fix(core): related-entities can handle records where the mapped field…
Browse files Browse the repository at this point in the history
… is undefined

fixes #2172
  • Loading branch information
sleidig committed Jan 11, 2024
1 parent 3c4b653 commit 3d70d13
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import { MockedTestingModule } from "../../../utils/mocked-testing.module";
import { EntityMapperService } from "../../entity/entity-mapper/entity-mapper.service";
import { Child } from "../../../child-dev-project/children/model/child";
import { ChildSchoolRelation } from "../../../child-dev-project/children/model/childSchoolRelation";
import { Note } from "../../../child-dev-project/notes/model/note";

describe("RelatedEntitiesComponent", () => {
let component: RelatedEntitiesComponent<ChildSchoolRelation>;
let fixture: ComponentFixture<RelatedEntitiesComponent<ChildSchoolRelation>>;
let component: RelatedEntitiesComponent<ChildSchoolRelation | Note>;
let fixture: ComponentFixture<
RelatedEntitiesComponent<ChildSchoolRelation | Note>
>;

beforeEach(async () => {
await TestBed.configureTestingModule({
Expand Down Expand Up @@ -56,6 +59,23 @@ describe("RelatedEntitiesComponent", () => {
expect(component.filter).toEqual({ ...filter, childId: c1.getId() });
});

it("should ignore entities of the related type where the matching field is undefined instead of array", async () => {
const c1 = new Child();
const r1 = new Note();
r1.children = [c1.getId()];
const rEmpty = new Note();
delete rEmpty.children; // some entity types will not have a default empty array
const entityMapper = TestBed.inject(EntityMapperService);
await entityMapper.saveAll([c1, r1, rEmpty]);

component.entity = c1;
component.entityType = Note.ENTITY_TYPE;
component.property = "children";
await component.ngOnInit();

expect(component.data).toEqual([r1]);
});

it("should create a new entity that references the related one", async () => {
const related = new Child();
component.entity = related;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class RelatedEntitiesComponent<E extends Entity> implements OnInit {
this.data = (await this.entityMapper.loadType<E>(this.entityType)).filter(
(e) =>
this.isArray
? e[this.property].includes(this.entity.getId())
? e[this.property]?.includes(this.entity.getId())
: e[this.property] === this.entity.getId(),
);
this.filter = {
Expand Down

0 comments on commit 3d70d13

Please sign in to comment.