Skip to content

Commit cbd6200

Browse files
committed
[DSC-1364] Fix issue with undefined top search request
1 parent e5cd675 commit cbd6200

3 files changed

Lines changed: 50 additions & 31 deletions

File tree

src/app/shared/search/search-filters/search-filter/search-hierarchy-filter/search-hierarchy-filter.component.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131
</div>
3232

3333
<a *ngIf="vocabularyExists$ | async"
34-
href="javascript:void(0);"
35-
id="show-{{filterConfig.name}}-tree"
36-
(click)="showVocabularyTree()">
37-
{{'search.filters.filter.show-tree' | translate: {name: ('search.filters.filter.' + filterConfig.name + '.head' | translate | lowercase )} }}
34+
href="javascript:void(0);"
35+
id="show-{{filterConfig.name}}-tree"
36+
data-test="btn-more"
37+
(click)="showVocabularyTree()">
38+
{{'search.filters.filter.show-tree' | translate: {name: ('search.filters.filter.' + filterConfig.name + '.head' | translate | lowercase )} }}
3839
</a>

src/app/shared/search/search-filters/search-filter/search-hierarchy-filter/search-hierarchy-filter.component.spec.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
33
import { DebugElement, EventEmitter, NO_ERRORS_SCHEMA } from '@angular/core';
44
import { By } from '@angular/platform-browser';
55
import { VocabularyService } from '../../../../../core/submission/vocabularies/vocabulary.service';
6-
import { of as observableOf, BehaviorSubject } from 'rxjs';
6+
import { BehaviorSubject, of as observableOf } from 'rxjs';
77
import { RemoteData } from '../../../../../core/data/remote-data';
88
import { RequestEntryState } from '../../../../../core/data/request-entry-state.model';
99
import { TranslateModule } from '@ngx-translate/core';
@@ -15,21 +15,24 @@ import { SearchService } from '../../../../../core/shared/search/search.service'
1515
import {
1616
FILTER_CONFIG,
1717
IN_PLACE_SEARCH,
18-
SearchFilterService,
19-
REFRESH_FILTER
18+
REFRESH_FILTER,
19+
SearchFilterService
2020
} from '../../../../../core/shared/search/search-filter.service';
2121
import { RemoteDataBuildService } from '../../../../../core/cache/builders/remote-data-build.service';
2222
import { Router } from '@angular/router';
2323
import { NgbModal, NgbModule } from '@ng-bootstrap/ng-bootstrap';
2424
import { SEARCH_CONFIG_SERVICE } from '../../../../../my-dspace-page/my-dspace-page.component';
2525
import { SearchConfigurationServiceStub } from '../../../../testing/search-configuration-service.stub';
26-
import { VocabularyEntryDetail } from '../../../../../core/submission/vocabularies/models/vocabulary-entry-detail.model';
27-
import { FacetValue} from '../../../models/facet-value.model';
26+
import {
27+
VocabularyEntryDetail
28+
} from '../../../../../core/submission/vocabularies/models/vocabulary-entry-detail.model';
29+
import { FacetValue } from '../../../models/facet-value.model';
2830
import { SearchFilterConfig } from '../../../models/search-filter-config.model';
2931

