Refactoring and fix unit test

This commit is contained in:
mauriziovitale84
2016-09-22 17:06:08 +01:00
parent 1dffc2d100
commit 63a7b27eed
3 changed files with 104 additions and 26 deletions

View File

@@ -77,30 +77,40 @@ export class ActivitiProcessFilters implements OnInit, OnChanges {
this.filters.push(filter); this.filters.push(filter);
}); });
this.load(); this.getFilters(this.appId, this.appName);
} }
ngOnChanges(changes: SimpleChanges) { ngOnChanges(changes: SimpleChanges) {
let appId = changes['appId']; let appId = changes['appId'];
if (appId && (appId.currentValue || appId.currentValue === null)) { if (appId && (appId.currentValue || appId.currentValue === null)) {
this.load(); this.getFiltersByAppId(appId.currentValue);
return;
}
let appName = changes['appName'];
if (appName && appName.currentValue) {
this.getFiltersByAppName(appName.currentValue);
return; return;
} }
} }
/** /**
* The method call the adapter data table component for render the task list * Return the task list filtered by appId or by appName
* @param tasks * @param appId
* @param appName
*/ */
private load() { getFilters(appId?: string, appName?: string) {
if (this.appName) { if (appName) {
this.filterByAppName(); this.getFiltersByAppName(appName);
} else { } else {
this.filterByAppId(this.appId); this.getFiltersByAppId(appId);
} }
} }
private filterByAppId(appId) { /**
* Return the filter list filtered by appId
* @param appId - optional
*/
getFiltersByAppId(appId?: string) {
this.activiti.getProcessFilters(appId).subscribe( this.activiti.getProcessFilters(appId).subscribe(
(res: FilterRepresentationModel[]) => { (res: FilterRepresentationModel[]) => {
this.resetFilter(); this.resetFilter();
@@ -117,10 +127,14 @@ export class ActivitiProcessFilters implements OnInit, OnChanges {
); );
} }
private filterByAppName() { /**
this.activiti.getDeployedApplications(this.appName).subscribe( * Return the filter list filtered by appName
* @param appName
*/
getFiltersByAppName(appName: string) {
this.activiti.getDeployedApplications(appName).subscribe(
application => { application => {
this.filterByAppId(application.id); this.getFiltersByAppId(application.id);
this.selectFirstFilter(); this.selectFirstFilter();
}, },
(err) => { (err) => {

View File

@@ -21,7 +21,7 @@ import {
expect, expect,
beforeEach beforeEach
} from '@angular/core/testing'; } from '@angular/core/testing';
import { SimpleChange } from '@angular/core';
import { ActivitiFilters } from './activiti-filters.component'; import { ActivitiFilters } from './activiti-filters.component';
import { ActivitiTaskListService } from '../services/activiti-tasklist.service'; import { ActivitiTaskListService } from '../services/activiti-tasklist.service';
import { Observable } from 'rxjs/Rx'; import { Observable } from 'rxjs/Rx';
@@ -89,6 +89,7 @@ describe('ActivitiFilters', () => {
}); });
it('should emit an error with a bad response', (done) => { it('should emit an error with a bad response', (done) => {
filterList.appId = '1';
spyOn(filterList.activiti, 'getTaskListFilters').and.returnValue(Observable.fromPromise(fakeErrorFilterPromise)); spyOn(filterList.activiti, 'getTaskListFilters').and.returnValue(Observable.fromPromise(fakeErrorFilterPromise));
filterList.onError.subscribe((err) => { filterList.onError.subscribe((err) => {
@@ -99,6 +100,18 @@ describe('ActivitiFilters', () => {
filterList.ngOnInit(); filterList.ngOnInit();
}); });
it('should emit an error with a bad response', (done) => {
filterList.appName = 'fake-app';
spyOn(filterList.activiti, 'getDeployedApplications').and.returnValue(Observable.fromPromise(fakeErrorFilterPromise));
filterList.onError.subscribe((err) => {
expect(err).toBeDefined();
done();
});
filterList.ngOnInit();
});
it('should emit an event when a filter is selected', (done) => { it('should emit an event when a filter is selected', (done) => {
let currentFilter = new FilterRepresentationModel({filter: { state: 'open', assignment: 'fake-involved'}}); let currentFilter = new FilterRepresentationModel({filter: { state: 'open', assignment: 'fake-involved'}});
@@ -112,4 +125,41 @@ describe('ActivitiFilters', () => {
filterList.selectFilter(currentFilter); filterList.selectFilter(currentFilter);
}); });
it('should reload filters by appId on binding changes', () => {
spyOn(filterList, 'getFiltersByAppId').and.stub();
const appId = '1';
let change = new SimpleChange(null, appId);
filterList.ngOnChanges({ 'appId': change });
expect(filterList.getFiltersByAppId).toHaveBeenCalledWith(appId);
});
it('should reload filters by appId null on binding changes', () => {
spyOn(filterList, 'getFiltersByAppId').and.stub();
const appId = null;
let change = new SimpleChange(null, appId);
filterList.ngOnChanges({ 'appId': change });
expect(filterList.getFiltersByAppId).toHaveBeenCalledWith(appId);
});
it('should reload filters by app name on binding changes', () => {
spyOn(filterList, 'getFiltersByAppName').and.stub();
const appName = 'fake-app-name';
let change = new SimpleChange(null, appName);
filterList.ngOnChanges({ 'appName': change });
expect(filterList.getFiltersByAppName).toHaveBeenCalledWith(appName);
});
it('should return the current filter after one is selected', () => {
let filter = new FilterRepresentationModel({name: 'FakeMyTasks', filter: { state: 'open', assignment: 'fake-assignee'}});
expect(filterList.currentFilter).toBeUndefined();
filterList.selectFilter(filter);
expect(filterList.getCurrentFilter()).toBe(filter);
});
}); });

View File

@@ -77,37 +77,47 @@ export class ActivitiFilters implements OnInit, OnChanges {
this.filters.push(filter); this.filters.push(filter);
}); });
this.load(); this.getFilters(this.appId, this.appName);
} }
ngOnChanges(changes: SimpleChanges) { ngOnChanges(changes: SimpleChanges) {
let appId = changes['appId']; let appId = changes['appId'];
if (appId && (appId.currentValue || appId.currentValue === null)) { if (appId && (appId.currentValue || appId.currentValue === null)) {
this.load(); this.getFiltersByAppId(appId.currentValue);
return;
}
let appName = changes['appName'];
if (appName && appName.currentValue) {
this.getFiltersByAppName(appName.currentValue);
return; return;
} }
} }
/** /**
* The method call the adapter data table component for render the task list * Return the task list filtered by appId or by appName
* @param tasks * @param appId
* @param appName
*/ */
private load() { getFilters(appId?: string, appName?: string) {
if (this.appName) { if (appName) {
this.filterByAppName(); this.getFiltersByAppName(appName);
} else { } else {
this.filterByAppId(this.appId); this.getFiltersByAppId(appId);
} }
} }
private filterByAppId(appId) { /**
* Return the filter list filtered by appId
* @param appId - optional
*/
getFiltersByAppId(appId?: string) {
this.activiti.getTaskListFilters(appId).subscribe( this.activiti.getTaskListFilters(appId).subscribe(
(res: FilterRepresentationModel[]) => { (res: FilterRepresentationModel[]) => {
this.resetFilter(); this.resetFilter();
res.forEach((filter) => { res.forEach((filter) => {
this.filterObserver.next(filter); this.filterObserver.next(filter);
this.selectFirstFilter();
}); });
this.selectFirstFilter();
this.onSuccess.emit(res); this.onSuccess.emit(res);
}, },
(err) => { (err) => {
@@ -117,10 +127,14 @@ export class ActivitiFilters implements OnInit, OnChanges {
); );
} }
private filterByAppName() { /**
this.activiti.getDeployedApplications(this.appName).subscribe( * Return the filter list filtered by appName
* @param appName
*/
getFiltersByAppName(appName: string) {
this.activiti.getDeployedApplications(appName).subscribe(
application => { application => {
this.filterByAppId(application.id); this.getFiltersByAppId(application.id);
this.selectFirstFilter(); this.selectFirstFilter();
}, },
(err) => { (err) => {