From ccd41ba6e13ea4fcac29a00c6991fd27357c74e2 Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Wed, 12 Oct 2016 14:24:21 +0100 Subject: [PATCH] Additional unit tests for form widgets --- .../functional-group.widget.spec.ts | 46 ++++++++++++++- .../functional-group.widget.ts | 10 +--- .../multiline-text.widget.spec.ts | 31 ++++++++++ .../widgets/number/number.widget.spec.ts | 31 ++++++++++ .../widgets/people/people.widget.spec.ts | 56 ++++++++++++++++++- .../widgets/people/people.widget.ts | 10 +--- .../widgets/tabs/tabs.widget.spec.ts | 10 ++++ .../widgets/text/text.widget.spec.ts | 32 +++++++++++ .../widgets/textfield-widget.component.ts | 9 +-- .../components/widgets/widget.component.ts | 14 ++++- 10 files changed, 223 insertions(+), 26 deletions(-) create mode 100644 ng2-components/ng2-activiti-form/src/components/widgets/multiline-text/multiline-text.widget.spec.ts create mode 100644 ng2-components/ng2-activiti-form/src/components/widgets/number/number.widget.spec.ts create mode 100644 ng2-components/ng2-activiti-form/src/components/widgets/text/text.widget.spec.ts diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/functional-group/functional-group.widget.spec.ts b/ng2-components/ng2-activiti-form/src/components/widgets/functional-group/functional-group.widget.spec.ts index 3f054e9e91..e314372f74 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/functional-group/functional-group.widget.spec.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/functional-group/functional-group.widget.spec.ts @@ -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 = ''; + 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 = ''; + expect(w.setupMaterialComponents(componentHandler)).toBeFalsy(); + + w = new FunctionalGroupWidget(formService, elementRef); + w.value = ''; + expect(w.setupMaterialComponents(componentHandler)).toBeTruthy(); + }); + + it('should require value to setup textfield', () => { + widget.value = ''; + expect(widget.setupMaterialComponents(componentHandler)).toBeTruthy(); + + widget.value = null; + expect(widget.setupMaterialComponents(componentHandler)).toBeFalsy(); + }); }); diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/functional-group/functional-group.widget.ts b/ng2-components/ng2-activiti-form/src/components/widgets/functional-group/functional-group.widget.ts index 0c9380b231..e44e3cf185 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/functional-group/functional-group.widget.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/functional-group/functional-group.widget.ts @@ -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; } diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/multiline-text/multiline-text.widget.spec.ts b/ng2-components/ng2-activiti-form/src/components/widgets/multiline-text/multiline-text.widget.spec.ts new file mode 100644 index 0000000000..54f18a3db2 --- /dev/null +++ b/ng2-components/ng2-activiti-form/src/components/widgets/multiline-text/multiline-text.widget.spec.ts @@ -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(); + }); +}); diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/number/number.widget.spec.ts b/ng2-components/ng2-activiti-form/src/components/widgets/number/number.widget.spec.ts new file mode 100644 index 0000000000..c43e209c61 --- /dev/null +++ b/ng2-components/ng2-activiti-form/src/components/widgets/number/number.widget.spec.ts @@ -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(); + }); +}); diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/people/people.widget.spec.ts b/ng2-components/ng2-activiti-form/src/components/widgets/people/people.widget.spec.ts index 5ca78d6b2a..0d120a45d5 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/people/people.widget.spec.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/people/people.widget.spec.ts @@ -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 = ''; + 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 = ''; + expect(w.setupMaterialComponents(componentHandler)).toBeFalsy(); + + w = new PeopleWidget(formService, elementRef); + w.value = ''; + expect(w.setupMaterialComponents(componentHandler)).toBeTruthy(); + }); + + it('should require value to setup textfield', () => { + widget.value = ''; + expect(widget.setupMaterialComponents(componentHandler)).toBeTruthy(); + + widget.value = null; + expect(widget.setupMaterialComponents(componentHandler)).toBeFalsy(); + }); }); diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/people/people.widget.ts b/ng2-components/ng2-activiti-form/src/components/widgets/people/people.widget.ts index c489a76269..ec0bf04db7 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/people/people.widget.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/people/people.widget.ts @@ -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; } diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/tabs/tabs.widget.spec.ts b/ng2-components/ng2-activiti-form/src/components/widgets/tabs/tabs.widget.spec.ts index f512a76fdc..509e47dc63 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/tabs/tabs.widget.spec.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/tabs/tabs.widget.spec.ts @@ -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); + }); + }); diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/text/text.widget.spec.ts b/ng2-components/ng2-activiti-form/src/components/widgets/text/text.widget.spec.ts new file mode 100644 index 0000000000..9248d824f3 --- /dev/null +++ b/ng2-components/ng2-activiti-form/src/components/widgets/text/text.widget.spec.ts @@ -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(); + }); + +}); 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 03f9e09fc9..12e8325c5d 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 @@ -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; } 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 6ba09fc9cf..4e57d890f8 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 @@ -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); }