diff --git a/lib/process-services-cloud/src/lib/process/process-filters/components/process-filters/process-filters-cloud.component.spec.ts b/lib/process-services-cloud/src/lib/process/process-filters/components/process-filters/process-filters-cloud.component.spec.ts index 0091ec8a86..11f11f60be 100644 --- a/lib/process-services-cloud/src/lib/process/process-filters/components/process-filters/process-filters-cloud.component.spec.ts +++ b/lib/process-services-cloud/src/lib/process/process-filters/components/process-filters/process-filters-cloud.component.spec.ts @@ -16,8 +16,8 @@ */ import { SimpleChange } from '@angular/core'; -import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { of, throwError } from 'rxjs'; +import { ComponentFixture, fakeAsync, flush, TestBed } from '@angular/core/testing'; +import { first, of, throwError } from 'rxjs'; import { ProcessFilterCloudService } from '../../services/process-filter-cloud.service'; import { ProcessFiltersCloudComponent } from './process-filters-cloud.component'; import { By } from '@angular/platform-browser'; @@ -529,7 +529,7 @@ describe('ProcessFiltersCloudComponent', () => { processFilterService.filterKeyToBeRefreshed$ = of(filterKeyTest); fixture.detectChanges(); - expect(component.updatedFiltersSet.size).toBe(0); + expect(component.updatedFiltersSet.has(filterKeyTest)).toBeFalsy(); }); describe('Highlight Selected Filter', () => { @@ -578,67 +578,61 @@ describe('ProcessFiltersCloudComponent', () => { expect(getProcessNotificationSubscriptionSpy).toHaveBeenCalled(); }); - it('should not emit filter key when filter counter is set for first time', () => { + it('should emit filter key when filter counter is set for first time', () => { component.currentFiltersValues = {}; const fakeFilterKey = 'testKey'; const fakeFilterValue = 10; const updatedFilterSpy = spyOn(component.updatedFilter, 'emit'); component.checkIfFilterValuesHasBeenUpdated(fakeFilterKey, fakeFilterValue); fixture.detectChanges(); - expect(component.currentFiltersValues).not.toEqual({}); expect(component.currentFiltersValues[fakeFilterKey]).toBe(fakeFilterValue); - expect(updatedFilterSpy).not.toHaveBeenCalled(); + expect(updatedFilterSpy).toHaveBeenCalled(); }); - - it('should not emit filter key when filter counter has not changd', () => { + it('should not emit filter key when filter counter has not changed', fakeAsync(() => { component.currentFiltersValues = {}; const fakeFilterKey = 'testKey'; const fakeFilterValue = 10; - const updatedFilterSpy = spyOn(component.updatedFilter, 'emit'); + component.checkIfFilterValuesHasBeenUpdated(fakeFilterKey, fakeFilterValue); fixture.detectChanges(); - expect(component.currentFiltersValues).not.toEqual({}); expect(component.currentFiltersValues[fakeFilterKey]).toBe(fakeFilterValue); - component.checkIfFilterValuesHasBeenUpdated(fakeFilterKey, fakeFilterValue); - expect(component.currentFiltersValues[fakeFilterKey]).toBe(fakeFilterValue); - expect(updatedFilterSpy).not.toHaveBeenCalled(); - }); + component.updatedFilter.pipe(first()).subscribe(() => { + fail('Should not have been called if the filterKey value is already there'); + }); - it('should emit filter key when filter counter is increased', () => { + component.checkIfFilterValuesHasBeenUpdated(fakeFilterKey, fakeFilterValue); + fixture.detectChanges(); + + expect(component.currentFiltersValues[fakeFilterKey]).toBe(fakeFilterValue); + flush(); + })); + + it('should emit filter key when filter counter is increased', (done) => { component.currentFiltersValues = {}; const fakeFilterKey = 'testKey'; - const updatedFilterSpy = spyOn(component.updatedFilter, 'emit'); component.checkIfFilterValuesHasBeenUpdated(fakeFilterKey, 10); - fixture.detectChanges(); - - expect(updatedFilterSpy).not.toHaveBeenCalledWith(fakeFilterKey); - expect(component.currentFiltersValues[fakeFilterKey]).toBe(10); - + component.updatedFilter.pipe(first()).subscribe((updatedFilter: string) => { + expect(updatedFilter).toBe(fakeFilterKey); + expect(component.currentFiltersValues[fakeFilterKey]).toBe(20); + done(); + }); component.checkIfFilterValuesHasBeenUpdated(fakeFilterKey, 20); fixture.detectChanges(); - - expect(updatedFilterSpy).toHaveBeenCalledWith(fakeFilterKey); - expect(component.currentFiltersValues[fakeFilterKey]).toBe(20); }); - - it('should emit filter key when filter counter is decreased', () => { + it('should emit filter key when filter counter is decreased', (done) => { component.currentFiltersValues = {}; const fakeFilterKey = 'testKey'; - const updatedFilterSpy = spyOn(component.updatedFilter, 'emit'); component.checkIfFilterValuesHasBeenUpdated(fakeFilterKey, 10); - fixture.detectChanges(); - - expect(updatedFilterSpy).not.toHaveBeenCalledWith(fakeFilterKey); - expect(component.currentFiltersValues[fakeFilterKey]).toBe(10); - + component.updatedFilter.pipe(first()).subscribe((updatedFilter: string) => { + expect(updatedFilter).toBe(fakeFilterKey); + expect(component.currentFiltersValues[fakeFilterKey]).toBe(5); + done(); + }); component.checkIfFilterValuesHasBeenUpdated(fakeFilterKey, 5); fixture.detectChanges(); - - expect(updatedFilterSpy).toHaveBeenCalledWith(fakeFilterKey); - expect(component.currentFiltersValues[fakeFilterKey]).toBe(5); }); }); }); diff --git a/lib/process-services-cloud/src/lib/process/process-filters/components/process-filters/process-filters-cloud.component.ts b/lib/process-services-cloud/src/lib/process/process-filters/components/process-filters/process-filters-cloud.component.ts index 389328b818..63cd5db8cd 100644 --- a/lib/process-services-cloud/src/lib/process/process-filters/components/process-filters/process-filters-cloud.component.ts +++ b/lib/process-services-cloud/src/lib/process/process-filters/components/process-filters/process-filters-cloud.component.ts @@ -293,11 +293,7 @@ export class ProcessFiltersCloudComponent implements OnInit, OnChanges { } checkIfFilterValuesHasBeenUpdated(filterKey: string, filterValue: number): void { - if (!this.currentFiltersValues[filterKey]) { - this.currentFiltersValues[filterKey] = filterValue; - return; - } - if (this.currentFiltersValues[filterKey] !== filterValue) { + if (this.currentFiltersValues[filterKey] === undefined || this.currentFiltersValues[filterKey] !== filterValue) { this.currentFiltersValues[filterKey] = filterValue; this.updatedFilter.emit(filterKey); this.updatedFiltersSet.add(filterKey); diff --git a/lib/process-services-cloud/src/lib/task/task-filters/components/task-filters/task-filters-cloud.component.spec.ts b/lib/process-services-cloud/src/lib/task/task-filters/components/task-filters/task-filters-cloud.component.spec.ts index 206d0deb90..f1dab6fc8a 100644 --- a/lib/process-services-cloud/src/lib/task/task-filters/components/task-filters/task-filters-cloud.component.spec.ts +++ b/lib/process-services-cloud/src/lib/task/task-filters/components/task-filters/task-filters-cloud.component.spec.ts @@ -17,7 +17,7 @@ import { AppConfigService } from '@alfresco/adf-core'; import { SimpleChange } from '@angular/core'; -import { ComponentFixture, TestBed, fakeAsync } from '@angular/core/testing'; +import { ComponentFixture, TestBed, fakeAsync, flush } from '@angular/core/testing'; import { By } from '@angular/platform-browser'; import { first, of, throwError } from 'rxjs'; import { TASK_FILTERS_SERVICE_TOKEN } from '../../../../services/cloud-token.service'; @@ -325,6 +325,7 @@ describe('TaskFiltersCloudComponent', () => { spyOn(appConfigService, 'get').and.returnValue(false); fixture.detectChanges(); + expect(fixture.componentInstance.counters).toBeDefined(); const updatedFilterCounters = fixture.debugElement.queryAll(By.css('span.adf-active')); expect(updatedFilterCounters.length).toBe(0); }); @@ -468,40 +469,42 @@ describe('TaskFiltersCloudComponent', () => { expect(component.getFilters).toHaveBeenCalledWith(appName); }); - it('should not emit filter key when filter counter is set for first time', () => { + it('should emit filter key when filter counter is set for first time', () => { component.currentFiltersValues = {}; const fakeFilterKey = 'testKey'; const fakeFilterValue = 10; const updatedFilterSpy = spyOn(component.updatedFilter, 'emit'); component.checkIfFilterValuesHasBeenUpdated(fakeFilterKey, fakeFilterValue); fixture.detectChanges(); - expect(component.currentFiltersValues).not.toEqual({}); expect(component.currentFiltersValues[fakeFilterKey]).toBe(fakeFilterValue); - expect(updatedFilterSpy).not.toHaveBeenCalled(); + expect(updatedFilterSpy).toHaveBeenCalled(); }); - - it('should not emit filter key when filter counter has not changd', () => { + it('should not emit filter key when filter counter has not changed', fakeAsync(() => { component.currentFiltersValues = {}; const fakeFilterKey = 'testKey'; const fakeFilterValue = 10; - const updatedFilterSpy = spyOn(component.updatedFilter, 'emit'); + component.checkIfFilterValuesHasBeenUpdated(fakeFilterKey, fakeFilterValue); fixture.detectChanges(); - expect(component.currentFiltersValues).not.toEqual({}); expect(component.currentFiltersValues[fakeFilterKey]).toBe(fakeFilterValue); + component.updatedFilter.pipe(first()).subscribe(() => { + fail('Should not have been called if the filterKey value is already there'); + }); + component.checkIfFilterValuesHasBeenUpdated(fakeFilterKey, fakeFilterValue); + fixture.detectChanges(); + expect(component.currentFiltersValues[fakeFilterKey]).toBe(fakeFilterValue); - expect(updatedFilterSpy).not.toHaveBeenCalled(); - }); + flush(); + })); it('should emit filter key when filter counter is increased', (done) => { component.currentFiltersValues = {}; const fakeFilterKey = 'testKey'; component.checkIfFilterValuesHasBeenUpdated(fakeFilterKey, 10); - component.updatedFilter.pipe(first()).subscribe((updatedFilter: string) => { expect(updatedFilter).toBe(fakeFilterKey); expect(component.currentFiltersValues[fakeFilterKey]).toBe(20); @@ -510,17 +513,14 @@ describe('TaskFiltersCloudComponent', () => { component.checkIfFilterValuesHasBeenUpdated(fakeFilterKey, 20); fixture.detectChanges(); }); - it('should emit filter key when filter counter is decreased', (done) => { component.currentFiltersValues = {}; const fakeFilterKey = 'testKey'; component.checkIfFilterValuesHasBeenUpdated(fakeFilterKey, 10); - component.updatedFilter.pipe(first()).subscribe((updatedFilter: string) => { expect(updatedFilter).toBe(fakeFilterKey); done(); }); - component.checkIfFilterValuesHasBeenUpdated(fakeFilterKey, 5); fixture.detectChanges(); }); @@ -534,7 +534,7 @@ describe('TaskFiltersCloudComponent', () => { taskFilterService.filterKeyToBeRefreshed$ = of(filterKeyTest); fixture.detectChanges(); - expect(component.updatedCountersSet.size).toBe(0); + expect(component.updatedCountersSet.has(filterKeyTest)).toBeFalsy(); }); it('should remove key from set of updated filters when clicked on filter', async () => { @@ -542,13 +542,13 @@ describe('TaskFiltersCloudComponent', () => { component.updatedCountersSet.add(filter.key); fixture.detectChanges(); - expect(component.updatedCountersSet.size).toBe(1); + expect(component.updatedCountersSet.has(filter.key)).toBeTruthy(); component.onFilterClick(filter); await fixture.whenStable(); fixture.detectChanges(); - expect(component.updatedCountersSet.size).toBe(0); + expect(component.updatedCountersSet.has(filter.key)).toBeFalsy(); }); it('should add key to set of updated filters when value has changed', () => { diff --git a/lib/process-services-cloud/src/lib/task/task-filters/components/task-filters/task-filters-cloud.component.ts b/lib/process-services-cloud/src/lib/task/task-filters/components/task-filters/task-filters-cloud.component.ts index 2994d3a4e2..2c59c68e59 100644 --- a/lib/process-services-cloud/src/lib/task/task-filters/components/task-filters/task-filters-cloud.component.ts +++ b/lib/process-services-cloud/src/lib/task/task-filters/components/task-filters/task-filters-cloud.component.ts @@ -156,7 +156,10 @@ export class TaskFiltersCloudComponent extends BaseTaskFiltersCloudComponent imp } initFilterCounterNotifications() { - if (this.appName && this.enableNotifications) { + if (!this.appName) { + return; + } + if (this.enableNotifications) { this.taskFilterCloudService .getTaskNotificationSubscription(this.appName) .pipe(debounceTime(1000)) @@ -168,6 +171,8 @@ export class TaskFiltersCloudComponent extends BaseTaskFiltersCloudComponent imp this.updateFilterCounters(); this.filterCounterUpdated.emit(result); }); + } else { + this.counters = {}; } } @@ -261,11 +266,7 @@ export class TaskFiltersCloudComponent extends BaseTaskFiltersCloudComponent imp } checkIfFilterValuesHasBeenUpdated(filterKey: string, filterValue: number) { - if (!this.currentFiltersValues[filterKey]) { - this.currentFiltersValues[filterKey] = filterValue; - return; - } - if (this.currentFiltersValues[filterKey] !== filterValue) { + if (this.currentFiltersValues[filterKey] === undefined || this.currentFiltersValues[filterKey] !== filterValue) { this.currentFiltersValues[filterKey] = filterValue; this.updatedFilter.emit(filterKey); this.updatedCountersSet.add(filterKey);