diff --git a/ng2-components/ng2-activiti-processlist/src/components/activiti-filters.component.ts b/ng2-components/ng2-activiti-processlist/src/components/activiti-filters.component.ts index 039c2571b3..46cb8178a6 100644 --- a/ng2-components/ng2-activiti-processlist/src/components/activiti-filters.component.ts +++ b/ng2-components/ng2-activiti-processlist/src/components/activiti-filters.component.ts @@ -77,30 +77,40 @@ export class ActivitiProcessFilters implements OnInit, OnChanges { this.filters.push(filter); }); - this.load(); + this.getFilters(this.appId, this.appName); } ngOnChanges(changes: SimpleChanges) { let appId = changes['appId']; 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; } } /** - * The method call the adapter data table component for render the task list - * @param tasks + * Return the task list filtered by appId or by appName + * @param appId + * @param appName */ - private load() { - if (this.appName) { - this.filterByAppName(); + getFilters(appId?: string, appName?: string) { + if (appName) { + this.getFiltersByAppName(appName); } 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( (res: FilterRepresentationModel[]) => { 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 => { - this.filterByAppId(application.id); + this.getFiltersByAppId(application.id); this.selectFirstFilter(); }, (err) => { diff --git a/ng2-components/ng2-activiti-tasklist/src/components/activiti-filters.component.spec.ts b/ng2-components/ng2-activiti-tasklist/src/components/activiti-filters.component.spec.ts index 04c5783015..4ca2831f56 100644 --- a/ng2-components/ng2-activiti-tasklist/src/components/activiti-filters.component.spec.ts +++ b/ng2-components/ng2-activiti-tasklist/src/components/activiti-filters.component.spec.ts @@ -21,7 +21,7 @@ import { expect, beforeEach } from '@angular/core/testing'; - +import { SimpleChange } from '@angular/core'; import { ActivitiFilters } from './activiti-filters.component'; import { ActivitiTaskListService } from '../services/activiti-tasklist.service'; import { Observable } from 'rxjs/Rx'; @@ -89,6 +89,7 @@ describe('ActivitiFilters', () => { }); it('should emit an error with a bad response', (done) => { + filterList.appId = '1'; spyOn(filterList.activiti, 'getTaskListFilters').and.returnValue(Observable.fromPromise(fakeErrorFilterPromise)); filterList.onError.subscribe((err) => { @@ -99,6 +100,18 @@ describe('ActivitiFilters', () => { 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) => { let currentFilter = new FilterRepresentationModel({filter: { state: 'open', assignment: 'fake-involved'}}); @@ -112,4 +125,41 @@ describe('ActivitiFilters', () => { 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); + }); + }); diff --git a/ng2-components/ng2-activiti-tasklist/src/components/activiti-filters.component.ts b/ng2-components/ng2-activiti-tasklist/src/components/activiti-filters.component.ts index 931e205783..9355ee363d 100644 --- a/ng2-components/ng2-activiti-tasklist/src/components/activiti-filters.component.ts +++ b/ng2-components/ng2-activiti-tasklist/src/components/activiti-filters.component.ts @@ -77,37 +77,47 @@ export class ActivitiFilters implements OnInit, OnChanges { this.filters.push(filter); }); - this.load(); + this.getFilters(this.appId, this.appName); } ngOnChanges(changes: SimpleChanges) { let appId = changes['appId']; 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; } } /** - * The method call the adapter data table component for render the task list - * @param tasks + * Return the task list filtered by appId or by appName + * @param appId + * @param appName */ - private load() { - if (this.appName) { - this.filterByAppName(); + getFilters(appId?: string, appName?: string) { + if (appName) { + this.getFiltersByAppName(appName); } 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( (res: FilterRepresentationModel[]) => { this.resetFilter(); res.forEach((filter) => { this.filterObserver.next(filter); - this.selectFirstFilter(); }); + this.selectFirstFilter(); this.onSuccess.emit(res); }, (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 => { - this.filterByAppId(application.id); + this.getFiltersByAppId(application.id); this.selectFirstFilter(); }, (err) => {