From b7fc44d57669a98996001dea7d365f827e20335b Mon Sep 17 00:00:00 2001 From: Maurizio Vitale Date: Wed, 25 Apr 2018 14:08:05 +0100 Subject: [PATCH] fix unit test (#3236) --- .../widget-visibility.service.spec.ts | 192 +++++++++++++++--- .../services/widget-visibility.service.ts | 2 +- 2 files changed, 164 insertions(+), 30 deletions(-) diff --git a/lib/core/form/services/widget-visibility.service.spec.ts b/lib/core/form/services/widget-visibility.service.spec.ts index 8f2fe00057..e98fce8a51 100644 --- a/lib/core/form/services/widget-visibility.service.spec.ts +++ b/lib/core/form/services/widget-visibility.service.spec.ts @@ -358,6 +358,10 @@ describe('WidgetVisibilityService', () => { jsonFieldFake.visibilityCondition = visibilityObjTest; }); + afterEach( () => { + service.cleanProcessVariable(); + }); + it('should be able to retrieve a field value searching in the form', () => { let formValue = service.searchValueInForm(stubFormWithFields, 'FIELD_WITH_CONDITION'); @@ -655,24 +659,59 @@ describe('WidgetVisibilityService', () => { service.getTaskProcessVariable('9999').subscribe( (res: TaskProcessVariableModel[]) => { expect(res).toBeDefined(); - let varValue = service.getVariableValue(formTest, 'FIELD_TEST', res); + let varValue = service.getVariableValue(formTest, 'FIELD_FORM_EMPTY', res); expect(varValue).not.toBeUndefined(); - expect(varValue).toBe('process_variable_value'); + expect(varValue).toBe('PROCESS_RIGHT_FORM_FIELD_VALUE'); - visibilityObjTest.leftFormFieldId = 'FIELD_TEST'; + visibilityObjTest.leftFormFieldId = 'FIELD_FORM_EMPTY'; visibilityObjTest.operator = '=='; visibilityObjTest.rightValue = 'RIGHT_FORM_FIELD_VALUE'; - const fieldWithoutVisibility = fakeFormWithField.getFieldById('FIELD_WITH_CONDITION'); - fieldWithoutVisibility.isVisible = false; - fieldWithoutVisibility.visibilityCondition = visibilityObjTest; + let myForm = new FormModel({ + id: '9999', + name: 'FORM_PROCESS_VARIABLE_VISIBILITY', + processDefinitionId: 'PROCESS_TEST:9:9999', + processDefinitionName: 'PROCESS_TEST', + processDefinitionKey: 'PROCESS_TEST', + taskId: '999', + taskName: 'TEST', + fields: [ + { + fieldType: 'ContainerRepresentation', + id: '000000000000000000', + name: 'Label', + type: 'container', + value: null, + numberOfColumns: 2, + fields: { + 1: [ + { + fieldType: 'FormFieldRepresentation', + id: 'FIELD_FORM_EMPTY', + name: 'FIELD_FORM_EMPTY', + type: 'text', + value: 'RIGHT_FORM_FIELD_VALUE', + visibilityCondition: null, + isVisible: true + }, + { + fieldType: 'FormFieldRepresentation', + id: 'FIELD_FORM_WITH_CONDITION', + name: 'FIELD_FORM_WITH_CONDITION', + type: 'text', + value: 'field_form_with_condition_value', + visibilityCondition: visibilityObjTest, + isVisible: false + } + ] + } + } + ] + }); - let contModel = new ContainerModel(new FormFieldModel(fakeFormWithField)); + service.refreshVisibility(myForm); - fakeFormWithField.fields.push(contModel); - service.refreshVisibility(fakeFormWithField); - - const fieldWithVisibilityAttached = contModel.form.getFieldById('FIELD_WITH_CONDITION'); + const fieldWithVisibilityAttached = myForm.getFieldById('FIELD_FORM_WITH_CONDITION'); expect(fieldWithVisibilityAttached.isVisible).toBeTruthy(); done(); } @@ -680,36 +719,131 @@ describe('WidgetVisibilityService', () => { jasmine.Ajax.requests.mostRecent().respondWith({ status: 200, contentType: 'json', - responseText: [{id: 'FIELD_TEST', type: 'string', value: 'process_variable_value'}] + responseText: [{id: 'FIELD_FORM_EMPTY', type: 'string', value: 'PROCESS_RIGHT_FORM_FIELD_VALUE'}] }); }); - it('should use the process value to evaluate the visibility condition if the form value is not defined', (done) => { + it('should use the process value to evaluate the True visibility condition if the form value is empty', (done) => { service.getTaskProcessVariable('9999').subscribe( (res: TaskProcessVariableModel[]) => { expect(res).toBeDefined(); - let varValue = service.getVariableValue(formTest, 'FIELD_TEST', res); - expect(varValue).not.toBeUndefined(); - expect(varValue).toBe('process_variable_value'); - visibilityObjTest.leftFormFieldId = 'FIELD_TEST'; + visibilityObjTest.leftFormFieldId = 'FIELD_FORM_EMPTY'; + visibilityObjTest.operator = '=='; + visibilityObjTest.rightValue = 'PROCESS_RIGHT_FORM_FIELD_VALUE'; + + let myForm = new FormModel({ + id: '9999', + name: 'FORM_PROCESS_VARIABLE_VISIBILITY', + processDefinitionId: 'PROCESS_TEST:9:9999', + processDefinitionName: 'PROCESS_TEST', + processDefinitionKey: 'PROCESS_TEST', + taskId: '999', + taskName: 'TEST', + fields: [ + { + fieldType: 'ContainerRepresentation', + id: '000000000000000000', + name: 'Label', + type: 'container', + value: null, + numberOfColumns: 2, + fields: { + 1: [ + { + fieldType: 'FormFieldRepresentation', + id: 'FIELD_FORM_EMPTY', + name: 'FIELD_FORM_EMPTY', + type: 'text', + value: '', + visibilityCondition: null, + isVisible: true + }, + { + fieldType: 'FormFieldRepresentation', + id: 'FIELD_FORM_WITH_CONDITION', + name: 'FIELD_FORM_WITH_CONDITION', + type: 'text', + value: 'field_form_with_condition_value', + visibilityCondition: visibilityObjTest, + isVisible: false + } + ] + } + } + ] + }); + + service.refreshVisibility(myForm); + + const fieldWithVisibilityAttached = myForm.getFieldById('FIELD_FORM_WITH_CONDITION'); + expect(fieldWithVisibilityAttached.isVisible).toBeTruthy(); + done(); + } + ); + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 200, + contentType: 'json', + responseText: [{id: 'FIELD_FORM_EMPTY', type: 'string', value: 'PROCESS_RIGHT_FORM_FIELD_VALUE'}] + }); + }); + + it('should use the process value to evaluate the False visibility condition if the form value is empty', (done) => { + + service.getTaskProcessVariable('9999').subscribe( + (res: TaskProcessVariableModel[]) => { + expect(res).toBeDefined(); + + visibilityObjTest.leftFormFieldId = 'FIELD_FORM_EMPTY'; visibilityObjTest.operator = '=='; visibilityObjTest.rightValue = 'RIGHT_FORM_FIELD_VALUE'; - const fieldRelatedToCondition = fakeFormWithField.getFieldById('FIELD_TEST'); - fieldRelatedToCondition.value = ''; + let myForm = new FormModel({ + id: '9999', + name: 'FORM_PROCESS_VARIABLE_VISIBILITY', + processDefinitionId: 'PROCESS_TEST:9:9999', + processDefinitionName: 'PROCESS_TEST', + processDefinitionKey: 'PROCESS_TEST', + taskId: '999', + taskName: 'TEST', + fields: [ + { + fieldType: 'ContainerRepresentation', + id: '000000000000000000', + name: 'Label', + type: 'container', + value: null, + numberOfColumns: 2, + fields: { + 1: [ + { + fieldType: 'FormFieldRepresentation', + id: 'FIELD_FORM_EMPTY', + name: 'FIELD_FORM_EMPTY', + type: 'text', + value: '', + visibilityCondition: null, + isVisible: true + }, + { + fieldType: 'FormFieldRepresentation', + id: 'FIELD_FORM_WITH_CONDITION', + name: 'FIELD_FORM_WITH_CONDITION', + type: 'text', + value: 'field_form_with_condition_value', + visibilityCondition: visibilityObjTest, + isVisible: true + } + ] + } + } + ] + }); - const fieldWithoutVisibility = fakeFormWithField.getFieldById('FIELD_WITH_CONDITION'); - fieldWithoutVisibility.isVisible = false; - fieldWithoutVisibility.visibilityCondition = visibilityObjTest; + service.refreshVisibility(myForm); - let contModel = new ContainerModel(new FormFieldModel(fakeFormWithField)); - - fakeFormWithField.fields.push(contModel); - service.refreshVisibility(fakeFormWithField); - - const fieldWithVisibilityAttached = contModel.form.getFieldById('FIELD_WITH_CONDITION'); + const fieldWithVisibilityAttached = myForm.getFieldById('FIELD_FORM_WITH_CONDITION'); expect(fieldWithVisibilityAttached.isVisible).toBeFalsy(); done(); } @@ -717,7 +851,7 @@ describe('WidgetVisibilityService', () => { jasmine.Ajax.requests.mostRecent().respondWith({ status: 200, contentType: 'json', - responseText: [{id: 'FIELD_TEST', type: 'string', value: 'process_variable_value'}] + responseText: [{id: 'FIELD_FORM_EMPTY', type: 'string', value: 'PROCESS_RIGHT_FORM_FIELD_VALUE'}] }); }); diff --git a/lib/core/form/services/widget-visibility.service.ts b/lib/core/form/services/widget-visibility.service.ts index 56beb41fb4..8bf36b1b0f 100644 --- a/lib/core/form/services/widget-visibility.service.ts +++ b/lib/core/form/services/widget-visibility.service.ts @@ -77,7 +77,7 @@ export class WidgetVisibilityService { let leftValue = ''; if (visibilityObj.leftRestResponseId && visibilityObj.leftRestResponseId !== 'null') { leftValue = this.getVariableValue(form, visibilityObj.leftRestResponseId, this.processVarList); - } else { + } else if (visibilityObj.leftFormFieldId) { leftValue = this.getFormValue(form, visibilityObj.leftFormFieldId); leftValue = leftValue ? leftValue : this.getVariableValue(form, visibilityObj.leftFormFieldId, this.processVarList); }