From c98786d456fa0bef2282ebfa50db7071ea400b75 Mon Sep 17 00:00:00 2001 From: Schottkyc137 Date: Mon, 14 Mar 2022 20:52:21 +0100 Subject: [PATCH 1/2] Progress is saved correctly after closing the dialog --- .../progress-dashboard.component.spec.ts | 27 +++++++++++++++++-- .../progress-dashboard.component.ts | 24 ++++++++--------- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/app/child-dev-project/progress-dashboard-widget/progress-dashboard/progress-dashboard.component.spec.ts b/src/app/child-dev-project/progress-dashboard-widget/progress-dashboard/progress-dashboard.component.spec.ts index 745d2ee89a..3ba08eaeae 100644 --- a/src/app/child-dev-project/progress-dashboard-widget/progress-dashboard/progress-dashboard.component.spec.ts +++ b/src/app/child-dev-project/progress-dashboard-widget/progress-dashboard/progress-dashboard.component.spec.ts @@ -1,27 +1,40 @@ -import { ComponentFixture, TestBed, waitForAsync } from "@angular/core/testing"; +import { + ComponentFixture, + fakeAsync, + TestBed, + waitForAsync, +} from "@angular/core/testing"; import { ProgressDashboardComponent } from "./progress-dashboard.component"; import { EntityMapperService } from "../../../core/entity/entity-mapper.service"; import { AlertService } from "../../../core/alerts/alert.service"; import { ProgressDashboardWidgetModule } from "../progress-dashboard-widget.module"; import { FontAwesomeTestingModule } from "@fortawesome/angular-fontawesome/testing"; +import { MatDialog } from "@angular/material/dialog"; +import { Subject } from "rxjs"; +import { take } from "rxjs/operators"; describe("ProgressDashboardComponent", () => { let component: ProgressDashboardComponent; let fixture: ComponentFixture; + const mockDialog = jasmine.createSpyObj("matDialog", ["open"]); + let mockEntityService: jasmine.SpyObj; beforeEach( waitForAsync(() => { - const mockEntityService = jasmine.createSpyObj("mockEntityService", [ + mockEntityService = jasmine.createSpyObj("mockEntityService", [ "load", "save", ]); mockEntityService.load.and.resolveTo({ title: "test", parts: [] }); + mockEntityService.save.and.resolveTo(); + // mockEntityService.save.and.callFake((next) => console.log("called with ", next)); TestBed.configureTestingModule({ imports: [ProgressDashboardWidgetModule, FontAwesomeTestingModule], providers: [ { provide: EntityMapperService, useValue: mockEntityService }, + { provide: MatDialog, useValue: mockDialog }, { provide: AlertService, useValue: jasmine.createSpyObj([ @@ -44,4 +57,14 @@ describe("ProgressDashboardComponent", () => { it("should create", () => { expect(component).toBeTruthy(); }); + + it("saves data after the dialog was closed", fakeAsync(() => { + const closeNotifier = new Subject(); + mockDialog.open.and.returnValue({ + afterClosed: () => closeNotifier.pipe(take(1)), + } as any); + component.showEditComponent(); + closeNotifier.next({}); + expect(mockEntityService.save).toHaveBeenCalled(); + })); }); diff --git a/src/app/child-dev-project/progress-dashboard-widget/progress-dashboard/progress-dashboard.component.ts b/src/app/child-dev-project/progress-dashboard-widget/progress-dashboard/progress-dashboard.component.ts index 0fd01baf9c..c39551e1d9 100644 --- a/src/app/child-dev-project/progress-dashboard-widget/progress-dashboard/progress-dashboard.component.ts +++ b/src/app/child-dev-project/progress-dashboard-widget/progress-dashboard/progress-dashboard.component.ts @@ -29,10 +29,7 @@ export class ProgressDashboardComponent ngOnInit() { this.data = new ProgressDashboardConfig(this.dashboardConfigId); this.entityMapper - .load( - ProgressDashboardConfig, - this.dashboardConfigId - ) + .load(ProgressDashboardConfig, this.dashboardConfigId) .then((config) => { this.data = config; }) @@ -60,13 +57,16 @@ export class ProgressDashboardComponent } showEditComponent() { - const dialog = this.dialog.open(EditProgressDashboardComponent, { - data: this.data, - }); - dialog.afterClosed().subscribe((next) => { - if (next) { - this.data.parts = next; - } - }); + this.dialog + .open(EditProgressDashboardComponent, { + data: this.data, + }) + .afterClosed() + .subscribe(async (next) => { + if (next) { + this.data.parts = next; + await this.save(); + } + }); } } From ccbfb249c45fc10be3f00d357ad2560ca0c4c38c Mon Sep 17 00:00:00 2001 From: Schottkyc137 Date: Mon, 14 Mar 2022 20:57:04 +0100 Subject: [PATCH 2/2] remove commented out code --- .../progress-dashboard/progress-dashboard.component.spec.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/app/child-dev-project/progress-dashboard-widget/progress-dashboard/progress-dashboard.component.spec.ts b/src/app/child-dev-project/progress-dashboard-widget/progress-dashboard/progress-dashboard.component.spec.ts index 3ba08eaeae..f095914376 100644 --- a/src/app/child-dev-project/progress-dashboard-widget/progress-dashboard/progress-dashboard.component.spec.ts +++ b/src/app/child-dev-project/progress-dashboard-widget/progress-dashboard/progress-dashboard.component.spec.ts @@ -28,7 +28,6 @@ describe("ProgressDashboardComponent", () => { ]); mockEntityService.load.and.resolveTo({ title: "test", parts: [] }); mockEntityService.save.and.resolveTo(); - // mockEntityService.save.and.callFake((next) => console.log("called with ", next)); TestBed.configureTestingModule({ imports: [ProgressDashboardWidgetModule, FontAwesomeTestingModule],