From cbd6200be2bfb3aa574d82e089e291d35ee0e916 Mon Sep 17 00:00:00 2001 From: Giuseppe Digilio Date: Mon, 13 Nov 2023 17:07:42 +0100 Subject: [PATCH] [DSC-1364] Fix issue with undefined top search request --- .../search-hierarchy-filter.component.html | 9 +++-- .../search-hierarchy-filter.component.spec.ts | 37 +++++++++++++------ .../search-hierarchy-filter.component.ts | 35 ++++++++++-------- 3 files changed, 50 insertions(+), 31 deletions(-) diff --git a/src/app/shared/search/search-filters/search-filter/search-hierarchy-filter/search-hierarchy-filter.component.html b/src/app/shared/search/search-filters/search-filter/search-hierarchy-filter/search-hierarchy-filter.component.html index eb49235641f..8bcd91d3a6d 100644 --- a/src/app/shared/search/search-filters/search-filter/search-hierarchy-filter/search-hierarchy-filter.component.html +++ b/src/app/shared/search/search-filters/search-filter/search-hierarchy-filter/search-hierarchy-filter.component.html @@ -31,8 +31,9 @@ - {{'search.filters.filter.show-tree' | translate: {name: ('search.filters.filter.' + filterConfig.name + '.head' | translate | lowercase )} }} + href="javascript:void(0);" + id="show-{{filterConfig.name}}-tree" + data-test="btn-more" + (click)="showVocabularyTree()"> + {{'search.filters.filter.show-tree' | translate: {name: ('search.filters.filter.' + filterConfig.name + '.head' | translate | lowercase )} }} diff --git a/src/app/shared/search/search-filters/search-filter/search-hierarchy-filter/search-hierarchy-filter.component.spec.ts b/src/app/shared/search/search-filters/search-filter/search-hierarchy-filter/search-hierarchy-filter.component.spec.ts index e6c74d80478..c9caa03f6a9 100644 --- a/src/app/shared/search/search-filters/search-filter/search-hierarchy-filter/search-hierarchy-filter.component.spec.ts +++ b/src/app/shared/search/search-filters/search-filter/search-hierarchy-filter/search-hierarchy-filter.component.spec.ts @@ -3,7 +3,7 @@ import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; import { DebugElement, EventEmitter, NO_ERRORS_SCHEMA } from '@angular/core'; import { By } from '@angular/platform-browser'; import { VocabularyService } from '../../../../../core/submission/vocabularies/vocabulary.service'; -import { of as observableOf, BehaviorSubject } from 'rxjs'; +import { BehaviorSubject, of as observableOf } from 'rxjs'; import { RemoteData } from '../../../../../core/data/remote-data'; import { RequestEntryState } from '../../../../../core/data/request-entry-state.model'; import { TranslateModule } from '@ngx-translate/core'; @@ -15,21 +15,24 @@ import { SearchService } from '../../../../../core/shared/search/search.service' import { FILTER_CONFIG, IN_PLACE_SEARCH, - SearchFilterService, - REFRESH_FILTER + REFRESH_FILTER, + SearchFilterService } from '../../../../../core/shared/search/search-filter.service'; import { RemoteDataBuildService } from '../../../../../core/cache/builders/remote-data-build.service'; import { Router } from '@angular/router'; import { NgbModal, NgbModule } from '@ng-bootstrap/ng-bootstrap'; import { SEARCH_CONFIG_SERVICE } from '../../../../../my-dspace-page/my-dspace-page.component'; import { SearchConfigurationServiceStub } from '../../../../testing/search-configuration-service.stub'; -import { VocabularyEntryDetail } from '../../../../../core/submission/vocabularies/models/vocabulary-entry-detail.model'; -import { FacetValue} from '../../../models/facet-value.model'; +import { + VocabularyEntryDetail +} from '../../../../../core/submission/vocabularies/models/vocabulary-entry-detail.model'; +import { FacetValue } from '../../../models/facet-value.model'; import { SearchFilterConfig } from '../../../models/search-filter-config.model'; describe('SearchHierarchyFilterComponent', () => { let fixture: ComponentFixture; + let component: SearchHierarchyFilterComponent; let showVocabularyTreeLink: DebugElement; const testSearchLink = 'test-search'; @@ -55,7 +58,7 @@ describe('SearchHierarchyFilterComponent', () => { searchTopEntries: () => undefined, }; - beforeEach(() => { + beforeEach(waitForAsync(() => { TestBed.configureTestingModule({ imports: [ CommonModule, @@ -79,21 +82,22 @@ describe('SearchHierarchyFilterComponent', () => { ], schemas: [NO_ERRORS_SCHEMA], }).compileComponents(); - }); + })); function init() { fixture = TestBed.createComponent(SearchHierarchyFilterComponent); - fixture.detectChanges(); - showVocabularyTreeLink = fixture.debugElement.query(By.css('a#show-test-search-filter-tree')); + component = fixture.componentInstance; } describe('if the vocabulary doesn\'t exist', () => { beforeEach(() => { + init(); spyOn(vocabularyService, 'searchTopEntries').and.returnValue(observableOf(new RemoteData( undefined, 0, 0, RequestEntryState.Error, undefined, undefined, 404 ))); - init(); + fixture.detectChanges(); + showVocabularyTreeLink = fixture.debugElement.query(By.css('a#show-test-search-filter-tree')); }); it('should not show the vocabulary tree link', () => { @@ -104,10 +108,19 @@ describe('SearchHierarchyFilterComponent', () => { describe('if the vocabulary exists', () => { beforeEach(() => { + init(); + const pageInfo = new PageInfo({ + elementsPerPage: 1, + totalElements: 1, + totalPages: 1, + currentPage: 1 + }); + spyOn(component, 'getVocabularyEntry').and.returnValue('test'); spyOn(vocabularyService, 'searchTopEntries').and.returnValue(observableOf(new RemoteData( - undefined, 0, 0, RequestEntryState.Success, undefined, buildPaginatedList(new PageInfo(), []), 200 + undefined, 0, 0, RequestEntryState.Success, undefined, buildPaginatedList(pageInfo, [new VocabularyEntryDetail()]), 200 ))); - init(); + fixture.detectChanges(); + showVocabularyTreeLink = fixture.debugElement.query(By.css('[data-test="btn-more"]')); }); it('should show the vocabulary tree link', () => { diff --git a/src/app/shared/search/search-filters/search-filter/search-hierarchy-filter/search-hierarchy-filter.component.ts b/src/app/shared/search/search-filters/search-filter/search-hierarchy-filter/search-hierarchy-filter.component.ts index 8504237f09f..4c5909f217e 100644 --- a/src/app/shared/search/search-filters/search-filter/search-hierarchy-filter/search-hierarchy-filter.component.ts +++ b/src/app/shared/search/search-filters/search-filter/search-hierarchy-filter/search-hierarchy-filter.component.ts @@ -11,7 +11,8 @@ import { SearchService } from '../../../../../core/shared/search/search.service' import { FILTER_CONFIG, IN_PLACE_SEARCH, - SearchFilterService, REFRESH_FILTER + REFRESH_FILTER, + SearchFilterService } from '../../../../../core/shared/search/search-filter.service'; import { Router } from '@angular/router'; import { RemoteDataBuildService } from '../../../../../core/cache/builders/remote-data-build.service'; @@ -19,13 +20,16 @@ import { SEARCH_CONFIG_SERVICE } from '../../../../../my-dspace-page/my-dspace-p import { SearchConfigurationService } from '../../../../../core/shared/search/search-configuration.service'; import { SearchFilterConfig } from '../../../models/search-filter-config.model'; import { FacetValue } from '../../../models/facet-value.model'; -import { getFacetValueForType } from '../../../search.utils'; -import { filter, map, take } from 'rxjs/operators'; +import { addOperatorToFilterValue, getFacetValueForType } from '../../../search.utils'; +import { map, take } from 'rxjs/operators'; import { VocabularyService } from '../../../../../core/submission/vocabularies/vocabulary.service'; -import { Observable, BehaviorSubject } from 'rxjs'; +import { BehaviorSubject } from 'rxjs'; import { PageInfo } from '../../../../../core/shared/page-info.model'; import { environment } from '../../../../../../environments/environment'; -import { addOperatorToFilterValue } from '../../../search.utils'; +import { isNotEmpty } from '../../../../empty.util'; +import { getFirstCompletedRemoteData } from '../../../../../core/shared/operators'; +import { RemoteData } from '../../../../../core/data/remote-data'; +import { PaginatedList } from '../../../../../core/data/paginated-list.model'; @Component({ selector: 'ds-search-hierarchy-filter', @@ -54,7 +58,7 @@ export class SearchHierarchyFilterComponent extends SearchFacetFilterComponent i super(searchService, filterService, rdbs, router, searchConfigService, inPlaceSearch, filterConfig, refreshFilters); } - vocabularyExists$: Observable; + vocabularyExists$: BehaviorSubject = new BehaviorSubject(false); /** * Submits a new active custom value to the filter from the input field @@ -67,15 +71,16 @@ export class SearchHierarchyFilterComponent extends SearchFacetFilterComponent i ngOnInit() { super.ngOnInit(); - this.vocabularyExists$ = this.vocabularyService.searchTopEntries( - this.getVocabularyEntry(), new PageInfo(), true, false, - ).pipe( - filter(rd => rd.hasCompleted), - take(1), - map(rd => { - return rd.hasSucceeded; - }), - ); + if (isNotEmpty(this.getVocabularyEntry())) { + this.vocabularyService.searchTopEntries( + this.getVocabularyEntry(), new PageInfo(), true, false, + ).pipe( + getFirstCompletedRemoteData(), + map((rd: RemoteData>) => rd.hasSucceeded && rd.payload?.totalElements > 0) + ).subscribe((res) => { + this.vocabularyExists$.next(res); + }); + } } /**