[ADF-647] Fix retrieving value for dropdown (#1888)

* [ADF-647] Fix retrieving value for dropdown
This commit is contained in:
Vito
2017-05-19 09:56:35 -07:00
committed by Eugenio Romano
parent e8889a5adb
commit 38a04d24fc
4 changed files with 94 additions and 21 deletions

View File

@@ -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();
});
}); });

View File

@@ -355,4 +355,8 @@ export class FormFieldModel extends FormWidgetModel {
let option: FormFieldOption = this.options.find(opt => opt.id === this.value); let option: FormFieldOption = this.options.find(opt => opt.id === this.value);
return option ? option.name : null; return option ? option.name : null;
} }
hasOptions() {
return this.options && this.options.length > 0;
}
} }

View File

@@ -50,7 +50,7 @@ describe('DisplayValueWidget', () => {
it('should take field value on init', () => { it('should take field value on init', () => {
let value = '<value>'; let value = '<value>';
widget.field = new FormFieldModel(null, {value: value}); widget.field = new FormFieldModel(null, { value: value });
widget.field.params = null; widget.field.params = null;
widget.ngOnInit(); widget.ngOnInit();
expect(widget.value).toBe(value); expect(widget.value).toBe(value);
@@ -150,7 +150,7 @@ describe('DisplayValueWidget', () => {
widget.field = new FormFieldModel(null, { widget.field = new FormFieldModel(null, {
type: FormFieldTypes.DISPLAY_VALUE, type: FormFieldTypes.DISPLAY_VALUE,
value: [ value: [
{name: 'file1'} { name: 'file1' }
], ],
params: { params: {
field: { field: {
@@ -238,8 +238,8 @@ describe('DisplayValueWidget', () => {
restUrl: null, restUrl: null,
value: '2', value: '2',
options: [ options: [
{id: '1', name: 'option 1'}, { id: '1', name: 'option 1' },
{id: '2', name: 'option 2'} { id: '2', name: 'option 2' }
], ],
params: { params: {
field: { field: {
@@ -257,8 +257,8 @@ describe('DisplayValueWidget', () => {
restUrl: null, restUrl: null,
value: '100', value: '100',
options: [ options: [
{id: '1', name: 'option 1'}, { id: '1', name: 'option 1' },
{id: '2', name: 'option 2'} { id: '2', name: 'option 2' }
], ],
params: { params: {
field: { field: {
@@ -306,22 +306,22 @@ describe('DisplayValueWidget', () => {
spyOn(formService, 'getRestFieldValues').and.returnValue( spyOn(formService, 'getRestFieldValues').and.returnValue(
Observable.create(observer => { Observable.create(observer => {
observer.next([ observer.next([
{id: '1', name: 'option 1'}, { id: '1', name: 'option 1' },
{id: '2', name: 'option 2'} { id: '2', name: 'option 2' }
]); ]);
observer.complete(); observer.complete();
}) })
); );
let form = new FormModel({taskId: '<id>'}); let form = new FormModel({ taskId: '<id>' });
widget.field = new FormFieldModel(form, { widget.field = new FormFieldModel(form, {
type: FormFieldTypes.DISPLAY_VALUE, type: FormFieldTypes.DISPLAY_VALUE,
restUrl: '<url>', restUrl: '<url>',
value: '2', value: '2',
options: [ options: [
{id: '1', name: 'option 1'}, { id: '1', name: 'option 1' },
{id: '2', name: 'option 2'} { id: '2', name: 'option 2' }
], ],
params: { params: {
field: { field: {
@@ -338,14 +338,14 @@ describe('DisplayValueWidget', () => {
spyOn(formService, 'getRestFieldValues').and.returnValue( spyOn(formService, 'getRestFieldValues').and.returnValue(
Observable.create(observer => { Observable.create(observer => {
observer.next([ observer.next([
{id: '1', name: 'option 1'}, { id: '1', name: 'option 1' },
{id: '2', name: 'option 2'} { id: '2', name: 'option 2' }
]); ]);
observer.complete(); observer.complete();
}) })
); );
let form = new FormModel({taskId: '<id>'}); let form = new FormModel({ taskId: '<id>' });
widget.field = new FormFieldModel(form, { widget.field = new FormFieldModel(form, {
type: FormFieldTypes.DISPLAY_VALUE, type: FormFieldTypes.DISPLAY_VALUE,
@@ -370,7 +370,7 @@ describe('DisplayValueWidget', () => {
}) })
); );
let form = new FormModel({taskId: '<id>'}); let form = new FormModel({ taskId: '<id>' });
widget.field = new FormFieldModel(form, { widget.field = new FormFieldModel(form, {
type: FormFieldTypes.DISPLAY_VALUE, type: FormFieldTypes.DISPLAY_VALUE,
@@ -393,7 +393,7 @@ describe('DisplayValueWidget', () => {
Observable.throw(error) Observable.throw(error)
); );
let form = new FormModel({taskId: '<id>'}); let form = new FormModel({ taskId: '<id>' });
widget.field = new FormFieldModel(form, { widget.field = new FormFieldModel(form, {
type: FormFieldTypes.DISPLAY_VALUE, type: FormFieldTypes.DISPLAY_VALUE,
@@ -628,7 +628,7 @@ describe('DisplayValueWidget', () => {
FormService, FormService,
WidgetVisibilityService WidgetVisibilityService
], ],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ] schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents().then(() => { }).compileComponents().then(() => {
fixture = TestBed.createComponent(DisplayValueWidget); fixture = TestBed.createComponent(DisplayValueWidget);
widgetUI = fixture.componentInstance; widgetUI = fixture.componentInstance;
@@ -686,5 +686,51 @@ describe('DisplayValueWidget', () => {
expect(elWidget.checked).toBeFalsy(); 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');
});
}));
}); });
}); });

View File

@@ -99,7 +99,6 @@ export class DisplayValueWidget extends WidgetComponent implements OnInit {
} }
break; break;
case FormFieldTypes.DOCUMENT: case FormFieldTypes.DOCUMENT:
console.log('document');
const file = this.field.value; const file = this.field.value;
if (file) { if (file) {
this.value = decodeURI(file.name); this.value = decodeURI(file.name);
@@ -117,7 +116,7 @@ export class DisplayValueWidget extends WidgetComponent implements OnInit {
if (this.field.restUrl) { if (this.field.restUrl) {
this.loadRestFieldValue(); this.loadRestFieldValue();
} else { } else {
this.value = this.field.getOptionName(); this.value = this.field.hasOptions() ? this.field.getOptionName() : this.value;
} }
break; break;
case FormFieldTypes.RADIO_BUTTONS: case FormFieldTypes.RADIO_BUTTONS: