From 063c54e93f4d04f01ac002e5e673d163891f4eb3 Mon Sep 17 00:00:00 2001 From: Alexandre Vryghem Date: Sat, 1 Jul 2023 13:20:41 +0200 Subject: [PATCH] Fix SearchHierarchyFilterComponent no expectation tests Reverted the way you detect when an item is selected by using observables again instead of promises. The reason why it didn't work without the old observable approach was because `VocabularyTreeviewModalComponent` didn't have a `select` `@Output` like `VocabularyTreeviewComponent` --- .../vocabulary-treeview-modal.component.ts | 9 ++++++++- .../search-hierarchy-filter.component.spec.ts | 13 +++++++------ .../search-hierarchy-filter.component.ts | 19 +++++++++++++------ 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/app/shared/form/vocabulary-treeview-modal/vocabulary-treeview-modal.component.ts b/src/app/shared/form/vocabulary-treeview-modal/vocabulary-treeview-modal.component.ts index c6b0bf20fe..8213b91f9c 100644 --- a/src/app/shared/form/vocabulary-treeview-modal/vocabulary-treeview-modal.component.ts +++ b/src/app/shared/form/vocabulary-treeview-modal/vocabulary-treeview-modal.component.ts @@ -1,4 +1,4 @@ -import { Component, Input } from '@angular/core'; +import { Component, EventEmitter, Input, Output } from '@angular/core'; import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; import { VocabularyOptions } from '../../../core/submission/vocabularies/models/vocabulary-options.model'; import { VocabularyEntryDetail } from '../../../core/submission/vocabularies/models/vocabulary-entry-detail.model'; @@ -33,6 +33,12 @@ export class VocabularyTreeviewModalComponent { */ @Input() multiSelect = false; + /** + * An event fired when a vocabulary entry is selected. + * Event's payload equals to {@link VocabularyEntryDetail} selected. + */ + @Output() select: EventEmitter = new EventEmitter(null); + /** * Initialize instance variables * @@ -46,6 +52,7 @@ export class VocabularyTreeviewModalComponent { * Method called on entry select */ onSelect(item: VocabularyEntryDetail) { + this.select.emit(item); this.activeModal.close(item); } } 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 e6c74d8047..33306c400b 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 @@ -1,5 +1,5 @@ import { SearchHierarchyFilterComponent } from './search-hierarchy-filter.component'; -import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; +import { ComponentFixture, fakeAsync, TestBed, tick } 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'; @@ -56,7 +56,7 @@ describe('SearchHierarchyFilterComponent', () => { }; beforeEach(() => { - TestBed.configureTestingModule({ + return TestBed.configureTestingModule({ imports: [ CommonModule, NgbModule, @@ -138,8 +138,9 @@ describe('SearchHierarchyFilterComponent', () => { describe('when selecting a value from the vocabulary tree', () => { - it('should add a new search filter to the existing search filters', () => { - waitForAsync(() => expect(router.navigate).toHaveBeenCalledWith([testSearchLink], { + it('should add a new search filter to the existing search filters', fakeAsync(() => { + tick(); + expect(router.navigate).toHaveBeenCalledWith([testSearchLink], { queryParams: { [`f.${testSearchFilter}`]: [ ...alreadySelectedValues, @@ -147,8 +148,8 @@ describe('SearchHierarchyFilterComponent', () => { ].map((value => `${value},equals`)), }, queryParamsHandling: 'merge', - })); - }); + }); + })); }); }); }); 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 f9b3f2bff9..0353ae20a8 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 @@ -1,4 +1,4 @@ -import { Component, Inject, OnInit } from '@angular/core'; +import { Component, Inject, OnDestroy, OnInit } from '@angular/core'; import { renderFacetFor } from '../search-filter-type-decorator'; import { FilterType } from '../../../models/filter-type.model'; import { facetLoad, SearchFacetFilterComponent } from '../search-facet-filter/search-facet-filter.component'; @@ -21,7 +21,7 @@ import { FacetValue } from '../../../models/facet-value.model'; import { getFacetValueForType } from '../../../search.utils'; import { filter, map, take } from 'rxjs/operators'; import { VocabularyService } from '../../../../../core/submission/vocabularies/vocabulary.service'; -import { Observable, BehaviorSubject } from 'rxjs'; +import { Observable, BehaviorSubject, Subscription } from 'rxjs'; import { PageInfo } from '../../../../../core/shared/page-info.model'; import { environment } from '../../../../../../environments/environment'; import { addOperatorToFilterValue } from '../../../search.utils'; @@ -38,7 +38,9 @@ import { VocabularyTreeviewModalComponent } from '../../../../form/vocabulary-tr * Component that represents a hierarchy facet for a specific filter configuration */ @renderFacetFor(FilterType.hierarchy) -export class SearchHierarchyFilterComponent extends SearchFacetFilterComponent implements OnInit { +export class SearchHierarchyFilterComponent extends SearchFacetFilterComponent implements OnDestroy, OnInit { + + subscriptions: Subscription[] = []; constructor(protected searchService: SearchService, protected filterService: SearchFilterService, @@ -56,6 +58,11 @@ export class SearchHierarchyFilterComponent extends SearchFacetFilterComponent i vocabularyExists$: Observable; + ngOnDestroy(): void { + super.ngOnDestroy(); + this.subscriptions.forEach((subscription: Subscription) => subscription.unsubscribe()); + } + /** * Submits a new active custom value to the filter from the input field * Overwritten method from parent component, adds the "query" operator to the received data before passing it on @@ -91,11 +98,11 @@ export class SearchHierarchyFilterComponent extends SearchFacetFilterComponent i name: this.getVocabularyEntry(), closed: true }; - modalRef.result.then((detail: VocabularyEntryDetail) => { + this.subscriptions.push(modalRef.componentInstance.select.subscribe((detail: VocabularyEntryDetail) => { this.selectedValues$ .pipe(take(1)) .subscribe((selectedValues) => { - this.router.navigate( + void this.router.navigate( [this.searchService.getSearchLink()], { queryParams: { @@ -106,7 +113,7 @@ export class SearchHierarchyFilterComponent extends SearchFacetFilterComponent i }, ); }); - }).catch(); + })); } /**