Skip to content

Commit

Permalink
fix(fuzzy-search): only update searchFilters if searchFilters.any not…
Browse files Browse the repository at this point in the history
… empty

to prevents undesired route changes
  • Loading branch information
tkohr committed Feb 28, 2024
1 parent 5b8d393 commit 8392121
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
25 changes: 19 additions & 6 deletions apps/datahub-e2e/src/e2e/header.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,25 @@ describe('header', () => {
.find('input')
.should('have.value', '')
})
it('should reset search results on click on cancel button', () => {
cy.get('gn-ui-fuzzy-search').type('velo')
cy.get('mat-icon')
.contains('close')
.trigger('click', { waitForAnimations: false })
cy.get('gn-ui-record-preview-row').should('have.length.gt', 1)
describe('when on search url path', () => {
it('should reset search results on click on cancel button', () => {
cy.visit('/search')
cy.get('gn-ui-fuzzy-search').type('velo')
cy.get('mat-icon')
.contains('close')
.trigger('click', { waitForAnimations: false })
cy.get('gn-ui-record-preview-row').should('have.length.gt', 1)
})
})
describe('when on news url path', () => {
it('should stay on news url path', () => {
cy.visit('/')
cy.get('gn-ui-fuzzy-search').type('velo')
cy.get('mat-icon')
.contains('close')
.trigger('click', { waitForAnimations: false })
cy.url().should('include', '/news')
})
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,10 @@ describe('FuzzySearchComponent', () => {
})

describe('search input clear', () => {
describe('when output is defined', () => {
describe('when output is defined and search filters are not empty', () => {
beforeEach(() => {
jest.resetAllMocks()
searchFacade.searchFilters$.next({ any: 'river' })
component.inputSubmitted.subscribe()
jest.spyOn(component.inputSubmitted, 'emit')
component.handleInputCleared()
Expand All @@ -154,6 +155,21 @@ describe('FuzzySearchComponent', () => {
expect(component.inputSubmitted.emit).not.toHaveBeenCalled()
})
})
describe('when output is defined but search filters are empty', () => {
beforeEach(() => {
jest.resetAllMocks()
searchFacade.searchFilters$.next({})
component.inputSubmitted.subscribe()
jest.spyOn(component.inputSubmitted, 'emit')
component.handleInputCleared()
})
it('does not clear the search filters (to prevent according navigation)', () => {
expect(searchService.updateFilters).not.toHaveBeenCalled()
})
it('does not emit inputSubmitted', () => {
expect(component.inputSubmitted.emit).not.toHaveBeenCalled()
})
})
})

describe('search suggestion selection', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import {
AutocompleteItem,
} from '@geonetwork-ui/ui/inputs'
import { Observable } from 'rxjs'
import { map } from 'rxjs/operators'
import { map, tap } from 'rxjs/operators'
import { SearchFacade } from '../state/search.facade'
import { SearchService } from '../utils/service/search.service'
import { CatalogRecord } from '@geonetwork-ui/common/domain/model/record'
import { RecordsRepositoryInterface } from '@geonetwork-ui/common/domain/repository/records-repository.interface'
import { SearchFilters } from '@geonetwork-ui/api/metadata-converter'

@Component({
selector: 'gn-ui-fuzzy-search',
Expand All @@ -30,6 +31,7 @@ export class FuzzySearchComponent implements OnInit {
@Output() itemSelected = new EventEmitter<CatalogRecord>()
@Output() inputSubmitted = new EventEmitter<string>()
searchInputValue$: Observable<{ title: string }>
currentSearchFilters: SearchFilters

displayWithFn: (record: CatalogRecord) => string = (record) => record?.title

Expand All @@ -46,6 +48,7 @@ export class FuzzySearchComponent implements OnInit {

ngOnInit(): void {
this.searchInputValue$ = this.searchFacade.searchFilters$.pipe(
tap((searchFilter) => (this.currentSearchFilters = searchFilter)),
map((searchFilter) => ({
title: searchFilter.any as string,
}))
Expand Down Expand Up @@ -76,6 +79,8 @@ export class FuzzySearchComponent implements OnInit {
}

handleInputCleared() {
this.searchService.updateFilters({ any: '' })
if (this.currentSearchFilters.any) {
this.searchService.updateFilters({ any: '' })
}
}
}

0 comments on commit 8392121

Please sign in to comment.