Additional unit tests for form widgets

This commit is contained in:
Denys Vuika
2016-10-12 14:24:21 +01:00
parent 39178f1788
commit ccd41ba6e1
10 changed files with 223 additions and 26 deletions

View File

@@ -15,6 +15,7 @@
* limitations under the License.
*/
import { ElementRef } from '@angular/core';
import { Observable } from 'rxjs/Rx';
import { FunctionalGroupWidget } from './functional-group.widget';
import { FormService } from '../../../services/form.service';
@@ -24,12 +25,20 @@ import { GroupModel } from '../core/group.model';
describe('FunctionalGroupWidget', () => {
let componentHandler;
let formService: FormService;
let elementRef: ElementRef;
let widget: FunctionalGroupWidget;
beforeEach(() => {
componentHandler = jasmine.createSpyObj('componentHandler', [
'upgradeAllRegistered'
]);
window['componentHandler'] = componentHandler;
formService = new FormService(null, null);
widget = new FunctionalGroupWidget(formService, null);
elementRef = new ElementRef(null);
widget = new FunctionalGroupWidget(formService, elementRef);
widget.field = new FormFieldModel(new FormModel());
});
@@ -39,7 +48,7 @@ describe('FunctionalGroupWidget', () => {
spyOn(formService, 'getWorkflowGroups').and.returnValue(
Observable.create(observer => {
observer.next([]);
observer.next(null);
observer.complete();
})
);
@@ -219,4 +228,37 @@ describe('FunctionalGroupWidget', () => {
expect(formService.getWorkflowGroups).not.toHaveBeenCalled();
expect(widget.popupVisible).toBeFalsy();
});
it('should setup mdl textfield on view init', () => {
spyOn(widget, 'setupMaterialComponents').and.callThrough();
spyOn(widget, 'setupMaterialTextField').and.callThrough();
widget.value = '<value>';
widget.ngAfterViewInit();
expect(widget.setupMaterialComponents).toHaveBeenCalledWith(componentHandler);
expect(widget.setupMaterialTextField).toHaveBeenCalled();
});
it('should require component handler to setup textfield', () => {
expect(widget.setupMaterialComponents(null)).toBeFalsy();
});
it('should require element reference to setup textfield', () => {
let w = new FunctionalGroupWidget(formService, null);
w.value = '<value>';
expect(w.setupMaterialComponents(componentHandler)).toBeFalsy();
w = new FunctionalGroupWidget(formService, elementRef);
w.value = '<value>';
expect(w.setupMaterialComponents(componentHandler)).toBeTruthy();
});
it('should require value to setup textfield', () => {
widget.value = '<value>';
expect(widget.setupMaterialComponents(componentHandler)).toBeTruthy();
widget.value = null;
expect(widget.setupMaterialComponents(componentHandler)).toBeFalsy();
});
});

View File

@@ -109,16 +109,12 @@ export class FunctionalGroupWidget extends WidgetComponent implements OnInit {
}
setupMaterialComponents(handler: any): boolean {
// workaround for MDL issues with dynamic components
super.setupMaterialComponents(handler);
if (handler) {
handler.upgradeAllRegistered();
if (this.elementRef && this.value) {
let container = this.elementRef.nativeElement.querySelector('.mdl-textfield');
if (container) {
container.MaterialTextfield.change(this.value);
}
this.setupMaterialTextField(this.elementRef, handler, this.value);
return true;
}
return true;
}
return false;
}

View File

@@ -0,0 +1,31 @@
/*!
* @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 { MultilineTextWidget } from './multiline-text.widget';
describe('MultilineTextWidget', () => {
let widget: MultilineTextWidget;
beforeEach(() => {
widget = new MultilineTextWidget(null);
});
it('should exist', () => {
expect(widget).toBeDefined();
});
});

View File

@@ -0,0 +1,31 @@
/*!
* @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 { NumberWidget } from './number.widget';
describe('NumberWidget', () => {
let widget: NumberWidget;
beforeEach(() => {
widget = new NumberWidget(null);
});
it('should exist', () => {
expect(widget).toBeDefined();
});
});

View File

@@ -15,6 +15,7 @@
* limitations under the License.
*/
import { ElementRef } from '@angular/core';
import { Observable } from 'rxjs/Rx';
import { PeopleWidget } from './people.widget';
import { FormService } from '../../../services/form.service';
@@ -24,12 +25,20 @@ import { GroupUserModel } from '../core/group-user.model';
describe('PeopleWidget', () => {
let componentHandler;
let elementRef: ElementRef;
let formService: FormService;
let widget: PeopleWidget;
beforeEach(() => {
componentHandler = jasmine.createSpyObj('componentHandler', [
'upgradeAllRegistered'
]);
window['componentHandler'] = componentHandler;
formService = new FormService(null, null);
widget = new PeopleWidget(formService, null);
elementRef = new ElementRef(null);
widget = new PeopleWidget(formService, elementRef);
widget.field = new FormFieldModel(new FormModel());
});
@@ -45,6 +54,16 @@ describe('PeopleWidget', () => {
expect(widget.getDisplayName(model)).toBe('John Doe');
});
it('should skip first name for display name', () => {
let model = new GroupUserModel({ firstName: null, lastName: 'Doe' });
expect(widget.getDisplayName(model)).toBe('Doe');
});
it('should skip last name for display name', () => {
let model = new GroupUserModel({ firstName: 'John', lastName: null });
expect(widget.getDisplayName(model)).toBe('John');
});
it('should flush value on blur', (done) => {
spyOn(widget, 'flushValue').and.stub();
widget.onBlur();
@@ -62,7 +81,7 @@ describe('PeopleWidget', () => {
});
spyOn(formService, 'getWorkflowUsers').and.returnValue(Observable.create(observer => {
observer.next([]);
observer.next(null);
observer.complete();
}));
@@ -209,4 +228,37 @@ describe('PeopleWidget', () => {
expect(widget.value).toBeNull();
expect(widget.field.value).toBeNull();
});
it('should setup mdl textfield on view init', () => {
spyOn(widget, 'setupMaterialComponents').and.callThrough();
spyOn(widget, 'setupMaterialTextField').and.callThrough();
widget.value = '<value>';
widget.ngAfterViewInit();
expect(widget.setupMaterialComponents).toHaveBeenCalledWith(componentHandler);
expect(widget.setupMaterialTextField).toHaveBeenCalled();
});
it('should require component handler to setup textfield', () => {
expect(widget.setupMaterialComponents(null)).toBeFalsy();
});
it('should require element reference to setup textfield', () => {
let w = new PeopleWidget(formService, null);
w.value = '<value>';
expect(w.setupMaterialComponents(componentHandler)).toBeFalsy();
w = new PeopleWidget(formService, elementRef);
w.value = '<value>';
expect(w.setupMaterialComponents(componentHandler)).toBeTruthy();
});
it('should require value to setup textfield', () => {
widget.value = '<value>';
expect(widget.setupMaterialComponents(componentHandler)).toBeTruthy();
widget.value = null;
expect(widget.setupMaterialComponents(componentHandler)).toBeFalsy();
});
});

View File

@@ -122,16 +122,12 @@ export class PeopleWidget extends WidgetComponent implements OnInit {
}
setupMaterialComponents(handler: any): boolean {
// workaround for MDL issues with dynamic components
super.setupMaterialComponents(handler);
if (handler) {
handler.upgradeAllRegistered();
if (this.elementRef && this.value) {
let container = this.elementRef.nativeElement.querySelector('.mdl-textfield');
if (container) {
container.MaterialTextfield.change(this.value);
}
this.setupMaterialTextField(this.elementRef, handler, this.value);
return true;
}
return true;
}
return false;
}

View File

@@ -17,6 +17,7 @@
import { TabsWidget } from './tabs.widget';
import { TabModel } from './../core/tab.model';
import { FormFieldModel } from './../core/form-field.model';
describe('TabsWidget', () => {
@@ -56,4 +57,13 @@ describe('TabsWidget', () => {
expect(widget.setupMaterialComponents()).toBeFalsy();
});
it('should emit tab changed event', (done) => {
let field = new FormFieldModel(null);
widget.formTabChanged.subscribe(tab => {
expect(tab).toBe(field);
done();
});
widget.tabChanged(field);
});
});

View File

@@ -0,0 +1,32 @@
/*!
* @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 { TextWidget } from './text.widget';
describe('TextWidget', () => {
let widget: TextWidget;
beforeEach(() => {
widget = new TextWidget(null);
});
it('should exist', () => {
expect(widget).toBeDefined();
});
});

View File

@@ -27,17 +27,12 @@ export abstract class TextFieldWidgetComponent extends WidgetComponent {
this.elementRef = elementRef;
}
// Overrides base implementation
setupMaterialComponents(handler: any): boolean {
super.setupMaterialComponents(handler);
// workaround for MDL issues with dynamic components
if (handler) {
handler.upgradeAllRegistered();
if (this.elementRef && this.hasValue()) {
let el = this.elementRef.nativeElement;
let container = el.querySelector('.mdl-textfield');
if (container) {
container.MaterialTextfield.change(this.field.value.toString());
}
super.setupMaterialTextField(this.elementRef, handler, this.field.value.toString());
}
return true;
}

View File

@@ -15,7 +15,7 @@
* limitations under the License.
*/
import { Input, AfterViewInit, Output, EventEmitter } from '@angular/core';
import { Input, AfterViewInit, Output, EventEmitter, ElementRef } from '@angular/core';
import { FormFieldModel } from './core/index';
/**
@@ -63,6 +63,18 @@ export class WidgetComponent implements AfterViewInit {
return false;
}
setupMaterialTextField(elementRef: ElementRef, handler: any, value: string) {
if (elementRef && handler) {
let el = elementRef.nativeElement;
if (el) {
let container = el.querySelector('.mdl-textfield');
if (container) {
container.MaterialTextfield.change(value);
}
}
}
}
checkVisibility(field: FormFieldModel) {
this.fieldChanged.emit(field);
}