diff --git a/ng2-components/ng2-activiti-form/src/components/activiti-form.component.spec.ts b/ng2-components/ng2-activiti-form/src/components/activiti-form.component.spec.ts index 9346878dcf..dd3a20fde4 100644 --- a/ng2-components/ng2-activiti-form/src/components/activiti-form.component.spec.ts +++ b/ng2-components/ng2-activiti-form/src/components/activiti-form.component.spec.ts @@ -34,7 +34,7 @@ describe('ActivitiForm', () => { 'upgradeAllRegistered' ]); visibilityService = jasmine.createSpyObj('WidgetVisibilityService', [ - 'updateVisibilityForForm', 'getTaskProcessVariableModelsForTask' + 'refreshVisibility', 'getTaskProcessVariable' ]); window['componentHandler'] = componentHandler; @@ -143,7 +143,7 @@ describe('ActivitiForm', () => { formComponent.loadForm(); expect(formComponent.getFormByTaskId).toHaveBeenCalledWith(taskId); - expect(visibilityService.getTaskProcessVariableModelsForTask).toHaveBeenCalledWith(taskId); + expect(visibilityService.getTaskProcessVariable).toHaveBeenCalledWith(taskId); }); it('should get form definition by form id on load', () => { @@ -623,15 +623,15 @@ describe('ActivitiForm', () => { it('should check visibility only if field with form provided', () => { formComponent.checkVisibility(null); - expect(visibilityService.updateVisibilityForForm).not.toHaveBeenCalled(); + expect(visibilityService.refreshVisibility).not.toHaveBeenCalled(); let field = new FormFieldModel(null); formComponent.checkVisibility(field); - expect(visibilityService.updateVisibilityForForm).not.toHaveBeenCalled(); + expect(visibilityService.refreshVisibility).not.toHaveBeenCalled(); field = new FormFieldModel(new FormModel()); formComponent.checkVisibility(field); - expect(visibilityService.updateVisibilityForForm).toHaveBeenCalledWith(field.form); + expect(visibilityService.refreshVisibility).toHaveBeenCalledWith(field.form); }); }); diff --git a/ng2-components/ng2-activiti-form/src/components/activiti-form.component.ts b/ng2-components/ng2-activiti-form/src/components/activiti-form.component.ts index f57bbe56b0..6c7378c347 100644 --- a/ng2-components/ng2-activiti-form/src/components/activiti-form.component.ts +++ b/ng2-components/ng2-activiti-form/src/components/activiti-form.component.ts @@ -277,7 +277,7 @@ export class ActivitiForm implements OnInit, AfterViewChecked, OnChanges { loadForm() { if (this.taskId) { this.getFormByTaskId(this.taskId); - this.visibilityService.getTaskProcessVariableModelsForTask(this.taskId); + this.visibilityService.getTaskProcessVariable(this.taskId); return; } @@ -414,7 +414,7 @@ export class ActivitiForm implements OnInit, AfterViewChecked, OnChanges { checkVisibility(field: FormFieldModel) { if (field && field.form) { - this.visibilityService.updateVisibilityForForm(field.form); + this.visibilityService.refreshVisibility(field.form); } } diff --git a/ng2-components/ng2-activiti-form/src/services/assets/widget-visibility.service.mock.ts b/ng2-components/ng2-activiti-form/src/services/assets/widget-visibility.service.mock.ts new file mode 100644 index 0000000000..15c5b8cdc8 --- /dev/null +++ b/ng2-components/ng2-activiti-form/src/services/assets/widget-visibility.service.mock.ts @@ -0,0 +1,97 @@ +/*! + * @license + * Copyright 2016 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { FormModel, FormValues } from '../../components/widgets/core/index'; + +export var formTest = new FormModel({}); + +export var fakeTaskProcessVariableModels = [ + {id: 'TEST_VAR_1', type: 'string', value: 'test_value_1'}, + {id: 'TEST_VAR_2', type: 'string', value: 'test_value_2'}, + {id: 'TEST_VAR_3', type: 'string', value: 'test_value_3'} +]; + +export var formValues: FormValues = { + 'test_1': 'value_1', + 'test_2': 'value_2', + 'test_3': 'value_1', + 'test_4': 'dropdown_id', + 'test_5': 'dropdown_label', + 'dropdown': {'id': 'dropdown_id', 'name': 'dropdown_label'} +}; + +export var fakeFormJson = { + id: '9999', + name: 'FORM_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_TEST', + name: 'FIELD_TEST', + type: 'text', + value: 'RIGHT_FORM_FIELD_VALUE', + visibilityCondition: null, + isVisible: true + }, + { + fieldType: 'FormFieldRepresentation', + id: 'FIELD_WITH_CONDITION', + name: 'FIELD_WITH_CONDITION', + type: 'text', + value: 'field_with_condition_value', + visibilityCondition: null, + isVisible: true + }, + { + fieldType: 'FormFieldRepresentation', + id: 'LEFT_FORM_FIELD_ID', + name: 'LEFT_FORM_FIELD_NAME', + type: 'text', + value: 'LEFT_FORM_FIELD_VALUE', + visibilityCondition: null, + isVisible: true + } + ], + 2: [ + { + fieldType: 'FormFieldRepresentation', + id: 'RIGHT_FORM_FIELD_ID', + name: 'RIGHT_FORM_FIELD_NAME', + type: 'text', + value: 'RIGHT_FORM_FIELD_VALUE', + visibilityCondition: null, + isVisible: true + } + ] + } + } + ] +}; 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 301eaa1bb2..a1f4c6fe57 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 @@ -15,218 +15,185 @@ * limitations under the License. */ - +import { HttpModule } from '@angular/http'; +import { TestBed } from '@angular/core/testing'; import { - async, inject, TestBed -} from '@angular/core/testing'; - -import { - MockBackend, - MockConnection -} from '@angular/http/testing'; - -import { - HttpModule, - Http, - XHRBackend, - Response, - ResponseOptions -} from '@angular/http'; + formTest, + fakeTaskProcessVariableModels, + formValues, + fakeFormJson +} from './assets/widget-visibility.service.mock'; import { WidgetVisibilityService } from './widget-visibility.service'; -import { AlfrescoSettingsService } from 'ng2-alfresco-core'; +import { AlfrescoSettingsService, AlfrescoAuthenticationService, AlfrescoApiService } from 'ng2-alfresco-core'; import { TaskProcessVariableModel } from '../models/task-process-variable.model'; import { WidgetVisibilityModel } from '../models/widget-visibility.model'; -import { FormModel, FormValues, FormFieldModel } from '../components/widgets/core/index'; +import { FormModel, FormFieldModel } from '../components/widgets/core/index'; +declare let jasmine: any; -//////// Tests ///////////// describe('WidgetVisibilityService (mockBackend)', () => { - let formTest = new FormModel({}); - let fakeTaskProcessVariableModels = [ - {id: 'TEST_VAR_1', type: 'string', value: 'test_value_1'}, - {id: 'TEST_VAR_2', type: 'string', value: 'test_value_2'}, - {id: 'TEST_VAR_3', type: 'string', value: 'test_value_3'} - ]; - let formValues: FormValues = { 'test_1': 'value_1', 'test_2': 'value_2', 'test_3': 'value_1' }; - let fakeFormJson = { - id: '9999', - name: 'FORM_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_WITH_CONDITION', - name: 'FIELD_WITH_CONDITION', - type: 'text', - value: 'field_with_condition_value', - visibilityCondition: null - }, - { - fieldType: 'FormFieldRepresentation', - id: 'LEFT_FORM_FIELD_ID', - name: 'LEFT_FORM_FIELD_NAME', - type: 'text', - value: 'LEFT_FORM_FIELD_VALUE', - visibilityCondition: null - } - ], - 2: [ - { - fieldType: 'FormFieldRepresentation', - id: 'RIGHT_FORM_FIELD_ID', - name: 'RIGHT_FORM_FIELD_NAME', - type: 'text', - value: 'RIGHT_FORM_FIELD_VALUE', - visibilityCondition: null - } - ] - } - } - ] - }; + let service: WidgetVisibilityService; + let booleanResult: boolean; + let stubFormWithFields = new FormModel(fakeFormJson); - beforeEach( async(() => { - TestBed.configureTestingModule({ - imports: [ HttpModule ], - providers: [ - WidgetVisibilityService, - AlfrescoSettingsService, - { provide: XHRBackend, useClass: MockBackend } - ] - }) - .compileComponents(); - })); + beforeAll(() => { + TestBed.configureTestingModule({ + imports: [HttpModule], + providers: [ + AlfrescoSettingsService, + AlfrescoAuthenticationService, + AlfrescoApiService, + WidgetVisibilityService + ] + }); + service = TestBed.get(WidgetVisibilityService); + }); - it('can instantiate service when inject service', - inject([WidgetVisibilityService], (service: WidgetVisibilityService) => { - expect(service instanceof WidgetVisibilityService).toBe(true); - })); + beforeEach(() => { + jasmine.Ajax.install(); + }); - it('can instantiate service with "new"', inject([Http], (http: Http) => { - expect(http).not.toBeNull('http should be provided'); - let service = new WidgetVisibilityService(http, null); - expect(service instanceof WidgetVisibilityService).toBe(true, 'new service should be ok'); - })); + afterEach(() => { + jasmine.Ajax.uninstall(); + }); + describe('should be able to evaluate logic operations', () => { - it('can provide the mockBackend as XHRBackend', - inject([XHRBackend], (backend: MockBackend) => { - expect(backend).not.toBeNull('backend should be provided'); - })); - - describe('when service is ready', () => { - let backend: MockBackend; - let service: WidgetVisibilityService; - - beforeEach(inject([Http, XHRBackend, AlfrescoSettingsService], - (http: Http, - be: MockBackend, - setting: AlfrescoSettingsService) => { - backend = be; - service = new WidgetVisibilityService(http, setting); - })); - - it('should evaluate logic operation for two values', () => { - let res: boolean; - - res = service.evaluateLogicalOperation( 'or', true, false); - - expect(res).toBeTruthy(); - - res = service.evaluateLogicalOperation( 'and', true, true); - - expect(res).toBeTruthy(); - - res = service.evaluateLogicalOperation( 'and-not', true, false); - - expect(res).toBeTruthy(); - - res = service.evaluateLogicalOperation( 'or-not', true, true ); - - expect(res).toBeTruthy(); - - res = service.evaluateLogicalOperation( 'or', false, false ); - - expect(res).toBeFalsy(); - - res = service.evaluateLogicalOperation( 'and', true, false ); - - expect(res).toBeFalsy(); - - res = service.evaluateLogicalOperation( 'and-not', false, false ); - - expect(res).toBeFalsy(); - - res = service.evaluateLogicalOperation( 'or-not', false, true ); - - expect(res).toBeFalsy(); + it('using AND and return true', () => { + booleanResult = service.evaluateLogicalOperation('and', true, true); + expect(booleanResult).toBeTruthy(); }); - it('should evaluate string operation for two values', () => { - let res: boolean; + it('using AND and return false', () => { + booleanResult = service.evaluateLogicalOperation('and', true, false); + expect(booleanResult).toBeFalsy(); + }); - res = service.evaluateCondition( 'test', 'test', '=='); + it('using OR and return true', () => { + booleanResult = service.evaluateLogicalOperation('or', true, false); + expect(booleanResult).toBeTruthy(); + }); - expect(res).toBeTruthy(); + it('using OR and return false', () => { + booleanResult = service.evaluateLogicalOperation('or', false, false); + expect(booleanResult).toBeFalsy(); + }); - res = service.evaluateCondition( 1, 2, '<'); + it('using AND NOT and return true', () => { + booleanResult = service.evaluateLogicalOperation('and-not', true, false); + expect(booleanResult).toBeTruthy(); + }); - expect(res).toBeTruthy(); + it('using AND NOT and return false', () => { + booleanResult = service.evaluateLogicalOperation('and-not', false, false); + expect(booleanResult).toBeFalsy(); + }); - res = service.evaluateCondition( true, false, '!=' ); + it('using OR NOT and return true', () => { + booleanResult = service.evaluateLogicalOperation('or-not', true, true); + expect(booleanResult).toBeTruthy(); + }); - expect(res).toBeTruthy(); + it('using OR NOT and return false', () => { + booleanResult = service.evaluateLogicalOperation('or-not', false, true); + expect(booleanResult).toBeFalsy(); + }); - res = service.evaluateCondition( 2, 3, '>' ); + it('should fail with invalid operation', () => { + booleanResult = service.evaluateLogicalOperation(undefined, false, true); + expect(booleanResult).toBeUndefined(); + }); + }); - expect(res).toBeFalsy(); + describe('should be able to evaluate next condition operations', () => { - res = service.evaluateCondition( 2, 2, '>=' ); + it('using == and return true', () => { + booleanResult = service.evaluateCondition('test', 'test', '=='); + expect(booleanResult).toBeTruthy(); + }); - expect(res).toBeTruthy(); + it('using < and return true', () => { + booleanResult = service.evaluateCondition(1, 2, '<'); + expect(booleanResult).toBeTruthy(); + }); - res = service.evaluateCondition( 4, 2, '<=' ); + it('using != and return true', () => { + booleanResult = service.evaluateCondition(true, false, '!='); + expect(booleanResult).toBeTruthy(); + }); - expect(res).toBeFalsy(); + it('using != and return false', () => { + booleanResult = service.evaluateCondition(true, true, '!='); + expect(booleanResult).toBeFalsy(); + }); - res = service.evaluateCondition( null, null, 'empty' ); + it('using >= and return true', () => { + booleanResult = service.evaluateCondition(2, 2, '>='); + expect(booleanResult).toBeTruthy(); + }); - expect(res).toBeTruthy(); + it('using empty with null values and return true', () => { + booleanResult = service.evaluateCondition(null, null, 'empty'); + expect(booleanResult).toBeTruthy(); + }); - res = service.evaluateCondition( '', '', 'empty' ); + it('using empty with empty strings values and return true', () => { + booleanResult = service.evaluateCondition('', '', 'empty'); + expect(booleanResult).toBeTruthy(); + }); - expect(res).toBeTruthy(); + it('using empty with empty string value and return false', () => { + booleanResult = service.evaluateCondition('fake_value', undefined, 'empty'); + expect(booleanResult).toBeFalsy(); + }); - res = service.evaluateCondition( null, null, '!empty' ); + it('using > and return false', () => { + booleanResult = service.evaluateCondition(2, 3, '>'); + expect(booleanResult).toBeFalsy(); + }); - expect(res).toBeFalsy(); + it('using not empty with null values and return false', () => { + booleanResult = service.evaluateCondition(null, null, '!empty'); + expect(booleanResult).toBeFalsy(); + }); - res = service.evaluateCondition( '', '', '!empty' ); + it('using OR NOT with empty strings and return false', () => { + booleanResult = service.evaluateCondition('', '', '!empty'); + expect(booleanResult).toBeFalsy(); + }); - expect(res).toBeFalsy(); + it('using <= and return false', () => { + booleanResult = service.evaluateCondition(2, 1, '<='); + expect(booleanResult).toBeFalsy(); + }); + + it('using <= and return true for different values', () => { + booleanResult = service.evaluateCondition(1, 2, '<='); + expect(booleanResult).toBeTruthy(); + }); + + it('using <= and return true for same values', () => { + booleanResult = service.evaluateCondition(2, 2, '<='); + expect(booleanResult).toBeTruthy(); + }); + + it('should return undefined for invalid operation', () => { + booleanResult = service.evaluateCondition(null, null, undefined); + expect(booleanResult).toBeUndefined(); + }); + }); + + describe('should retrive the process variables', () => { + let fakeFormWithField = new FormModel(fakeFormJson); + let visibilityObjTest: WidgetVisibilityModel; + let chainedVisibilityObj = new WidgetVisibilityModel(); + + beforeEach(() => { + visibilityObjTest = new WidgetVisibilityModel(); }); it('should return the process variables for task', (done) => { - let options = new ResponseOptions({status: 200, - body: fakeTaskProcessVariableModels }); - let response = new Response(options); - backend.connections.subscribe((c: MockConnection) => c.mockRespond(response)); - - - service.getTaskProcessVariableModelsForTask('9999').subscribe( + service.getTaskProcessVariable('9999').subscribe( (res: TaskProcessVariableModel[]) => { expect(res).toBeDefined(); expect(res.length).toEqual(3); @@ -236,346 +203,423 @@ describe('WidgetVisibilityService (mockBackend)', () => { done(); } ); + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 200, + contentType: 'json', + responseText: fakeTaskProcessVariableModels + }); }); it('should be able to retrieve the value of a process variable', (done) => { - let options = new ResponseOptions({status: 200, - body: fakeTaskProcessVariableModels }); - let response = new Response(options); - backend.connections.subscribe((c: MockConnection) => c.mockRespond(response)); - - service.getTaskProcessVariableModelsForTask('9999').subscribe( + service.getTaskProcessVariable('9999').subscribe( (res: TaskProcessVariableModel[]) => { - expect(res).toBeDefined(); - let varValue = service.getValueFromVariable(formTest, 'TEST_VAR_1', res); - expect(varValue).not.toBeUndefined(); - expect(varValue).toBe('test_value_1'); - done(); + expect(res).toBeDefined(); + let varValue = service.getVariableValue(formTest, 'TEST_VAR_1', res); + expect(varValue).not.toBeUndefined(); + expect(varValue).toBe('test_value_1'); + done(); } - ); - }); - - it('should be able to retrieve the value of a form variable', () => { - let fakeForm = new FormModel({variables: [ - { name: 'FORM_VARIABLE_TEST', - type: 'string', - value: 'form_value_test' } - ]}); - let varValue = service.getValueFromVariable(fakeForm, 'FORM_VARIABLE_TEST', null); - - expect(varValue).not.toBeUndefined(); - expect(varValue).toBe('form_value_test'); + ); + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 200, + contentType: 'json', + responseText: fakeTaskProcessVariableModels + }); }); it('should return undefined if the variable does not exist', (done) => { - let options = new ResponseOptions({status: 200, - body: fakeTaskProcessVariableModels }); - let response = new Response(options); - backend.connections.subscribe((c: MockConnection) => c.mockRespond(response)); - - service.getTaskProcessVariableModelsForTask('9999').subscribe( + service.getTaskProcessVariable('9999').subscribe( (res: TaskProcessVariableModel[]) => { - let varValue = service.getValueFromVariable(formTest, 'TEST_MYSTERY_VAR', res); - expect(varValue).toBeUndefined(); - done(); + let varValue = service.getVariableValue(formTest, 'TEST_MYSTERY_VAR', res); + expect(varValue).toBeUndefined(); + done(); } - ); - }); - - it('should be able to retrieve a field value searching in the form', () => { - let stubFormWithFields = new FormModel(fakeFormJson); - let formValue = service.getFormValueByName(stubFormWithFields, 'FIELD_WITH_CONDITION'); - - expect(formValue).not.toBeNull(); - expect(formValue).toBe('field_with_condition_value'); - }); - - it('should return undefined if the field value is not in the form', () => { - let stubFormWithFields = new FormModel(fakeFormJson); - - let formValue = service.getFormValueByName(stubFormWithFields, 'FIELD_MYSTERY'); - - expect(formValue).toBeUndefined(); - }); - - it('should take the value from form values if it is present', () => { - formTest.values = formValues; - - let formValue = service.getValueOField(formTest, 'test_1'); - - expect(formValue).not.toBeNull(); - expect(formValue).toBe('value_1'); - }); - - it('should search in the form if element value is not in form values', () => { - let fakeFormWithField = new FormModel(fakeFormJson); - fakeFormWithField.values = formValues; - - let value = service.getValueOField(fakeFormWithField, 'FIELD_WITH_CONDITION'); - - expect(value).not.toBeNull(); - expect(value).toBe('field_with_condition_value'); - }); - - it('should return undefined if the element is not present anywhere', () => { - let fakeFormWithField = new FormModel(fakeFormJson); - fakeFormWithField.values = formValues; - - let formValue = service.getValueOField(fakeFormWithField, 'FIELD_MYSTERY'); - - expect(formValue).toBeUndefined(); - }); - - it('should retrieve the value for the right field when it is a value', () => { - let visibilityObjTest = new WidgetVisibilityModel(); - visibilityObjTest.rightValue = '100'; - - let rightValue = service.getRightValue(formTest, visibilityObjTest); - - expect(rightValue).toBe('100'); + ); + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 200, + contentType: 'json', + responseText: fakeTaskProcessVariableModels + }); }); it('should retrieve the value for the right field when it is a process variable', (done) => { - let options = new ResponseOptions({status: 200, - body: fakeTaskProcessVariableModels }); - let response = new Response(options); - backend.connections.subscribe((c: MockConnection) => c.mockRespond(response)); - - service.getTaskProcessVariableModelsForTask('9999').subscribe( + service.getTaskProcessVariable('9999').subscribe( (res: TaskProcessVariableModel[]) => { - let visibilityObjTest = new WidgetVisibilityModel(); - visibilityObjTest.rightRestResponseId = 'TEST_VAR_2'; + visibilityObjTest.rightRestResponseId = 'TEST_VAR_2'; + let rightValue = service.getRightValue(formTest, visibilityObjTest); - let rightValue = service.getRightValue(formTest, visibilityObjTest); - - expect(rightValue).not.toBeNull(); - expect(rightValue).toBe('test_value_2'); - done(); + expect(rightValue).not.toBeNull(); + expect(rightValue).toBe('test_value_2'); + done(); } - ); - }); - - it('should retrieve the value for the right field when it is a form variable', () => { - let fakeFormWithField = new FormModel(fakeFormJson); - let visibilityObjTest = new WidgetVisibilityModel(); - visibilityObjTest.rightFormFieldId = 'RIGHT_FORM_FIELD_ID'; - - let rightValue = service.getRightValue(fakeFormWithField, visibilityObjTest); - - expect(rightValue).not.toBeNull(); - expect(rightValue).toBe('RIGHT_FORM_FIELD_VALUE'); - }); - - it('should retrieve right value from form values if it is present', () => { - formTest.values = formValues; - let visibilityObjTest = new WidgetVisibilityModel(); - visibilityObjTest.rightFormFieldId = 'test_2'; - - let rightValue = service.getRightValue(formTest, visibilityObjTest); - - expect(rightValue).not.toBeNull(); - expect(formTest.values).toEqual(formValues); - expect(rightValue).toBe('value_2'); - }); - - it('should return undefined for a value that is not on variable or form', () => { - let fakeFormWithField = new FormModel(fakeFormJson); - fakeFormWithField.values = formValues; - let visibilityObjTest = new WidgetVisibilityModel(); - visibilityObjTest.rightFormFieldId = 'NO_FIELD_FORM'; - - let rightValue = service.getRightValue(fakeFormWithField, visibilityObjTest); - - expect(rightValue).toBeUndefined(); + ); + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 200, + contentType: 'json', + responseText: fakeTaskProcessVariableModels + }); }); it('should retrieve the value for the left field when it is a process variable', (done) => { - let options = new ResponseOptions({status: 200, - body: fakeTaskProcessVariableModels }); - let response = new Response(options); - backend.connections.subscribe((c: MockConnection) => c.mockRespond(response)); - - service.getTaskProcessVariableModelsForTask('9999').subscribe( + service.getTaskProcessVariable('9999').subscribe( (res: TaskProcessVariableModel[]) => { - let visibilityObjTest = new WidgetVisibilityModel(); - visibilityObjTest.leftRestResponseId = 'TEST_VAR_2'; + visibilityObjTest.leftRestResponseId = 'TEST_VAR_2'; + let rightValue = service.getLeftValue(formTest, visibilityObjTest); - let rightValue = service.getLeftValue(formTest, visibilityObjTest); - - expect(rightValue).not.toBeNull(); - expect(rightValue).toBe('test_value_2'); - done(); + expect(rightValue).not.toBeNull(); + expect(rightValue).toBe('test_value_2'); + done(); } - ); - }); - - it('should retrieve the value for the left field when it is a form variable', () => { - let fakeForm = new FormModel({variables: [ - { name: 'FORM_VARIABLE_TEST', - type: 'string', - value: 'form_value_test' } - ]}); - let visibilityObjTest = new WidgetVisibilityModel(); - visibilityObjTest.leftRestResponseId = 'FORM_VARIABLE_TEST'; - - let leftValue = service.getLeftValue(fakeForm, visibilityObjTest); - - expect(leftValue).not.toBeNull(); - expect(leftValue).toBe('form_value_test'); - }); - - it('should retrieve the value for the left field when it is a form value', () => { - let fakeFormWithField = new FormModel(fakeFormJson); - let visibilityObjTest = new WidgetVisibilityModel(); - visibilityObjTest.leftFormFieldId = 'FIELD_WITH_CONDITION'; - - let leftValue = service.getLeftValue(fakeFormWithField, visibilityObjTest); - - expect(leftValue).not.toBeNull(); - expect(leftValue).toBe('field_with_condition_value'); - }); - - it('should retrieve left value from form values if it is present', () => { - formTest.values = formValues; - let visibilityObjTest = new WidgetVisibilityModel(); - visibilityObjTest.leftFormFieldId = 'test_2'; - - let leftValue = service.getLeftValue(formTest, visibilityObjTest); - - expect(leftValue).not.toBeNull(); - expect(leftValue).toBe('value_2'); - }); - - it('should return undefined for a value that is not on variable or form', () => { - let fakeFormWithField = new FormModel(fakeFormJson); - let visibilityObjTest = new WidgetVisibilityModel(); - - let leftValue = service.getLeftValue(fakeFormWithField, visibilityObjTest); - - expect(leftValue).toBeUndefined(); - }); - - it('should evaluate the visibility for the field with single visibility condition between two field values', () => { - formTest.values = formValues; - let visibilityObjTest = new WidgetVisibilityModel(); - visibilityObjTest.leftFormFieldId = 'test_1'; - visibilityObjTest.operator = '=='; - visibilityObjTest.rightFormFieldId = 'test_3'; - - let isVisible = service.evaluateVisibilityForField(formTest, visibilityObjTest); - - expect(isVisible).toBeTruthy(); - }); - - it('should evaluate true visibility for the field with single visibility condition between a field and a value', () => { - formTest.values = formValues; - let visibilityObjTest = new WidgetVisibilityModel(); - visibilityObjTest.leftFormFieldId = 'test_1'; - visibilityObjTest.operator = '=='; - visibilityObjTest.rightValue = 'value_1'; - - let isVisible = service.evaluateVisibilityForField(formTest, visibilityObjTest); - - expect(isVisible).toBeTruthy(); - }); - - it('should evaluate the visibility for the field with single visibility condition between form values', () => { - let fakeFormWithField = new FormModel(fakeFormJson); - let visibilityObjTest = new WidgetVisibilityModel(); - visibilityObjTest.leftFormFieldId = 'LEFT_FORM_FIELD_ID'; - visibilityObjTest.operator = '!='; - visibilityObjTest.rightFormFieldId = 'RIGHT_FORM_FIELD_ID'; - - let isVisible = service.evaluateVisibilityForField(fakeFormWithField, visibilityObjTest); - - expect(isVisible).toBeTruthy(); + ); + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 200, + contentType: 'json', + responseText: fakeTaskProcessVariableModels + }); }); it('should evaluate the visibility for the field between form value and process var', (done) => { - let options = new ResponseOptions({status: 200, - body: fakeTaskProcessVariableModels }); - let response = new Response(options); - backend.connections.subscribe((c: MockConnection) => c.mockRespond(response)); - - service.getTaskProcessVariableModelsForTask('9999').subscribe( + service.getTaskProcessVariable('9999').subscribe( (res: TaskProcessVariableModel[]) => { - let fakeFormWithField = new FormModel(fakeFormJson); - let visibilityObjTest = new WidgetVisibilityModel(); - visibilityObjTest.leftFormFieldId = 'LEFT_FORM_FIELD_ID'; - visibilityObjTest.operator = '!='; - visibilityObjTest.rightRestResponseId = 'TEST_VAR_2'; + visibilityObjTest.leftFormFieldId = 'LEFT_FORM_FIELD_ID'; + visibilityObjTest.operator = '!='; + visibilityObjTest.rightRestResponseId = 'TEST_VAR_2'; + let isVisible = service.isFieldVisible(fakeFormWithField, visibilityObjTest); - let isVisible = service.evaluateVisibilityForField(fakeFormWithField, visibilityObjTest); - - expect(isVisible).toBeTruthy(); - done(); + expect(isVisible).toBeTruthy(); + done(); } - ); + ); + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 200, + contentType: 'json', + responseText: fakeTaskProcessVariableModels + }); }); it('should evaluate visibility with multiple conditions', (done) => { - let options = new ResponseOptions({status: 200, - body: fakeTaskProcessVariableModels }); - let response = new Response(options); - backend.connections.subscribe((c: MockConnection) => c.mockRespond(response)); - - service.getTaskProcessVariableModelsForTask('9999').subscribe( + service.getTaskProcessVariable('9999').subscribe( (res: TaskProcessVariableModel[]) => { - let fakeFormWithField = new FormModel(fakeFormJson); - let visibilityObjTest = new WidgetVisibilityModel(); - let chainedVisibilityObj = new WidgetVisibilityModel(); - visibilityObjTest.leftFormFieldId = 'LEFT_FORM_FIELD_ID'; - visibilityObjTest.operator = '!='; - visibilityObjTest.rightRestResponseId = 'TEST_VAR_2'; - visibilityObjTest.nextConditionOperator = 'and'; - chainedVisibilityObj.leftRestResponseId = 'TEST_VAR_2'; - chainedVisibilityObj.operator = '!empty'; - visibilityObjTest.nextCondition = chainedVisibilityObj; + visibilityObjTest.leftFormFieldId = 'LEFT_FORM_FIELD_ID'; + visibilityObjTest.operator = '!='; + visibilityObjTest.rightRestResponseId = 'TEST_VAR_2'; + visibilityObjTest.nextConditionOperator = 'and'; + chainedVisibilityObj.leftRestResponseId = 'TEST_VAR_2'; + chainedVisibilityObj.operator = '!empty'; + visibilityObjTest.nextCondition = chainedVisibilityObj; - let isVisible = service.evaluateVisibilityForField(fakeFormWithField, visibilityObjTest); + let isVisible = service.isFieldVisible(fakeFormWithField, visibilityObjTest); - expect(isVisible).toBeTruthy(); - done(); + expect(isVisible).toBeTruthy(); + done(); } - ); + ); + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 200, + contentType: 'json', + responseText: fakeTaskProcessVariableModels + }); + }); + }); + + describe('should return the value of the field', () => { + let visibilityObjTest: WidgetVisibilityModel; + let fakeFormWithField = new FormModel(fakeFormJson); + let jsonFieldFake = { + id: 'FAKE_FORM_FIELD_ID', + value: 'FAKE_FORM_FIELD_VALUE', + visibilityCondition: undefined + }; + let fakeForm = new FormModel({ + variables: [ + { + name: 'FORM_VARIABLE_TEST', + type: 'string', + value: 'form_value_test' + }] }); - it('should return true when the visibility condition is not valid', () => { - let visibilityObjTest = new WidgetVisibilityModel(); - visibilityObjTest.leftFormFieldId = ''; - visibilityObjTest.leftRestResponseId = ''; - visibilityObjTest.operator = '!='; + beforeEach(() => { + visibilityObjTest = new WidgetVisibilityModel(); + formTest.values = formValues; + jsonFieldFake.visibilityCondition = visibilityObjTest; + }); - let isVisible = service.getVisiblityForField(formTest, visibilityObjTest); + it('should be able to retrieve a field value searching in the form', () => { + let formValue = service.searchForm(stubFormWithFields, 'FIELD_WITH_CONDITION'); - expect(isVisible).toBeTruthy(); + expect(formValue).not.toBeNull(); + expect(formValue).toBe('field_with_condition_value'); + }); + + it('should return undefined if the field value is not in the form', () => { + let formValue = service.searchForm(stubFormWithFields, 'FIELD_MYSTERY'); + + expect(formValue).toBeUndefined(); + }); + + it('should search in the form if element value is not in form values', () => { + let value = service.getFormValue(fakeFormWithField, 'FIELD_WITH_CONDITION'); + + expect(value).not.toBeNull(); + expect(value).toBe('field_with_condition_value'); + }); + + it('should return undefined if the element is not present anywhere', () => { + let formValue = service.getFormValue(fakeFormWithField, 'FIELD_MYSTERY'); + + expect(formValue).toBeUndefined(); + }); + + it('should retrieve the value for the right field when it is a value', () => { + visibilityObjTest.rightValue = '100'; + let rightValue = service.getRightValue(formTest, visibilityObjTest); + + expect(rightValue).toBe('100'); + }); + + it('should retrieve the value for the right field when it is a form variable', () => { + visibilityObjTest.rightFormFieldId = 'RIGHT_FORM_FIELD_ID'; + let rightValue = service.getRightValue(fakeFormWithField, visibilityObjTest); + + expect(rightValue).not.toBeNull(); + expect(rightValue).toBe('RIGHT_FORM_FIELD_VALUE'); + }); + + it('should take the value from form values if it is present', () => { + let formValue = service.getFormValue(formTest, 'test_1'); + + expect(formValue).not.toBeNull(); + expect(formValue).toBe('value_1'); + }); + + it('should retrieve right value from form values if it is present', () => { + visibilityObjTest.rightFormFieldId = 'test_2'; + let rightValue = service.getRightValue(formTest, visibilityObjTest); + + expect(rightValue).not.toBeNull(); + expect(formTest.values).toEqual(formValues); + expect(rightValue).toBe('value_2'); + }); + + it('should retrieve the value for the left field when it is a form value', () => { + visibilityObjTest.leftFormFieldId = 'FIELD_WITH_CONDITION'; + let leftValue = service.getLeftValue(fakeFormWithField, visibilityObjTest); + + expect(leftValue).not.toBeNull(); + expect(leftValue).toBe('field_with_condition_value'); + }); + + it('should retrieve left value from form values if it is present', () => { + visibilityObjTest.leftFormFieldId = 'test_2'; + let leftValue = service.getLeftValue(formTest, visibilityObjTest); + + expect(leftValue).not.toBeNull(); + expect(leftValue).toBe('value_2'); + }); + + it('should return undefined for a value that is not on variable or form', () => { + let leftValue = service.getLeftValue(fakeFormWithField, visibilityObjTest); + + expect(leftValue).toBeUndefined(); + }); + + it('should evaluate the visibility for the field with single visibility condition between two field values', () => { + visibilityObjTest.leftFormFieldId = 'test_1'; + visibilityObjTest.operator = '=='; + visibilityObjTest.rightFormFieldId = 'test_3'; + let isVisible = service.isFieldVisible(formTest, visibilityObjTest); + + expect(isVisible).toBeTruthy(); + }); + + it('should evaluate true visibility for the field with single visibility condition between a field and a value', () => { + visibilityObjTest.leftFormFieldId = 'test_1'; + visibilityObjTest.operator = '=='; + visibilityObjTest.rightValue = 'value_1'; + let isVisible = service.isFieldVisible(formTest, visibilityObjTest); + + expect(isVisible).toBeTruthy(); + }); + + it('should return undefined for a value that is not on variable or form', () => { + visibilityObjTest.rightFormFieldId = 'NO_FIELD_FORM'; + let rightValue = service.getRightValue(fakeFormWithField, visibilityObjTest); + + expect(rightValue).toBeUndefined(); + }); + + it('should evaluate the visibility for the field with single visibility condition between form values', () => { + visibilityObjTest.leftFormFieldId = 'LEFT_FORM_FIELD_ID'; + visibilityObjTest.operator = '!='; + visibilityObjTest.rightFormFieldId = 'RIGHT_FORM_FIELD_ID'; + let isVisible = service.isFieldVisible(fakeFormWithField, visibilityObjTest); + + expect(isVisible).toBeTruthy(); }); it('should refresh the visibility for a form field object', () => { - let fakeFormWithField = new FormModel(fakeFormJson); - let visibilityObjTest = new WidgetVisibilityModel(); - fakeFormWithField.values = formValues; - visibilityObjTest.leftFormFieldId = 'test_1'; - visibilityObjTest.operator = '!='; - visibilityObjTest.rightFormFieldId = 'test_3'; - let jsonFieldFake = {id: 'FAKE_FORM_FIELD_ID', value: 'FAKE_FORM_FIELD_VALUE', visibilityCondition: visibilityObjTest}; - let fakeFormField: FormFieldModel = new FormFieldModel(fakeFormWithField, jsonFieldFake); + visibilityObjTest.leftFormFieldId = 'test_1'; + visibilityObjTest.operator = '!='; + visibilityObjTest.rightFormFieldId = 'test_3'; + let fakeFormField: FormFieldModel = new FormFieldModel(formTest, jsonFieldFake); + service.refreshFieldVisibility(fakeFormField); - service.refreshVisibilityForField(fakeFormField); + expect(fakeFormField.isVisible).toBeFalsy(); + }); - expect(fakeFormField.isVisible).toBeFalsy(); + it('should return true when the visibility condition is not valid', () => { + visibilityObjTest.leftFormFieldId = ''; + visibilityObjTest.leftRestResponseId = ''; + visibilityObjTest.operator = '!='; + let isVisible = service.evaluateVisibility(formTest, visibilityObjTest); + + expect(isVisible).toBeTruthy(); }); it('should not change the isVisible if field does not have visibility condition', () => { - let fakeFormWithField = new FormModel(fakeFormJson); - let jsonFieldFake = {id: 'FAKE_FORM_FIELD_ID', - value: 'FAKE_FORM_FIELD_VALUE', - visibilityCondition: null - }; - let fakeFormField: FormFieldModel = new FormFieldModel(fakeFormWithField, jsonFieldFake); - fakeFormField.isVisible = false; - service.refreshVisibilityForField(fakeFormField); - expect(fakeFormField.isVisible).toBeFalsy(); + jsonFieldFake.visibilityCondition = null; + let fakeFormField: FormFieldModel = new FormFieldModel(fakeFormWithField, jsonFieldFake); + fakeFormField.isVisible = false; + service.refreshFieldVisibility(fakeFormField); + + expect(fakeFormField.isVisible).toBeFalsy(); }); - }); + + it('should be able to retrieve the value of a form variable', () => { + let varValue = service.getVariableValue(fakeForm, 'FORM_VARIABLE_TEST', null); + + expect(varValue).not.toBeUndefined(); + expect(varValue).toBe('form_value_test'); + }); + + it('should return undefined for not existing form variable', () => { + let varValue = service.getVariableValue(fakeForm, 'MISTERY_FORM_VARIABLE', null); + + expect(varValue).toBeUndefined(); + }); + + it('should retrieve the value for the left field when it is a form variable', () => { + visibilityObjTest.leftRestResponseId = 'FORM_VARIABLE_TEST'; + let leftValue = service.getLeftValue(fakeForm, visibilityObjTest); + + expect(leftValue).not.toBeNull(); + expect(leftValue).toBe('form_value_test'); + }); + + it('should determine visibility for dropdown on label condition', () => { + let dropdownValue = service.getDropDownName(formTest.values, 'dropdown_LABEL'); + + expect(dropdownValue).not.toBeNull(); + expect(dropdownValue).toBeDefined(); + expect(dropdownValue).toBe('dropdown_label'); + }); + + it('should be able to get the value for a dropdown filtered with Label', () => { + let dropdownValue = service.getValue(formTest.values, 'dropdown_LABEL'); + + expect(dropdownValue).not.toBeNull(); + expect(dropdownValue).toBeDefined(); + expect(dropdownValue).toBe('dropdown_label'); + }); + + it('should be able to get the value for a standard field', () => { + let dropdownValue = service.getValue(formTest.values, 'test_2'); + + expect(dropdownValue).not.toBeNull(); + expect(dropdownValue).toBeDefined(); + expect(dropdownValue).toBe('value_2'); + }); + + it('should get the dropdown label value from a form', () => { + let dropdownValue = service.getFormValue(formTest, 'dropdown_LABEL'); + + expect(dropdownValue).not.toBeNull(); + expect(dropdownValue).toBeDefined(); + expect(dropdownValue).toBe('dropdown_label'); + }); + + it('should get the dropdown id value from a form', () => { + let dropdownValue = service.getFormValue(formTest, 'dropdown'); + + expect(dropdownValue).not.toBeNull(); + expect(dropdownValue).toBeDefined(); + expect(dropdownValue).toBe('dropdown_id'); + }); + + it('should retrieve the value for the right field when it is a dropdown id', () => { + visibilityObjTest.rightFormFieldId = 'dropdown'; + let rightValue = service.getRightValue(formTest, visibilityObjTest); + + expect(rightValue).toBeDefined(); + expect(rightValue).toBe('dropdown_id'); + }); + + it('should retrieve the value for the right field when it is a dropdown label', () => { + visibilityObjTest.rightFormFieldId = 'dropdown_LABEL'; + let rightValue = service.getRightValue(formTest, visibilityObjTest); + + expect(rightValue).toBeDefined(); + expect(rightValue).toBe('dropdown_label'); + }); + + it('should be able to evaluate condition with a dropdown