Skip to content

Commit

Permalink
[DSC-1364] Fix issue with undefined top search request
Browse files Browse the repository at this point in the history
  • Loading branch information
atarix83 committed Nov 13, 2023
1 parent e5cd675 commit cbd6200
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
</div>

<a *ngIf="vocabularyExists$ | async"
href="javascript:void(0);"
id="show-{{filterConfig.name}}-tree"
(click)="showVocabularyTree()">
{{'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 )} }}
</a>
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<SearchHierarchyFilterComponent>;
let component: SearchHierarchyFilterComponent;
let showVocabularyTreeLink: DebugElement;

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

beforeEach(() => {
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [
CommonModule,
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,25 @@ 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';
import { SEARCH_CONFIG_SERVICE } from '../../../../../my-dspace-page/my-dspace-page.component';
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',
Expand Down Expand Up @@ -54,7 +58,7 @@ export class SearchHierarchyFilterComponent extends SearchFacetFilterComponent i
super(searchService, filterService, rdbs, router, searchConfigService, inPlaceSearch, filterConfig, refreshFilters);
}

vocabularyExists$: Observable<boolean>;
vocabularyExists$: BehaviorSubject<boolean> = new BehaviorSubject(false);

/**
* Submits a new active custom value to the filter from the input field
Expand All @@ -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<PaginatedList<VocabularyEntryDetail>>) => rd.hasSucceeded && rd.payload?.totalElements > 0)
).subscribe((res) => {
this.vocabularyExists$.next(res);
});
}
}

/**
Expand Down

0 comments on commit cbd6200

Please sign in to comment.