From 38a04d24fc879bd22744403bbaff0b9302764086 Mon Sep 17 00:00:00 2001 From: Vito Date: Fri, 19 May 2017 09:56:35 -0700 Subject: [PATCH] [ADF-647] Fix retrieving value for dropdown (#1888) * [ADF-647] Fix retrieving value for dropdown --- .../widgets/core/form-field.model.spec.ts | 24 ++++++ .../widgets/core/form-field.model.ts | 8 +- .../display-value.widget.spec.ts | 80 +++++++++++++++---- .../display-value/display-value.widget.ts | 3 +- 4 files changed, 94 insertions(+), 21 deletions(-) diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/core/form-field.model.spec.ts b/ng2-components/ng2-activiti-form/src/components/widgets/core/form-field.model.spec.ts index 45b47162a5..0a9fbaf1a4 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/core/form-field.model.spec.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/core/form-field.model.spec.ts @@ -332,4 +332,28 @@ describe('FormFieldModel', () => { }); }); + it('should be able to check if the field has options available', () =>{ + let form = new FormModel(); + let field = new FormFieldModel(form, { + id: 'dropdown-happy', + type: FormFieldTypes.DROPDOWN, + options: [ + { id: 'opt1', name: 'Option 1' }, + { id: 'opt2', name: 'Option 2' } + ] + }); + + expect(field.hasOptions()).toBeTruthy(); + }); + + it('should return false if field has no options', () =>{ + let form = new FormModel(); + let field = new FormFieldModel(form, { + id: 'dropdown-sad', + type: FormFieldTypes.DROPDOWN + }); + + expect(field.hasOptions()).toBeFalsy(); + }); + }); diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/core/form-field.model.ts b/ng2-components/ng2-activiti-form/src/components/widgets/core/form-field.model.ts index 6a20425aee..7b55bef94a 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/core/form-field.model.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/core/form-field.model.ts @@ -244,8 +244,8 @@ export class FormFieldModel extends FormWidgetModel { } /* - This is needed due to Activiti displaying/editing dates in d-M-YYYY format - but storing on server in ISO8601 format (i.e. 2013-02-04T22:44:30.652Z) + This is needed due to Activiti displaying/editing dates in d-M-YYYY format + but storing on server in ISO8601 format (i.e. 2013-02-04T22:44:30.652Z) */ if (json.type === FormFieldTypes.DATE) { if (value) { @@ -355,4 +355,8 @@ export class FormFieldModel extends FormWidgetModel { let option: FormFieldOption = this.options.find(opt => opt.id === this.value); return option ? option.name : null; } + + hasOptions() { + return this.options && this.options.length > 0; + } } diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/display-value/display-value.widget.spec.ts b/ng2-components/ng2-activiti-form/src/components/widgets/display-value/display-value.widget.spec.ts index 17af6953c6..cefe81373f 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/display-value/display-value.widget.spec.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/display-value/display-value.widget.spec.ts @@ -50,7 +50,7 @@ describe('DisplayValueWidget', () => { it('should take field value on init', () => { let value = ''; - widget.field = new FormFieldModel(null, {value: value}); + widget.field = new FormFieldModel(null, { value: value }); widget.field.params = null; widget.ngOnInit(); expect(widget.value).toBe(value); @@ -150,7 +150,7 @@ describe('DisplayValueWidget', () => { widget.field = new FormFieldModel(null, { type: FormFieldTypes.DISPLAY_VALUE, value: [ - {name: 'file1'} + { name: 'file1' } ], params: { field: { @@ -238,8 +238,8 @@ describe('DisplayValueWidget', () => { restUrl: null, value: '2', options: [ - {id: '1', name: 'option 1'}, - {id: '2', name: 'option 2'} + { id: '1', name: 'option 1' }, + { id: '2', name: 'option 2' } ], params: { field: { @@ -257,8 +257,8 @@ describe('DisplayValueWidget', () => { restUrl: null, value: '100', options: [ - {id: '1', name: 'option 1'}, - {id: '2', name: 'option 2'} + { id: '1', name: 'option 1' }, + { id: '2', name: 'option 2' } ], params: { field: { @@ -306,22 +306,22 @@ describe('DisplayValueWidget', () => { spyOn(formService, 'getRestFieldValues').and.returnValue( Observable.create(observer => { observer.next([ - {id: '1', name: 'option 1'}, - {id: '2', name: 'option 2'} + { id: '1', name: 'option 1' }, + { id: '2', name: 'option 2' } ]); observer.complete(); }) ); - let form = new FormModel({taskId: ''}); + let form = new FormModel({ taskId: '' }); widget.field = new FormFieldModel(form, { type: FormFieldTypes.DISPLAY_VALUE, restUrl: '', value: '2', options: [ - {id: '1', name: 'option 1'}, - {id: '2', name: 'option 2'} + { id: '1', name: 'option 1' }, + { id: '2', name: 'option 2' } ], params: { field: { @@ -338,14 +338,14 @@ describe('DisplayValueWidget', () => { spyOn(formService, 'getRestFieldValues').and.returnValue( Observable.create(observer => { observer.next([ - {id: '1', name: 'option 1'}, - {id: '2', name: 'option 2'} + { id: '1', name: 'option 1' }, + { id: '2', name: 'option 2' } ]); observer.complete(); }) ); - let form = new FormModel({taskId: ''}); + let form = new FormModel({ taskId: '' }); widget.field = new FormFieldModel(form, { type: FormFieldTypes.DISPLAY_VALUE, @@ -370,7 +370,7 @@ describe('DisplayValueWidget', () => { }) ); - let form = new FormModel({taskId: ''}); + let form = new FormModel({ taskId: '' }); widget.field = new FormFieldModel(form, { type: FormFieldTypes.DISPLAY_VALUE, @@ -393,7 +393,7 @@ describe('DisplayValueWidget', () => { Observable.throw(error) ); - let form = new FormModel({taskId: ''}); + let form = new FormModel({ taskId: '' }); widget.field = new FormFieldModel(form, { type: FormFieldTypes.DISPLAY_VALUE, @@ -628,7 +628,7 @@ describe('DisplayValueWidget', () => { FormService, WidgetVisibilityService ], - schemas: [ CUSTOM_ELEMENTS_SCHEMA ] + schemas: [CUSTOM_ELEMENTS_SCHEMA] }).compileComponents().then(() => { fixture = TestBed.createComponent(DisplayValueWidget); widgetUI = fixture.componentInstance; @@ -686,5 +686,51 @@ describe('DisplayValueWidget', () => { expect(elWidget.checked).toBeFalsy(); }); })); + + it('should show the dropdown value taken from options when field has options', async(() => { + widgetUI.field = new FormFieldModel(null, { + id: 'fake-dropdown-id', + type: FormFieldTypes.DISPLAY_VALUE, + value: '1', + options: [ + { id: '1', name: 'Option 1' }, + { id: '2', name: 'Option 2' } + ], + params: { + field: { + type: FormFieldTypes.DROPDOWN + } + } + }); + + fixture.whenStable() + .then(() => { + fixture.detectChanges(); + let elWidget: any = element.querySelector('#fake-dropdown-id'); + expect(elWidget).toBeDefined(); + expect(elWidget.value).toBe('Option 1'); + }); + })); + + it('should show the dropdown value taken from value when field has no options', async(() => { + widgetUI.field = new FormFieldModel(null, { + id: 'fake-dropdown-id', + type: FormFieldTypes.DISPLAY_VALUE, + value: 'FAKE', + params: { + field: { + type: FormFieldTypes.DROPDOWN + } + } + }); + + fixture.whenStable() + .then(() => { + fixture.detectChanges(); + let elWidget: any = element.querySelector('#fake-dropdown-id'); + expect(elWidget).toBeDefined(); + expect(elWidget.value).toBe('FAKE'); + }); + })); }); }); diff --git a/ng2-components/ng2-activiti-form/src/components/widgets/display-value/display-value.widget.ts b/ng2-components/ng2-activiti-form/src/components/widgets/display-value/display-value.widget.ts index c60846d69d..4251972a2a 100644 --- a/ng2-components/ng2-activiti-form/src/components/widgets/display-value/display-value.widget.ts +++ b/ng2-components/ng2-activiti-form/src/components/widgets/display-value/display-value.widget.ts @@ -99,7 +99,6 @@ export class DisplayValueWidget extends WidgetComponent implements OnInit { } break; case FormFieldTypes.DOCUMENT: - console.log('document'); const file = this.field.value; if (file) { this.value = decodeURI(file.name); @@ -117,7 +116,7 @@ export class DisplayValueWidget extends WidgetComponent implements OnInit { if (this.field.restUrl) { this.loadRestFieldValue(); } else { - this.value = this.field.getOptionName(); + this.value = this.field.hasOptions() ? this.field.getOptionName() : this.value; } break; case FormFieldTypes.RADIO_BUTTONS: