diff --git a/ng2-components/ng2-activiti-form/src/components/form-field/form-field.component.spec.ts b/ng2-components/ng2-activiti-form/src/components/form-field/form-field.component.spec.ts new file mode 100644 index 0000000000..1c347ad16a --- /dev/null +++ b/ng2-components/ng2-activiti-form/src/components/form-field/form-field.component.spec.ts @@ -0,0 +1,101 @@ +/*! + * @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 { CoreModule } from 'ng2-alfresco-core'; +import { ActivitiFormModule } from './../../../index'; +import { ComponentFixture, TestBed, async } from '@angular/core/testing'; +import { FormFieldComponent } from './form-field.component'; +// import { WidgetVisibilityService } from './../../services/widget-visibility.service'; +import { FormRenderingService } from './../../services/form-rendering.service'; +// import { WidgetComponent } from './../widgets/widget.component'; +import { FormFieldModel, FormFieldTypes } from './../widgets/core/index'; +import { TextWidget, CheckboxWidget } from './../widgets/index'; + +describe('FormFieldComponent', () => { + + let fixture: ComponentFixture; + let component: FormFieldComponent; + let componentHandler: any; + + let formRenderingService: FormRenderingService; + // let visibilityService: WidgetVisibilityService; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + imports: [ CoreModule, ActivitiFormModule ] + }) + .compileComponents(); + })); + + beforeEach(() => { + componentHandler = jasmine.createSpyObj('componentHandler', [ + 'upgradeAllRegistered', + 'upgradeElement' + ]); + window['componentHandler'] = componentHandler; + + fixture = TestBed.createComponent(FormFieldComponent); + component = fixture.componentInstance; + formRenderingService = fixture.debugElement.injector.get(FormRenderingService); + }); + + it('should create default component instance', () => { + let field = new FormFieldModel(null, { + type: FormFieldTypes.TEXT + }); + + component.field = field; + fixture.detectChanges(); + + expect(component.componentRef).toBeDefined(); + expect(component.componentRef.componentType).toBe(TextWidget); + }); + + it('should create custom component instance', () => { + let field = new FormFieldModel(null, { + type: FormFieldTypes.TEXT + }); + + formRenderingService.setComponentTypeResolver(FormFieldTypes.TEXT, () => CheckboxWidget, true); + component.field = field; + fixture.detectChanges(); + + expect(component.componentRef).toBeDefined(); + expect(component.componentRef.componentType).toBe(CheckboxWidget); + }); + + it('should require field to create component', () => { + component.field = null; + fixture.detectChanges(); + + expect(component.componentRef).toBeUndefined(); + }); + + it('should require component type to be resolved', () => { + let field = new FormFieldModel(null, { + type: FormFieldTypes.TEXT + }); + + spyOn(formRenderingService, 'resolveComponentType').and.returnValue(null); + component.field = field; + fixture.detectChanges(); + + expect(formRenderingService.resolveComponentType).toHaveBeenCalled(); + expect(component.componentRef).toBeUndefined(); + }); + +}); diff --git a/ng2-components/ng2-activiti-form/src/components/form-field/form-field.component.ts b/ng2-components/ng2-activiti-form/src/components/form-field/form-field.component.ts index 6f05a34b80..f324b07f1f 100644 --- a/ng2-components/ng2-activiti-form/src/components/form-field/form-field.component.ts +++ b/ng2-components/ng2-activiti-form/src/components/form-field/form-field.component.ts @@ -15,7 +15,7 @@ * limitations under the License. */ -import { Component, OnInit, ViewChild, ViewContainerRef, Input, ComponentRef, ComponentFactoryResolver/*,Injector*/ } from '@angular/core'; +import { Component, OnInit, ViewChild, ViewContainerRef, Input, ComponentRef, ComponentFactoryResolver } from '@angular/core'; import { WidgetVisibilityService } from './../../services/widget-visibility.service'; import { FormRenderingService } from './../../services/form-rendering.service'; import { WidgetComponent } from './../widgets/widget.component'; @@ -24,7 +24,7 @@ import { FormFieldModel } from './../widgets/core/index'; @Component({ selector: 'form-field', template: ` -
+
` @@ -37,13 +37,12 @@ export class FormFieldComponent implements OnInit { @Input() field: FormFieldModel = null; - private componentRef: ComponentRef<{}>; + componentRef: ComponentRef<{}>; constructor( private formRenderingService: FormRenderingService, private componentFactoryResolver: ComponentFactoryResolver, - private visibilityService: WidgetVisibilityService - /*,private injector: Injector*/) { + private visibilityService: WidgetVisibilityService) { } ngOnInit() { @@ -51,10 +50,11 @@ export class FormFieldComponent implements OnInit { let componentType = this.formRenderingService.resolveComponentType(this.field); if (componentType) { let factory = this.componentFactoryResolver.resolveComponentFactory(componentType); - this.componentRef = this.container.createComponent(factory/*, 0, this.injector*/); + this.componentRef = this.container.createComponent(factory); let instance = this.componentRef.instance; instance.field = this.field; instance.fieldChanged.subscribe(field => { + console.log('WidgetComponent.fieldChanged was used only to trigger visibility engine, components should do that internally if needed'); if (field && field.form) { this.visibilityService.refreshVisibility(field.form); } diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/widget.component.ts b/ng2-components/ng2-activiti-form/src/components/widgets/widget.component.ts index b873ce0b33..9e9fc1d4cd 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/widget.component.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/widget.component.ts @@ -29,6 +29,7 @@ export class WidgetComponent implements AfterViewInit { @Input() field: FormFieldModel; + /** @deprecated used only to trigger visibility engine, components should do that internally if needed */ @Output() fieldChanged: EventEmitter = new EventEmitter(); @@ -79,13 +80,15 @@ export class WidgetComponent implements AfterViewInit { return false; } - /** @deprecated use onFieldChanged instead */ + /** @deprecated used only to trigger visibility engine, components should do that internally if needed */ checkVisibility(field: FormFieldModel) { - console.log('checkVisibility is deprecated, use onFieldChanged instead'); + console.log('WidgetComponent.checkVisibility was used only to trigger visibility engine, components should do that internally if needed'); this.fieldChanged.emit(field); } + /** @deprecated used only to trigger visibility engine, components should do that internally if needed */ onFieldChanged(field: FormFieldModel) { + console.log('WidgetComponent.onFieldChanged was used only to trigger visibility engine, components should do that internally if needed'); this.fieldChanged.emit(field); }