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,229 +15,148 @@
* 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 { 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;
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',
'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
}
]
}
}
]
};
let service: WidgetVisibilityService;
let booleanResult: boolean;
let stubFormWithFields = new FormModel(fakeFormJson);
beforeEach(async(() => {
beforeAll(() => {
TestBed.configureTestingModule({
imports: [HttpModule],
providers: [
WidgetVisibilityService,
AlfrescoSettingsService,
{provide: XHRBackend, useClass: MockBackend}
WidgetVisibilityService
]
}).compileComponents();
}));
});
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');
}));
it('can provide the mockBackend as XHRBackend',
inject([XHRBackend], (backend: MockBackend) => {
expect(backend).not.toBeNull('backend should be provided');
}));
afterEach(() => {
jasmine.Ajax.uninstall();
});
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();
booleanResult = service.evaluateLogicalOperation('and', true, true);
expect(booleanResult).toBeTruthy();
});
it('using AND and return false', () => {
res = service.evaluateLogicalOperation('and', true, false);
expect(res).toBeFalsy();
booleanResult = service.evaluateLogicalOperation('and', true, false);
expect(booleanResult).toBeFalsy();
});
it('using OR and return true', () => {
res = service.evaluateLogicalOperation('or', true, false);
expect(res).toBeTruthy();
booleanResult = service.evaluateLogicalOperation('or', true, false);
expect(booleanResult).toBeTruthy();
});
it('using OR and return false', () => {
res = service.evaluateLogicalOperation('or', false, false);
expect(res).toBeFalsy();
booleanResult = service.evaluateLogicalOperation('or', false, false);
expect(booleanResult).toBeFalsy();
});
it('using AND NOT and return true', () => {
res = service.evaluateLogicalOperation('and-not', true, false);
expect(res).toBeTruthy();
booleanResult = service.evaluateLogicalOperation('and-not', true, false);
expect(booleanResult).toBeTruthy();
});
it('using AND NOT and return false', () => {
res = service.evaluateLogicalOperation('and-not', false, false);
expect(res).toBeFalsy();
booleanResult = service.evaluateLogicalOperation('and-not', false, false);
expect(booleanResult).toBeFalsy();
});
it('using OR NOT and return true', () => {
res = service.evaluateLogicalOperation('or-not', true, true);
expect(res).toBeTruthy();
booleanResult = service.evaluateLogicalOperation('or-not', true, true);
expect(booleanResult).toBeTruthy();
});
it('using OR NOT and return false', () => {
res = service.evaluateLogicalOperation('or-not', false, true);
expect(res).toBeFalsy();
booleanResult = service.evaluateLogicalOperation('or-not', false, true);
expect(booleanResult).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();
booleanResult = service.evaluateCondition('test', 'test', '==');
expect(booleanResult).toBeTruthy();
});
it('using < and return true', () => {
res = service.evaluateCondition(1, 2, '<');
expect(res).toBeTruthy();
booleanResult = service.evaluateCondition(1, 2, '<');
expect(booleanResult).toBeTruthy();
});
it('using != and return true', () => {
res = service.evaluateCondition(true, false, '!=');
expect(res).toBeTruthy();
booleanResult = service.evaluateCondition(true, false, '!=');
expect(booleanResult).toBeTruthy();
});
it('using != and return false', () => {
res = service.evaluateCondition(true, true, '!=');
expect(res).toBeFalsy();
booleanResult = service.evaluateCondition(true, true, '!=');
expect(booleanResult).toBeFalsy();
});
it('using >= and return true', () => {
res = service.evaluateCondition(2, 2, '>=');
expect(res).toBeTruthy();
booleanResult = service.evaluateCondition(2, 2, '>=');
expect(booleanResult).toBeTruthy();
});
it('using empty with null values and return true', () => {
res = service.evaluateCondition(null, null, 'empty');
expect(res).toBeTruthy();
booleanResult = service.evaluateCondition(null, null, 'empty');
expect(booleanResult).toBeTruthy();
});
it('using empty with empty strings values and return true', () => {
res = service.evaluateCondition('', '', 'empty');
expect(res).toBeTruthy();
booleanResult = service.evaluateCondition('', '', 'empty');
expect(booleanResult).toBeTruthy();
});
it('using > and return false', () => {
res = service.evaluateCondition(2, 3, '>');
expect(res).toBeFalsy();
booleanResult = service.evaluateCondition(2, 3, '>');
expect(booleanResult).toBeFalsy();
});
it('using not empty with null values and return false', () => {
res = service.evaluateCondition(null, null, '!empty');
expect(res).toBeFalsy();
booleanResult = service.evaluateCondition(null, null, '!empty');
expect(booleanResult).toBeFalsy();
});
it('using OR NOT with empty strings and return false', () => {
res = service.evaluateCondition('', '', '!empty');
expect(res).toBeFalsy();
booleanResult = service.evaluateCondition('', '', '!empty');
expect(booleanResult).toBeFalsy();
});
});
describe('after backend is mocked', () => {
let backend: MockBackend;
let service: WidgetVisibilityService;
let stubFormWithFields = new FormModel(fakeFormJson);
beforeEach(inject([Http, XHRBackend, AlfrescoSettingsService],
(http: Http,
be: MockBackend,
setting: AlfrescoSettingsService) => {
backend = be;
service = new WidgetVisibilityService(http, setting);
}));
describe('should retrive the process variables', () => {
let fakeFormWithField = new FormModel(fakeFormJson);
let visibilityObjTest: WidgetVisibilityModel;
let chainedVisibilityObj = new WidgetVisibilityModel();
beforeEach(() => {
let options = new ResponseOptions({
status: 200,
body: fakeTaskProcessVariableModels
});
let response = new Response(options);
backend.connections.subscribe((c: MockConnection) => c.mockRespond(response));
visibilityObjTest = new WidgetVisibilityModel();
});
@@ -252,6 +171,11 @@ 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) => {
@@ -264,6 +188,11 @@ describe('WidgetVisibilityService (mockBackend)', () => {
done();
}
);
jasmine.Ajax.requests.mostRecent().respondWith({
status: 200,
contentType: 'json',
responseText: fakeTaskProcessVariableModels
});
});
it('should return undefined if the variable does not exist', (done) => {
@@ -274,6 +203,11 @@ describe('WidgetVisibilityService (mockBackend)', () => {
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 process variable', (done) => {
@@ -287,6 +221,11 @@ describe('WidgetVisibilityService (mockBackend)', () => {
done();
}
);
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) => {
@@ -300,6 +239,11 @@ describe('WidgetVisibilityService (mockBackend)', () => {
done();
}
);
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) => {
@@ -314,6 +258,11 @@ describe('WidgetVisibilityService (mockBackend)', () => {
done();
}
);
jasmine.Ajax.requests.mostRecent().respondWith({
status: 200,
contentType: 'json',
responseText: fakeTaskProcessVariableModels
});
});
it('should evaluate visibility with multiple conditions', (done) => {
@@ -333,6 +282,11 @@ describe('WidgetVisibilityService (mockBackend)', () => {
done();
}
);
jasmine.Ajax.requests.mostRecent().respondWith({
status: 200,
contentType: 'json',
responseText: fakeTaskProcessVariableModels
});
});
});
@@ -373,14 +327,14 @@ describe('WidgetVisibilityService (mockBackend)', () => {
});
it('should search in the form if element value is not in form values', () => {
let value = service.getValueOField(fakeFormWithField, 'FIELD_WITH_CONDITION');
let value = service.getFieldValueFromForm(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');
let formValue = service.getFieldValueFromForm(fakeFormWithField, 'FIELD_MYSTERY');
expect(formValue).toBeUndefined();
});
@@ -401,7 +355,7 @@ describe('WidgetVisibilityService (mockBackend)', () => {
});
it('should take the value from form values if it is present', () => {
let formValue = service.getValueOField(formTest, 'test_1');
let formValue = service.getFieldValueFromForm(formTest, 'test_1');
expect(formValue).not.toBeNull();
expect(formValue).toBe('value_1');
@@ -540,7 +494,7 @@ describe('WidgetVisibilityService (mockBackend)', () => {
});
it('should get the dropdown label value from a form', () => {
let dropdownValue = service.getValueOField(formTest, 'dropdown_LABEL');
let dropdownValue = service.getFieldValueFromForm(formTest, 'dropdown_LABEL');
expect(dropdownValue).not.toBeNull();
expect(dropdownValue).toBeDefined();
@@ -548,7 +502,7 @@ describe('WidgetVisibilityService (mockBackend)', () => {
});
it('should get the dropdown id value from a form', () => {
let dropdownValue = service.getValueOField(formTest, 'dropdown');
let dropdownValue = service.getFieldValueFromForm(formTest, 'dropdown');
expect(dropdownValue).not.toBeNull();
expect(dropdownValue).toBeDefined();
@@ -592,7 +546,7 @@ describe('WidgetVisibilityService (mockBackend)', () => {
});
it('should be able to get value from form values', () => {
let res = service.getFieldValue(formTest.values, 'test_1');
let res = service.getFieldValueFromForm(formTest, 'test_1');
expect(res).not.toBeNull();
expect(res).toBeDefined();
@@ -600,4 +554,3 @@ describe('WidgetVisibilityService (mockBackend)', () => {
});
});
});
});

View File

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