refactor method

This commit is contained in:
Ketevani Kvirikashvili
2022-05-31 19:05:20 +02:00
parent 0ecb7298b9
commit c3b63a05bc
2 changed files with 38 additions and 83 deletions

View File

@@ -65,7 +65,7 @@ const mockOauth2Auth: any = {
reply: jasmine.createSpy('reply') reply: jasmine.createSpy('reply')
}; };
fdescribe('FormCloudComponent', () => { describe('FormCloudComponent', () => {
let formCloudService: FormCloudService; let formCloudService: FormCloudService;
let fixture: ComponentFixture<FormCloudComponent>; let fixture: ComponentFixture<FormCloudComponent>;
let formComponent: FormCloudComponent; let formComponent: FormCloudComponent;
@@ -689,7 +689,6 @@ fdescribe('FormCloudComponent', () => {
}); });
it('should require form with appName and taskId to complete', () => { it('should require form with appName and taskId to complete', () => {
debugger;
spyOn(formCloudService, 'completeTaskForm').and.stub(); spyOn(formCloudService, 'completeTaskForm').and.stub();
formComponent.form = null; formComponent.form = null;
@@ -746,8 +745,6 @@ fdescribe('FormCloudComponent', () => {
}); });
it('should open confirmation dialog on complete task', () => { 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); spyOn(matDialog, 'open').and.returnValue({ afterClosed: () => of(false) } as any);
formComponent.form = new FormModel({ formComponent.form = new FormModel({
confirmMessage: { confirmMessage: {
@@ -762,42 +759,24 @@ fdescribe('FormCloudComponent', () => {
it('should submit form when user confirms', () => { it('should submit form when user confirms', () => {
fixture.detectChanges(); fixture.detectChanges();
const afterClose = of(true); const formModel = new FormModel({
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' }
],
confirmMessage: { confirmMessage: {
show: true, show: true,
message: 'Are you sure you want to submit the form?' message: 'Are you sure you want to submit the form?'
} }
}); });
formComponent.appVersion = appVersion;
formComponent.form = formModel; formComponent.form = formModel;
formComponent.taskId = taskId; spyOn(matDialog, 'open').and.returnValue({
formComponent.appName = appName; afterClosed: () => of(true)
formComponent.processInstanceId = processInstanceId; } as any);
formComponent.completeTaskForm(outcome);
expect(matDialog.open).toHaveBeenCalled();
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', () => { it('should not confirm form if user rejects', () => {
@@ -807,9 +786,19 @@ fdescribe('FormCloudComponent', () => {
afterClosed: () => of(false) afterClosed: () => of(false)
} as any); } 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); formComponent.completeTaskForm(outcome);
expect(matDialog.open).toHaveBeenCalled();
expect(formComponent['formCloudService'].completeTaskForm).not.toHaveBeenCalled(); expect(formComponent['formCloudService'].completeTaskForm).not.toHaveBeenCalled();
}); });

View File

@@ -271,10 +271,6 @@ export class FormCloudComponent extends FormBaseComponent implements OnChanges,
} }
completeTaskForm(outcome?: string) { completeTaskForm(outcome?: string) {
debugger;
// const confirmMessage = this.form.json.confirmMessage.message;
if (this.form?.confirmMessage?.show === true) { if (this.form?.confirmMessage?.show === true) {
const dialogRef = this.dialog.open(ConfirmDialogComponent, { const dialogRef = this.dialog.open(ConfirmDialogComponent, {
data: { data: {
@@ -286,56 +282,26 @@ export class FormCloudComponent extends FormBaseComponent implements OnChanges,
dialogRef.afterClosed().subscribe((result) => { dialogRef.afterClosed().subscribe((result) => {
if (result === true) { if (result === true) {
if (this.form && this.appName && this.taskId) { this.completeTaskFormWithConfirmMessage(outcome);
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)
);
}
} }
}); });
} else { } else {
if (this.form && this.appName && this.taskId) { this.completeTaskFormWithConfirmMessage(outcome);
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)
);
}
} }
}
// const dialogRef = this.dialog.open(ConfirmDialogComponent, { completeTaskFormWithConfirmMessage(outcome?: string) {
// data: { if (this.form && this.appName && this.taskId) {
// title: 'Save the form', this.formCloudService
// message: this.form.confirmMessage.message .completeTaskForm(this.appName, this.taskId, this.processInstanceId, `${this.form.id}`, this.form.values, outcome, this.appVersion)
// }, .pipe(takeUntil(this.onDestroy$))
// minWidth: '450px' .subscribe(
// }); () => {
this.onTaskCompleted(this.form);
// dialogRef.afterClosed().subscribe((result) => { },
// if (result === true) { (error) => this.onTaskCompletedError(error)
// 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 { parseForm(formCloudRepresentationJSON: any): FormModel {