mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-31 17:38:48 +00:00
Unit tests and code cleanup
This commit is contained in:
committed by
Mario Romano
parent
1121a387fd
commit
1032f74100
@@ -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();
|
||||
});
|
||||
|
||||
});
|
@@ -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: `
|
||||
<div [hidden]="!field.isVisible">
|
||||
<div [hidden]="!field?.isVisible">
|
||||
<div #container></div>
|
||||
</div>
|
||||
`
|
||||
@@ -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 = <WidgetComponent>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);
|
||||
}
|
||||
|
@@ -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<FormFieldModel> = new EventEmitter<FormFieldModel>();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user