/*! * @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 { Observable } from 'rxjs/Rx'; import { SimpleChange } from '@angular/core'; import { ActivitiForm } from './activiti-form.component'; import { FormModel, FormOutcomeModel, FormFieldModel, FormOutcomeEvent, FormFieldTypes } from './widgets/index'; import { FormService } from './../services/form.service'; import { WidgetVisibilityService } from './../services/widget-visibility.service'; import { NodeService } from './../services/node.service'; describe('ActivitiForm', () => { let componentHandler: any; let formService: FormService; let formComponent: ActivitiForm; let visibilityService: WidgetVisibilityService; let nodeService: NodeService; beforeEach(() => { componentHandler = jasmine.createSpyObj('componentHandler', [ 'upgradeAllRegistered' ]); window['componentHandler'] = componentHandler; visibilityService = new WidgetVisibilityService(null); spyOn(visibilityService, 'refreshVisibility').and.stub(); formService = new FormService(null, null); nodeService = new NodeService(null); formComponent = new ActivitiForm(formService, visibilityService, null, nodeService); }); it('should upgrade MDL content on view checked', () => { formComponent.ngAfterViewChecked(); expect(componentHandler.upgradeAllRegistered).toHaveBeenCalled(); }); it('should setup MDL content only if component handler available', () => { expect(formComponent.setupMaterialComponents()).toBeTruthy(); window['componentHandler'] = null; expect(formComponent.setupMaterialComponents()).toBeFalsy(); }); it('should start loading form on init', () => { spyOn(formComponent, 'loadForm').and.stub(); formComponent.ngOnInit(); expect(formComponent.loadForm).toHaveBeenCalled(); }); it('should check form', () => { expect(formComponent.hasForm()).toBeFalsy(); formComponent.form = new FormModel(); expect(formComponent.hasForm()).toBeTruthy(); }); it('should allow title if task name available', () => { let formModel = new FormModel(); formComponent.form = formModel; expect(formComponent.showTitle).toBeTruthy(); expect(formModel.taskName).toBe(FormModel.UNSET_TASK_NAME); expect(formComponent.isTitleEnabled()).toBeTruthy(); // override property as it's the readonly one Object.defineProperty(formModel, 'taskName', { enumerable: false, configurable: false, writable: false, value: null }); expect(formComponent.isTitleEnabled()).toBeFalsy(); }); it('should not allow title', () => { let formModel = new FormModel(); formComponent.form = formModel; formComponent.showTitle = false; expect(formModel.taskName).toBe(FormModel.UNSET_TASK_NAME); expect(formComponent.isTitleEnabled()).toBeFalsy(); }); it('should not enable outcome button when model missing', () => { expect(formComponent.isOutcomeButtonVisible(null)).toBeFalsy(); }); it('should enable custom outcome buttons', () => { let formModel = new FormModel(); let outcome = new FormOutcomeModel(formModel, {id: 'action1', name: 'Action 1'}); expect(formComponent.isOutcomeButtonVisible(outcome)).toBeTruthy(); }); it('should allow controlling [complete] button visibility', () => { let formModel = new FormModel(); let outcome = new FormOutcomeModel(formModel, {id: '$save', name: FormOutcomeModel.SAVE_ACTION}); formComponent.showSaveButton = true; expect(formComponent.isOutcomeButtonVisible(outcome)).toBeTruthy(); formComponent.showSaveButton = false; expect(formComponent.isOutcomeButtonVisible(outcome)).toBeFalsy(); }); it('should allow controlling [save] button visibility', () => { let formModel = new FormModel(); let outcome = new FormOutcomeModel(formModel, {id: '$save', name: FormOutcomeModel.COMPLETE_ACTION}); formComponent.showCompleteButton = true; expect(formComponent.isOutcomeButtonVisible(outcome)).toBeTruthy(); formComponent.showCompleteButton = false; expect(formComponent.isOutcomeButtonVisible(outcome)).toBeFalsy(); }); it('should load form on refresh', () => { spyOn(formComponent, 'loadForm').and.stub(); formComponent.onRefreshClicked(); expect(formComponent.loadForm).toHaveBeenCalled(); }); it('should get form by task id on load', () => { spyOn(formComponent, 'getFormByTaskId').and.stub(); const taskId = '123'; formComponent.taskId = taskId; formComponent.loadForm(); expect(formComponent.getFormByTaskId).toHaveBeenCalledWith(taskId); }); it('should get process variable if is a process task', () => { spyOn(formService, 'getTaskForm').and.callFake((taskId) => { return Observable.create(observer => { observer.next({taskId: taskId}); observer.complete(); }); }); spyOn(visibilityService, 'getTaskProcessVariable').and.returnValue(Observable.of({})); spyOn(formService, 'getTask').and.callFake((taskId) => { return Observable.create(observer => { observer.next({taskId: taskId, processDefinitionId: '10201'}); observer.complete(); }); }); const taskId = '123'; formComponent.taskId = taskId; formComponent.loadForm(); expect(visibilityService.getTaskProcessVariable).toHaveBeenCalledWith(taskId); }); it('should not get process variable if is not a process task', () => { spyOn(formService, 'getTaskForm').and.callFake((taskId) => { return Observable.create(observer => { observer.next({taskId: taskId}); observer.complete(); }); }); spyOn(visibilityService, 'getTaskProcessVariable').and.returnValue(Observable.of({})); spyOn(formService, 'getTask').and.callFake((taskId) => { return Observable.create(observer => { observer.next({taskId: taskId, processDefinitionId: 'null'}); observer.complete(); }); }); const taskId = '123'; formComponent.taskId = taskId; formComponent.loadForm(); expect(visibilityService.getTaskProcessVariable).toHaveBeenCalledWith(taskId); }); it('should get form definition by form id on load', () => { spyOn(formComponent, 'getFormDefinitionByFormId').and.stub(); const formId = '123'; formComponent.formId = formId; formComponent.loadForm(); expect(formComponent.getFormDefinitionByFormId).toHaveBeenCalledWith(formId); }); it('should get form definition by form name on load', () => { spyOn(formComponent, 'getFormDefinitionByFormName').and.stub(); const formName = '