Dropdown widget tests

This commit is contained in:
Denys Vuika
2016-10-12 12:52:28 +01:00
parent 24e0dd6add
commit 39178f1788

View File

@@ -20,6 +20,7 @@ import { FormService } from '../../../services/form.service';
import { DropdownWidget } from './dropdown.widget';
import { FormModel } from './../core/form.model';
import { FormFieldModel } from './../core/form-field.model';
import { FormFieldOption } from './../core/form-field-option';
describe('DropdownWidget', () => {
@@ -32,6 +33,18 @@ describe('DropdownWidget', () => {
widget.field = new FormFieldModel(new FormModel());
});
it('should require field with restUrl', () => {
spyOn(formService, 'getRestFieldValues').and.stub();
widget.field = null;
widget.ngOnInit();
expect(formService.getRestFieldValues).not.toHaveBeenCalled();
widget.field = new FormFieldModel(null, { restUrl: null });
widget.ngOnInit();
expect(formService.getRestFieldValues).not.toHaveBeenCalled();
});
it('should request field values from service', () => {
const taskId = '<form-id>';
const fieldId = '<field-id>';
@@ -58,4 +71,27 @@ describe('DropdownWidget', () => {
widget.handleError('Err');
expect(console.error).toHaveBeenCalledWith('Err');
});
it('should preserve empty option when loading fields', () => {
let restFieldValue: FormFieldOption = <FormFieldOption> { id: '1', name: 'Option1' };
spyOn(formService, 'getRestFieldValues').and.returnValue(Observable.create(observer => {
observer.next([restFieldValue]);
observer.complete();
}));
let form = new FormModel({ taskId: '<id>' });
let emptyOption: FormFieldOption = <FormFieldOption> { id: 'empty', name: 'Empty' };
widget.field = new FormFieldModel(form, {
id: '<id>',
restUrl: '/some/url/address',
hasEmptyValue: true,
options: [emptyOption]
});
widget.ngOnInit();
expect(formService.getRestFieldValues).toHaveBeenCalled();
expect(widget.field.options.length).toBe(2);
expect(widget.field.options[0]).toBe(emptyOption);
expect(widget.field.options[1]).toBe(restFieldValue);
});
});