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`
This commit is contained in:
Alexandre Vryghem
2023-07-01 13:20:41 +02:00
parent e0db4284b3
commit 063c54e93f
3 changed files with 28 additions and 13 deletions

View File

@@ -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<VocabularyEntryDetail> = new EventEmitter<VocabularyEntryDetail>(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);
}
}

View File

@@ -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,9 +148,9 @@ describe('SearchHierarchyFilterComponent', () => {
].map((value => `${value},equals`)),
},
queryParamsHandling: 'merge',
});
}));
});
});
});
});
});

View File

@@ -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<boolean>;
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();
}));
}
/**