Skip to content

Commit

Permalink
[ACS-5480] Bug Fix for multiple process/task filters (#8757)
Browse files Browse the repository at this point in the history
* added a check before pushing filters

* moved the check inside a method and added unit tests for method

* renamed duplicate variables

* improved unit tests

* linting fixes

* linting fixes

* linting fixes

* removed unnecessary expects

* linting fix and added JS doc

* linting fixes - removed types from doc

* added unit tests for createDefaultFilter scenario

* linting fixes

* moved mock test data to mock data files
  • Loading branch information
SheenaMalhotra182 authored Jul 21, 2023
1 parent 2c93706 commit c8b4083
Show file tree
Hide file tree
Showing 6 changed files with 337 additions and 36 deletions.
52 changes: 52 additions & 0 deletions lib/process-services/src/lib/mock/process/process-filters.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,55 @@ export const fakeProcessFiltersResponse = {
filter: { sort: 'created-desc', name: '', state: 'running' }
})]
};

export const dummyRunningFilter = {
appId: 123,
name: 'Running',
filter: { sort: 'created-desc', name: '', state: 'running' },
icon: 'fa-random',
id: 18,
index: 10,
recent: false,
hasFilter: () => {
return true;
}
};

export const dummyCompletedFilter = {
appId: 123,
name: 'Completed',
filter: { sort: 'created-desc', name: '', state: 'completed' },
icon: 'fa-random',
id: 19,
index: 11,
recent: false,
hasFilter: () => {
return true;
}
};

export const dummyAllFilter = {
appId: 123,
name: 'All',
filter: { sort: 'created-desc', name: '', state: 'all' },
icon: 'fa-random',
id: 20,
index: 12,
recent: false,
hasFilter: () => {
return true;
}
};

export const dummyDuplicateRunningFilter = {
appId: 123,
name: 'Running',
filter: { sort: 'created-desc', name: '', state: 'running' },
icon: 'fa-random',
id: 21,
index: 13,
recent: false,
hasFilter: () => {
return true;
}
};
65 changes: 65 additions & 0 deletions lib/process-services/src/lib/mock/task/task-filters.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,68 @@ export const fakeRepresentationFilter2: FilterRepresentationModel = new FilterRe
dueBefore: null
}
});

export const dummyMyTasksFilter = {
appId: 101,
name: 'My Tasks',
filter: { sort: 'created-desc', name: '', state: 'open', assignment: 'fake-mytasks' },
icon: 'fa-random',
id: 81,
index: 21,
recent: false,
hasFilter: () => {
return true;
}
};

export const dummyInvolvedTasksFilter = {
appId: 101,
name: 'Involved Tasks',
filter: { sort: 'created-desc', name: '', state: 'open', assignment: 'fake-involved' },
icon: 'fa-random',
id: 82,
index: 22,
recent: false,
hasFilter: () => {
return true;
}
};

export const dummyQueuedTasksFilter = {
appId: 101,
name: 'Queued Tasks',
filter: { sort: 'created-desc', name: '', state: 'open', assignment: 'fake-queued' },
icon: 'fa-random',
id: 83,
index: 23,
recent: false,
hasFilter: () => {
return true;
}
};

export const dummyCompletedTasksFilter = {
appId: 101,
name: 'Completed',
filter: { sort: 'created-desc', name: '', state: 'open', assignment: 'fake-completed' },
icon: 'fa-random',
id: 84,
index: 24,
recent: false,
hasFilter: () => {
return true;
}
};

export const dummyDuplicateMyTasksFilter = {
appId: 101,
name: 'My Tasks',
filter: { sort: 'created-desc', name: '', state: 'open', assignment: 'fake-mytasks' },
icon: 'fa-random',
id: 85,
index: 25,
recent: false,
hasFilter: () => {
return true;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
*/

import { TestBed } from '@angular/core/testing';
import { mockError, fakeProcessFiltersResponse } from '../../mock';
import { mockError, fakeProcessFiltersResponse, dummyRunningFilter, dummyAllFilter, dummyCompletedFilter, dummyDuplicateRunningFilter } from '../../mock';
import { FilterProcessRepresentationModel } from '../models/filter-process.model';
import { ProcessFilterService } from './process-filter.service';
import { CoreTestingModule } from '@alfresco/adf-core';
import { ProcessInstanceFilterRepresentation } from '@alfresco/js-api';
import { of } from 'rxjs';

declare let jasmine: any;

Expand Down Expand Up @@ -234,5 +236,78 @@ describe('Process filter', () => {
});

});

