mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-15 14:03:06 +00:00
Fixed issue occurred when deselecting a selected facet value
This commit is contained in:
@@ -24,7 +24,6 @@ describe('SearchFacetOptionComponent', () => {
|
|||||||
const value1 = 'testvalue1';
|
const value1 = 'testvalue1';
|
||||||
const value2 = 'test2';
|
const value2 = 'test2';
|
||||||
const operator = 'authority';
|
const operator = 'authority';
|
||||||
const value3 = 'another value3';
|
|
||||||
|
|
||||||
const mockFilterConfig = Object.assign(new SearchFilterConfig(), {
|
const mockFilterConfig = Object.assign(new SearchFilterConfig(), {
|
||||||
name: filterName1,
|
name: filterName1,
|
||||||
@@ -51,6 +50,13 @@ describe('SearchFacetOptionComponent', () => {
|
|||||||
search: ``
|
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 = {
|
const authorityValue: FacetValue = {
|
||||||
label: value2,
|
label: value2,
|
||||||
value: value2,
|
value: value2,
|
||||||
@@ -59,7 +65,7 @@ describe('SearchFacetOptionComponent', () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const searchLink = '/search';
|
const searchLink = '/search';
|
||||||
const selectedValues = [value1];
|
const selectedValues = [selectedValue];
|
||||||
const selectedValues$ = observableOf(selectedValues);
|
const selectedValues$ = observableOf(selectedValues);
|
||||||
let filterService;
|
let filterService;
|
||||||
let searchService;
|
let searchService;
|
||||||
|
@@ -94,7 +94,7 @@ export class SearchFacetOptionComponent implements OnInit, OnDestroy {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO to review after https://github.com/DSpace/dspace-angular/issues/368 is resolved
|
* 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 {
|
private getFacetValue(): string {
|
||||||
if (this.filterConfig.type === FilterType.authority) {
|
if (this.filterConfig.type === FilterType.authority) {
|
||||||
|
@@ -13,13 +13,18 @@ import { RouterStub } from '../../../../../shared/testing/router-stub';
|
|||||||
import { SearchConfigurationService } from '../../../../search-service/search-configuration.service';
|
import { SearchConfigurationService } from '../../../../search-service/search-configuration.service';
|
||||||
import { SearchFilterService } from '../../search-filter.service';
|
import { SearchFilterService } from '../../search-filter.service';
|
||||||
import { SearchFacetSelectedOptionComponent } from './search-facet-selected-option.component';
|
import { SearchFacetSelectedOptionComponent } from './search-facet-selected-option.component';
|
||||||
|
import { FacetValue } from '../../../../search-service/facet-value.model';
|
||||||
|
|
||||||
describe('SearchFacetSelectedOptionComponent', () => {
|
describe('SearchFacetSelectedOptionComponent', () => {
|
||||||
let comp: SearchFacetSelectedOptionComponent;
|
let comp: SearchFacetSelectedOptionComponent;
|
||||||
let fixture: ComponentFixture<SearchFacetSelectedOptionComponent>;
|
let fixture: ComponentFixture<SearchFacetSelectedOptionComponent>;
|
||||||
const filterName1 = 'test name';
|
const filterName1 = 'test name';
|
||||||
|
const filterName2 = 'testAuthorityname';
|
||||||
|
const label1 = 'test value 1';
|
||||||
const value1 = 'testvalue1';
|
const value1 = 'testvalue1';
|
||||||
|
const label2 = 'test 2';
|
||||||
const value2 = 'test2';
|
const value2 = 'test2';
|
||||||
|
const operator = 'authority';
|
||||||
const mockFilterConfig = Object.assign(new SearchFilterConfig(), {
|
const mockFilterConfig = Object.assign(new SearchFilterConfig(), {
|
||||||
name: filterName1,
|
name: filterName1,
|
||||||
type: FilterType.range,
|
type: FilterType.range,
|
||||||
@@ -29,16 +34,55 @@ describe('SearchFacetSelectedOptionComponent', () => {
|
|||||||
minValue: 200,
|
minValue: 200,
|
||||||
maxValue: 3000,
|
maxValue: 3000,
|
||||||
});
|
});
|
||||||
|
const mockAuthorityFilterConfig = Object.assign(new SearchFilterConfig(), {
|
||||||
|
name: filterName2,
|
||||||
|
type: FilterType.authority,
|
||||||
|
hasFacets: false,
|
||||||
|
isOpenByDefault: false,
|
||||||
|
pageSize: 2
|
||||||
|
});
|
||||||
|
|
||||||
const searchLink = '/search';
|
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 = {
|
const facetValue = {
|
||||||
label: value2,
|
label: value2,
|
||||||
value: value2,
|
value: value2,
|
||||||
count: 1,
|
count: 1,
|
||||||
search: ''
|
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 selectedValues$ = observableOf(selectedValues);
|
||||||
|
const selectedAuthorityValues$ = observableOf(selectedAuthorityValues);
|
||||||
let filterService;
|
let filterService;
|
||||||
let searchService;
|
let searchService;
|
||||||
let router;
|
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
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@@ -7,6 +7,7 @@ import { SearchFilterService } from '../../search-filter.service';
|
|||||||
import { hasValue } from '../../../../../shared/empty.util';
|
import { hasValue } from '../../../../../shared/empty.util';
|
||||||
import { SearchConfigurationService } from '../../../../search-service/search-configuration.service';
|
import { SearchConfigurationService } from '../../../../search-service/search-configuration.service';
|
||||||
import { FacetValue } from '../../../../search-service/facet-value.model';
|
import { FacetValue } from '../../../../search-service/facet-value.model';
|
||||||
|
import { FilterType } from '../../../../search-service/filter-type.model';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'ds-search-facet-selected-option',
|
selector: 'ds-search-facet-selected-option',
|
||||||
@@ -30,7 +31,7 @@ export class SearchFacetSelectedOptionComponent implements OnInit, OnDestroy {
|
|||||||
/**
|
/**
|
||||||
* Emits the active values for this filter
|
* Emits the active values for this filter
|
||||||
*/
|
*/
|
||||||
@Input() selectedValues$: Observable<string[]>;
|
@Input() selectedValues$: Observable<FacetValue[]>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* UI parameters when this filter is removed
|
* 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
|
* 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
|
* @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.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
|
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
|
* Make sure the subscription is unsubscribed from when this component is destroyed
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user