Text widget unit tests

This commit is contained in:
Denys Vuika 2016-10-13 16:51:55 +01:00
parent 4c66b9ef93
commit 16231791c4
2 changed files with 41 additions and 5 deletions

View File

@ -15,18 +15,54 @@
* limitations under the License.
*/
import { ElementRef } from '@angular/core';
import { TextWidget } from './text.widget';
import { FormFieldModel } from './../core/form-field.model';
import { FormFieldTypes } from '../core/form-field-types';
describe('TextWidget', () => {
let widget: TextWidget;
let elementRef: ElementRef;
let componentHandler;
beforeEach(() => {
widget = new TextWidget(null);
elementRef = new ElementRef(null);
widget = new TextWidget(elementRef);
componentHandler = jasmine.createSpyObj('componentHandler', [
'upgradeAllRegistered'
]);
window['componentHandler'] = componentHandler;
});
it('should exist', () => {
expect(widget).toBeDefined();
it('should upgrade material textfield', () => {
spyOn(widget, 'setupMaterialTextField').and.stub();
widget.field = new FormFieldModel(null, {
type: FormFieldTypes.TEXT,
value: '<text>'
});
widget.ngAfterViewInit();
expect(widget.setupMaterialTextField).toHaveBeenCalled();
});
it('should require mdl component handler to setup textfield', () => {
expect(widget.setupMaterialComponents(null)).toBeFalsy();
});
it('should require element reference to setup textfield', () => {
widget = new TextWidget(null);
expect(widget.setupMaterialComponents(componentHandler)).toBeFalsy();
});
it('should require field value to setup textfield', () => {
widget.field = new FormFieldModel(null, {
type: FormFieldTypes.TEXT,
value: null
});
expect(widget.setupMaterialComponents(componentHandler)).toBeFalsy();
});
});

View File

@ -32,10 +32,10 @@ export abstract class TextFieldWidgetComponent extends WidgetComponent {
// workaround for MDL issues with dynamic components
if (handler) {
if (this.elementRef && this.hasValue()) {
super.setupMaterialTextField(this.elementRef, handler, this.field.value.toString());
}
this.setupMaterialTextField(this.elementRef, handler, this.field.value.toString());
return true;
}
}
return false;
}