From a915c0ad6aa220b71553d0a44a55abe9434e7c89 Mon Sep 17 00:00:00 2001 From: Vito Albano Date: Mon, 14 Nov 2016 17:28:15 +0000 Subject: [PATCH] Improved form search for visibility service --- .../widget-visibility.service.spec.ts | 20 +++++++++++-------- .../src/services/widget-visibility.service.ts | 19 +++++++++--------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/ng2-components/ng2-activiti-form/src/services/widget-visibility.service.spec.ts b/ng2-components/ng2-activiti-form/src/services/widget-visibility.service.spec.ts index ffd7a8e177..b5ecc21faa 100644 --- a/ng2-components/ng2-activiti-form/src/services/widget-visibility.service.spec.ts +++ b/ng2-components/ng2-activiti-form/src/services/widget-visibility.service.spec.ts @@ -363,10 +363,11 @@ describe('WidgetVisibilityService', () => { expect(formValue).toBe('field_with_condition_value'); }); - it('should return undefined if the field value is not in the form', () => { + it('should return empty string if the field value is not in the form', () => { let formValue = service.searchForm(stubFormWithFields, 'FIELD_MYSTERY'); - expect(formValue).toBeUndefined(); + expect(formValue).not.toBeUndefined(); + expect(formValue).toBe(''); }); it('should search in the form if element value is not in form values', () => { @@ -376,10 +377,11 @@ describe('WidgetVisibilityService', () => { expect(value).toBe('field_with_condition_value'); }); - it('should return undefined if the element is not present anywhere', () => { + it('should return empty string if the element is not present anywhere', () => { let formValue = service.getFormValue(fakeFormWithField, 'FIELD_MYSTERY'); - expect(formValue).toBeUndefined(); + expect(formValue).not.toBeUndefined(); + expect(formValue).toBe(''); }); it('should retrieve the value for the right field when it is a value', () => { @@ -443,10 +445,11 @@ describe('WidgetVisibilityService', () => { expect(leftValue).toBe('value_2'); }); - it('should return undefined for a value that is not on variable or form', () => { + it('should return empty string for a value that is not on variable or form', () => { let leftValue = service.getLeftValue(fakeFormWithField, visibilityObjTest); - expect(leftValue).toBeUndefined(); + expect(leftValue).not.toBeUndefined(); + expect(leftValue).toBe(''); }); it('should evaluate the visibility for the field with single visibility condition between two field values', () => { @@ -467,11 +470,12 @@ describe('WidgetVisibilityService', () => { expect(isVisible).toBeTruthy(); }); - it('should return undefined for a value that is not on variable or form', () => { + it('should return empty string for a value that is not on variable or form', () => { visibilityObjTest.rightFormFieldId = 'NO_FIELD_FORM'; let rightValue = service.getRightValue(fakeFormWithField, visibilityObjTest); - expect(rightValue).toBeUndefined(); + expect(rightValue).not.toBeUndefined(); + expect(rightValue).toBe(''); }); it('should evaluate the visibility for the field with single visibility condition between form values', () => { diff --git a/ng2-components/ng2-activiti-form/src/services/widget-visibility.service.ts b/ng2-components/ng2-activiti-form/src/services/widget-visibility.service.ts index 1bc357e65a..1eb5c1a7cc 100644 --- a/ng2-components/ng2-activiti-form/src/services/widget-visibility.service.ts +++ b/ng2-components/ng2-activiti-form/src/services/widget-visibility.service.ts @@ -119,18 +119,19 @@ export class WidgetVisibilityService { } searchForm(form: FormModel, name: string) { - let res; + let fieldValue = ''; form.json.fields.forEach(columns => { - for (let i in columns.fields) { - if (columns.fields.hasOwnProperty(i)) { - res = columns.fields[i].find(field => field.id === name); - if (res) { - return res.value; + for (let i in columns.fields) { + if (columns.fields.hasOwnProperty(i)) { + let field = columns.fields[i].find(field => field.id === name); + if (field) { + fieldValue = field.value; + } } } } - }); - return res ? res.value : res; + ); + return fieldValue; } getVariableValue(form: FormModel, name: string, processVarList: TaskProcessVariableModel[]) { @@ -212,7 +213,7 @@ export class WidgetVisibilityService { private getRequestOptions(): RequestOptions { let headers = this.getHeaders(); - return new RequestOptions({headers: headers}); + return new RequestOptions({ headers: headers }); } private handleError(error: Response) {