3032
describe('SearchHierarchyFilterComponent', () => {
3133

3234
let fixture: ComponentFixture<SearchHierarchyFilterComponent>;
35+
let component: SearchHierarchyFilterComponent;
3336
let showVocabularyTreeLink: DebugElement;
3437

3538
const testSearchLink = 'test-search';
@@ -55,7 +58,7 @@ describe('SearchHierarchyFilterComponent', () => {
5558
searchTopEntries: () => undefined,
5659
};
5760

58-
beforeEach(() => {
61+
beforeEach(waitForAsync(() => {
5962
TestBed.configureTestingModule({
6063
imports: [
6164
CommonModule,
@@ -79,21 +82,22 @@ describe('SearchHierarchyFilterComponent', () => {
7982
],
8083
schemas: [NO_ERRORS_SCHEMA],
8184
}).compileComponents();
82-
});
85+
}));
8386

8487
function init() {
8588
fixture = TestBed.createComponent(SearchHierarchyFilterComponent);
86-
fixture.detectChanges();
87-
showVocabularyTreeLink = fixture.debugElement.query(By.css('a#show-test-search-filter-tree'));
89+
component = fixture.componentInstance;
8890
}
8991

9092
describe('if the vocabulary doesn\'t exist', () => {
9193

9294
beforeEach(() => {
95+
init();
9396
spyOn(vocabularyService, 'searchTopEntries').and.returnValue(observableOf(new RemoteData(
9497
undefined, 0, 0, RequestEntryState.Error, undefined, undefined, 404
9598
)));
96-
init();
99+
fixture.detectChanges();
100+
showVocabularyTreeLink = fixture.debugElement.query(By.css('a#show-test-search-filter-tree'));
97101
});
98102

99103
it('should not show the vocabulary tree link', () => {
@@ -104,10 +108,19 @@ describe('SearchHierarchyFilterComponent', () => {
104108
describe('if the vocabulary exists', () => {
105109

106110
beforeEach(() => {
111+
init();
112+
const pageInfo = new PageInfo({
113+
elementsPerPage: 1,
114+
totalElements: 1,
115+
totalPages: 1,
116+
currentPage: 1
117+
});
118+
spyOn(component, 'getVocabularyEntry').and.returnValue('test');
107119
spyOn(vocabularyService, 'searchTopEntries').and.returnValue(observableOf(new RemoteData(
108-
undefined, 0, 0, RequestEntryState.Success, undefined, buildPaginatedList(new PageInfo(), []), 200
120+
undefined, 0, 0, RequestEntryState.Success, undefined, buildPaginatedList(pageInfo, [new VocabularyEntryDetail()]), 200
109121
)));
110-
init();
122+
fixture.detectChanges();
123+
showVocabularyTreeLink = fixture.debugElement.query(By.css('[data-test="btn-more"]'));
111124
});
112125

113126
it('should show the vocabulary tree link', () => {

src/app/shared/search/search-filters/search-filter/search-hierarchy-filter/search-hierarchy-filter.component.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,25 @@ import { SearchService } from '../../../../../core/shared/search/search.service'
1111
import {
1212
FILTER_CONFIG,
1313
IN_PLACE_SEARCH,
14-
SearchFilterService, REFRESH_FILTER
14+
REFRESH_FILTER,
15+
SearchFilterService
1516
} from '../../../../../core/shared/search/search-filter.service';
1617
import { Router } from '@angular/router';
1718
import { RemoteDataBuildService } from '../../../../../core/cache/builders/remote-data-build.service';
1819
import { SEARCH_CONFIG_SERVICE } from '../../../../../my-dspace-page/my-dspace-page.component';
1920
import { SearchConfigurationService } from '../../../../../core/shared/search/search-configuration.service';
2021
import { SearchFilterConfig } from '../../../models/search-filter-config.model';
2122
import { FacetValue } from '../../../models/facet-value.model';
22-
import { getFacetValueForType } from '../../../search.utils';
23-
import { filter, map, take } from 'rxjs/operators';
23+
import { addOperatorToFilterValue, getFacetValueForType } from '../../../search.utils';
24+
import { map, take } from 'rxjs/operators';
2425
import { VocabularyService } from '../../../../../core/submission/vocabularies/vocabulary.service';
25-
import { Observable, BehaviorSubject } from 'rxjs';
26+
import { BehaviorSubject } from 'rxjs';
2627
import { PageInfo } from '../../../../../core/shared/page-info.model';
2728
import { environment } from '../../../../../../environments/environment';
28-
import { addOperatorToFilterValue } from '../../../search.utils';
29+
import { isNotEmpty } from '../../../../empty.util';
30+
import { getFirstCompletedRemoteData } from '../../../../../core/shared/operators';
31+
import { RemoteData } from '../../../../../core/data/remote-data';
32+
import { PaginatedList } from '../../../../../core/data/paginated-list.model';
2933

3034
@Component({
3135
selector: 'ds-search-hierarchy-filter',
@@ -54,7 +58,7 @@ export class SearchHierarchyFilterComponent extends SearchFacetFilterComponent i
5458
super(searchService, filterService, rdbs, router, searchConfigService, inPlaceSearch, filterConfig, refreshFilters);
5559
}
5660

57-
vocabularyExists$: Observable<boolean>;
61+
vocabularyExists$: BehaviorSubject<boolean> = new BehaviorSubject(false);
5862

5963
/**
6064
* Submits a new active custom value to the filter from the input field
@@ -67,15 +71,16 @@ export class SearchHierarchyFilterComponent extends SearchFacetFilterComponent i
6771

6872
ngOnInit() {
6973
super.ngOnInit();
70-
this.vocabularyExists$ = this.vocabularyService.searchTopEntries(
71-
this.getVocabularyEntry(), new PageInfo(), true, false,
72-
).pipe(
73-
filter(rd => rd.hasCompleted),
74-
take(1),
75-
map(rd => {
76-
return rd.hasSucceeded;
77-
}),
78-
);
74+
if (isNotEmpty(this.getVocabularyEntry())) {
75+
this.vocabularyService.searchTopEntries(
76+
this.getVocabularyEntry(), new PageInfo(), true, false,
77+
).pipe(
78+
getFirstCompletedRemoteData(),
79+
map((rd: RemoteData<PaginatedList<VocabularyEntryDetail>>) => rd.hasSucceeded && rd.payload?.totalElements > 0)
80+
).subscribe((res) => {
81+
this.vocabularyExists$.next(res);
82+
});
83+
}
7984
}
8085

8186
/**

0 commit comments

Comments
 (0)