Unit tests for base widget component

This commit is contained in:
Denys Vuika 2016-10-13 17:15:17 +01:00
parent 16231791c4
commit 3d3b156194
3 changed files with 56 additions and 3 deletions

View File

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

View File

@ -15,6 +15,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { ElementRef } from '@angular/core';
import { WidgetComponent } from './widget.component'; import { WidgetComponent } from './widget.component';
import { FormFieldModel } from './core/form-field.model'; import { FormFieldModel } from './core/form-field.model';
import { FormModel } from './core/form.model'; import { FormModel } from './core/form.model';
@ -80,4 +81,53 @@ describe('WidgetComponent', () => {
component.checkVisibility(fakeField); component.checkVisibility(fakeField);
}); });
it('should eval isRequired state of the field', () => {
let widget = new WidgetComponent();
expect(widget.isRequired()).toBeFalsy();
widget.field = new FormFieldModel(null);
expect(widget.isRequired()).toBeFalsy();
widget.field = new FormFieldModel(null, { required: false });
expect(widget.isRequired()).toBeFalsy();
widget.field = new FormFieldModel(null, { required: true });
expect(widget.isRequired()).toBeTruthy();
});
it('should require element reference to setup textfield', () => {
let widget = new WidgetComponent();
expect(widget.setupMaterialTextField(null, {}, 'value')).toBeFalsy();
});
it('should require component handler to setup textfield', () => {
let elementRef = new ElementRef(null);
let widget = new WidgetComponent();
expect(widget.setupMaterialTextField(elementRef, null, 'value')).toBeFalsy();
});
it('should require field value to setup textfield', () => {
let elementRef = new ElementRef(null);
let widget = new WidgetComponent();
expect(widget.setupMaterialTextField(elementRef, {}, null)).toBeFalsy();
});
it('should setup textfield', () => {
let changeCalled = false;
let elementRef = new ElementRef({
querySelector: function () {
return {
MaterialTextfield: {
change: function() {
changeCalled = true;
}
}
};
}
});
let widget = new WidgetComponent();
expect(widget.setupMaterialTextField(elementRef, {}, 'value')).toBeTruthy();
expect(changeCalled).toBeTruthy();
});
}); });

View File

@ -36,6 +36,8 @@ export class WidgetComponent implements AfterViewInit {
return this.field ? true : false; return this.field ? true : false;
} }
// Note for developers:
// returns <any> object to be able binding it to the <element reguired="required"> attribute
isRequired(): any { isRequired(): any {
if (this.field && this.field.required) { if (this.field && this.field.required) {
return true; return true;
@ -63,16 +65,18 @@ export class WidgetComponent implements AfterViewInit {
return false; return false;
} }
setupMaterialTextField(elementRef: ElementRef, handler: any, value: string) { setupMaterialTextField(elementRef: ElementRef, handler: any, value: string): boolean {
if (elementRef && handler) { if (elementRef && handler) {
let el = elementRef.nativeElement; let el = elementRef.nativeElement;
if (el) { if (el) {
let container = el.querySelector('.mdl-textfield'); let container = el.querySelector('.mdl-textfield');
if (container) { if (container) {
container.MaterialTextfield.change(value); container.MaterialTextfield.change(value);
return true;
} }
} }
} }
return false;
} }
checkVisibility(field: FormFieldModel) { checkVisibility(field: FormFieldModel) {