From 3d3b156194dc4ad5e6ad6567b6f74ae076e195c1 Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Thu, 13 Oct 2016 17:15:17 +0100 Subject: [PATCH] Unit tests for base widget component --- .../widgets/textfield-widget.component.ts | 3 +- .../widgets/widget.component.spec.ts | 50 +++++++++++++++++++ .../components/widgets/widget.component.ts | 6 ++- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/textfield-widget.component.ts b/ng2-components/ng2-activiti-form/src/components/widgets/textfield-widget.component.ts index f14ecaca81..5037574e62 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/textfield-widget.component.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/textfield-widget.component.ts @@ -32,8 +32,7 @@ export abstract class TextFieldWidgetComponent extends WidgetComponent { // workaround for MDL issues with dynamic components if (handler) { if (this.elementRef && this.hasValue()) { - this.setupMaterialTextField(this.elementRef, handler, this.field.value.toString()); - return true; + return this.setupMaterialTextField(this.elementRef, handler, this.field.value.toString()); } } return false; diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/widget.component.spec.ts b/ng2-components/ng2-activiti-form/src/components/widgets/widget.component.spec.ts index 69ce18b6f1..0b2046438d 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/widget.component.spec.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/widget.component.spec.ts @@ -15,6 +15,7 @@ * limitations under the License. */ +import { ElementRef } from '@angular/core'; import { WidgetComponent } from './widget.component'; import { FormFieldModel } from './core/form-field.model'; import { FormModel } from './core/form.model'; @@ -80,4 +81,53 @@ describe('WidgetComponent', () => { 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(); + }); }); diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/widget.component.ts b/ng2-components/ng2-activiti-form/src/components/widgets/widget.component.ts index 5e72afa2ef..b3d6f4e54d 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/widget.component.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/widget.component.ts @@ -36,6 +36,8 @@ export class WidgetComponent implements AfterViewInit { return this.field ? true : false; } + // Note for developers: + // returns object to be able binding it to the attribute isRequired(): any { if (this.field && this.field.required) { return true; @@ -63,16 +65,18 @@ export class WidgetComponent implements AfterViewInit { return false; } - setupMaterialTextField(elementRef: ElementRef, handler: any, value: string) { + setupMaterialTextField(elementRef: ElementRef, handler: any, value: string): boolean { if (elementRef && handler) { let el = elementRef.nativeElement; if (el) { let container = el.querySelector('.mdl-textfield'); if (container) { container.MaterialTextfield.change(value); + return true; } } } + return false; } checkVisibility(field: FormFieldModel) {