Added fix for visibility on dropdown

This commit is contained in:
Vito Albano 2016-10-10 09:48:17 +01:00
parent 8b1b5e9926
commit 82632e5e89
2 changed files with 658 additions and 626 deletions

View File

@ -15,7 +15,6 @@
* limitations under the License.
*/
import {
async, inject, TestBed
} from '@angular/core/testing';
@ -32,14 +31,12 @@ import {
Response,
ResponseOptions
} from '@angular/http';
import { WidgetVisibilityService } from './widget-visibility.service';
import { AlfrescoSettingsService } 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 {WidgetVisibilityService} from './widget-visibility.service';
import {AlfrescoSettingsService} 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';
//////// Tests /////////////
describe('WidgetVisibilityService (mockBackend)', () => {
let formTest = new FormModel({});
let fakeTaskProcessVariableModels = [
@ -47,7 +44,14 @@ describe('WidgetVisibilityService (mockBackend)', () => {
{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 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'}
};
let fakeFormJson = {
id: '9999',
name: 'FORM_VISIBILITY',
@ -98,16 +102,15 @@ describe('WidgetVisibilityService (mockBackend)', () => {
]
};
beforeEach( async(() => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ HttpModule ],
imports: [HttpModule],
providers: [
WidgetVisibilityService,
AlfrescoSettingsService,
{ provide: XHRBackend, useClass: MockBackend }
{provide: XHRBackend, useClass: MockBackend}
]
})
.compileComponents();
}).compileComponents();
}));
it('can instantiate service when inject service',
@ -121,15 +124,99 @@ describe('WidgetVisibilityService (mockBackend)', () => {
expect(service instanceof WidgetVisibilityService).toBe(true, 'new service should be ok');
}));
it('can provide the mockBackend as XHRBackend',
inject([XHRBackend], (backend: MockBackend) => {
expect(backend).not.toBeNull('backend should be provided');
}));
describe('when service is ready', () => {
describe('should be able to evaluate logic operations', () => {
let res: boolean;
let service: WidgetVisibilityService = new WidgetVisibilityService(null, null);
it('using AND and return true', () => {
res = service.evaluateLogicalOperation('and', true, true);
expect(res).toBeTruthy();
});
it('using AND and return false', () => {
res = service.evaluateLogicalOperation('and', true, false);
expect(res).toBeFalsy();
});
it('using OR and return true', () => {
res = service.evaluateLogicalOperation('or', true, false);
expect(res).toBeTruthy();
});
it('using OR and return false', () => {
res = service.evaluateLogicalOperation('or', false, false);
expect(res).toBeFalsy();
});
it('using AND NOT and return true', () => {
res = service.evaluateLogicalOperation('and-not', true, false);
expect(res).toBeTruthy();
});
it('using AND NOT and return false', () => {
res = service.evaluateLogicalOperation('and-not', false, false);
expect(res).toBeFalsy();
});
it('using OR NOT and return true', () => {
res = service.evaluateLogicalOperation('or-not', true, true);
expect(res).toBeTruthy();
});
it('using OR NOT and return false', () => {
res = service.evaluateLogicalOperation('or-not', false, true);
expect(res).toBeFalsy();
});
});
describe('should be able to evaluate next condition operations', () => {
let res: boolean;
let service: WidgetVisibilityService = new WidgetVisibilityService(null, null);
it('using == and return true', () => {
res = service.evaluateCondition('test', 'test', '==');
expect(res).toBeTruthy();
});
it('using < and return true', () => {
res = service.evaluateCondition(1, 2, '<');
expect(res).toBeTruthy();
});
it('using != and return true', () => {
res = service.evaluateCondition(true, false, '!=');
expect(res).toBeTruthy();
});
it('using != and return false', () => {
res = service.evaluateCondition(true, true, '!=');
expect(res).toBeFalsy();
});
it('using >= and return true', () => {
res = service.evaluateCondition(2, 2, '>=');
expect(res).toBeTruthy();
});
it('using empty with null values and return true', () => {
res = service.evaluateCondition(null, null, 'empty');
expect(res).toBeTruthy();
});
it('using empty with empty strings values and return true', () => {
res = service.evaluateCondition('', '', 'empty');
expect(res).toBeTruthy();
});
it('using > and return false', () => {
res = service.evaluateCondition(2, 3, '>');
expect(res).toBeFalsy();
});
it('using not empty with null values and return false', () => {
res = service.evaluateCondition(null, null, '!empty');
expect(res).toBeFalsy();
});
it('using OR NOT with empty strings and return false', () => {
res = service.evaluateCondition('', '', '!empty');
expect(res).toBeFalsy();
});
});
describe('after backend is mocked', () => {
let backend: MockBackend;
let service: WidgetVisibilityService;
let stubFormWithFields = new FormModel(fakeFormJson);
beforeEach(inject([Http, XHRBackend, AlfrescoSettingsService],
(http: Http,
@ -139,93 +226,22 @@ describe('WidgetVisibilityService (mockBackend)', () => {
service = new WidgetVisibilityService(http, setting);
}));
it('should evaluate logic operation for two values', () => {
let res: boolean;
describe('should retrive the process variables', () => {
let fakeFormWithField = new FormModel(fakeFormJson);
let visibilityObjTest: WidgetVisibilityModel;
let chainedVisibilityObj = new WidgetVisibilityModel();
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();
beforeEach(() => {
let options = new ResponseOptions({
status: 200,
body: fakeTaskProcessVariableModels
});
it('should evaluate string operation for two values', () => {
let res: boolean;
res = service.evaluateCondition( 'test', 'test', '==');
expect(res).toBeTruthy();
res = service.evaluateCondition( 1, 2, '<');
expect(res).toBeTruthy();
res = service.evaluateCondition( true, false, '!=' );
expect(res).toBeTruthy();
res = service.evaluateCondition( 2, 3, '>' );
expect(res).toBeFalsy();
res = service.evaluateCondition( 2, 2, '>=' );
expect(res).toBeTruthy();
res = service.evaluateCondition( 4, 2, '<=' );
expect(res).toBeFalsy();
res = service.evaluateCondition( null, null, 'empty' );
expect(res).toBeTruthy();
res = service.evaluateCondition( '', '', 'empty' );
expect(res).toBeTruthy();
res = service.evaluateCondition( null, null, '!empty' );
expect(res).toBeFalsy();
res = service.evaluateCondition( '', '', '!empty' );
expect(res).toBeFalsy();
let response = new Response(options);
backend.connections.subscribe((c: MockConnection) => c.mockRespond(response));
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(
(res: TaskProcessVariableModel[]) => {
expect(res).toBeDefined();
@ -239,11 +255,6 @@ describe('WidgetVisibilityService (mockBackend)', () => {
});
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(
(res: TaskProcessVariableModel[]) => {
expect(res).toBeDefined();
@ -255,24 +266,7 @@ describe('WidgetVisibilityService (mockBackend)', () => {
);
});
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');
});
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(
(res: TaskProcessVariableModel[]) => {
let varValue = service.getValueFromVariable(formTest, 'TEST_MYSTERY_VAR', res);
@ -282,70 +276,10 @@ describe('WidgetVisibilityService (mockBackend)', () => {
);
});
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');
});
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(
(res: TaskProcessVariableModel[]) => {
let visibilityObjTest = new WidgetVisibilityModel();
visibilityObjTest.rightRestResponseId = 'TEST_VAR_2';
let rightValue = service.getRightValue(formTest, visibilityObjTest);
expect(rightValue).not.toBeNull();
@ -355,51 +289,10 @@ describe('WidgetVisibilityService (mockBackend)', () => {
);
});
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();
});
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(
(res: TaskProcessVariableModel[]) => {
let visibilityObjTest = new WidgetVisibilityModel();
visibilityObjTest.leftRestResponseId = 'TEST_VAR_2';
let rightValue = service.getLeftValue(formTest, visibilityObjTest);
expect(rightValue).not.toBeNull();
@ -409,102 +302,12 @@ describe('WidgetVisibilityService (mockBackend)', () => {
);
});
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();
});
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(
(res: TaskProcessVariableModel[]) => {
let fakeFormWithField = new FormModel(fakeFormJson);
let visibilityObjTest = new WidgetVisibilityModel();
visibilityObjTest.leftFormFieldId = 'LEFT_FORM_FIELD_ID';
visibilityObjTest.operator = '!=';
visibilityObjTest.rightRestResponseId = 'TEST_VAR_2';
let isVisible = service.evaluateVisibilityForField(fakeFormWithField, visibilityObjTest);
expect(isVisible).toBeTruthy();
@ -514,16 +317,8 @@ describe('WidgetVisibilityService (mockBackend)', () => {
});
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(
(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';
@ -539,43 +334,270 @@ describe('WidgetVisibilityService (mockBackend)', () => {
}
);
});
});
it('should return true when the visibility condition is not valid', () => {
let visibilityObjTest = new WidgetVisibilityModel();
visibilityObjTest.leftFormFieldId = '';
visibilityObjTest.leftRestResponseId = '';
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'
}]
});
beforeEach(() => {
visibilityObjTest = new WidgetVisibilityModel();
formTest.values = formValues;
jsonFieldFake.visibilityCondition = visibilityObjTest;
});
it('should be able to retrieve a field value searching in the form', () => {
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 formValue = service.getFormValueByName(stubFormWithFields, 'FIELD_MYSTERY');
expect(formValue).toBeUndefined();
});
it('should search in the form if element value is not in form values', () => {
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 formValue = service.getValueOField(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.getValueOField(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.evaluateVisibilityForField(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.evaluateVisibilityForField(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 = '!=';
let isVisible = service.getVisiblityForField(formTest, visibilityObjTest);
visibilityObjTest.rightFormFieldId = 'RIGHT_FORM_FIELD_ID';
let isVisible = service.evaluateVisibilityForField(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);
let fakeFormField: FormFieldModel = new FormFieldModel(formTest, jsonFieldFake);
service.refreshVisibilityForField(fakeFormField);
expect(fakeFormField.isVisible).toBeFalsy();
});
it('should return true when the visibility condition is not valid', () => {
visibilityObjTest.leftFormFieldId = '';
visibilityObjTest.leftRestResponseId = '';
visibilityObjTest.operator = '!=';
let isVisible = service.getVisiblityForField(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
};
jsonFieldFake.visibilityCondition = null;
let fakeFormField: FormFieldModel = new FormFieldModel(fakeFormWithField, jsonFieldFake);
fakeFormField.isVisible = false;
service.refreshVisibilityForField(fakeFormField);
expect(fakeFormField.isVisible).toBeFalsy();
});
it('should be able to retrieve the value of a form variable', () => {
let varValue = service.getValueFromVariable(fakeForm, 'FORM_VARIABLE_TEST', null);
expect(varValue).not.toBeUndefined();
expect(varValue).toBe('form_value_test');
});
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.getDropDownValueForLabel(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.getValueFromFormValues(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.getValueFromFormValues(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.getValueOField(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.getValueOField(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 <label>', () => {
visibilityObjTest.leftFormFieldId = 'test_5';
visibilityObjTest.operator = '==';
visibilityObjTest.rightFormFieldId = 'dropdown_LABEL';
let fakeFormField: FormFieldModel = new FormFieldModel(formTest, jsonFieldFake);
service.refreshVisibilityForField(fakeFormField);
expect(fakeFormField.isVisible).toBeTruthy();
});
it('should be able to evaluate condition with a dropdown <id>', () => {
visibilityObjTest.leftFormFieldId = 'test_4';
visibilityObjTest.operator = '==';
visibilityObjTest.rightFormFieldId = 'dropdown';
let fakeFormField: FormFieldModel = new FormFieldModel(formTest, jsonFieldFake);
service.refreshVisibilityForField(fakeFormField);
expect(fakeFormField.isVisible).toBeTruthy();
});
it('should be able to get value from form values', () => {
let res = service.getFieldValue(formTest.values, 'test_1');
expect(res).not.toBeNull();
expect(res).toBeDefined();
expect(res).toBe('value_1');
});
});
});
});

View File

@ -15,13 +15,13 @@
* limitations under the License.
*/
import { Injectable } from '@angular/core';
import { Response, Http, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { AlfrescoSettingsService } from 'ng2-alfresco-core';
import { FormModel, FormFieldModel, TabModel } from '../components/widgets/core/index';
import { WidgetVisibilityModel } from '../models/widget-visibility.model';
import { TaskProcessVariableModel } from '../models/task-process-variable.model';
import {Injectable} from '@angular/core';
import {Response, Http, Headers, RequestOptions} from '@angular/http';
import {Observable} from 'rxjs/Rx';
import {AlfrescoSettingsService} from 'ng2-alfresco-core';
import {FormModel, FormFieldModel, TabModel} from '../components/widgets/core/index';
import {WidgetVisibilityModel} from '../models/widget-visibility.model';
import {TaskProcessVariableModel} from '../models/task-process-variable.model';
@Injectable()
export class WidgetVisibilityService {
@ -33,10 +33,10 @@ export class WidgetVisibilityService {
}
public updateVisibilityForForm(form: FormModel) {
if ( form && form.tabs && form.tabs.length > 0) {
form.tabs.map( tabModel => this.refreshVisibilityForTab(tabModel) );
if (form && form.tabs && form.tabs.length > 0) {
form.tabs.map(tabModel => this.refreshVisibilityForTab(tabModel));
}
if ( form && form.fields.length > 0 ) {
if (form && form.fields.length > 0) {
form.fields
.map(
contModel =>
@ -46,29 +46,29 @@ export class WidgetVisibilityService {
contColModel
.fields.map(
field =>
this.refreshVisibilityForField(field) )
this.refreshVisibilityForField(field))
)
);
}
}
public refreshVisibilityForField(field: FormFieldModel) {
if ( field.visibilityCondition ) {
refreshVisibilityForField(field: FormFieldModel) {
if (field.visibilityCondition) {
field.isVisible = this.getVisiblityForField(field.form, field.visibilityCondition);
}
}
public refreshVisibilityForTab(tab: TabModel) {
if ( tab.visibilityCondition ) {
refreshVisibilityForTab(tab: TabModel) {
if (tab.visibilityCondition) {
tab.isVisible = this.getVisiblityForField(tab.form, tab.visibilityCondition);
}
}
public getVisiblityForField(form: FormModel, visibilityObj: WidgetVisibilityModel): boolean {
getVisiblityForField(form: FormModel, visibilityObj: WidgetVisibilityModel): boolean {
let isLeftFieldPresent = visibilityObj.leftFormFieldId || visibilityObj.leftRestResponseId;
if ( !isLeftFieldPresent || isLeftFieldPresent === 'null' ) {
if (!isLeftFieldPresent || isLeftFieldPresent === 'null') {
return true;
}else {
} else {
return this.evaluateVisibilityForField(form, visibilityObj);
}
}
@ -77,19 +77,19 @@ export class WidgetVisibilityService {
let leftValue = this.getLeftValue(form, visibilityObj);
let rightValue = this.getRightValue(form, visibilityObj);
let actualResult = this.evaluateCondition(leftValue, rightValue, visibilityObj.operator);
if ( visibilityObj.nextCondition ) {
if (visibilityObj.nextCondition) {
return this.evaluateLogicalOperation(visibilityObj.nextConditionOperator,
actualResult,
this.evaluateVisibilityForField(
form, visibilityObj.nextCondition)
);
}else {
} else {
return actualResult;
}
}
getLeftValue(form: FormModel, visibilityObj: WidgetVisibilityModel) {
if ( visibilityObj.leftRestResponseId ) {
if (visibilityObj.leftRestResponseId) {
return this.getValueFromVariable(form, visibilityObj.leftRestResponseId, this.processVarList);
}
return this.getValueOField(form, visibilityObj.leftFormFieldId);
@ -97,29 +97,46 @@ export class WidgetVisibilityService {
getRightValue(form: FormModel, visibilityObj: WidgetVisibilityModel) {
let valueFound = null;
if ( visibilityObj.rightRestResponseId ) {
if (visibilityObj.rightRestResponseId) {
valueFound = this.getValueFromVariable(form, visibilityObj.rightRestResponseId, this.processVarList);
}else if ( visibilityObj.rightFormFieldId ) {
} else if (visibilityObj.rightFormFieldId) {
valueFound = this.getValueOField(form, visibilityObj.rightFormFieldId);
}else {
} else {
valueFound = visibilityObj.rightValue;
}
return valueFound;
}
getValueOField(form: FormModel, field: string) {
let value = form.values[field] ?
form.values[field] :
this.getFormValueByName(form, field);
return value;
let value = this.getValueFromFormValues(form.values, field);
value = value && value.id ? value.id : value;
return value ? value : this.getFormValueByName(form, field);
}
getValueFromFormValues(values: any, fieldName: string) {
return this.getFieldValue(values, fieldName) ||
this.getDropDownValueForLabel(values, fieldName);
}
getFieldValue(valueList: any, fieldName: string) {
return fieldName ? valueList[fieldName] : fieldName;
}
getDropDownValueForLabel(valueList: any, fieldName: string) {
let dropDownFilterByName;
if (fieldName && fieldName.indexOf('_LABEL') > 0) {
dropDownFilterByName = fieldName.substring(0, fieldName.length - 6);
}
return ( dropDownFilterByName && valueList[dropDownFilterByName] ) ?
valueList[dropDownFilterByName].name : dropDownFilterByName;
}
getFormValueByName(form: FormModel, name: string) {
for (let columns of form.json.fields) {
for ( let i in columns.fields ) {
if ( columns.fields.hasOwnProperty( i ) ) {
for (let i in columns.fields) {
if (columns.fields.hasOwnProperty(i)) {
let res = columns.fields[i].find(field => field.id === name);
if ( res ) {
if (res) {
return res.value;
}
}
@ -127,31 +144,31 @@ export class WidgetVisibilityService {
}
}
getValueFromVariable( form: FormModel, name: string, processVarList: TaskProcessVariableModel[] ) {
getValueFromVariable(form: FormModel, name: string, processVarList: TaskProcessVariableModel[]) {
return this.getFormVariableValue(form, name) ||
this.getProcessVariableValue(name, processVarList);
}
private getFormVariableValue(form: FormModel, name: string ) {
if ( form.json.variables) {
private getFormVariableValue(form: FormModel, name: string) {
if (form.json.variables) {
let variableFromForm = form.json.variables.find(formVar => formVar.name === name);
if ( variableFromForm ) {
if (variableFromForm) {
return variableFromForm ? variableFromForm.value : variableFromForm;
}
}
}
private getProcessVariableValue(name: string, processVarList: TaskProcessVariableModel[]) {
if ( this.processVarList ) {
if (this.processVarList) {
let variableFromProcess = this.processVarList.find(variable => variable.id === name);
if ( variableFromProcess ) {
if (variableFromProcess) {
return variableFromProcess ? variableFromProcess.value : variableFromProcess;
}
}
}
evaluateLogicalOperation(logicOp, previousValue, newValue): boolean {
switch ( logicOp ) {
switch (logicOp) {
case 'and':
return previousValue && newValue;
case 'or' :
@ -161,13 +178,13 @@ export class WidgetVisibilityService {
case 'or-not':
return previousValue || !newValue;
default:
console.error( 'NO valid operation! wrong op request : ' + logicOp );
console.error('NO valid operation! wrong op request : ' + logicOp);
break;
}
}
evaluateCondition(leftValue, rightValue, operator): boolean {
switch ( operator ) {
switch (operator) {
case '==':
return String(leftValue) === String(rightValue);
case '<':
@ -185,7 +202,7 @@ export class WidgetVisibilityService {
case '!empty':
return leftValue ? leftValue !== '' : false;
default:
console.error( 'NO valid operation!' );
console.error('NO valid operation!');
break;
}
return;
@ -196,17 +213,10 @@ export class WidgetVisibilityService {
let options = this.getRequestOptions();
return this.http
.get(url, options)
.map( (response: Response) => this.processVarList = <TaskProcessVariableModel[]> response.json())
.map((response: Response) => this.processVarList = <TaskProcessVariableModel[]> response.json())
.catch(this.handleError);
}
getTaskProcessVariableModelForTaskByName(taskId: string, processVarName: string): Observable<TaskProcessVariableModel> {
return this.getTaskProcessVariableModelsForTask(taskId)
.map(
(variables: TaskProcessVariableModel[]) =>
variables.find(variable => variable.id === processVarName));
}
private getHeaders(): Headers {
return new Headers({
'Accept': 'application/json',