From 285a61a997548361fb40b12c7f5f0d14601002d6 Mon Sep 17 00:00:00 2001 From: Maciej Zieniuk Date: Thu, 13 Mar 2025 17:06:23 +0000 Subject: [PATCH] error handling in remove password component --- .../remove-password.component.spec.ts | 41 ++++++++++++++++++- .../components/remove-password.component.ts | 37 +++++++++-------- 2 files changed, 58 insertions(+), 20 deletions(-) diff --git a/libs/angular/src/auth/components/remove-password.component.spec.ts b/libs/angular/src/auth/components/remove-password.component.spec.ts index 00a90db9169..973f5e7c0f1 100644 --- a/libs/angular/src/auth/components/remove-password.component.spec.ts +++ b/libs/angular/src/auth/components/remove-password.component.spec.ts @@ -129,7 +129,25 @@ describe("RemovePasswordComponent", () => { expect(mockRouter.navigate).toHaveBeenCalledWith([""]); }); - it("should handle response errors and show error toast", async () => { + it("should handle errors and show error toast", async () => { + const errorMessage = "Can't migrate user error"; + mockKeyConnectorService.migrateUser.mockRejectedValue(new Error(errorMessage)); + mockI18nService.t.mockReturnValue("error occurred"); + + await component.convert(); + + expect(component.continuing).toBe(false); + expect(mockKeyConnectorService.migrateUser).toHaveBeenCalledWith(userId); + expect(mockToastService.showToast).toHaveBeenCalledWith({ + variant: "error", + title: "error occurred", + message: errorMessage, + }); + expect(mockKeyConnectorService.removeConvertAccountRequired).not.toHaveBeenCalled(); + expect(mockRouter.navigate).not.toHaveBeenCalled(); + }); + + it("should handle error response and show error toast", async () => { const errorMessage = "Can't migrate user error"; mockKeyConnectorService.migrateUser.mockRejectedValue( new ErrorResponse( @@ -178,7 +196,26 @@ describe("RemovePasswordComponent", () => { expect(mockRouter.navigate).toHaveBeenCalledWith([""]); }); - it("should handle response errors and show error toast", async () => { + it("should handle error response and show error toast", async () => { + const errorMessage = "Can't leave organization error"; + mockDialogService.openSimpleDialog.mockResolvedValue(true); + mockOrganizationApiService.leave.mockRejectedValue(new Error(errorMessage)); + mockI18nService.t.mockReturnValue("error occurred"); + + await component.leave(); + + expect(component.leaving).toBe(false); + expect(mockOrganizationApiService.leave).toHaveBeenCalledWith(organization.id); + expect(mockToastService.showToast).toHaveBeenCalledWith({ + variant: "error", + title: "error occurred", + message: errorMessage, + }); + expect(mockKeyConnectorService.removeConvertAccountRequired).not.toHaveBeenCalled(); + expect(mockRouter.navigate).not.toHaveBeenCalled(); + }); + + it("should handle error response and show error toast", async () => { const errorMessage = "Can't leave organization error"; mockDialogService.openSimpleDialog.mockResolvedValue(true); mockOrganizationApiService.leave.mockRejectedValue( diff --git a/libs/angular/src/auth/components/remove-password.component.ts b/libs/angular/src/auth/components/remove-password.component.ts index 2ecf49701b5..ca04dd24cdc 100644 --- a/libs/angular/src/auth/components/remove-password.component.ts +++ b/libs/angular/src/auth/components/remove-password.component.ts @@ -61,7 +61,7 @@ export class RemovePasswordComponent implements OnInit { return this.continuing || this.leaving; } - convert = async () => { + async convert() { this.continuing = true; try { @@ -77,17 +77,11 @@ export class RemovePasswordComponent implements OnInit { } catch (e) { this.continuing = false; - if (e instanceof ErrorResponse) { - this.toastService.showToast({ - variant: "error", - title: this.i18nService.t("errorOccurred"), - message: e.message, - }); - } + this.handleActionError(e); } - }; + } - leave = async () => { + async leave() { const confirmed = await this.dialogService.openSimpleDialog({ title: this.organization.name, content: { key: "leaveOrganizationConfirmation" }, @@ -112,13 +106,20 @@ export class RemovePasswordComponent implements OnInit { } catch (e) { this.leaving = false; - if (e instanceof ErrorResponse) { - this.toastService.showToast({ - variant: "error", - title: this.i18nService.t("errorOccurred"), - message: e.message, - }); - } + this.handleActionError(e); } - }; + } + + handleActionError(e: unknown) { + let message = ""; + if (e instanceof ErrorResponse || e instanceof Error) { + message = e.message; + } + + this.toastService.showToast({ + variant: "error", + title: this.i18nService.t("errorOccurred"), + message: message, + }); + } }