Fixed issue occurred when deselecting a selected facet value

This commit is contained in:
Giuseppe Digilio
2019-04-18 13:15:48 +02:00
parent ce9e334f42
commit b48693d71f
4 changed files with 96 additions and 7 deletions

View File

@@ -24,7 +24,6 @@ describe('SearchFacetOptionComponent', () => {
const value1 = 'testvalue1';
const value2 = 'test2';
const operator = 'authority';
const value3 = 'another value3';
const mockFilterConfig = Object.assign(new SearchFilterConfig(), {
name: filterName1,
@@ -51,6 +50,13 @@ describe('SearchFacetOptionComponent', () => {
search: ``
};
const selectedValue: FacetValue = {
label: value1,
value: value1,
count: 20,
search: `http://test.org/api/discover/search/objects?f.${filterName1}=${value1},${operator}`
};
const authorityValue: FacetValue = {
label: value2,
value: value2,
@@ -59,7 +65,7 @@ describe('SearchFacetOptionComponent', () => {
};
const searchLink = '/search';
const selectedValues = [value1];
const selectedValues = [selectedValue];
const selectedValues$ = observableOf(selectedValues);
let filterService;
let searchService;

View File

@@ -94,7 +94,7 @@ export class SearchFacetOptionComponent implements OnInit, OnDestroy {
/**
* TODO to review after https://github.com/DSpace/dspace-angular/issues/368 is resolved
* Retrieve facet value from search link
* Retrieve facet value related to facet type
*/
private getFacetValue(): string {
if (this.filterConfig.type === FilterType.authority) {

View File

@@ -13,13 +13,18 @@ import { RouterStub } from '../../../../../shared/testing/router-stub';
import { SearchConfigurationService } from '../../../../search-service/search-configuration.service';
import { SearchFilterService } from '../../search-filter.service';
import { SearchFacetSelectedOptionComponent } from './search-facet-selected-option.component';
import { FacetValue } from '../../../../search-service/facet-value.model';
describe('SearchFacetSelectedOptionComponent', () => {
let comp: SearchFacetSelectedOptionComponent;
let fixture: ComponentFixture<SearchFacetSelectedOptionComponent>;
const filterName1 = 'test name';
const filterName2 = 'testAuthorityname';
const label1 = 'test value 1';
const value1 = 'testvalue1';
const label2 = 'test 2';
const value2 = 'test2';
const operator = 'authority';
const mockFilterConfig = Object.assign(new SearchFilterConfig(), {
name: filterName1,
type: FilterType.range,
@@ -29,16 +34,55 @@ describe('SearchFacetSelectedOptionComponent', () => {
minValue: 200,
maxValue: 3000,
});
const mockAuthorityFilterConfig = Object.assign(new SearchFilterConfig(), {
name: filterName2,
type: FilterType.authority,
hasFacets: false,
isOpenByDefault: false,
pageSize: 2
});
const searchLink = '/search';
const selectedValues = [value1, value2];
const selectedValue: FacetValue = {
label: value1,
value: value1,
count: 20,
search: `http://test.org/api/discover/search/objects?f.${filterName1}=${value1}`
};
const selectedValue2: FacetValue = {
label: value2,
value: value2,
count: 20,
search: `http://test.org/api/discover/search/objects?f.${filterName1}=${value2}`
};
const selectedAuthorityValue: FacetValue = {
label: label1,
value: value1,
count: 20,
search: `http://test.org/api/discover/search/objects?f.${filterName2}=${value1},${operator}`
};
const selectedAuthorityValue2: FacetValue = {
label: label2,
value: value2,
count: 20,
search: `http://test.org/api/discover/search/objects?f.${filterName2}=${value2},${operator}`
};
const selectedValues = [selectedValue, selectedValue2];
const selectedAuthorityValues = [selectedAuthorityValue, selectedAuthorityValue2];
const facetValue = {
label: value2,
value: value2,
count: 1,
search: ''
};
const authorityValue: FacetValue = {
label: label2,
value: value2,
count: 20,
search: `http://test.org/api/discover/search/objects?f.${filterName2}=${value2},${operator}`
};
const selectedValues$ = observableOf(selectedValues);
const selectedAuthorityValues$ = observableOf(selectedAuthorityValues);
let filterService;
let searchService;
let router;
@@ -98,4 +142,20 @@ describe('SearchFacetSelectedOptionComponent', () => {
});
});
});
describe('when filter type is authority and the updateRemoveParams method is called with a value', () => {
it('should update the removeQueryParams with the new parameter values', () => {
spyOn(filterService, 'getSelectedValuesForFilter').and.returnValue(selectedAuthorityValues);
comp.selectedValue = authorityValue;
comp.selectedValues$ = selectedAuthorityValues$;
comp.filterConfig = mockAuthorityFilterConfig;
comp.removeQueryParams = {};
fixture.detectChanges();
(comp as any).updateRemoveParams(selectedAuthorityValues);
expect(comp.removeQueryParams).toEqual({
[mockAuthorityFilterConfig.paramName]: [`${value1},${operator}`],
page: 1
});
});
});
});

View File

@@ -7,6 +7,7 @@ import { SearchFilterService } from '../../search-filter.service';
import { hasValue } from '../../../../../shared/empty.util';
import { SearchConfigurationService } from '../../../../search-service/search-configuration.service';
import { FacetValue } from '../../../../search-service/facet-value.model';
import { FilterType } from '../../../../search-service/filter-type.model';
@Component({
selector: 'ds-search-facet-selected-option',
@@ -30,7 +31,7 @@ export class SearchFacetSelectedOptionComponent implements OnInit, OnDestroy {
/**
* Emits the active values for this filter
*/
@Input() selectedValues$: Observable<string[]>;
@Input() selectedValues$: Observable<FacetValue[]>;
/**
* UI parameters when this filter is removed
@@ -70,13 +71,35 @@ export class SearchFacetSelectedOptionComponent implements OnInit, OnDestroy {
* Calculates the parameters that should change if a given value for this filter would be removed from the active filters
* @param {string[]} selectedValues The values that are currently selected for this filter
*/
private updateRemoveParams(selectedValues: string[]): void {
private updateRemoveParams(selectedValues: FacetValue[]): void {
this.removeQueryParams = {
[this.filterConfig.paramName]: selectedValues.filter((v) => v !== this.selectedValue.label),
[this.filterConfig.paramName]: selectedValues
.filter((facetValue: FacetValue) => facetValue.label !== this.selectedValue.label)
.map((facetValue: FacetValue) => this.getFacetValue(facetValue)),
page: 1
};
}
/**
* TODO to review after https://github.com/DSpace/dspace-angular/issues/368 is resolved
* Retrieve facet value related to facet type
*/
private getFacetValue(facetValue: FacetValue): string {
if (this.filterConfig.type === FilterType.authority) {
const search = facetValue.search;
const hashes = search.slice(search.indexOf('?') + 1).split('&');
const params = {};
hashes.map((hash) => {
const [key, val] = hash.split('=');
params[key] = decodeURIComponent(val)
});
return params[this.filterConfig.paramName];
} else {
return facetValue.value;
}
}
/**
* Make sure the subscription is unsubscribed from when this component is destroyed
*/