Skip to content

Commit

Permalink
111731: Hide the search facets when there are no facet suggestions & …
Browse files Browse the repository at this point in the history
…the applied filters of that facet don't have the operator equals, authority or range (because those should be displayed in the facets)
  • Loading branch information
alexandrevryghem committed May 7, 2024
1 parent 28088bc commit 49cb776
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ import { currentPath } from '../../../../utils/route.utils';
import { FacetValues } from '../../../models/facet-values.model';
import { AppliedFilter } from '../../../models/applied-filter.model';

/**
* The operators the {@link AppliedFilter} should have in order to be shown in the facets
*/
export const FACET_OPERATORS: string[] = [
'equals',
'authority',
'range',
];

@Component({
selector: 'ds-search-facet-filter',
template: ``,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ChangeDetectionStrategy, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';

import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { ComponentFixture, TestBed, waitForAsync, fakeAsync } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { TranslateModule } from '@ngx-translate/core';
import { Observable, of as observableOf } from 'rxjs';
Expand All @@ -16,10 +16,22 @@ import { SequenceService } from '../../../../core/shared/sequence.service';
import { BrowserOnlyMockPipe } from '../../../testing/browser-only-mock.pipe';
import { SearchServiceStub } from '../../../testing/search-service.stub';
import { SearchFilterServiceStub } from '../../../testing/search-filter-service.stub';
import { cold } from 'jasmine-marbles';
import { AppliedFilter } from '../../models/applied-filter.model';
import { FacetValues } from '../../models/facet-values.model';
import { createSuccessfulRemoteDataObject$ } from '../../../remote-data.utils';

describe('SearchFilterComponent', () => {
let comp: SearchFilterComponent;
let fixture: ComponentFixture<SearchFilterComponent>;

const appliedFilter1: AppliedFilter = Object.assign(new AppliedFilter(), {
operator: 'equals',
});
const appliedFilter2: AppliedFilter = Object.assign(new AppliedFilter(), {
operator: 'notauthority',
});

const filterName1 = 'test name';

const mockFilterConfig: SearchFilterConfig = Object.assign(new SearchFilterConfig(), {
Expand All @@ -30,12 +42,13 @@ describe('SearchFilterComponent', () => {
});
let searchFilterService: SearchFilterServiceStub;
let sequenceService;
const mockResults = observableOf(['test', 'data']);
let searchService: SearchServiceStub;
let searchConfigurationService: SearchConfigurationServiceStub;

beforeEach(waitForAsync(() => {
searchFilterService = new SearchFilterServiceStub();
searchService = new SearchServiceStub();
searchConfigurationService = new SearchConfigurationServiceStub();
sequenceService = jasmine.createSpyObj('sequenceService', { next: 17 });

TestBed.configureTestingModule({
Expand All @@ -47,7 +60,7 @@ describe('SearchFilterComponent', () => {
providers: [
{ provide: SearchService, useValue: searchService },
{ provide: SearchFilterService, useValue: searchFilterService },
{ provide: SEARCH_CONFIG_SERVICE, useValue: new SearchConfigurationServiceStub() },
{ provide: SEARCH_CONFIG_SERVICE, useValue: searchConfigurationService },
{ provide: SequenceService, useValue: sequenceService },
],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
Expand All @@ -57,7 +70,6 @@ describe('SearchFilterComponent', () => {
}));

beforeEach(() => {
spyOn(searchService, 'getFacetValuesFor').and.returnValue(mockResults);
fixture = TestBed.createComponent(SearchFilterComponent);
comp = fixture.componentInstance; // SearchPageComponent test instance
comp.filter = mockFilterConfig;
Expand Down Expand Up @@ -121,4 +133,59 @@ describe('SearchFilterComponent', () => {
sub.unsubscribe();
});
});

describe('isActive', () => {
it('should return true when there are facet value suggestions & no valid applied values', fakeAsync(() => {
spyOn(searchService, 'getFacetValuesFor').and.returnValue(createSuccessfulRemoteDataObject$(Object.assign(new FacetValues(), {
pageInfo: {
totalElements: 5,
},
} as FacetValues)));
comp.appliedFilters$ = observableOf([appliedFilter2]);

expect(comp.isActive()).toBeObservable(cold('(tt)', {
t: true,
}));
}));

it('should return false when there are no facet value suggestions & no valid applied values', () => {
spyOn(searchService, 'getFacetValuesFor').and.returnValue(createSuccessfulRemoteDataObject$(Object.assign(new FacetValues(), {
pageInfo: {
totalElements: 0,
},
} as FacetValues)));
comp.appliedFilters$ = observableOf([appliedFilter2]);

expect(comp.isActive()).toBeObservable(cold('(tf)', {
t: true,
f: false,
}));
});

it('should return true when there are no facet value suggestions & but there are valid applied values', (() => {
spyOn(searchService, 'getFacetValuesFor').and.returnValue(createSuccessfulRemoteDataObject$(Object.assign(new FacetValues(), {
pageInfo: {
totalElements: 0,
},
} as FacetValues)));
comp.appliedFilters$ = observableOf([appliedFilter1, appliedFilter2]);

expect(comp.isActive()).toBeObservable(cold('(tt)', {
t: true,
}));
}));

it('should return true when there are facet value suggestions & there are valid applied values', (() => {
spyOn(searchService, 'getFacetValuesFor').and.returnValue(createSuccessfulRemoteDataObject$(Object.assign(new FacetValues(), {
pageInfo: {
totalElements: 5,
},
} as FacetValues)));
comp.appliedFilters$ = observableOf([appliedFilter1, appliedFilter2]);

expect(comp.isActive()).toBeObservable(cold('(tt)', {
t: true,
}));
}));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { FacetValues } from '../../models/facet-values.model';
import { RemoteData } from '../../../../core/data/remote-data';
import { AppliedFilter } from '../../models/applied-filter.model';
import { SearchOptions } from '../../models/search-options.model';
import { FACET_OPERATORS } from './search-facet-filter/search-facet-filter.component';

@Component({
selector: 'ds-search-filter',
Expand Down Expand Up @@ -166,15 +167,17 @@ export class SearchFilterComponent implements OnInit, OnDestroy {
* Check if a given filter is supposed to be shown or not
* @returns {Observable<boolean>} Emits true whenever a given filter config should be shown
*/
private isActive(): Observable<boolean> {
isActive(): Observable<boolean> {
return combineLatest([
this.appliedFilters$,
this.searchConfigService.searchOptions,
]).pipe(
switchMap(([selectedValues, options]: [AppliedFilter[], SearchOptions]) => {
if (isNotEmpty(selectedValues)) {
console.log('switchmap', selectedValues, isNotEmpty(selectedValues.filter((appliedFilter: AppliedFilter) => FACET_OPERATORS.includes(appliedFilter.operator))));
if (isNotEmpty(selectedValues.filter((appliedFilter: AppliedFilter) => FACET_OPERATORS.includes(appliedFilter.operator)))) {
return observableOf(true);
} else {
console.log(this.searchService.getFacetValuesFor(this.filter, 1, options));
return this.searchService.getFacetValuesFor(this.filter, 1, options).pipe(
filter((RD: RemoteData<FacetValues>) => !RD.isLoading),
map((valuesRD: RemoteData<FacetValues>) => {
Expand Down
6 changes: 4 additions & 2 deletions src/app/shared/testing/search-configuration-service.stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
FilterConfig,
SearchConfig,
} from '../../core/shared/search/search-filters/search-config.model';
import { SearchOptions } from '../search/models/search-options.model';
import { PaginatedSearchOptions } from '../search/models/paginated-search-options.model';

/**
* Stub class of {@link SearchConfigurationService}
Expand All @@ -12,8 +14,8 @@ export class SearchConfigurationServiceStub {

public paginationID = 'test-id';

private searchOptions: BehaviorSubject<any> = new BehaviorSubject<any>({});
private paginatedSearchOptions: BehaviorSubject<any> = new BehaviorSubject<any>({});
public searchOptions: BehaviorSubject<SearchOptions> = new BehaviorSubject(new SearchOptions({}));
public paginatedSearchOptions: BehaviorSubject<PaginatedSearchOptions> = new BehaviorSubject(new PaginatedSearchOptions({}));

getCurrentFrontendFilters() {
return observableOf([]);
Expand Down

0 comments on commit 49cb776

Please sign in to comment.