Unit tests and code cleanup

This commit is contained in:
Denys Vuika
2016-11-16 14:22:37 +00:00
committed by Mario Romano
parent 1121a387fd
commit 1032f74100
3 changed files with 112 additions and 8 deletions

View File

@@ -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<FormFieldComponent>;
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();
});
});

View File

@@ -15,7 +15,7 @@
* limitations under the License. * 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 { WidgetVisibilityService } from './../../services/widget-visibility.service';
import { FormRenderingService } from './../../services/form-rendering.service'; import { FormRenderingService } from './../../services/form-rendering.service';
import { WidgetComponent } from './../widgets/widget.component'; import { WidgetComponent } from './../widgets/widget.component';
@@ -24,7 +24,7 @@ import { FormFieldModel } from './../widgets/core/index';
@Component({ @Component({
selector: 'form-field', selector: 'form-field',
template: ` template: `
<div [hidden]="!field.isVisible"> <div [hidden]="!field?.isVisible">
<div #container></div> <div #container></div>
</div> </div>
` `
@@ -37,13 +37,12 @@ export class FormFieldComponent implements OnInit {
@Input() @Input()
field: FormFieldModel = null; field: FormFieldModel = null;
private componentRef: ComponentRef<{}>; componentRef: ComponentRef<{}>;
constructor( constructor(
private formRenderingService: FormRenderingService, private formRenderingService: FormRenderingService,
private componentFactoryResolver: ComponentFactoryResolver, private componentFactoryResolver: ComponentFactoryResolver,
private visibilityService: WidgetVisibilityService private visibilityService: WidgetVisibilityService) {
/*,private injector: Injector*/) {
} }
ngOnInit() { ngOnInit() {
@@ -51,10 +50,11 @@ export class FormFieldComponent implements OnInit {
let componentType = this.formRenderingService.resolveComponentType(this.field); let componentType = this.formRenderingService.resolveComponentType(this.field);
if (componentType) { if (componentType) {
let factory = this.componentFactoryResolver.resolveComponentFactory(componentType); let factory = this.componentFactoryResolver.resolveComponentFactory(componentType);
this.componentRef = this.container.createComponent(factory/*, 0, this.injector*/); this.componentRef = this.container.createComponent(factory);
let instance = <WidgetComponent>this.componentRef.instance; let instance = <WidgetComponent>this.componentRef.instance;
instance.field = this.field; instance.field = this.field;
instance.fieldChanged.subscribe(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) { if (field && field.form) {
this.visibilityService.refreshVisibility(field.form); this.visibilityService.refreshVisibility(field.form);
} }

View File

@@ -29,6 +29,7 @@ export class WidgetComponent implements AfterViewInit {
@Input() @Input()
field: FormFieldModel; field: FormFieldModel;
/** @deprecated used only to trigger visibility engine, components should do that internally if needed */
@Output() @Output()
fieldChanged: EventEmitter<FormFieldModel> = new EventEmitter<FormFieldModel>(); fieldChanged: EventEmitter<FormFieldModel> = new EventEmitter<FormFieldModel>();
@@ -79,13 +80,15 @@ export class WidgetComponent implements AfterViewInit {
return false; return false;
} }
/** @deprecated use onFieldChanged instead */ /** @deprecated used only to trigger visibility engine, components should do that internally if needed */
checkVisibility(field: FormFieldModel) { 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); this.fieldChanged.emit(field);
} }
/** @deprecated used only to trigger visibility engine, components should do that internally if needed */
onFieldChanged(field: FormFieldModel) { 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); this.fieldChanged.emit(field);
} }