describe('isFilterAlreadyExisting', () => {
let dummyProcessFilters: FilterProcessRepresentationModel[];
let filterRepresentationData: ProcessInstanceFilterRepresentation;

beforeEach(() => {
dummyProcessFilters = [
{
appId: 0,
filter: filterRepresentationData,
icon: 'fa-random',
id: 8,
index: 0,
name: 'Running',
recent: false,
hasFilter: () => {
return true;
}
}
];

filterRepresentationData = {
name : '',
sort : 'created-desc',
state : 'running'
};
});

it('should return true if the process filter already exists', () => {
const processFilterName = 'Running';
const result = service.isFilterAlreadyExisting(dummyProcessFilters, processFilterName);
expect(result).toBe(true);
});

it('should return false if the process filter does not exist', () => {
const processFilterName = 'All';
const result = service.isFilterAlreadyExisting(dummyProcessFilters, processFilterName);
expect(result).toBe(false);
});
});

describe('createDefaultFilters', () => {

it('should return an array with unique process filters', (done) => {
const appId = 123;

const runningFilter = dummyRunningFilter;
const completedFilter = dummyCompletedFilter;
const allFilter = dummyAllFilter;
const duplicateRunningFilter = dummyDuplicateRunningFilter;

const runningObservable = of(runningFilter);
const completedObservable = of(completedFilter);
const allObservable = of(allFilter);
const duplicateRunningObservable = of(duplicateRunningFilter);

spyOn(service, 'getRunningFilterInstance').and.returnValue(runningFilter);
spyOn<any>(service, 'getCompletedFilterInstance').and.returnValue(completedFilter);
spyOn<any>(service, 'getAllFilterInstance').and.returnValue(allFilter);

spyOn(service, 'addProcessFilter').and.returnValues(runningObservable, completedObservable, allObservable, duplicateRunningObservable);

service.createDefaultFilters(appId).subscribe((result) => {
expect(result).toEqual([
new FilterProcessRepresentationModel({ ...runningFilter, filter: runningFilter.filter, appId }),
new FilterProcessRepresentationModel({ ...completedFilter, filter: completedFilter.filter, appId }),
new FilterProcessRepresentationModel({ ...allFilter, filter: allFilter.filter, appId })
]);
done();
});
});

});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ export class ProcessFilterService {
map((response) => {
const filters: FilterProcessRepresentationModel[] = [];
response.data.forEach((filter) => {
const filterModel = new FilterProcessRepresentationModel(filter);
filters.push(filterModel);
if (!this.isFilterAlreadyExisting(filters, filter.name)) {
const filterModel = new FilterProcessRepresentationModel(filter);
filters.push(filterModel);
}
});
return filters;
}),
Expand Down Expand Up @@ -116,12 +118,14 @@ export class ProcessFilterService {
(res) => {
const filters: FilterProcessRepresentationModel[] = [];
res.forEach((filter) => {
if (filter.name === runningFilter.name) {
filters.push(new FilterProcessRepresentationModel({ ...filter, filter: runningFilter.filter, appId }));
} else if (filter.name === completedFilter.name) {
filters.push(new FilterProcessRepresentationModel({ ...filter, filter: completedFilter.filter, appId }));
} else if (filter.name === allFilter.name) {
filters.push(new FilterProcessRepresentationModel({ ...filter, filter: allFilter.filter, appId }));
if (!this.isFilterAlreadyExisting(filters, filter.name)) {
if (filter.name === runningFilter.name) {
filters.push(new FilterProcessRepresentationModel({ ...filter, filter: runningFilter.filter, appId }));
} else if (filter.name === completedFilter.name) {
filters.push(new FilterProcessRepresentationModel({ ...filter, filter: completedFilter.filter, appId }));
} else if (filter.name === allFilter.name) {
filters.push(new FilterProcessRepresentationModel({ ...filter, filter: allFilter.filter, appId }));
}
}
});
observer.next(filters);
Expand All @@ -133,6 +137,17 @@ export class ProcessFilterService {
});
}

/**
* Checks if a filter with the given name already exists in the list of filters.
*
* @param filters - An array of FilterProcessRepresentationModel objects representing the existing filters.
* @param filterName - The name of the filter to check for existence.
* @returns - True if a filter with the specified name already exists, false otherwise.
*/
isFilterAlreadyExisting(filters: FilterProcessRepresentationModel[], filterName: string): boolean {
return filters.some((existingFilter) => existingFilter.name === filterName);
}

/**
* Creates and returns a filter that matches "running" process instances.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@

import { TestBed } from '@angular/core/testing';
import { fakeAppPromise } from '../../mock';
import { fakeFiltersResponse, fakeAppFilter } from '../../mock/task/task-filters.mock';
import { fakeFiltersResponse, fakeAppFilter, dummyMyTasksFilter, dummyInvolvedTasksFilter, dummyQueuedTasksFilter, dummyCompletedTasksFilter, dummyDuplicateMyTasksFilter } from '../../mock/task/task-filters.mock';
import { FilterRepresentationModel } from '../models/filter.model';
import { TaskFilterService } from './task-filter.service';
import { CoreModule } from '@alfresco/adf-core';
import { ProcessTestingModule } from '../../testing/process.testing.module';
import { TaskFilterRepresentation } from '@alfresco/js-api';
import { of } from 'rxjs';

declare let jasmine: any;

Expand Down Expand Up @@ -251,4 +253,81 @@ describe('Activiti Task filter Service', () => {
});
});
});

describe('isFilterAlreadyExisting', () => {
let dummyTaskFilters: FilterRepresentationModel[];
let filterRepresentationData: TaskFilterRepresentation;

beforeEach(() => {
dummyTaskFilters = [
{
appId: 0,
filter: filterRepresentationData,
icon: 'fa-random',
id: 9,
index: 0,
name: 'My Tasks',
recent: false,
hasFilter: () => {
return true;
}
}
];

filterRepresentationData = {
name : '',
sort : 'created-desc',
state : 'running'
};
});

it('should return true if the task filter already exists', () => {
const taskFilterName = 'My Tasks';
const result = service.isFilterAlreadyExisting(dummyTaskFilters, taskFilterName);
expect(result).toBe(true);
});

it('should return false if the task filter does not exist', () => {
const taskFilterName = 'Involved Tasks';
const result = service.isFilterAlreadyExisting(dummyTaskFilters, taskFilterName);
expect(result).toBe(false);
});
});

describe('createDefaultFilters', () => {

it('should return an array with unique task filters', (done) => {
const appId = 101;

const myTasksFilter = dummyMyTasksFilter;
const involvedTasksFilter = dummyInvolvedTasksFilter;
const queuedTasksFilter = dummyQueuedTasksFilter;
const completedTasksFilter = dummyCompletedTasksFilter;
const duplicateMyTasksFilter = dummyDuplicateMyTasksFilter;

const myTasksObservableObservable = of(myTasksFilter);
const involvedTasksObservable = of(involvedTasksFilter);
const queuedTasksObservable = of(queuedTasksFilter);
const completedTasksObservable = of(completedTasksFilter);
const duplicateMyTasksObservableObservable = of(duplicateMyTasksFilter);

spyOn(service, 'getMyTasksFilterInstance').and.returnValue(myTasksFilter);
spyOn(service, 'getInvolvedTasksFilterInstance').and.returnValue(involvedTasksFilter);
spyOn(service, 'getQueuedTasksFilterInstance').and.returnValue(queuedTasksFilter);
spyOn(service, 'getCompletedTasksFilterInstance').and.returnValue(completedTasksFilter);

spyOn(service, 'addFilter').and.returnValues(myTasksObservableObservable, involvedTasksObservable, queuedTasksObservable, completedTasksObservable, duplicateMyTasksObservableObservable);

service.createDefaultFilters(appId).subscribe((result) => {
expect(result).toEqual([
new FilterRepresentationModel({ ...myTasksFilter, filter: myTasksFilter.filter, appId }),
new FilterRepresentationModel({ ...involvedTasksFilter, filter: involvedTasksFilter.filter, appId }),
new FilterRepresentationModel({ ...queuedTasksFilter, filter: queuedTasksFilter.filter, appId }),
new FilterRepresentationModel({ ...completedTasksFilter, filter: completedTasksFilter.filter, appId })
]);
done();
});
});

});
});
Loading

0 comments on commit c8b4083

Please sign in to comment.