/*! * @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 { SimpleChange } from '@angular/core'; import { Observable, of, throwError } from 'rxjs'; import { FormFieldModel, FormFieldTypes, FormModel, FormOutcomeEvent, FormOutcomeModel, FormService, WidgetVisibilityService, NodeService, LogService, ContainerModel, fakeForm, FormRenderingService } from '@alfresco/adf-core'; import { FormComponent } from './form.component'; describe('FormComponent', () => { let formService: FormService; let formComponent: FormComponent; let visibilityService: WidgetVisibilityService; let nodeService: NodeService; let logService: LogService; let formRenderingService: FormRenderingService; beforeEach(() => { logService = new LogService(null); visibilityService = new WidgetVisibilityService(null, logService); spyOn(visibilityService, 'refreshVisibility').and.stub(); formService = new FormService(null, null, logService); nodeService = new NodeService(null); formRenderingService = new FormRenderingService(); formComponent = new FormComponent(formService, visibilityService, null, nodeService, formRenderingService); }); it('should check form', () => { expect(formComponent.hasForm()).toBeFalsy(); formComponent.form = new FormModel(); expect(formComponent.hasForm()).toBeTruthy(); }); it('should allow title if task name available', () => { const formModel = new FormModel(); formComponent.form = formModel; expect(formComponent.showTitle).toBeTruthy(); expect(formModel.taskName).toBe(FormModel.UNSET_TASK_NAME); expect(formComponent.isTitleEnabled()).toBeTruthy(); formComponent.form = null; expect(formComponent.isTitleEnabled()).toBeFalsy(); }); it('should not allow title', () => { const formModel = new FormModel(); formComponent.form = formModel; formComponent.showTitle = false; expect(formModel.taskName).toBe(FormModel.UNSET_TASK_NAME); expect(formComponent.isTitleEnabled()).toBeFalsy(); }); it('should return primary color for complete button', () => { expect(formComponent.getColorForOutcome('COMPLETE')).toBe('primary'); }); it('should not enable outcome button when model missing', () => { expect(formComponent.isOutcomeButtonVisible(null, false)).toBeFalsy(); }); it('should enable custom outcome buttons', () => { const formModel = new FormModel(); formComponent.form = formModel; const outcome = new FormOutcomeModel(formModel, { id: 'action1', name: 'Action 1' }); expect(formComponent.isOutcomeButtonVisible(outcome, formComponent.form.readOnly)).toBeTruthy(); }); it('should allow controlling [complete] button visibility', () => { const formModel = new FormModel(); formComponent.form = formModel; const outcome = new FormOutcomeModel(formModel, { id: '$save', name: FormOutcomeModel.SAVE_ACTION }); formComponent.showSaveButton = true; expect(formComponent.isOutcomeButtonVisible(outcome, formComponent.form.readOnly)).toBeTruthy(); formComponent.showSaveButton = false; expect(formComponent.isOutcomeButtonVisible(outcome, formComponent.form.readOnly)).toBeFalsy(); }); it('should show only [complete] button with readOnly form ', () => { const formModel = new FormModel(); formModel.readOnly = true; formComponent.form = formModel; const outcome = new FormOutcomeModel(formModel, { id: '$complete', name: FormOutcomeModel.COMPLETE_ACTION }); formComponent.showCompleteButton = true; expect(formComponent.isOutcomeButtonVisible(outcome, formComponent.form.readOnly)).toBeTruthy(); }); it('should not show [save] button with readOnly form ', () => { const formModel = new FormModel(); formModel.readOnly = true; formComponent.form = formModel; const outcome = new FormOutcomeModel(formModel, { id: '$save', name: FormOutcomeModel.SAVE_ACTION }); formComponent.showSaveButton = true; expect(formComponent.isOutcomeButtonVisible(outcome, formComponent.form.readOnly)).toBeFalsy(); }); it('should show [custom-outcome] button with readOnly form and selected custom-outcome', () => { const formModel = new FormModel({ selectedOutcome: 'custom-outcome' }); formModel.readOnly = true; formComponent.form = formModel; let outcome = new FormOutcomeModel(formModel, { id: '$customoutome', name: 'custom-outcome' }); formComponent.showCompleteButton = true; formComponent.showSaveButton = true; expect(formComponent.isOutcomeButtonVisible(outcome, formComponent.form.readOnly)).toBeTruthy(); outcome = new FormOutcomeModel(formModel, { id: '$customoutome2', name: 'custom-outcome2' }); expect(formComponent.isOutcomeButtonVisible(outcome, formComponent.form.readOnly)).toBeFalsy(); }); it('should allow controlling [save] button visibility', () => { const formModel = new FormModel(); formModel.readOnly = false; formComponent.form = formModel; const outcome = new FormOutcomeModel(formModel, { id: '$save', name: FormOutcomeModel.COMPLETE_ACTION }); formComponent.showCompleteButton = true; expect(formComponent.isOutcomeButtonVisible(outcome, formComponent.form.readOnly)).toBeTruthy(); formComponent.showCompleteButton = false; expect(formComponent.isOutcomeButtonVisible(outcome, formComponent.form.readOnly)).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((currentTaskId) => { return new Observable((observer) => { observer.next({ taskId: currentTaskId }); observer.complete(); }); }); spyOn(visibilityService, 'getTaskProcessVariable').and.returnValue(of({})); spyOn(formService, 'getTask').and.callFake((currentTaskId) => { return new Observable((observer) => { observer.next({ taskId: currentTaskId, 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((currentTaskId) => { return new Observable((observer) => { observer.next({ taskId: currentTaskId }); observer.complete(); }); }); spyOn(visibilityService, 'getTaskProcessVariable').and.returnValue(of({})); spyOn(formService, 'getTask').and.callFake((currentTaskId) => { return new Observable((observer) => { observer.next({ taskId: currentTaskId, 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 refresh visibility when the form is loaded', () => { spyOn(formService, 'getFormDefinitionById').and.returnValue(of(JSON.parse(JSON.stringify(fakeForm)))); const formId = 123; formComponent.formId = formId; formComponent.loadForm(); expect(formService.getFormDefinitionById).toHaveBeenCalledWith(formId); expect(visibilityService.refreshVisibility).toHaveBeenCalled(); }); it('should get form definition by form name on load', () => { spyOn(formComponent, 'getFormDefinitionByFormName').and.stub(); const formName = '