Improved test readability

This commit is contained in:
Vito Albano
2016-10-10 18:05:30 +01:00
parent 82632e5e89
commit 83b24308ce
3 changed files with 495 additions and 457 deletions

View File

@@ -0,0 +1,85 @@
/*!
* @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_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
}
]
}
}
]
};

View File

@@ -15,589 +15,542 @@
* limitations under the License. * limitations under the License.
*/ */
import { HttpModule } from '@angular/http';
import { TestBed } from '@angular/core/testing';
import { import {
async, inject, TestBed formTest,
} from '@angular/core/testing'; fakeTaskProcessVariableModels,
formValues,
fakeFormJson
} from './assets/widget-visibility.service.mock';
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, FormFieldModel } from '../components/widgets/core/index';
import { declare let jasmine: any;
MockBackend,
MockConnection
} from '@angular/http/testing';
import {
HttpModule,
Http,
XHRBackend,
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';
describe('WidgetVisibilityService (mockBackend)', () => { describe('WidgetVisibilityService (mockBackend)', () => {
let formTest = new FormModel({}); let service: WidgetVisibilityService;
let fakeTaskProcessVariableModels = [ let booleanResult: boolean;
{id: 'TEST_VAR_1', type: 'string', value: 'test_value_1'}, let stubFormWithFields = new FormModel(fakeFormJson);
{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',
'test_4': 'dropdown_id',
'test_5': 'dropdown_label',
'dropdown': {'id': 'dropdown_id', 'name': 'dropdown_label'}
};
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
}
]
}
}
]
};
beforeEach(async(() => { beforeAll(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [HttpModule], imports: [HttpModule],
providers: [ providers: [
WidgetVisibilityService,
AlfrescoSettingsService, AlfrescoSettingsService,
{provide: XHRBackend, useClass: MockBackend} WidgetVisibilityService
] ]
}).compileComponents(); });
})); service = TestBed.get(WidgetVisibilityService);
});
it('can instantiate service when inject service', beforeEach(() => {
inject([WidgetVisibilityService], (service: WidgetVisibilityService) => { jasmine.Ajax.install();
expect(service instanceof WidgetVisibilityService).toBe(true); });
}));
it('can instantiate service with "new"', inject([Http], (http: Http) => { afterEach(() => {
expect(http).not.toBeNull('http should be provided'); jasmine.Ajax.uninstall();
let service = new WidgetVisibilityService(http, null); });
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('should be able to evaluate logic operations', () => { describe('should be able to evaluate logic operations', () => {
let res: boolean;
let service: WidgetVisibilityService = new WidgetVisibilityService(null, null);
it('using AND and return true', () => { it('using AND and return true', () => {
res = service.evaluateLogicalOperation('and', true, true); booleanResult = service.evaluateLogicalOperation('and', true, true);
expect(res).toBeTruthy(); expect(booleanResult).toBeTruthy();
}); });
it('using AND and return false', () => { it('using AND and return false', () => {
res = service.evaluateLogicalOperation('and', true, false); booleanResult = service.evaluateLogicalOperation('and', true, false);
expect(res).toBeFalsy(); expect(booleanResult).toBeFalsy();
}); });
it('using OR and return true', () => { it('using OR and return true', () => {
res = service.evaluateLogicalOperation('or', true, false); booleanResult = service.evaluateLogicalOperation('or', true, false);
expect(res).toBeTruthy(); expect(booleanResult).toBeTruthy();
}); });
it('using OR and return false', () => { it('using OR and return false', () => {
res = service.evaluateLogicalOperation('or', false, false); booleanResult = service.evaluateLogicalOperation('or', false, false);
expect(res).toBeFalsy(); expect(booleanResult).toBeFalsy();
}); });
it('using AND NOT and return true', () => { it('using AND NOT and return true', () => {
res = service.evaluateLogicalOperation('and-not', true, false); booleanResult = service.evaluateLogicalOperation('and-not', true, false);
expect(res).toBeTruthy(); expect(booleanResult).toBeTruthy();
}); });
it('using AND NOT and return false', () => { it('using AND NOT and return false', () => {
res = service.evaluateLogicalOperation('and-not', false, false); booleanResult = service.evaluateLogicalOperation('and-not', false, false);
expect(res).toBeFalsy(); expect(booleanResult).toBeFalsy();
}); });
it('using OR NOT and return true', () => { it('using OR NOT and return true', () => {
res = service.evaluateLogicalOperation('or-not', true, true); booleanResult = service.evaluateLogicalOperation('or-not', true, true);
expect(res).toBeTruthy(); expect(booleanResult).toBeTruthy();
}); });
it('using OR NOT and return false', () => { it('using OR NOT and return false', () => {
res = service.evaluateLogicalOperation('or-not', false, true); booleanResult = service.evaluateLogicalOperation('or-not', false, true);
expect(res).toBeFalsy(); expect(booleanResult).toBeFalsy();
}); });
}); });
describe('should be able to evaluate next condition operations', () => { describe('should be able to evaluate next condition operations', () => {
let res: boolean;
let service: WidgetVisibilityService = new WidgetVisibilityService(null, null);
it('using == and return true', () => { it('using == and return true', () => {
res = service.evaluateCondition('test', 'test', '=='); booleanResult = service.evaluateCondition('test', 'test', '==');
expect(res).toBeTruthy(); expect(booleanResult).toBeTruthy();
}); });
it('using < and return true', () => { it('using < and return true', () => {
res = service.evaluateCondition(1, 2, '<'); booleanResult = service.evaluateCondition(1, 2, '<');
expect(res).toBeTruthy(); expect(booleanResult).toBeTruthy();
}); });
it('using != and return true', () => { it('using != and return true', () => {
res = service.evaluateCondition(true, false, '!='); booleanResult = service.evaluateCondition(true, false, '!=');
expect(res).toBeTruthy(); expect(booleanResult).toBeTruthy();
}); });
it('using != and return false', () => { it('using != and return false', () => {
res = service.evaluateCondition(true, true, '!='); booleanResult = service.evaluateCondition(true, true, '!=');
expect(res).toBeFalsy(); expect(booleanResult).toBeFalsy();
}); });
it('using >= and return true', () => { it('using >= and return true', () => {
res = service.evaluateCondition(2, 2, '>='); booleanResult = service.evaluateCondition(2, 2, '>=');
expect(res).toBeTruthy(); expect(booleanResult).toBeTruthy();
}); });
it('using empty with null values and return true', () => { it('using empty with null values and return true', () => {
res = service.evaluateCondition(null, null, 'empty'); booleanResult = service.evaluateCondition(null, null, 'empty');
expect(res).toBeTruthy(); expect(booleanResult).toBeTruthy();
}); });
it('using empty with empty strings values and return true', () => { it('using empty with empty strings values and return true', () => {
res = service.evaluateCondition('', '', 'empty'); booleanResult = service.evaluateCondition('', '', 'empty');
expect(res).toBeTruthy(); expect(booleanResult).toBeTruthy();
}); });
it('using > and return false', () => { it('using > and return false', () => {
res = service.evaluateCondition(2, 3, '>'); booleanResult = service.evaluateCondition(2, 3, '>');
expect(res).toBeFalsy(); expect(booleanResult).toBeFalsy();
}); });
it('using not empty with null values and return false', () => { it('using not empty with null values and return false', () => {
res = service.evaluateCondition(null, null, '!empty'); booleanResult = service.evaluateCondition(null, null, '!empty');
expect(res).toBeFalsy(); expect(booleanResult).toBeFalsy();
}); });
it('using OR NOT with empty strings and return false', () => { it('using OR NOT with empty strings and return false', () => {
res = service.evaluateCondition('', '', '!empty'); booleanResult = service.evaluateCondition('', '', '!empty');
expect(res).toBeFalsy(); expect(booleanResult).toBeFalsy();
}); });
}); });
describe('after backend is mocked', () => { describe('should retrive the process variables', () => {
let backend: MockBackend; let fakeFormWithField = new FormModel(fakeFormJson);
let service: WidgetVisibilityService; let visibilityObjTest: WidgetVisibilityModel;
let stubFormWithFields = new FormModel(fakeFormJson); let chainedVisibilityObj = new WidgetVisibilityModel();
beforeEach(inject([Http, XHRBackend, AlfrescoSettingsService], beforeEach(() => {
(http: Http, visibilityObjTest = new WidgetVisibilityModel();
be: MockBackend, });
setting: AlfrescoSettingsService) => {
backend = be;
service = new WidgetVisibilityService(http, setting);
}));
describe('should retrive the process variables', () => { it('should return the process variables for task', (done) => {
let fakeFormWithField = new FormModel(fakeFormJson); service.getTaskProcessVariableModelsForTask('9999').subscribe(
let visibilityObjTest: WidgetVisibilityModel; (res: TaskProcessVariableModel[]) => {
let chainedVisibilityObj = new WidgetVisibilityModel(); expect(res).toBeDefined();
expect(res.length).toEqual(3);
beforeEach(() => { expect(res[0].id).toEqual('TEST_VAR_1');
let options = new ResponseOptions({ expect(res[0].type).toEqual('string');
status: 200, expect(res[0].value).toEqual('test_value_1');
body: fakeTaskProcessVariableModels done();
}); }
let response = new Response(options); );
backend.connections.subscribe((c: MockConnection) => c.mockRespond(response)); jasmine.Ajax.requests.mostRecent().respondWith({
visibilityObjTest = new WidgetVisibilityModel(); status: 200,
}); contentType: 'json',
responseText: fakeTaskProcessVariableModels
it('should return the process variables for task', (done) => {
service.getTaskProcessVariableModelsForTask('9999').subscribe(
(res: TaskProcessVariableModel[]) => {
expect(res).toBeDefined();
expect(res.length).toEqual(3);
expect(res[0].id).toEqual('TEST_VAR_1');
expect(res[0].type).toEqual('string');
expect(res[0].value).toEqual('test_value_1');
done();
}
);
});
it('should be able to retrieve the value of a process variable', (done) => {
service.getTaskProcessVariableModelsForTask('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();
}
);
});
it('should return undefined if the variable does not exist', (done) => {
service.getTaskProcessVariableModelsForTask('9999').subscribe(
(res: TaskProcessVariableModel[]) => {
let varValue = service.getValueFromVariable(formTest, 'TEST_MYSTERY_VAR', res);
expect(varValue).toBeUndefined();
done();
}
);
});
it('should retrieve the value for the right field when it is a process variable', (done) => {
service.getTaskProcessVariableModelsForTask('9999').subscribe(
(res: TaskProcessVariableModel[]) => {
visibilityObjTest.rightRestResponseId = 'TEST_VAR_2';
let rightValue = service.getRightValue(formTest, visibilityObjTest);
expect(rightValue).not.toBeNull();
expect(rightValue).toBe('test_value_2');
done();
}
);
});
it('should retrieve the value for the left field when it is a process variable', (done) => {
service.getTaskProcessVariableModelsForTask('9999').subscribe(
(res: TaskProcessVariableModel[]) => {
visibilityObjTest.leftRestResponseId = 'TEST_VAR_2';
let rightValue = service.getLeftValue(formTest, visibilityObjTest);
expect(rightValue).not.toBeNull();
expect(rightValue).toBe('test_value_2');
done();
}
);
});
it('should evaluate the visibility for the field between form value and process var', (done) => {
service.getTaskProcessVariableModelsForTask('9999').subscribe(
(res: TaskProcessVariableModel[]) => {
visibilityObjTest.leftFormFieldId = 'LEFT_FORM_FIELD_ID';
visibilityObjTest.operator = '!=';
visibilityObjTest.rightRestResponseId = 'TEST_VAR_2';
let isVisible = service.evaluateVisibilityForField(fakeFormWithField, visibilityObjTest);
expect(isVisible).toBeTruthy();
done();
}
);
});
it('should evaluate visibility with multiple conditions', (done) => {
service.getTaskProcessVariableModelsForTask('9999').subscribe(
(res: TaskProcessVariableModel[]) => {
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);
expect(isVisible).toBeTruthy();
done();
}
);
}); });
}); });
describe('should return the value of the field', () => { it('should be able to retrieve the value of a process variable', (done) => {
let visibilityObjTest: WidgetVisibilityModel; service.getTaskProcessVariableModelsForTask('9999').subscribe(
let fakeFormWithField = new FormModel(fakeFormJson); (res: TaskProcessVariableModel[]) => {
let jsonFieldFake = { expect(res).toBeDefined();
id: 'FAKE_FORM_FIELD_ID', let varValue = service.getValueFromVariable(formTest, 'TEST_VAR_1', res);
value: 'FAKE_FORM_FIELD_VALUE', expect(varValue).not.toBeUndefined();
visibilityCondition: undefined expect(varValue).toBe('test_value_1');
}; done();
let fakeForm = new FormModel({ }
variables: [ );
{ jasmine.Ajax.requests.mostRecent().respondWith({
name: 'FORM_VARIABLE_TEST', status: 200,
type: 'string', contentType: 'json',
value: 'form_value_test' responseText: fakeTaskProcessVariableModels
}]
}); });
});
beforeEach(() => { it('should return undefined if the variable does not exist', (done) => {
visibilityObjTest = new WidgetVisibilityModel(); service.getTaskProcessVariableModelsForTask('9999').subscribe(
formTest.values = formValues; (res: TaskProcessVariableModel[]) => {
jsonFieldFake.visibilityCondition = visibilityObjTest; let varValue = service.getValueFromVariable(formTest, 'TEST_MYSTERY_VAR', res);
expect(varValue).toBeUndefined();
done();
}
);
jasmine.Ajax.requests.mostRecent().respondWith({
status: 200,
contentType: 'json',
responseText: fakeTaskProcessVariableModels
}); });
});
it('should be able to retrieve a field value searching in the form', () => { it('should retrieve the value for the right field when it is a process variable', (done) => {
let formValue = service.getFormValueByName(stubFormWithFields, 'FIELD_WITH_CONDITION'); service.getTaskProcessVariableModelsForTask('9999').subscribe(
(res: TaskProcessVariableModel[]) => {
visibilityObjTest.rightRestResponseId = 'TEST_VAR_2';
let rightValue = service.getRightValue(formTest, visibilityObjTest);
expect(formValue).not.toBeNull(); expect(rightValue).not.toBeNull();
expect(formValue).toBe('field_with_condition_value'); expect(rightValue).toBe('test_value_2');
done();
}
);
jasmine.Ajax.requests.mostRecent().respondWith({
status: 200,
contentType: 'json',
responseText: fakeTaskProcessVariableModels
}); });
});
it('should return undefined if the field value is not in the form', () => { it('should retrieve the value for the left field when it is a process variable', (done) => {
let formValue = service.getFormValueByName(stubFormWithFields, 'FIELD_MYSTERY'); service.getTaskProcessVariableModelsForTask('9999').subscribe(
(res: TaskProcessVariableModel[]) => {
visibilityObjTest.leftRestResponseId = 'TEST_VAR_2';
let rightValue = service.getLeftValue(formTest, visibilityObjTest);
expect(formValue).toBeUndefined(); expect(rightValue).not.toBeNull();
expect(rightValue).toBe('test_value_2');
done();
}
);
jasmine.Ajax.requests.mostRecent().respondWith({
status: 200,
contentType: 'json',
responseText: fakeTaskProcessVariableModels
}); });
});
it('should search in the form if element value is not in form values', () => { it('should evaluate the visibility for the field between form value and process var', (done) => {
let value = service.getValueOField(fakeFormWithField, 'FIELD_WITH_CONDITION'); service.getTaskProcessVariableModelsForTask('9999').subscribe(
(res: TaskProcessVariableModel[]) => {
visibilityObjTest.leftFormFieldId = 'LEFT_FORM_FIELD_ID';
visibilityObjTest.operator = '!=';
visibilityObjTest.rightRestResponseId = 'TEST_VAR_2';
let isVisible = service.evaluateVisibilityForField(fakeFormWithField, visibilityObjTest);
expect(value).not.toBeNull(); expect(isVisible).toBeTruthy();
expect(value).toBe('field_with_condition_value'); done();
}
);
jasmine.Ajax.requests.mostRecent().respondWith({
status: 200,
contentType: 'json',
responseText: fakeTaskProcessVariableModels
}); });
});
it('should return undefined if the element is not present anywhere', () => { it('should evaluate visibility with multiple conditions', (done) => {
let formValue = service.getValueOField(fakeFormWithField, 'FIELD_MYSTERY'); service.getTaskProcessVariableModelsForTask('9999').subscribe(
(res: TaskProcessVariableModel[]) => {
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;
expect(formValue).toBeUndefined(); let isVisible = service.evaluateVisibilityForField(fakeFormWithField, visibilityObjTest);
expect(isVisible).toBeTruthy();
done();
}
);
jasmine.Ajax.requests.mostRecent().respondWith({
status: 200,
contentType: 'json',
responseText: fakeTaskProcessVariableModels
}); });
});
});
it('should retrieve the value for the right field when it is a value', () => { describe('should return the value of the field', () => {
visibilityObjTest.rightValue = '100'; let visibilityObjTest: WidgetVisibilityModel;
let rightValue = service.getRightValue(formTest, visibilityObjTest); 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'
}]
});
expect(rightValue).toBe('100'); beforeEach(() => {
}); visibilityObjTest = new WidgetVisibilityModel();
formTest.values = formValues;
jsonFieldFake.visibilityCondition = visibilityObjTest;
});
it('should retrieve the value for the right field when it is a form variable', () => { it('should be able to retrieve a field value searching in the form', () => {
visibilityObjTest.rightFormFieldId = 'RIGHT_FORM_FIELD_ID'; let formValue = service.getFormValueByName(stubFormWithFields, 'FIELD_WITH_CONDITION');
let rightValue = service.getRightValue(fakeFormWithField, visibilityObjTest);
expect(rightValue).not.toBeNull(); expect(formValue).not.toBeNull();
expect(rightValue).toBe('RIGHT_FORM_FIELD_VALUE'); expect(formValue).toBe('field_with_condition_value');
}); });
it('should take the value from form values if it is present', () => { it('should return undefined if the field value is not in the form', () => {
let formValue = service.getValueOField(formTest, 'test_1'); let formValue = service.getFormValueByName(stubFormWithFields, 'FIELD_MYSTERY');
expect(formValue).not.toBeNull(); expect(formValue).toBeUndefined();
expect(formValue).toBe('value_1'); });
});
it('should retrieve right value from form values if it is present', () => { it('should search in the form if element value is not in form values', () => {
visibilityObjTest.rightFormFieldId = 'test_2'; let value = service.getFieldValueFromForm(fakeFormWithField, 'FIELD_WITH_CONDITION');
let rightValue = service.getRightValue(formTest, visibilityObjTest);
expect(rightValue).not.toBeNull(); expect(value).not.toBeNull();
expect(formTest.values).toEqual(formValues); expect(value).toBe('field_with_condition_value');
expect(rightValue).toBe('value_2'); });
});
it('should retrieve the value for the left field when it is a form value', () => { it('should return undefined if the element is not present anywhere', () => {
visibilityObjTest.leftFormFieldId = 'FIELD_WITH_CONDITION'; let formValue = service.getFieldValueFromForm(fakeFormWithField, 'FIELD_MYSTERY');
let leftValue = service.getLeftValue(fakeFormWithField, visibilityObjTest);
expect(leftValue).not.toBeNull(); expect(formValue).toBeUndefined();
expect(leftValue).toBe('field_with_condition_value'); });
});
it('should retrieve left value from form values if it is present', () => { it('should retrieve the value for the right field when it is a value', () => {
visibilityObjTest.leftFormFieldId = 'test_2'; visibilityObjTest.rightValue = '100';
let leftValue = service.getLeftValue(formTest, visibilityObjTest); let rightValue = service.getRightValue(formTest, visibilityObjTest);
expect(leftValue).not.toBeNull(); expect(rightValue).toBe('100');
expect(leftValue).toBe('value_2'); });
});
it('should return undefined for a value that is not on variable or form', () => { it('should retrieve the value for the right field when it is a form variable', () => {
let leftValue = service.getLeftValue(fakeFormWithField, visibilityObjTest); visibilityObjTest.rightFormFieldId = 'RIGHT_FORM_FIELD_ID';
let rightValue = service.getRightValue(fakeFormWithField, visibilityObjTest);
expect(leftValue).toBeUndefined(); expect(rightValue).not.toBeNull();
}); expect(rightValue).toBe('RIGHT_FORM_FIELD_VALUE');
});
it('should evaluate the visibility for the field with single visibility condition between two field values', () => { it('should take the value from form values if it is present', () => {
visibilityObjTest.leftFormFieldId = 'test_1'; let formValue = service.getFieldValueFromForm(formTest, 'test_1');
visibilityObjTest.operator = '==';
visibilityObjTest.rightFormFieldId = 'test_3';
let isVisible = service.evaluateVisibilityForField(formTest, visibilityObjTest);
expect(isVisible).toBeTruthy(); expect(formValue).not.toBeNull();
}); expect(formValue).toBe('value_1');
});
it('should evaluate true visibility for the field with single visibility condition between a field and a value', () => { it('should retrieve right value from form values if it is present', () => {
visibilityObjTest.leftFormFieldId = 'test_1'; visibilityObjTest.rightFormFieldId = 'test_2';
visibilityObjTest.operator = '=='; let rightValue = service.getRightValue(formTest, visibilityObjTest);
visibilityObjTest.rightValue = 'value_1';
let isVisible = service.evaluateVisibilityForField(formTest, visibilityObjTest);
expect(isVisible).toBeTruthy(); 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', () => { it('should retrieve the value for the left field when it is a form value', () => {
visibilityObjTest.rightFormFieldId = 'NO_FIELD_FORM'; visibilityObjTest.leftFormFieldId = 'FIELD_WITH_CONDITION';
let rightValue = service.getRightValue(fakeFormWithField, visibilityObjTest); let leftValue = service.getLeftValue(fakeFormWithField, visibilityObjTest);
expect(rightValue).toBeUndefined(); expect(leftValue).not.toBeNull();
}); expect(leftValue).toBe('field_with_condition_value');
});
it('should evaluate the visibility for the field with single visibility condition between form values', () => { it('should retrieve left value from form values if it is present', () => {
visibilityObjTest.leftFormFieldId = 'LEFT_FORM_FIELD_ID'; visibilityObjTest.leftFormFieldId = 'test_2';
visibilityObjTest.operator = '!='; let leftValue = service.getLeftValue(formTest, visibilityObjTest);
visibilityObjTest.rightFormFieldId = 'RIGHT_FORM_FIELD_ID';
let isVisible = service.evaluateVisibilityForField(fakeFormWithField, visibilityObjTest);
expect(isVisible).toBeTruthy(); expect(leftValue).not.toBeNull();
}); expect(leftValue).toBe('value_2');
});
it('should refresh the visibility for a form field object', () => { it('should return undefined for a value that is not on variable or form', () => {
visibilityObjTest.leftFormFieldId = 'test_1'; let leftValue = service.getLeftValue(fakeFormWithField, visibilityObjTest);
visibilityObjTest.operator = '!=';
visibilityObjTest.rightFormFieldId = 'test_3';
let fakeFormField: FormFieldModel = new FormFieldModel(formTest, jsonFieldFake);
service.refreshVisibilityForField(fakeFormField);
expect(fakeFormField.isVisible).toBeFalsy(); expect(leftValue).toBeUndefined();
}); });
it('should return true when the visibility condition is not valid', () => { it('should evaluate the visibility for the field with single visibility condition between two field values', () => {
visibilityObjTest.leftFormFieldId = ''; visibilityObjTest.leftFormFieldId = 'test_1';
visibilityObjTest.leftRestResponseId = ''; visibilityObjTest.operator = '==';
visibilityObjTest.operator = '!='; visibilityObjTest.rightFormFieldId = 'test_3';
let isVisible = service.getVisiblityForField(formTest, visibilityObjTest); let isVisible = service.evaluateVisibilityForField(formTest, visibilityObjTest);
expect(isVisible).toBeTruthy(); expect(isVisible).toBeTruthy();
}); });
it('should not change the isVisible if field does not have visibility condition', () => { it('should evaluate true visibility for the field with single visibility condition between a field and a value', () => {
jsonFieldFake.visibilityCondition = null; visibilityObjTest.leftFormFieldId = 'test_1';
let fakeFormField: FormFieldModel = new FormFieldModel(fakeFormWithField, jsonFieldFake); visibilityObjTest.operator = '==';
fakeFormField.isVisible = false; visibilityObjTest.rightValue = 'value_1';
service.refreshVisibilityForField(fakeFormField); let isVisible = service.evaluateVisibilityForField(formTest, visibilityObjTest);
expect(fakeFormField.isVisible).toBeFalsy(); expect(isVisible).toBeTruthy();
}); });
it('should be able to retrieve the value of a form variable', () => { it('should return undefined for a value that is not on variable or form', () => {
let varValue = service.getValueFromVariable(fakeForm, 'FORM_VARIABLE_TEST', null); visibilityObjTest.rightFormFieldId = 'NO_FIELD_FORM';
let rightValue = service.getRightValue(fakeFormWithField, visibilityObjTest);
expect(varValue).not.toBeUndefined(); expect(rightValue).toBeUndefined();
expect(varValue).toBe('form_value_test'); });
});
it('should retrieve the value for the left field when it is a form variable', () => { it('should evaluate the visibility for the field with single visibility condition between form values', () => {
visibilityObjTest.leftRestResponseId = 'FORM_VARIABLE_TEST'; visibilityObjTest.leftFormFieldId = 'LEFT_FORM_FIELD_ID';
let leftValue = service.getLeftValue(fakeForm, visibilityObjTest); visibilityObjTest.operator = '!=';
visibilityObjTest.rightFormFieldId = 'RIGHT_FORM_FIELD_ID';
let isVisible = service.evaluateVisibilityForField(fakeFormWithField, visibilityObjTest);
expect(leftValue).not.toBeNull(); expect(isVisible).toBeTruthy();
expect(leftValue).toBe('form_value_test'); });
});
it('should determine visibility for dropdown on label condition', () => { it('should refresh the visibility for a form field object', () => {
let dropdownValue = service.getDropDownValueForLabel(formTest.values, 'dropdown_LABEL'); visibilityObjTest.leftFormFieldId = 'test_1';
visibilityObjTest.operator = '!=';
visibilityObjTest.rightFormFieldId = 'test_3';
let fakeFormField: FormFieldModel = new FormFieldModel(formTest, jsonFieldFake);
service.refreshVisibilityForField(fakeFormField);
expect(dropdownValue).not.toBeNull(); expect(fakeFormField.isVisible).toBeFalsy();
expect(dropdownValue).toBeDefined(); });
expect(dropdownValue).toBe('dropdown_label');
});
it('should be able to get the value for a dropdown filtered with Label', () => { it('should return true when the visibility condition is not valid', () => {
let dropdownValue = service.getValueFromFormValues(formTest.values, 'dropdown_LABEL'); visibilityObjTest.leftFormFieldId = '';
visibilityObjTest.leftRestResponseId = '';
visibilityObjTest.operator = '!=';
let isVisible = service.getVisiblityForField(formTest, visibilityObjTest);
expect(dropdownValue).not.toBeNull(); expect(isVisible).toBeTruthy();
expect(dropdownValue).toBeDefined(); });
expect(dropdownValue).toBe('dropdown_label');
});
it('should be able to get the value for a standard field', () => { it('should not change the isVisible if field does not have visibility condition', () => {
let dropdownValue = service.getValueFromFormValues(formTest.values, 'test_2'); jsonFieldFake.visibilityCondition = null;
let fakeFormField: FormFieldModel = new FormFieldModel(fakeFormWithField, jsonFieldFake);
fakeFormField.isVisible = false;
service.refreshVisibilityForField(fakeFormField);
expect(dropdownValue).not.toBeNull(); expect(fakeFormField.isVisible).toBeFalsy();
expect(dropdownValue).toBeDefined(); });
expect(dropdownValue).toBe('value_2');
});
it('should get the dropdown label value from a form', () => { it('should be able to retrieve the value of a form variable', () => {
let dropdownValue = service.getValueOField(formTest, 'dropdown_LABEL'); let varValue = service.getValueFromVariable(fakeForm, 'FORM_VARIABLE_TEST', null);
expect(dropdownValue).not.toBeNull(); expect(varValue).not.toBeUndefined();
expect(dropdownValue).toBeDefined(); expect(varValue).toBe('form_value_test');
expect(dropdownValue).toBe('dropdown_label'); });
});
it('should get the dropdown id value from a form', () => { it('should retrieve the value for the left field when it is a form variable', () => {
let dropdownValue = service.getValueOField(formTest, 'dropdown'); visibilityObjTest.leftRestResponseId = 'FORM_VARIABLE_TEST';
let leftValue = service.getLeftValue(fakeForm, visibilityObjTest);
expect(dropdownValue).not.toBeNull(); expect(leftValue).not.toBeNull();
expect(dropdownValue).toBeDefined(); expect(leftValue).toBe('form_value_test');
expect(dropdownValue).toBe('dropdown_id'); });
});
it('should retrieve the value for the right field when it is a dropdown id', () => { it('should determine visibility for dropdown on label condition', () => {
visibilityObjTest.rightFormFieldId = 'dropdown'; let dropdownValue = service.getDropDownValueForLabel(formTest.values, 'dropdown_LABEL');
let rightValue = service.getRightValue(formTest, visibilityObjTest);
expect(rightValue).toBeDefined(); expect(dropdownValue).not.toBeNull();
expect(rightValue).toBe('dropdown_id'); expect(dropdownValue).toBeDefined();
}); expect(dropdownValue).toBe('dropdown_label');
});
it('should retrieve the value for the right field when it is a dropdown label', () => { it('should be able to get the value for a dropdown filtered with Label', () => {
visibilityObjTest.rightFormFieldId = 'dropdown_LABEL'; let dropdownValue = service.getValueFromFormValues(formTest.values, 'dropdown_LABEL');
let rightValue = service.getRightValue(formTest, visibilityObjTest);
expect(rightValue).toBeDefined(); expect(dropdownValue).not.toBeNull();
expect(rightValue).toBe('dropdown_label'); expect(dropdownValue).toBeDefined();
}); expect(dropdownValue).toBe('dropdown_label');
});
it('should be able to evaluate condition with a dropdown <label>', () => { it('should be able to get the value for a standard field', () => {
visibilityObjTest.leftFormFieldId = 'test_5'; let dropdownValue = service.getValueFromFormValues(formTest.values, 'test_2');
visibilityObjTest.operator = '==';
visibilityObjTest.rightFormFieldId = 'dropdown_LABEL';
let fakeFormField: FormFieldModel = new FormFieldModel(formTest, jsonFieldFake);
service.refreshVisibilityForField(fakeFormField);
expect(fakeFormField.isVisible).toBeTruthy(); expect(dropdownValue).not.toBeNull();
}); expect(dropdownValue).toBeDefined();
expect(dropdownValue).toBe('value_2');
});
it('should be able to evaluate condition with a dropdown <id>', () => { it('should get the dropdown label value from a form', () => {
visibilityObjTest.leftFormFieldId = 'test_4'; let dropdownValue = service.getFieldValueFromForm(formTest, 'dropdown_LABEL');
visibilityObjTest.operator = '==';
visibilityObjTest.rightFormFieldId = 'dropdown';
let fakeFormField: FormFieldModel = new FormFieldModel(formTest, jsonFieldFake);
service.refreshVisibilityForField(fakeFormField);
expect(fakeFormField.isVisible).toBeTruthy(); expect(dropdownValue).not.toBeNull();
}); expect(dropdownValue).toBeDefined();
expect(dropdownValue).toBe('dropdown_label');
});
it('should be able to get value from form values', () => { it('should get the dropdown id value from a form', () => {
let res = service.getFieldValue(formTest.values, 'test_1'); let dropdownValue = service.getFieldValueFromForm(formTest, 'dropdown');
expect(res).not.toBeNull(); expect(dropdownValue).not.toBeNull();
expect(res).toBeDefined(); expect(dropdownValue).toBeDefined();
expect(res).toBe('value_1'); 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.getFieldValueFromForm(formTest, 'test_1');
expect(res).not.toBeNull();
expect(res).toBeDefined();
expect(res).toBe('value_1');
}); });
}); });
}); });

