test fixes

This commit is contained in:
lotte
2018-07-25 14:16:27 +02:00
parent 1906f07be3
commit df37e339ee
14 changed files with 155 additions and 131 deletions

View File

@@ -22,5 +22,4 @@ import {
@renderFacetFor(FilterType.boolean)
export class SearchBooleanFilterComponent extends SearchFacetFilterComponent implements OnInit {
currentPage: Observable<number>;
}

View File

@@ -1,8 +1,8 @@
import { ChangeDetectionStrategy, NO_ERRORS_SCHEMA } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { async, ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
import { TranslateModule } from '@ngx-translate/core';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { FILTER_CONFIG, SearchFilterService, SELECTED_VALUES } from '../search-filter.service';
import { FILTER_CONFIG, SearchFilterService } from '../search-filter.service';
import { SearchFilterConfig } from '../../../search-service/search-filter-config.model';
import { FilterType } from '../../../search-service/filter-type.model';
import { FacetValue } from '../../../search-service/facet-value.model';
@@ -15,9 +15,9 @@ import { PaginatedList } from '../../../../core/data/paginated-list';
import { SearchOptions } from '../../../search-options.model';
import { RouterStub } from '../../../../shared/testing/router-stub';
import { Router } from '@angular/router';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { PageInfo } from '../../../../core/shared/page-info.model';
import { SearchFacetFilterComponent } from './search-facet-filter.component';
import { RemoteDataBuildService } from '../../../../core/cache/builders/remote-data-build.service';
describe('SearchFacetFilterComponent', () => {
let comp: SearchFacetFilterComponent;
@@ -65,9 +65,10 @@ describe('SearchFacetFilterComponent', () => {
{ provide: SearchService, useValue: new SearchServiceStub(searchLink) },
{ provide: Router, useValue: new RouterStub() },
{ provide: FILTER_CONFIG, useValue: new SearchFilterConfig() },
{ provide: SELECTED_VALUES, useValue: {} },
{ provide: RemoteDataBuildService, useValue: {aggregate: () => Observable.of({})} },
{
provide: SearchFilterService, useValue: {
getSelectedValuesForFilter: () => Observable.of(selectedValues),
isFilterActiveWithValue: (paramName: string, filterValue: string) => true,
getPage: (paramName: string) => page,
/* tslint:disable:no-empty */
@@ -91,8 +92,7 @@ describe('SearchFacetFilterComponent', () => {
comp = fixture.componentInstance; // SearchPageComponent test instance
comp.filterConfig = mockFilterConfig;
comp.filterValues = [mockValues];
comp.filterValues$ = new BehaviorSubject(comp.filterValues);
comp.selectedValues = selectedValues;
// comp.filterValues$ = new BehaviorSubject({});
filterService = (comp as any).filterService;
searchService = (comp as any).searchService;
spyOn(searchService, 'getFacetValuesFor').and.returnValue(mockValues);
@@ -125,14 +125,14 @@ describe('SearchFacetFilterComponent', () => {
describe('when the getAddParams method is called wih a value', () => {
it('should return the selectedValue list with the new parameter value', () => {
const result = comp.getAddParams(value3);
expect(result[mockFilterConfig.paramName]).toEqual([value1, value2, value3]);
result.subscribe((r) => expect(r[mockFilterConfig.paramName]).toEqual([value1, value2, value3]));
});
});
describe('when the getRemoveParams method is called wih a value', () => {
it('should return the selectedValue list with the parameter value left out', () => {
const result = comp.getRemoveParams(value1);
expect(result[mockFilterConfig.paramName]).toEqual([value2]);
result.subscribe((r) => expect(r[mockFilterConfig.paramName]).toEqual([value2]));
});
});
@@ -170,7 +170,7 @@ describe('SearchFacetFilterComponent', () => {
});
describe('when the getCurrentUrl method is called', () => {
const url = 'test.url/test'
const url = 'test.url/test';
beforeEach(() => {
router.navigateByUrl(url);
});
@@ -198,48 +198,20 @@ describe('SearchFacetFilterComponent', () => {
});
describe('when updateFilterValueList is called', () => {
const cPage = 10;
const searchOptions = new SearchOptions();
beforeEach(() => {
// spyOn(searchService, 'getFacetValuesFor'); Already spied upon
comp.currentPage = Observable.of(cPage);
comp.updateFilterValueList(searchOptions);
});
it('should call getFacetValuesFor on the searchService with the correct parameters', () => {
expect(searchService.getFacetValuesFor).toHaveBeenCalledWith(mockFilterConfig, cPage, searchOptions);
});
});
describe('when updateFilterValueList is called and pageChange is set to true', () => {
const searchOptions = new SearchOptions();
beforeEach(() => {
comp.pageChange = true;
spyOn(comp, 'showFirstPageOnly');
comp.updateFilterValueList(searchOptions);
comp.updateFilterValueList(searchOptions)
});
it('should not call showFirstPageOnly on the component', () => {
expect(comp.showFirstPageOnly).not.toHaveBeenCalled();
});
it('should set pageChange to false', () => {
expect(comp.pageChange).toBeFalsy();
it('should call showFirstPageOnly and empty the filter', () => {
expect(comp.animationState).toEqual('loading');
expect((comp as any).collapseNextUpdate).toBeTruthy();
expect(comp.filter).toEqual('');
});
});
describe('when updateFilterValueList is called and pageChange is set to false', () => {
const searchOptions = new SearchOptions();
beforeEach(() => {
comp.pageChange = false;
spyOn(comp, 'showFirstPageOnly');
comp.updateFilterValueList(searchOptions);
});
it('should call showFirstPageOnly on the component', () => {
expect(comp.showFirstPageOnly).toHaveBeenCalled();
});
});
describe('when findSuggestions is called with query \'test\'', () => {
const query = 'test';
beforeEach(() => {

View File

@@ -33,7 +33,6 @@ export class SearchFacetFilterComponent implements OnInit, OnDestroy {
currentPage: Observable<number>;
isLastPage$: BehaviorSubject<boolean> = new BehaviorSubject(false);
filter: string;
pageChange = false;
private subs: Subscription[] = [];
filterSearchResults: Observable<any[]> = Observable.of([]);
selectedValues: Observable<string[]>;
@@ -55,11 +54,12 @@ export class SearchFacetFilterComponent implements OnInit, OnDestroy {
this.subs.push(searchOptions.subscribe((options) => this.updateFilterValueList(options)));
const facetValues = Observable.combineLatest(searchOptions, this.currentPage, (options, page) => {
return {values: this.searchService.getFacetValuesFor(this.filterConfig, page, options), page: page};
return {
values: this.searchService.getFacetValuesFor(this.filterConfig, page, options),
page: page
};
});
this.subs.push(facetValues.subscribe((facetOutcome) => {
const newValues$ = facetOutcome.values;
@@ -86,7 +86,6 @@ export class SearchFacetFilterComponent implements OnInit, OnDestroy {
}
updateFilterValueList(options: SearchOptions) {
// this.showFirstPageOnly();
this.animationState = 'loading';
this.collapseNextUpdate = true;
this.filter = '';

View File

@@ -42,6 +42,10 @@ describe('SearchFilterService', () => {
addQueryParameterValue: (param: string, value: string) => {
},
getQueryParameterValues: (param: string) => {
return Observable.of({});
},
getQueryParamsWithPrefix: (param: string) => {
return Observable.of({});
}
/* tslint:enable:no-empty */
};

View File

@@ -23,5 +23,4 @@ import {
@renderFacetFor(FilterType.hierarchy)
export class SearchHierarchyFilterComponent extends SearchFacetFilterComponent implements OnInit {
currentPage: Observable<number>;
}

View File

@@ -2,7 +2,7 @@ import { ChangeDetectionStrategy, NO_ERRORS_SCHEMA } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { TranslateModule } from '@ngx-translate/core';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { FILTER_CONFIG, SearchFilterService, SELECTED_VALUES } from '../search-filter.service';
import { FILTER_CONFIG, SearchFilterService } from '../search-filter.service';
import { SearchFilterConfig } from '../../../search-service/search-filter-config.model';
import { FilterType } from '../../../search-service/filter-type.model';
import { FacetValue } from '../../../search-service/facet-value.model';
@@ -13,13 +13,13 @@ import { SearchServiceStub } from '../../../../shared/testing/search-service-stu
import { RemoteData } from '../../../../core/data/remote-data';
import { PaginatedList } from '../../../../core/data/paginated-list';
import { RouterStub } from '../../../../shared/testing/router-stub';
import { ActivatedRoute, Router } from '@angular/router';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Router } from '@angular/router';
import { PageInfo } from '../../../../core/shared/page-info.model';
import { SearchRangeFilterComponent } from './search-range-filter.component';
import { MockActivatedRoute } from '../../../../shared/mocks/mock-active-router';
import { RouteService } from '../../../../shared/services/route.service';
import { RemoteDataBuildService } from '../../../../core/cache/builders/remote-data-build.service';
describe('SearchFacetFilterComponent', () => {
describe('SearchRangeFilterComponent', () => {
let comp: SearchRangeFilterComponent;
let fixture: ComponentFixture<SearchRangeFilterComponent>;
const minSuffix = '.min';
@@ -55,12 +55,11 @@ describe('SearchFacetFilterComponent', () => {
];
const searchLink = '/search';
const selectedValues = [value1];
const selectedValues = Observable.of([value1]);
let filterService;
let searchService;
let router;
const page = Observable.of(0);
const activatedRouteStub = new MockActivatedRoute();
const mockValues = Observable.of(new RemoteData(false, false, true, null, new PaginatedList(new PageInfo(), values)));
beforeEach(async(() => {
@@ -71,10 +70,11 @@ describe('SearchFacetFilterComponent', () => {
{ provide: SearchService, useValue: new SearchServiceStub(searchLink) },
{ provide: Router, useValue: new RouterStub() },
{ provide: FILTER_CONFIG, useValue: mockFilterConfig },
{ provide: SELECTED_VALUES, useValue: selectedValues },
{ provide: ActivatedRoute, useValue: activatedRouteStub },
{ provide: RemoteDataBuildService, useValue: {aggregate: () => Observable.of({})} },
{ provide: RouteService, useValue: {getQueryParameterValue: () => Observable.of({})} },
{
provide: SearchFilterService, useValue: {
getSelectedValuesForFilter: () => selectedValues,
isFilterActiveWithValue: (paramName: string, filterValue: string) => true,
getPage: (paramName: string) => page,
/* tslint:disable:no-empty */
@@ -97,7 +97,6 @@ describe('SearchFacetFilterComponent', () => {
fixture = TestBed.createComponent(SearchRangeFilterComponent);
comp = fixture.componentInstance; // SearchPageComponent test instance
comp.filterValues = [mockValues];
comp.filterValues$ = new BehaviorSubject(comp.filterValues);
filterService = (comp as any).filterService;
searchService = (comp as any).searchService;
spyOn(searchService, 'getFacetValuesFor').and.returnValue(mockValues);
@@ -107,32 +106,40 @@ describe('SearchFacetFilterComponent', () => {
describe('when the getAddParams method is called wih a value', () => {
it('should return the selectedValue list with the new parameter value', () => {
const result = comp.getAddParams(value3);
const result$ = comp.getAddParams(value3);
result$.subscribe((result) => {
expect(result[mockFilterConfig.paramName + minSuffix]).toEqual(['1990']);
expect(result[mockFilterConfig.paramName + maxSuffix]).toEqual(['1992']);
});
});
});
describe('when the getRemoveParams method is called wih a value', () => {
it('should return the selectedValue list with the parameter value left out', () => {
const result = comp.getRemoveParams(value1);
const result$ = comp.getRemoveParams(value1);
result$.subscribe((result) => {
expect(result[mockFilterConfig.paramName + minSuffix]).toBeNull();
expect(result[mockFilterConfig.paramName + maxSuffix]).toBeNull();
});
});
});
describe('when the onSubmit method is called with data', () => {
const searchUrl = '/search/path';
comp.range = [1900, 1950];
// const data = { [mockFilterConfig.paramName + minSuffix]: '1900', [mockFilterConfig.paramName + maxSuffix]: '1950' };
beforeEach(() => {
comp.range = [1900, 1950];
spyOn(comp, 'getSearchLink').and.returnValue(searchUrl);
comp.onSubmit();
});
it('should call navigate on the router with the right searchlink and parameters', () => {
expect(router.navigate).toHaveBeenCalledWith([searchUrl], {
queryParams: { [mockFilterConfig.paramName + minSuffix]: ['1900'], [mockFilterConfig.paramName + maxSuffix]: ['1950']},
queryParams: {
[mockFilterConfig.paramName + minSuffix]: [1900],
[mockFilterConfig.paramName + maxSuffix]: [1950]
},
queryParamsHandling: 'merge'
});
});

View File

@@ -1,5 +1,5 @@
import { isPlatformBrowser } from '@angular/common';
import { Component, Inject, OnInit, PLATFORM_ID } from '@angular/core';
import { Component, Inject, OnDestroy, OnInit, PLATFORM_ID } from '@angular/core';
import { RemoteDataBuildService } from '../../../../core/cache/builders/remote-data-build.service';
import { FilterType } from '../../../search-service/filter-type.model';
import { renderFacetFor } from '../search-filter-type-decorator';
@@ -10,9 +10,12 @@ import {
import { SearchFilterConfig } from '../../../search-service/search-filter-config.model';
import { FILTER_CONFIG, SearchFilterService } from '../search-filter.service';
import { SearchService } from '../../../search-service/search.service';
import { ActivatedRoute, Router } from '@angular/router';
import { Router } from '@angular/router';
import * as moment from 'moment';
import { Observable } from 'rxjs/Observable';
import { RouteService } from '../../../../shared/services/route.service';
import { hasValue } from '../../../../shared/empty.util';
import { Subscription } from 'rxjs/Subscription';
/**
* This component renders a simple item page.
@@ -32,10 +35,11 @@ const rangeDelimiter = '-';
})
@renderFacetFor(FilterType.range)
export class SearchRangeFilterComponent extends SearchFacetFilterComponent implements OnInit {
export class SearchRangeFilterComponent extends SearchFacetFilterComponent implements OnInit, OnDestroy {
min = 1950;
max = 2018;
range;
sub: Subscription;
constructor(protected searchService: SearchService,
protected filterService: SearchFilterService,
@@ -43,18 +47,22 @@ export class SearchRangeFilterComponent extends SearchFacetFilterComponent imple
protected rdbs: RemoteDataBuildService,
@Inject(FILTER_CONFIG) public filterConfig: SearchFilterConfig,
@Inject(PLATFORM_ID) private platformId: any,
private route: ActivatedRoute) {
private route: RouteService) {
super(searchService, filterService, rdbs, router, filterConfig);
}
ngOnInit(): void {
super.ngOnInit();
this.min = moment(this.filterConfig.minValue, dateFormats).year() || this.min;
this.max = moment(this.filterConfig.maxValue, dateFormats).year() || this.max;
const iniMin = this.route.snapshot.queryParams[this.filterConfig.paramName + minSuffix] || this.min;
const iniMax = this.route.snapshot.queryParams[this.filterConfig.paramName + maxSuffix] || this.max;
this.range = [iniMin, iniMax];
const iniMin = this.route.getQueryParameterValue(this.filterConfig.paramName + minSuffix).startWith(undefined);
const iniMax = this.route.getQueryParameterValue(this.filterConfig.paramName + maxSuffix).startWith(undefined);
this.sub = Observable.combineLatest(iniMin, iniMax, (min, max) => {
const minimum = hasValue(min) ? min : this.min;
const maximum = hasValue(max) ? max : this.max;
return [minimum, maximum]
}).subscribe((minmax) => this.range = minmax);
}
getAddParams(value: string) {
@@ -79,7 +87,6 @@ export class SearchRangeFilterComponent extends SearchFacetFilterComponent imple
);
}
onSubmit() {
const newMin = this.range[0] !== this.min ? [this.range[0]] : null;
const newMax = this.range[1] !== this.max ? [this.range[1]] : null;
@@ -101,4 +108,9 @@ export class SearchRangeFilterComponent extends SearchFacetFilterComponent imple
return isPlatformBrowser(this.platformId);
}
ngOnDestroy() {
if (hasValue(this.sub)) {
this.sub.unsubscribe();
}
}
}

View File

@@ -23,5 +23,4 @@ import { renderFacetFor } from '../search-filter-type-decorator';
@renderFacetFor(FilterType.text)
export class SearchTextFilterComponent extends SearchFacetFilterComponent implements OnInit {
currentPage: Observable<number>;
}

View File

@@ -8,6 +8,8 @@ import { FormsModule } from '@angular/forms';
import { SearchServiceStub } from '../../shared/testing/search-service-stub';
import { Observable } from 'rxjs/Observable';
import { Params } from '@angular/router';
import { ObjectKeysPipe } from '../../shared/utils/object-keys-pipe';
import { SearchFilterService } from '../search-filters/search-filter/search-filter.service';
describe('SearchLabelsComponent', () => {
let comp: SearchLabelsComponent;
@@ -30,9 +32,10 @@ describe('SearchLabelsComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [TranslateModule.forRoot(), NoopAnimationsModule, FormsModule],
declarations: [SearchLabelsComponent],
declarations: [SearchLabelsComponent, ObjectKeysPipe],
providers: [
{ provide: SearchService, useValue: new SearchServiceStub(searchLink) }
{ provide: SearchService, useValue: new SearchServiceStub(searchLink) },
{ provide: SearchFilterService, useValue: {getCurrentFrontendFilters : () => Observable.of({})} }
],
schemas: [NO_ERRORS_SCHEMA]
}).overrideComponent(SearchLabelsComponent, {

View File

@@ -38,7 +38,8 @@ describe('SearchPageComponent', () => {
const mockResults = Observable.of(['test', 'data']);
const searchServiceStub = jasmine.createSpyObj('SearchService', {
search: mockResults,
getSearchLink: '/search'
getSearchLink: '/search',
getScopes: Observable.of(['test-scope'])
});
const queryParam = 'test query';
const scopeParam = '7669c72a-3f2a-451f-a3b9-9210e7a4c02f';
@@ -91,7 +92,11 @@ describe('SearchPageComponent', () => {
useValue: jasmine.createSpyObj('SearchFilterService', {
getPaginatedSearchOptions: hot('a', {
a: paginatedSearchOptions
})
}),
getCurrentScope: hot('a', {
a: 'test-id'
}),
})
},
],

View File

@@ -26,6 +26,8 @@ import {
} from '../../core/cache/response-cache.models';
import { SearchQueryResponse } from './search-query-response.model';
import { SearchFilterConfig } from './search-filter-config.model';
import { CollectionDataService } from '../../core/data/collection-data.service';
import { CommunityDataService } from '../../core/data/community-data.service';
@Component({ template: '' })
class DummyComponent {
@@ -54,6 +56,8 @@ describe('SearchService', () => {
{ provide: RequestService, useValue: getMockRequestService() },
{ provide: RemoteDataBuildService, useValue: {} },
{ provide: HALEndpointService, useValue: {} },
{ provide: CommunityDataService, useValue: {}},
{ provide: CollectionDataService, useValue: {}},
SearchService
],
});
@@ -109,6 +113,8 @@ describe('SearchService', () => {
{ provide: RequestService, useValue: getMockRequestService() },
{ provide: RemoteDataBuildService, useValue: remoteDataBuildService },
{ provide: HALEndpointService, useValue: halService },
{ provide: CommunityDataService, useValue: {}},
{ provide: CollectionDataService, useValue: {}},
SearchService
],
});

View File

@@ -11,6 +11,8 @@ import { SearchSidebarService } from '../search-sidebar/search-sidebar.service';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { EnumKeysPipe } from '../../shared/utils/enum-keys-pipe';
import { By } from '@angular/platform-browser';
import { SearchFilterService } from '../search-filters/search-filter/search-filter.service';
import { hot } from 'jasmine-marbles';
describe('SearchSettingsComponent', () => {
@@ -28,8 +30,16 @@ describe('SearchSettingsComponent', () => {
searchOptions: { pagination: pagination, sort: sort },
search: () => mockResults
};
const queryParam = 'test query';
const scopeParam = '7669c72a-3f2a-451f-a3b9-9210e7a4c02f';
const paginatedSearchOptions = {
query: queryParam,
scope: scopeParam,
pagination,
sort
};
const activatedRouteStub = {
queryParams: Observable.of({
query: queryParam,
@@ -41,7 +51,7 @@ describe('SearchSettingsComponent', () => {
isCollapsed: Observable.of(true),
collapse: () => this.isCollapsed = Observable.of(true),
expand: () => this.isCollapsed = Observable.of(false)
}
};
beforeEach(async(() => {
TestBed.configureTestingModule({
@@ -55,6 +65,18 @@ describe('SearchSettingsComponent', () => {
provide: SearchSidebarService,
useValue: sidebarService
},
{
provide: SearchFilterService,
useValue: jasmine.createSpyObj('SearchFilterService', {
getPaginatedSearchOptions: hot('a', {
a: paginatedSearchOptions
}),
getCurrentScope: hot('a', {
a: 'test-id'
}),
})
},
],
schemas: [NO_ERRORS_SCHEMA]
}).compileComponents();
@@ -77,15 +99,18 @@ describe('SearchSettingsComponent', () => {
const orderSetting = fixture.debugElement.query(By.css('div.result-order-settings'));
expect(orderSetting).toBeDefined();
const childElements = orderSetting.query(By.css('.form-control')).children;
expect(childElements.length).toEqual(2);
expect(childElements.length).toEqual(comp.searchOptionPossibilities.length);
});
it('it should show the size settings with the respective selectable options', () => {
(comp as any).filterService.getPaginatedSearchOptions().first().subscribe((options) => {
fixture.detectChanges()
const pageSizeSetting = fixture.debugElement.query(By.css('div.page-size-settings'));
expect(pageSizeSetting).toBeDefined();
const childElements = pageSizeSetting.query(By.css('.form-control')).children;
expect(childElements.length).toEqual(7);
expect(childElements.length).toEqual(options.pagination.pageSizeOptions.length);
}
)
});
it('should have the proper order value selected by default', () => {

View File

@@ -1,11 +1,9 @@
import { Component, Input, OnDestroy, OnInit } from '@angular/core';
import { Component, Input, OnInit } from '@angular/core';
import { SearchService } from '../search-service/search.service';
import { SortDirection, SortOptions } from '../../core/cache/models/sort-options.model';
import { ActivatedRoute, NavigationExtras, Router } from '@angular/router';
import { PaginatedSearchOptions } from '../paginated-search-options.model';
import { SearchFilterService } from '../search-filters/search-filter/search-filter.service';
import { hasValue } from '../../shared/empty.util';
import { Subscription } from 'rxjs/Subscription';
@Component({
selector: 'ds-search-settings',
@@ -25,8 +23,6 @@ export class SearchSettingsComponent implements OnInit {
public pageSize;
@Input() public pageSizeOptions;
private sub: Subscription;
private scope: string;
query: string;
page: number;
direction: SortDirection;
@@ -49,10 +45,9 @@ export class SearchSettingsComponent implements OnInit {
}
ngOnInit(): void {
this.filterService.getPaginatedSearchOptions(this.defaults).first().subscribe((options) => {
this.filterService.getPaginatedSearchOptions(this.defaults).subscribe((options) => {
this.direction = options.sort.direction;
this.field = options.sort.field;
this.pageSize = options.pagination.pageSize;
this.searchOptions = options;
this.pageSize = options.pagination.pageSize;
this.pageSizeOptions = options.pagination.pageSizeOptions
@@ -81,6 +76,4 @@ export class SearchSettingsComponent implements OnInit {
};
this.router.navigate([ '/search' ], navigationExtras);
}
}

View File

@@ -34,7 +34,8 @@ describe('LoadingComponent (inline template)', () => {
fixture = TestBed.createComponent(LoadingComponent);
comp = fixture.componentInstance; // LoadingComponent test instance
comp.message = 'test message';
fixture.detectChanges();
// query for the message <label> by CSS element selector
de = fixture.debugElement.query(By.css('label'));
el = de.nativeElement;