From 96b955366c7c7edccad878bc4887216bed090845 Mon Sep 17 00:00:00 2001 From: SheenaMalhotra182 Date: Fri, 14 Jul 2023 21:13:35 +0530 Subject: [PATCH] moved the check inside a method and added unit tests for method --- .../services/process-filter.service.spec.ts | 31 +++++++++++++++++++ .../services/process-filter.service.ts | 10 +++--- .../services/task-filter.service.spec.ts | 31 +++++++++++++++++++ .../task-list/services/task-filter.service.ts | 10 +++--- 4 files changed, 74 insertions(+), 8 deletions(-) diff --git a/lib/process-services/src/lib/process-list/services/process-filter.service.spec.ts b/lib/process-services/src/lib/process-list/services/process-filter.service.spec.ts index 37a9694980f..67a51e01a47 100644 --- a/lib/process-services/src/lib/process-list/services/process-filter.service.spec.ts +++ b/lib/process-services/src/lib/process-list/services/process-filter.service.spec.ts @@ -234,5 +234,36 @@ describe('Process filter', () => { }); }); + + describe('isFilterAlreadyExisting', () => { + let dummyFilter; + + beforeEach(() => { + dummyFilter = [ + { name: 'filter1' }, + { name: 'filter2' } + ]; + }); + + it('should return true if the filter already exists', () => { + const filterName = 'filter2'; + const isFilterAlreadyExistingSpy = spyOn(service, 'isFilterAlreadyExisting').and.callThrough(); + + const result = service.isFilterAlreadyExisting(dummyFilter, filterName); + + expect(isFilterAlreadyExistingSpy).toHaveBeenCalled(); + expect(result).toBe(true); + }); + + it('should return false if the filter does not exist', () => { + const filterName = 'filter3'; + const isFilterAlreadyExistingSpy = spyOn(service, 'isFilterAlreadyExisting').and.callThrough(); + + const result = service.isFilterAlreadyExisting(dummyFilter, filterName); + + expect(isFilterAlreadyExistingSpy).toHaveBeenCalled(); + expect(result).toBe(false); + }); + }); }); }); diff --git a/lib/process-services/src/lib/process-list/services/process-filter.service.ts b/lib/process-services/src/lib/process-list/services/process-filter.service.ts index c8c448d5af1..6fb818da630 100644 --- a/lib/process-services/src/lib/process-list/services/process-filter.service.ts +++ b/lib/process-services/src/lib/process-list/services/process-filter.service.ts @@ -51,8 +51,7 @@ export class ProcessFilterService { map((response) => { const filters: FilterProcessRepresentationModel[] = []; response.data.forEach((filter) => { - const isFilterAlreadyExisting = filters.some((existingFilter) => existingFilter.name === filter.name); - if (!isFilterAlreadyExisting) { + if (!this.isFilterAlreadyExisting(filters, filter.name)) { const filterModel = new FilterProcessRepresentationModel(filter); filters.push(filterModel); } @@ -119,8 +118,7 @@ export class ProcessFilterService { (res) => { const filters: FilterProcessRepresentationModel[] = []; res.forEach((filter) => { - const isFilterAlreadyExisting = filters.some((existingFilter) => existingFilter.name === filter.name); - if (!isFilterAlreadyExisting) { + 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) { @@ -139,6 +137,10 @@ export class ProcessFilterService { }); } + isFilterAlreadyExisting(filters: any, filterName: string): boolean { + return filters.some((existingFilter) => existingFilter.name === filterName); + } + /** * Creates and returns a filter that matches "running" process instances. * diff --git a/lib/process-services/src/lib/task-list/services/task-filter.service.spec.ts b/lib/process-services/src/lib/task-list/services/task-filter.service.spec.ts index d69f2a3ebf7..03e7041cbab 100644 --- a/lib/process-services/src/lib/task-list/services/task-filter.service.spec.ts +++ b/lib/process-services/src/lib/task-list/services/task-filter.service.spec.ts @@ -251,4 +251,35 @@ describe('Activiti Task filter Service', () => { }); }); }); + + describe('isFilterAlreadyExisting', () => { + let dummyFilter; + + beforeEach(() => { + dummyFilter = [ + { name: 'filter1' }, + { name: 'filter2' } + ]; + }); + + it('should return true if the filter already exists', () => { + const filterName = 'filter2'; + const isFilterAlreadyExistingSpy = spyOn(service, 'isFilterAlreadyExisting').and.callThrough(); + + const result = service.isFilterAlreadyExisting(dummyFilter, filterName); + + expect(isFilterAlreadyExistingSpy).toHaveBeenCalled(); + expect(result).toBe(true); + }); + + it('should return false if the filter does not exist', () => { + const filterName = 'filter3'; + const isFilterAlreadyExistingSpy = spyOn(service, 'isFilterAlreadyExisting').and.callThrough(); + + const result = service.isFilterAlreadyExisting(dummyFilter, filterName); + + expect(isFilterAlreadyExistingSpy).toHaveBeenCalled(); + expect(result).toBe(false); + }); + }); }); diff --git a/lib/process-services/src/lib/task-list/services/task-filter.service.ts b/lib/process-services/src/lib/task-list/services/task-filter.service.ts index ccf38149a95..cf72975708f 100644 --- a/lib/process-services/src/lib/task-list/services/task-filter.service.ts +++ b/lib/process-services/src/lib/task-list/services/task-filter.service.ts @@ -67,8 +67,7 @@ export class TaskFilterService { (res) => { const filters: FilterRepresentationModel[] = []; res.forEach((filter) => { - const isFilterAlreadyExisting = filters.some((existingFilter) => existingFilter.name === filter.name); - if (!isFilterAlreadyExisting) { + if (!this.isFilterAlreadyExisting(filters, filter.name)) { if (filter.name === involvedTasksFilter.name) { filters.push(new FilterRepresentationModel({ ...filter, @@ -117,8 +116,7 @@ export class TaskFilterService { map((response: any) => { const filters: FilterRepresentationModel[] = []; response.data.forEach((filter: FilterRepresentationModel) => { - const isFilterAlreadyExisting = filters.some((existingFilter) => existingFilter.name === filter.name); - if (!isFilterAlreadyExisting) { + if (!this.isFilterAlreadyExisting(filters, filter.name)) { const filterModel = new FilterRepresentationModel(filter); filters.push(filterModel); } @@ -129,6 +127,10 @@ export class TaskFilterService { ); } + isFilterAlreadyExisting(filters: FilterRepresentationModel[], filterName: string): boolean { + return filters.some((existingFilter) => existingFilter.name === filterName); + } + /** * Gets a task filter by ID. *