From c3b63a05bc818116f5dfb07104c47a703d2be666 Mon Sep 17 00:00:00 2001 From: Ketevani Kvirikashvili Date: Tue, 31 May 2022 19:05:20 +0200 Subject: [PATCH] refactor method --- .../components/form-cloud.component.spec.ts | 57 +++++++---------- .../form/components/form-cloud.component.ts | 64 +++++-------------- 2 files changed, 38 insertions(+), 83 deletions(-) diff --git a/lib/process-services-cloud/src/lib/form/components/form-cloud.component.spec.ts b/lib/process-services-cloud/src/lib/form/components/form-cloud.component.spec.ts index 648ba00bbb..2e6dcc6ea3 100644 --- a/lib/process-services-cloud/src/lib/form/components/form-cloud.component.spec.ts +++ b/lib/process-services-cloud/src/lib/form/components/form-cloud.component.spec.ts @@ -65,7 +65,7 @@ const mockOauth2Auth: any = { reply: jasmine.createSpy('reply') }; -fdescribe('FormCloudComponent', () => { +describe('FormCloudComponent', () => { let formCloudService: FormCloudService; let fixture: ComponentFixture; let formComponent: FormCloudComponent; @@ -689,7 +689,6 @@ fdescribe('FormCloudComponent', () => { }); it('should require form with appName and taskId to complete', () => { - debugger; spyOn(formCloudService, 'completeTaskForm').and.stub(); formComponent.form = null; @@ -746,8 +745,6 @@ fdescribe('FormCloudComponent', () => { }); it('should open confirmation dialog on complete task', () => { - // spyOn(matDialog, 'open').and.returnValue({ beforeClosed: () => of() } as any); - spyOn(matDialog, 'open').and.returnValue({ afterClosed: () => of(false) } as any); formComponent.form = new FormModel({ confirmMessage: { @@ -755,49 +752,31 @@ fdescribe('FormCloudComponent', () => { message: 'Are you sure you want to submit the form?' } }); - + formComponent.completeTaskForm(); expect(matDialog.open).toHaveBeenCalled(); }); it('should submit form when user confirms', () => { fixture.detectChanges(); - const afterClose = of(true); - - spyOn(matDialog, 'open').and.returnValue({ - afterClosed: () => afterClose - } as any); - - spyOn(formComponent['formCloudService'], 'completeTaskForm').and.returnValue(Promise.resolve(false)); - const outcome = 'complete'; - - const taskId = '123-223'; - const appVersion = 1; - const appName = 'test-app'; - const processInstanceId = '333-444'; - - const formModel = new FormModel({ - id: '23', - taskId, - fields: [ - { id: 'field1' }, - { id: 'field2' } - ], + const formModel = new FormModel({ confirmMessage: { show: true, message: 'Are you sure you want to submit the form?' } }); - formComponent.appVersion = appVersion; formComponent.form = formModel; - formComponent.taskId = taskId; - formComponent.appName = appName; - formComponent.processInstanceId = processInstanceId; - formComponent.completeTaskForm(outcome); - expect(matDialog.open).toHaveBeenCalled(); + spyOn(matDialog, 'open').and.returnValue({ + afterClosed: () => of(true) + } as any); - expect(formComponent['formCloudService'].completeTaskForm).toHaveBeenCalledWith(appName, formModel.taskId, processInstanceId, formModel.id, formModel.values, outcome, appVersion); + spyOn(formComponent['formCloudService'], 'completeTaskForm').and.returnValue(Promise.resolve(true)); + + formComponent.completeTaskForm('complete'); + + expect(matDialog.open).toHaveBeenCalled(); + expect(formComponent['formCloudService'].completeTaskForm).toHaveBeenCalled(); }); it('should not confirm form if user rejects', () => { @@ -807,9 +786,19 @@ fdescribe('FormCloudComponent', () => { afterClosed: () => of(false) } as any); - spyOn(formComponent['formCloudService'], 'completeTaskForm').and.returnValue(Promise.resolve(false)); + const formModel = new FormModel({ + confirmMessage: { + show: true, + message: 'Are you sure you want to submit the form?' + } + }); + + formComponent.form = formModel; + spyOn(formComponent['formCloudService'], 'completeTaskForm').and.returnValue(Promise.resolve(true)); + formComponent.completeTaskForm(outcome); + expect(matDialog.open).toHaveBeenCalled(); expect(formComponent['formCloudService'].completeTaskForm).not.toHaveBeenCalled(); }); diff --git a/lib/process-services-cloud/src/lib/form/components/form-cloud.component.ts b/lib/process-services-cloud/src/lib/form/components/form-cloud.component.ts index 9ecba447e0..ae11fafab1 100644 --- a/lib/process-services-cloud/src/lib/form/components/form-cloud.component.ts +++ b/lib/process-services-cloud/src/lib/form/components/form-cloud.component.ts @@ -271,10 +271,6 @@ export class FormCloudComponent extends FormBaseComponent implements OnChanges, } completeTaskForm(outcome?: string) { - debugger; - - // const confirmMessage = this.form.json.confirmMessage.message; - if (this.form?.confirmMessage?.show === true) { const dialogRef = this.dialog.open(ConfirmDialogComponent, { data: { @@ -286,56 +282,26 @@ export class FormCloudComponent extends FormBaseComponent implements OnChanges, dialogRef.afterClosed().subscribe((result) => { if (result === true) { - if (this.form && this.appName && this.taskId) { - this.formCloudService - .completeTaskForm(this.appName, this.taskId, this.processInstanceId, `${this.form.id}`, this.form.values, outcome, this.appVersion) - .pipe(takeUntil(this.onDestroy$)) - .subscribe( - () => { - this.onTaskCompleted(this.form); - }, - (error) => this.onTaskCompletedError(error) - ); - } + this.completeTaskFormWithConfirmMessage(outcome); } }); } else { - if (this.form && this.appName && this.taskId) { - this.formCloudService - .completeTaskForm(this.appName, this.taskId, this.processInstanceId, `${this.form.id}`, this.form.values, outcome, this.appVersion) - .pipe(takeUntil(this.onDestroy$)) - .subscribe( - () => { - this.onTaskCompleted(this.form); - }, - (error) => this.onTaskCompletedError(error) - ); - } + this.completeTaskFormWithConfirmMessage(outcome); } - - // const dialogRef = this.dialog.open(ConfirmDialogComponent, { - // data: { - // title: 'Save the form', - // message: this.form.confirmMessage.message - // }, - // minWidth: '450px' - // }); + } - // dialogRef.afterClosed().subscribe((result) => { - // if (result === true) { - // if (this.form && this.appName && this.taskId) { - // this.formCloudService - // .completeTaskForm(this.appName, this.taskId, this.processInstanceId, `${this.form.id}`, this.form.values, outcome, this.appVersion) - // .pipe(takeUntil(this.onDestroy$)) - // .subscribe( - // () => { - // this.onTaskCompleted(this.form); - // }, - // (error) => this.onTaskCompletedError(error) - // ); - // } - // } - // }); + completeTaskFormWithConfirmMessage(outcome?: string) { + if (this.form && this.appName && this.taskId) { + this.formCloudService + .completeTaskForm(this.appName, this.taskId, this.processInstanceId, `${this.form.id}`, this.form.values, outcome, this.appVersion) + .pipe(takeUntil(this.onDestroy$)) + .subscribe( + () => { + this.onTaskCompleted(this.form); + }, + (error) => this.onTaskCompletedError(error) + ); + } } parseForm(formCloudRepresentationJSON: any): FormModel {