diff --git a/libs/feature/search/src/lib/search-filters-summary/search-filters-summary.component.spec.ts b/libs/feature/search/src/lib/search-filters-summary/search-filters-summary.component.spec.ts index 58ef75ebd..f6f8f8809 100644 --- a/libs/feature/search/src/lib/search-filters-summary/search-filters-summary.component.spec.ts +++ b/libs/feature/search/src/lib/search-filters-summary/search-filters-summary.component.spec.ts @@ -4,7 +4,6 @@ import { SearchFiltersSummaryComponent, } from './search-filters-summary.component' import { MockComponent, MockProvider } from 'ng-mocks' -import { SearchService } from '../utils/service/search.service' import { SearchFacade } from '../state/search.facade' import { BehaviorSubject, firstValueFrom } from 'rxjs' import { TranslateModule } from '@ngx-translate/core' @@ -18,15 +17,13 @@ class SearchFacadeMock { license: {}, 'userInfo.keyword': {}, }) -} -class SearchServiceMock { setFilters = jest.fn() } + describe('SearchFiltersSummaryComponent', () => { let component: SearchFiltersSummaryComponent let fixture: ComponentFixture let searchFacade: SearchFacade - let searchService: SearchService beforeEach(async () => { await TestBed.configureTestingModule({ @@ -35,10 +32,7 @@ describe('SearchFiltersSummaryComponent', () => { SearchFiltersSummaryComponent, MockComponent(SearchFiltersSummaryItemComponent), ], - providers: [ - MockProvider(SearchFacade, SearchFacadeMock, 'useClass'), - MockProvider(SearchService, SearchServiceMock, 'useClass'), - ], + providers: [MockProvider(SearchFacade, SearchFacadeMock, 'useClass')], }) }) @@ -47,7 +41,6 @@ describe('SearchFiltersSummaryComponent', () => { await TestBed.compileComponents() fixture = TestBed.createComponent(SearchFiltersSummaryComponent) searchFacade = TestBed.inject(SearchFacade) - searchService = TestBed.inject(SearchService) component = fixture.componentInstance fixture.detectChanges() }) @@ -93,7 +86,7 @@ describe('SearchFiltersSummaryComponent', () => { it('should clear filters', () => { component.clearFilters() - expect(searchService.setFilters).toHaveBeenCalledWith({}) + expect(searchFacade.setFilters).toHaveBeenCalledWith({}) }) }) @@ -105,7 +98,6 @@ describe('SearchFiltersSummaryComponent', () => { await TestBed.compileComponents() fixture = TestBed.createComponent(SearchFiltersSummaryComponent) searchFacade = TestBed.inject(SearchFacade) - searchService = TestBed.inject(SearchService) component = fixture.componentInstance fixture.detectChanges() }) @@ -135,5 +127,21 @@ describe('SearchFiltersSummaryComponent', () => { const isActive = await firstValueFrom(component.searchFilterActive$) expect(isActive).toBeTruthy() }) + it('should clear filters except with keys from FILTER_SUMMARY_IGNORE_LIST', () => { + const filters = { + owner: { 1: true }, + format: {}, + isSpatial: {}, + license: {}, + 'userInfo.keyword': { 'admin|admin|admin|Administrator': true }, + } + ;(searchFacade.searchFilters$ as BehaviorSubject).next( + filters + ) + component.clearFilters() + expect(searchFacade.setFilters).toHaveBeenCalledWith({ + owner: { 1: true }, + }) + }) }) }) diff --git a/libs/feature/search/src/lib/search-filters-summary/search-filters-summary.component.ts b/libs/feature/search/src/lib/search-filters-summary/search-filters-summary.component.ts index 6ab1d6adf..77cad4959 100644 --- a/libs/feature/search/src/lib/search-filters-summary/search-filters-summary.component.ts +++ b/libs/feature/search/src/lib/search-filters-summary/search-filters-summary.component.ts @@ -1,10 +1,9 @@ import { Component, Inject, Input, OnInit, Optional } from '@angular/core' import { CommonModule } from '@angular/common' -import { map, Observable } from 'rxjs' +import { first, map, Observable } from 'rxjs' import { SearchFiltersSummaryItemComponent } from '../search-filters-summary-item/search-filters-summary-item.component' import { TranslateModule } from '@ngx-translate/core' import { SearchFacade } from '../state/search.facade' -import { SearchService } from '../utils/service/search.service' import { FieldFilters } from '@geonetwork-ui/common/domain/model/search' import { InjectionToken } from '@angular/core' @@ -26,7 +25,6 @@ export class SearchFiltersSummaryComponent implements OnInit { constructor( private searchFacade: SearchFacade, - private searchService: SearchService, @Optional() @Inject(FILTER_SUMMARY_IGNORE_LIST) filterSummaryIgnoreList: string[] @@ -56,6 +54,19 @@ export class SearchFiltersSummaryComponent implements OnInit { } clearFilters() { - this.searchService.setFilters({}) + this.searchFacade.searchFilters$ + .pipe( + first(), + map((filters) => { + const newFilters = { ...filters } + Object.keys(newFilters).forEach((key) => { + if (!this.filterSummaryIgnoreList.includes(key)) { + delete newFilters[key] + } + }) + return newFilters + }) + ) + .subscribe((filters) => this.searchFacade.setFilters(filters)) } }