/*! * @license * Copyright 2019 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 { TestBed } from '@angular/core/testing'; import { ContainerModel, FormFieldModel, FormFieldTypes, FormModel, TabModel } from './../components/widgets/core/index'; import { TaskProcessVariableModel } from './../models/task-process-variable.model'; import { WidgetVisibilityModel } from './../models/widget-visibility.model'; import { WidgetVisibilityService } from './widget-visibility.service'; import { setupTestBed } from '../../testing/setup-test-bed'; import { fakeTaskProcessVariableModels, fakeFormJson, formTest, formValues, complexVisibilityJsonVisible, complexVisibilityJsonNotVisible, tabVisibilityJsonMock, tabInvalidFormVisibility } from 'core/mock/form/widget-visibility.service.mock'; import { CoreTestingModule } from '../../testing/core.testing.module'; import { TranslateModule } from '@ngx-translate/core'; declare let jasmine: any; describe('WidgetVisibilityService', () => { let service: WidgetVisibilityService; let booleanResult: boolean; const stubFormWithFields = new FormModel(fakeFormJson); setupTestBed({ imports: [ TranslateModule.forRoot(), CoreTestingModule ] }); beforeEach(() => { service = TestBed.inject(WidgetVisibilityService); jasmine.Ajax.install(); }); afterEach(() => { jasmine.Ajax.uninstall(); }); describe('should be able to evaluate logic operations', () => { it('using AND and return true', () => { booleanResult = service.evaluateLogicalOperation('and', true, true); expect(booleanResult).toBeTruthy(); }); it('using AND and return false', () => { booleanResult = service.evaluateLogicalOperation('and', true, false); expect(booleanResult).toBeFalsy(); }); it('using OR and return true', () => { booleanResult = service.evaluateLogicalOperation('or', true, false); expect(booleanResult).toBeTruthy(); }); it('using OR and return false', () => { booleanResult = service.evaluateLogicalOperation('or', false, false); expect(booleanResult).toBeFalsy(); }); it('using AND NOT and return true', () => { booleanResult = service.evaluateLogicalOperation('and-not', true, false); expect(booleanResult).toBeTruthy(); }); it('using AND NOT and return false', () => { booleanResult = service.evaluateLogicalOperation('and-not', false, false); expect(booleanResult).toBeFalsy(); }); it('using OR NOT and return true', () => { booleanResult = service.evaluateLogicalOperation('or-not', true, true); expect(booleanResult).toBeTruthy(); }); it('using OR NOT and return false', () => { booleanResult = service.evaluateLogicalOperation('or-not', false, true); expect(booleanResult).toBeFalsy(); }); it('should fail with invalid operation', () => { booleanResult = service.evaluateLogicalOperation(undefined, false, true); expect(booleanResult).toBeUndefined(); }); }); describe('should be able to evaluate next condition operations', () => { it('using == and return true', () => { booleanResult = service.evaluateCondition('test', 'test', '=='); expect(booleanResult).toBeTruthy(); }); it('using < and return true', () => { booleanResult = service.evaluateCondition(1, 2, '<'); expect(booleanResult).toBeTruthy(); }); it('using != and return true', () => { booleanResult = service.evaluateCondition(true, false, '!='); expect(booleanResult).toBeTruthy(); }); it('using != and return false', () => { booleanResult = service.evaluateCondition(true, true, '!='); expect(booleanResult).toBeFalsy(); }); it('using >= and return true', () => { booleanResult = service.evaluateCondition(2, 2, '>='); expect(booleanResult).toBeTruthy(); }); it('using empty with null values and return true', () => { booleanResult = service.evaluateCondition(null, null, 'empty'); expect(booleanResult).toBeTruthy(); }); it('using empty with empty strings values and return true', () => { booleanResult = service.evaluateCondition('', '', 'empty'); expect(booleanResult).toBeTruthy(); }); it('using empty with empty string value and return false', () => { booleanResult = service.evaluateCondition('fake_value', undefined, 'empty'); expect(booleanResult).toBeFalsy(); }); it('using > and return false', () => { booleanResult = service.evaluateCondition(2, 3, '>'); expect(booleanResult).toBeFalsy(); }); it('using not empty with null values and return false', () => { booleanResult = service.evaluateCondition(null, null, '!empty'); expect(booleanResult).toBeFalsy(); }); it('using OR NOT with empty strings and return false', () => { booleanResult = service.evaluateCondition('', '', '!empty'); expect(booleanResult).toBeFalsy(); }); it('using <= and return false', () => { booleanResult = service.evaluateCondition(2, 1, '<='); expect(booleanResult).toBeFalsy(); }); it('using <= and return true for different values', () => { booleanResult = service.evaluateCondition(1, 2, '<='); expect(booleanResult).toBeTruthy(); }); it('using <= and return true for same values', () => { booleanResult = service.evaluateCondition(2, 2, '<='); expect(booleanResult).toBeTruthy(); }); it('should return undefined for invalid operation', () => { booleanResult = service.evaluateCondition(null, null, undefined); expect(booleanResult).toBeUndefined(); }); }); describe('should retrieve the process variables', () => { let fakeFormWithField: FormModel; let visibilityObjTest: WidgetVisibilityModel; const chainedVisibilityObj = new WidgetVisibilityModel({}); beforeEach(() => { fakeFormWithField = new FormModel(fakeFormJson); visibilityObjTest = new WidgetVisibilityModel({}); fakeFormWithField = new FormModel(fakeFormJson); }); it('should return the process variables for task', (done) => { service.getTaskProcessVariable('9999').subscribe( (res) => { 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(); } ); jasmine.Ajax.requests.mostRecent().respondWith({ status: 200, contentType: 'json', responseText: fakeTaskProcessVariableModels }); }); it('should be able to retrieve the value of a process variable', (done) => { service.getTaskProcessVariable('9999').subscribe( (res: TaskProcessVariableModel[]) => { expect(res).toBeDefined(); const varValue = service.getVariableValue(formTest, 'TEST_VAR_1', res); expect(varValue).not.toBeUndefined(); expect(varValue).toBe('test_value_1'); done(); } ); jasmine.Ajax.requests.mostRecent().respondWith({ status: 200, contentType: 'json', responseText: fakeTaskProcessVariableModels }); }); it('should return undefined if the variable does not exist', (done) => { service.getTaskProcessVariable('9999').subscribe( (res: TaskProcessVariableModel[]) => { const varValue = service.getVariableValue(formTest, 'TEST_MYSTERY_VAR', res); expect(varValue).toBeUndefined(); 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) => { service.getTaskProcessVariable('9999').subscribe( () => { visibilityObjTest.rightRestResponseId = 'TEST_VAR_2'; const rightValue = service.getRightValue(formTest, visibilityObjTest); expect(rightValue).not.toBeNull(); expect(rightValue).toBe('test_value_2'); 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) => { service.getTaskProcessVariable('9999').subscribe( () => { visibilityObjTest.leftRestResponseId = 'TEST_VAR_2'; const leftValue = service.getLeftValue(formTest, visibilityObjTest); expect(leftValue).not.toBeNull(); expect(leftValue).toBe('test_value_2'); 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) => { service.getTaskProcessVariable('9999').subscribe( () => { visibilityObjTest.leftFormFieldId = 'LEFT_FORM_FIELD_ID'; visibilityObjTest.operator = '!='; visibilityObjTest.rightRestResponseId = 'TEST_VAR_2'; const isVisible = service.isFieldVisible(fakeFormWithField, new WidgetVisibilityModel(visibilityObjTest)); expect(isVisible).toBeTruthy(); done(); } ); jasmine.Ajax.requests.mostRecent().respondWith({ status: 200, contentType: 'json', responseText: fakeTaskProcessVariableModels }); }); it('should evaluate visibility with multiple conditions', (done) => { service.getTaskProcessVariable('9999').subscribe( () => { 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; const isVisible = service.isFieldVisible(fakeFormWithField, visibilityObjTest); expect(isVisible).toBeTruthy(); done(); } ); jasmine.Ajax.requests.mostRecent().respondWith({ status: 200, contentType: 'json', responseText: fakeTaskProcessVariableModels }); }); it('should catch error on 403 response', (done) => { service.getTaskProcessVariable('9999').subscribe(() => { }, () => { done(); }); jasmine.Ajax.requests.mostRecent().respondWith({ status: 403 }); }); }); describe('should return the value of the field', () => { let visibilityObjTest: WidgetVisibilityModel; let fakeFormWithField: FormModel; const jsonFieldFake = { id: 'FAKE_FORM_FIELD_ID', value: 'FAKE_FORM_FIELD_VALUE', visibilityCondition: undefined }; const fakeForm = new FormModel({ variables: [ { name: 'FORM_VARIABLE_TEST', type: 'string', value: 'form_value_test' }] }); beforeEach(() => { fakeFormWithField = new FormModel(fakeFormJson); visibilityObjTest = new WidgetVisibilityModel(); fakeFormWithField = new FormModel(fakeFormJson); formTest.values = formValues; jsonFieldFake.visibilityCondition = visibilityObjTest; }); afterEach(() => { service.cleanProcessVariable(); }); it('should be able to retrieve a field value searching in the form', () => { const formField = service.getFormFieldById(stubFormWithFields, 'FIELD_WITH_CONDITION'); const formValue = service.searchValueInForm(formField, 'FIELD_WITH_CONDITION'); expect(formValue).not.toBeNull(); expect(formValue).toBe('field_with_condition_value'); }); it('should return empty string if the field value is not in the form', () => { const formField = service.getFormFieldById(stubFormWithFields, 'FIELD_MYSTERY'); const formValue = service.searchValueInForm(formField, 'FIELD_MYSTERY'); expect(formValue).not.toBeUndefined(); expect(formValue).toBe(''); }); it('should search in the form if element value is not in form values', () => { const value = service.getFormValue(fakeFormWithField, 'FIELD_WITH_CONDITION'); expect(value).not.toBeNull(); expect(value).toBe('field_with_condition_value'); }); it('should return empty string if the element is not present anywhere', () => { const formValue = service.getFormValue(fakeFormWithField, 'FIELD_MYSTERY'); expect(formValue).toBeUndefined(); }); it('should retrieve the value for the right field when it is a value', () => { visibilityObjTest.rightValue = '100'; spyOn(service, 'isFormFieldValid').and.returnValue(true); const rightValue = service.getRightValue(formTest, visibilityObjTest); expect(rightValue).toBe('100'); }); it('should return formatted date when right value is a date', () => { visibilityObjTest.rightValue = '9999-12-31'; spyOn(service, 'isFormFieldValid').and.returnValue(true); const rightValue = service.getRightValue(formTest, visibilityObjTest); expect(rightValue).toBe('9999-12-31T00:00:00.000Z'); }); it('should return the value when right value is not a date', () => { visibilityObjTest.rightValue = '9999-99-99'; const rightValue = service.getRightValue(formTest, visibilityObjTest); expect(rightValue).toBe('9999-99-99'); }); it('should retrieve the value for the right field when it is a form variable', () => { visibilityObjTest.rightFormFieldId = 'RIGHT_FORM_FIELD_ID'; const 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', () => { spyOn(service, 'isFormFieldValid').and.returnValue(true); const formValue = service.getFormValue(formTest, 'test_1'); expect(formValue).not.toBeNull(); expect(formValue).toBe('value_1'); }); it('should retrieve right value from form values if it is present', () => { visibilityObjTest.rightFormFieldId = 'test_2'; spyOn(service, 'isFormFieldValid').and.returnValue(true); const 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'; const 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'; spyOn(service, 'isFormFieldValid').and.returnValue(true); const leftValue = service.getLeftValue(formTest, visibilityObjTest); expect(leftValue).not.toBeNull(); expect(leftValue).toBe('value_2'); }); it('should return empty string for a value that is not on variable or form', () => { const leftValue = service.getLeftValue(fakeFormWithField, visibilityObjTest); expect(leftValue).toBe(''); }); 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'; spyOn(service, 'isFormFieldValid').and.returnValue(true); const isVisible = service.isFieldVisible(formTest, visibilityObjTest); expect(isVisible).toBeTruthy(); }); it('should evaluate true visibility for the field with single visibility condition between a field and a value', () => { visibilityObjTest.leftFormFieldId = 'test_1'; visibilityObjTest.operator = '=='; visibilityObjTest.rightValue = 'value_1'; spyOn(service, 'isFormFieldValid').and.returnValue(true); const isVisible = service.isFieldVisible(formTest, visibilityObjTest); expect(isVisible).toBeTruthy(); }); it('should undefined string for a value that is not on variable or form', () => { visibilityObjTest.rightFormFieldId = 'NO_FIELD_FORM'; const rightValue = service.getRightValue(fakeFormWithField, visibilityObjTest); expect(rightValue).toBeUndefined(); }); it('should evaluate the visibility for the field with single visibility condition between form values', () => { visibilityObjTest.leftFormFieldId = 'LEFT_FORM_FIELD_ID'; visibilityObjTest.operator = '!='; visibilityObjTest.rightFormFieldId = 'RIGHT_FORM_FIELD_ID'; const isVisible = service.isFieldVisible(fakeFormWithField, visibilityObjTest); expect(isVisible).toBeTruthy(); }); it('should refresh the visibility for a form field object', () => { visibilityObjTest.leftFormFieldId = 'test_1'; visibilityObjTest.operator = '!='; visibilityObjTest.rightFormFieldId = 'test_3'; const fakeFormField: FormFieldModel = new FormFieldModel(formTest, jsonFieldFake); service.refreshEntityVisibility(fakeFormField); expect(fakeFormField.isVisible).toBeFalsy(); }); it('should return true when the visibility condition is not valid', () => { visibilityObjTest.leftFormFieldId = ''; visibilityObjTest.leftRestResponseId = ''; visibilityObjTest.operator = '!='; const isVisible = service.evaluateVisibility(formTest, visibilityObjTest); expect(isVisible).toBeTruthy(); }); it('should return always true when field does not have a visibility condition', () => { jsonFieldFake.visibilityCondition = null; const fakeFormField: FormFieldModel = new FormFieldModel(fakeFormWithField, jsonFieldFake); fakeFormField.isVisible = false; service.refreshEntityVisibility(fakeFormField); expect(fakeFormField.isVisible).toBeTruthy(); }); it('should be able to retrieve the value of a form variable', () => { const varValue = service.getVariableValue(fakeForm, 'FORM_VARIABLE_TEST', null); expect(varValue).not.toBeUndefined(); expect(varValue).toBe('form_value_test'); }); it('should return undefined for not existing form variable', () => { const varValue = service.getVariableValue(fakeForm, 'MYSTERY_FORM_VARIABLE', null); expect(varValue).toBeUndefined(); }); it('should retrieve the value for the left field when it is a form variable', () => { visibilityObjTest.leftRestResponseId = 'FORM_VARIABLE_TEST'; const leftValue = service.getLeftValue(fakeForm, visibilityObjTest); expect(leftValue).not.toBeNull(); expect(leftValue).toBe('form_value_test'); }); it('should determine visibility for dropdown on label condition', () => { const dropdownValue = service.getFieldValue(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', () => { const dropdownValue = service.getFieldValue(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', () => { const dropdownValue = service.getFieldValue(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', () => { spyOn(service, 'isFormFieldValid').and.returnValue(true); const dropdownValue = service.getFormValue(formTest, 'dropdown_LABEL'); expect(dropdownValue).not.toBeNull(); expect(dropdownValue).toBeDefined(); expect(dropdownValue).toBe('dropdown_label'); }); it('should get the dropdown id value from a form', () => { spyOn(service, 'isFormFieldValid').and.returnValue(true); const dropdownValue = service.getFormValue(formTest, 'dropdown'); expect(dropdownValue).not.toBeNull(); expect(dropdownValue).toBeDefined(); expect(dropdownValue).toBe('dropdown_id'); }); it('should retrieve the value for the right field when it is a dropdown id', () => { visibilityObjTest.rightFormFieldId = 'dropdown'; spyOn(service, 'isFormFieldValid').and.returnValue(true); const 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'; spyOn(service, 'isFormFieldValid').and.returnValue(true); const rightValue = service.getRightValue(formTest, visibilityObjTest); expect(rightValue).toBeDefined(); expect(rightValue).toBe('dropdown_label'); }); it('should be able to evaluate condition with a dropdown