View File

@@ -92,7 +92,7 @@ export class WidgetVisibilityService {
if (visibilityObj.leftRestResponseId) { if (visibilityObj.leftRestResponseId) {
return this.getValueFromVariable(form, visibilityObj.leftRestResponseId, this.processVarList); return this.getValueFromVariable(form, visibilityObj.leftRestResponseId, this.processVarList);
} }
return this.getValueOField(form, visibilityObj.leftFormFieldId); return this.getFieldValueFromForm(form, visibilityObj.leftFormFieldId);
} }
getRightValue(form: FormModel, visibilityObj: WidgetVisibilityModel) { getRightValue(form: FormModel, visibilityObj: WidgetVisibilityModel) {
@@ -100,14 +100,14 @@ export class WidgetVisibilityService {
if (visibilityObj.rightRestResponseId) { if (visibilityObj.rightRestResponseId) {
valueFound = this.getValueFromVariable(form, visibilityObj.rightRestResponseId, this.processVarList); valueFound = this.getValueFromVariable(form, visibilityObj.rightRestResponseId, this.processVarList);
} else if (visibilityObj.rightFormFieldId) { } else if (visibilityObj.rightFormFieldId) {
valueFound = this.getValueOField(form, visibilityObj.rightFormFieldId); valueFound = this.getFieldValueFromForm(form, visibilityObj.rightFormFieldId);
} else { } else {
valueFound = visibilityObj.rightValue; valueFound = visibilityObj.rightValue;
} }
return valueFound; return valueFound;
} }
getValueOField(form: FormModel, field: string) { getFieldValueFromForm(form: FormModel, field: string) {
let value = this.getValueFromFormValues(form.values, field); let value = this.getValueFromFormValues(form.values, field);
value = value && value.id ? value.id : value; value = value && value.id ? value.id : value;
return value ? value : this.getFormValueByName(form, field); return value ? value : this.getFormValueByName(form, field);