Skip to content

Commit

Permalink
fix: change detection is triggered once children are updated with sch…
Browse files Browse the repository at this point in the history
…ool info
  • Loading branch information
TheSlimvReal committed Feb 20, 2023
1 parent c665a89 commit 0c49249
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
ComponentFixture,
fakeAsync,
TestBed,
tick,
waitForAsync,
} from "@angular/core/testing";
import { ComponentFixture, TestBed, waitForAsync } from "@angular/core/testing";
import { ChildrenListComponent } from "./children-list.component";
import { ChildrenService } from "../children.service";
import { of } from "rxjs";
Expand Down Expand Up @@ -105,17 +99,19 @@ describe("ChildrenListComponent", () => {
expect(component).toBeTruthy();
});

it("should load children on init", fakeAsync(() => {
it("should load children on init", () => {
component.isLoading = true;
const child1 = new Child("c1");
const child2 = new Child("c2");
mockChildrenService.getChildren.and.returnValue(of([child1, child2]));
component.ngOnInit();
tick();

let children = [];
component.childrenList.subscribe((val) => (children = val));
expect(mockChildrenService.getChildren).toHaveBeenCalled();
expect(component.childrenList).toEqual([child1, child2]);
expect(children).toEqual([child1, child2]);
expect(component.isLoading).toBeFalse();
}));
});

it("should route to the given id", () => {
const router = fixture.debugElement.injector.get(Router);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { Component, OnInit } from "@angular/core";
import { Child } from "../model/child";
import { ActivatedRoute, Router } from "@angular/router";
import { UntilDestroy, untilDestroyed } from "@ngneat/until-destroy";
import { ChildrenService } from "../children.service";
import { EntityListConfig } from "../../../core/entity-components/entity-list/EntityListConfig";
import { RouteData } from "../../../core/view/dynamic-routing/view-config.interface";
import { RouteTarget } from "../../../app.routing";
import { EntityListComponent } from "../../../core/entity-components/entity-list/entity-list.component";
import { Observable } from "rxjs";
import { AsyncPipe } from "@angular/common";
import { startWith, tap } from "rxjs/operators";

@UntilDestroy()
@RouteTarget("ChildrenList")
@Component({
selector: "app-children-list",
template: `
<app-entity-list
[allEntities]="childrenList"
[allEntities]="childrenList | async"
[listConfig]="listConfig"
[isLoading]="isLoading"
[entityConstructor]="childConstructor"
Expand All @@ -23,10 +24,10 @@ import { EntityListComponent } from "../../../core/entity-components/entity-list
></app-entity-list>
`,
standalone: true,
imports: [EntityListComponent],
imports: [EntityListComponent, AsyncPipe],
})
export class ChildrenListComponent implements OnInit {
childrenList: Child[] = [];
childrenList: Observable<Child[]>;
listConfig: EntityListConfig;
childConstructor = Child;
isLoading = true;
Expand All @@ -41,13 +42,10 @@ export class ChildrenListComponent implements OnInit {
this.route.data.subscribe(
(data: RouteData<EntityListConfig>) => (this.listConfig = data.config)
);
this.childrenService
.getChildren()
.pipe(untilDestroyed(this))
.subscribe((children) => {
this.childrenList = children;
this.isLoading = false;
});
this.childrenList = this.childrenService.getChildren().pipe(
startWith([]),
tap((res) => (this.isLoading = res.length === 0))
);
}

routeTo(route: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/app/child-dev-project/children/children.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class ChildrenService {
loadedChild.schoolClass = childCurrentSchoolInfo?.schoolClass;
loadedChild.schoolId = childCurrentSchoolInfo?.schoolId;
}
results.next(loadedChildren);
results.next([...loadedChildren]);
results.complete();
});

Expand Down

0 comments on commit 0c49249

Please sign in to comment.