#689 greatly reduced dropdown code

removed custom http data requests with the corresponding activiti REST
api
This commit is contained in:
Denys Vuika 2016-09-06 14:17:12 +01:00
parent 5676b2e289
commit c1aa70169e
2 changed files with 32 additions and 211 deletions

View File

@ -16,197 +16,46 @@
*/
import { it, describe, expect, beforeEach } from '@angular/core/testing';
import { Http, RequestOptionsArgs, Response, ResponseOptions } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { FormService } from '../../../services/form.service';
import { DropdownWidget } from './dropdown.widget';
import { FormModel } from './../core/form.model';
import { FormFieldModel } from './../core/form-field.model';
describe('DropdownWidget', () => {
let http: Http;
let formService: FormService;
let widget: DropdownWidget;
beforeEach(() => {
http = <Http> {
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
return null;
}
};
widget = new DropdownWidget(http);
});
it('should fetch and parse REST data on init', () => {
let data = [
{ uid: '1', text: 'One' },
{ uid: '2', text: 'Two' }
];
spyOn(http, 'get').and.callFake((url) => {
return Observable.create(observer => {
let options = new ResponseOptions({
body: data,
url: url
});
let response = new Response(options);
observer.next(response);
observer.complete();
});
});
let field = new FormFieldModel(new FormModel(), {
optionType: 'rest',
restUrl: 'http://<address>',
restIdProperty: 'uid',
restLabelProperty: 'text'
});
widget.field = field;
widget.ngOnInit();
expect((<any>http.get).calls.argsFor(0)).toEqual([field.restUrl]);
expect(field.options.length).toBe(2);
expect(field.options[0].id).toBe(data[0].uid);
expect(field.options[0].name).toBe(data[0].text);
expect(field.options[1].id).toBe(data[1].uid);
expect(field.options[1].name).toBe(data[1].text);
});
it('should require REST settings to fetch data', () => {
let form = new FormModel();
spyOn(http, 'get').and.stub();
// 1) Null field
widget.field = null;
widget.ngOnInit();
expect(http.get).not.toHaveBeenCalled();
// 2) Missing [optionType]
widget.field = new FormFieldModel(form, {
optionType: null,
restUrl: 'http://<address>',
restIdProperty: 'uid',
restLabelProperty: 'text'
});
widget.ngOnInit();
expect(http.get).not.toHaveBeenCalled();
// 3) Missing [restUrl]
widget.field = new FormFieldModel(form, {
optionType: 'rest',
restUrl: null,
restIdProperty: 'uid',
restLabelProperty: 'text'
});
widget.ngOnInit();
expect(http.get).not.toHaveBeenCalled();
// 4) Missing [restIdProperty]
widget.field = new FormFieldModel(form, {
optionType: 'rest',
restUrl: 'http://<address>',
restIdProperty: null,
restLabelProperty: 'text'
});
widget.ngOnInit();
expect(http.get).not.toHaveBeenCalled();
// 4) Missing [restLabelProperty]
widget.field = new FormFieldModel(form, {
optionType: 'rest',
restUrl: 'http://<address>',
restIdProperty: null,
restLabelProperty: null
});
widget.ngOnInit();
expect(http.get).not.toHaveBeenCalled();
});
it('should parse only array response', () => {
expect(widget.loadFromJson([])).toBeFalsy();
formService = new FormService(null, null);
widget = new DropdownWidget(formService);
widget.field = new FormFieldModel(new FormModel());
expect(widget.loadFromJson([])).toBeTruthy();
expect(widget.loadFromJson(null)).toBeFalsy();
expect(widget.loadFromJson({})).toBeFalsy();
});
it('should bind to nested properties', () => {
let data = [
{ uid: { value: 1 }, name: { fullName: 'John Doe' } }
];
it('should request field values from service', () => {
const taskId = '<form-id>';
const fieldId = '<field-id>';
spyOn(http, 'get').and.callFake((url) => {
return Observable.create(observer => {
let options = new ResponseOptions({
body: data,
url: url
});
let response = new Response(options);
observer.next(response);
observer.complete();
});
let form = new FormModel({
taskId: taskId
});
let field = new FormFieldModel(new FormModel(), {
optionType: 'rest',
restUrl: 'http://<address>',
restIdProperty: 'uid.value',
restLabelProperty: 'name.fullName'
widget.field = new FormFieldModel(form, {
id: fieldId
});
widget.field = field;
spyOn(formService, 'getRestFieldValues').and.returnValue(Observable.create(observer => {
observer.next(null);
observer.complete();
}));
widget.ngOnInit();
expect(field.options.length).toBe(1);
expect(field.options[0].id).toBe(data[0].uid.value.toString());
expect(field.options[0].name).toBe(data[0].name.fullName);
expect(formService.getRestFieldValues).toHaveBeenCalledWith(taskId, fieldId);
});
it('should update form upon loading REST data', () => {
let field = new FormFieldModel(new FormModel());
widget.field = field;
spyOn(field, 'updateForm').and.stub();
expect(widget.loadFromJson([])).toBeTruthy();
expect(field.updateForm).toHaveBeenCalled();
});
it('should handle error with generic message', () => {
it('should log error to console by default', () => {
spyOn(console, 'error').and.stub();
widget.handleError(null);
expect(console.error).toHaveBeenCalledWith(DropdownWidget.UNKNOWN_ERROR_MESSAGE);
widget.handleError('Err');
expect(console.error).toHaveBeenCalledWith('Err');
});
it('should handle error with error message', () => {
spyOn(console, 'error').and.stub();
const message = '<error>';
widget.handleError({ message: message });
expect(console.error).toHaveBeenCalledWith(message);
});
it('should handle error with detailed message', () => {
spyOn(console, 'error').and.stub();
widget.handleError({
status: '400',
statusText: 'Bad request'
});
expect(console.error).toHaveBeenCalledWith('400 - Bad request');
});
it('should handle error with generic message', () => {
spyOn(console, 'error').and.stub();
widget.handleError({});
expect(console.error).toHaveBeenCalledWith(DropdownWidget.GENERIC_ERROR_MESSAGE);
});
});

View File

@ -16,9 +16,9 @@
*/
import { Component, OnInit } from '@angular/core';
import { Http } from '@angular/http';
import { ObjectUtils } from 'ng2-alfresco-core';
import { FormService } from '../../../services/form.service';
import { WidgetComponent } from './../widget.component';
import { FormFieldOption } from './../core/form-field-option';
declare let __moduleName: string;
declare var componentHandler;
@ -31,55 +31,27 @@ declare var componentHandler;
})
export class DropdownWidget extends WidgetComponent implements OnInit {
static UNKNOWN_ERROR_MESSAGE: string = 'Unknown error';
static GENERIC_ERROR_MESSAGE: string = 'Server error';
constructor(private http: Http) {
constructor(private formService: FormService) {
super();
}
ngOnInit() {
if (this.field &&
this.field.optionType === 'rest' &&
this.field.restUrl &&
this.field.restIdProperty &&
this.field.restLabelProperty) {
let url = `${this.field.restUrl}`;
this.http.get(url).subscribe(
response => {
let json: any = response.json();
this.loadFromJson(json);
this.formService
.getRestFieldValues(
this.field.form.taskId,
this.field.id
)
.subscribe(
(result: FormFieldOption[]) => {
this.field.options = result || [];
this.field.updateForm();
},
this.handleError
);
}
}
// TODO: support 'restResponsePath'
loadFromJson(json: any): boolean {
if (this.field && json && json instanceof Array) {
let options = json.map(obj => {
return {
id: ObjectUtils.getValue(obj, this.field.restIdProperty).toString(),
name: ObjectUtils.getValue(obj, this.field.restLabelProperty).toString()
};
});
this.field.options = options;
this.field.updateForm();
return true;
}
return false;
}
handleError(error: any) {
let errMsg = DropdownWidget.UNKNOWN_ERROR_MESSAGE;
if (error) {
errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : DropdownWidget.GENERIC_ERROR_MESSAGE;
}
console.error(errMsg);
console.error(error);
}
}