Skip to content

Commit

Permalink
fix(search-filters-summary): ignore filters in FILTER_SUMMARY_IGNORE_…
Browse files Browse the repository at this point in the history
…LIST also when clearing filters
  • Loading branch information
tkohr committed Dec 10, 2024
1 parent 5ee6f32 commit 01d9e22
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -18,15 +17,13 @@ class SearchFacadeMock {
license: {},
'userInfo.keyword': {},
})
}
class SearchServiceMock {
setFilters = jest.fn()
}

describe('SearchFiltersSummaryComponent', () => {
let component: SearchFiltersSummaryComponent
let fixture: ComponentFixture<SearchFiltersSummaryComponent>
let searchFacade: SearchFacade
let searchService: SearchService

beforeEach(async () => {
await TestBed.configureTestingModule({
Expand All @@ -35,10 +32,7 @@ describe('SearchFiltersSummaryComponent', () => {
SearchFiltersSummaryComponent,
MockComponent(SearchFiltersSummaryItemComponent),
],
providers: [
MockProvider(SearchFacade, SearchFacadeMock, 'useClass'),
MockProvider(SearchService, SearchServiceMock, 'useClass'),
],
providers: [MockProvider(SearchFacade, SearchFacadeMock, 'useClass')],
})
})

Expand All @@ -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()
})
Expand Down Expand Up @@ -93,7 +86,7 @@ describe('SearchFiltersSummaryComponent', () => {

it('should clear filters', () => {
component.clearFilters()
expect(searchService.setFilters).toHaveBeenCalledWith({})
expect(searchFacade.setFilters).toHaveBeenCalledWith({})
})
})

Expand All @@ -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()
})
Expand Down Expand Up @@ -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<FieldFilters>).next(
filters
)
component.clearFilters()
expect(searchFacade.setFilters).toHaveBeenCalledWith({
owner: { 1: true },
})
})
})
})
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -26,7 +25,6 @@ export class SearchFiltersSummaryComponent implements OnInit {

constructor(
private searchFacade: SearchFacade,
private searchService: SearchService,
@Optional()
@Inject(FILTER_SUMMARY_IGNORE_LIST)
filterSummaryIgnoreList: string[]
Expand Down Expand Up @@ -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))
}
}

0 comments on commit 01d9e22

Please sign in to comment.