From df37e339eed1066251cf6785404612d019ae959c Mon Sep 17 00:00:00 2001 From: lotte Date: Wed, 25 Jul 2018 14:16:27 +0200 Subject: [PATCH] test fixes --- .../search-boolean-filter.component.ts | 1 - .../search-facet-filter.component.spec.ts | 80 ++++++------------- .../search-facet-filter.component.ts | 9 +-- .../search-filter.service.spec.ts | 4 + .../search-hierarchy-filter.component.ts | 1 - .../search-range-filter.component.spec.ts | 65 ++++++++------- .../search-range-filter.component.ts | 30 ++++--- .../search-text-filter.component.ts | 1 - .../search-labels.component.spec.ts | 7 +- .../search-page.component.spec.ts | 21 +++-- .../search-service/search.service.spec.ts | 6 ++ .../search-settings.component.spec.ts | 47 ++++++++--- .../search-settings.component.ts | 11 +-- .../shared/loading/loading.component.spec.ts | 3 +- 14 files changed, 155 insertions(+), 131 deletions(-) diff --git a/src/app/+search-page/search-filters/search-filter/search-boolean-filter/search-boolean-filter.component.ts b/src/app/+search-page/search-filters/search-filter/search-boolean-filter/search-boolean-filter.component.ts index 037fd7bfcd..381f7d82eb 100644 --- a/src/app/+search-page/search-filters/search-filter/search-boolean-filter/search-boolean-filter.component.ts +++ b/src/app/+search-page/search-filters/search-filter/search-boolean-filter/search-boolean-filter.component.ts @@ -22,5 +22,4 @@ import { @renderFacetFor(FilterType.boolean) export class SearchBooleanFilterComponent extends SearchFacetFilterComponent implements OnInit { - currentPage: Observable; } diff --git a/src/app/+search-page/search-filters/search-filter/search-facet-filter/search-facet-filter.component.spec.ts b/src/app/+search-page/search-filters/search-filter/search-facet-filter/search-facet-filter.component.spec.ts index 88da2a89cb..fcbc31be32 100644 --- a/src/app/+search-page/search-filters/search-filter/search-facet-filter/search-facet-filter.component.spec.ts +++ b/src/app/+search-page/search-filters/search-filter/search-facet-filter/search-facet-filter.component.spec.ts @@ -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; @@ -64,20 +64,21 @@ describe('SearchFacetFilterComponent', () => { providers: [ { provide: SearchService, useValue: new SearchServiceStub(searchLink) }, { provide: Router, useValue: new RouterStub() }, - { provide: FILTER_CONFIG, useValue: new SearchFilterConfig()}, - { provide: SELECTED_VALUES, useValue: {} }, + { provide: FILTER_CONFIG, useValue: new SearchFilterConfig() }, + { provide: RemoteDataBuildService, useValue: {aggregate: () => Observable.of({})} }, { provide: SearchFilterService, useValue: { - isFilterActiveWithValue: (paramName: string, filterValue: string) => true, - getPage: (paramName: string) => page, - /* tslint:disable:no-empty */ - incrementPage: (filterName: string) => { - }, - resetPage: (filterName: string) => { - }, - getSearchOptions: () => Observable.of({}), - /* tslint:enable:no-empty */ - } + getSelectedValuesForFilter: () => Observable.of(selectedValues), + isFilterActiveWithValue: (paramName: string, filterValue: string) => true, + getPage: (paramName: string) => page, + /* tslint:disable:no-empty */ + incrementPage: (filterName: string) => { + }, + resetPage: (filterName: string) => { + }, + getSearchOptions: () => Observable.of({}), + /* tslint:enable:no-empty */ + } } ], schemas: [NO_ERRORS_SCHEMA] @@ -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); }); @@ -183,7 +183,7 @@ describe('SearchFacetFilterComponent', () => { describe('when the onSubmit method is called with data', () => { const searchUrl = '/search/path'; const testValue = 'test'; - const data = testValue ; + const data = testValue; beforeEach(() => { spyOn(comp, 'getSearchLink').and.returnValue(searchUrl); comp.onSubmit(data); @@ -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(() => { diff --git a/src/app/+search-page/search-filters/search-filter/search-facet-filter/search-facet-filter.component.ts b/src/app/+search-page/search-filters/search-filter/search-facet-filter/search-facet-filter.component.ts index 0c2590dd97..8c9083327a 100644 --- a/src/app/+search-page/search-filters/search-filter/search-facet-filter/search-facet-filter.component.ts +++ b/src/app/+search-page/search-filters/search-filter/search-facet-filter/search-facet-filter.component.ts @@ -33,7 +33,6 @@ export class SearchFacetFilterComponent implements OnInit, OnDestroy { currentPage: Observable; isLastPage$: BehaviorSubject = new BehaviorSubject(false); filter: string; - pageChange = false; private subs: Subscription[] = []; filterSearchResults: Observable = Observable.of([]); selectedValues: Observable; @@ -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 = ''; diff --git a/src/app/+search-page/search-filters/search-filter/search-filter.service.spec.ts b/src/app/+search-page/search-filters/search-filter/search-filter.service.spec.ts index 8a7b3eddb0..b2d6847aac 100644 --- a/src/app/+search-page/search-filters/search-filter/search-filter.service.spec.ts +++ b/src/app/+search-page/search-filters/search-filter/search-filter.service.spec.ts @@ -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 */ }; diff --git a/src/app/+search-page/search-filters/search-filter/search-hierarchy-filter/search-hierarchy-filter.component.ts b/src/app/+search-page/search-filters/search-filter/search-hierarchy-filter/search-hierarchy-filter.component.ts index 0df32e1435..b469f677ce 100644 --- a/src/app/+search-page/search-filters/search-filter/search-hierarchy-filter/search-hierarchy-filter.component.ts +++ b/src/app/+search-page/search-filters/search-filter/search-hierarchy-filter/search-hierarchy-filter.component.ts @@ -23,5 +23,4 @@ import { @renderFacetFor(FilterType.hierarchy) export class SearchHierarchyFilterComponent extends SearchFacetFilterComponent implements OnInit { - currentPage: Observable; } diff --git a/src/app/+search-page/search-filters/search-filter/search-range-filter/search-range-filter.component.spec.ts b/src/app/+search-page/search-filters/search-filter/search-range-filter/search-range-filter.component.spec.ts index 445d3720fc..5915568033 100644 --- a/src/app/+search-page/search-filters/search-filter/search-range-filter/search-range-filter.component.spec.ts +++ b/src/app/+search-page/search-filters/search-filter/search-range-filter/search-range-filter.component.spec.ts @@ -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; 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(() => { @@ -70,21 +69,22 @@ describe('SearchFacetFilterComponent', () => { providers: [ { 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: FILTER_CONFIG, useValue: mockFilterConfig }, + { provide: RemoteDataBuildService, useValue: {aggregate: () => Observable.of({})} }, + { provide: RouteService, useValue: {getQueryParameterValue: () => Observable.of({})} }, { provide: SearchFilterService, useValue: { - isFilterActiveWithValue: (paramName: string, filterValue: string) => true, - getPage: (paramName: string) => page, - /* tslint:disable:no-empty */ - incrementPage: (filterName: string) => { - }, - resetPage: (filterName: string) => { - }, - getSearchOptions: () => Observable.of({}), - /* tslint:enable:no-empty */ - } + getSelectedValuesForFilter: () => selectedValues, + isFilterActiveWithValue: (paramName: string, filterValue: string) => true, + getPage: (paramName: string) => page, + /* tslint:disable:no-empty */ + incrementPage: (filterName: string) => { + }, + resetPage: (filterName: string) => { + }, + getSearchOptions: () => Observable.of({}), + /* tslint:enable:no-empty */ + } } ], schemas: [NO_ERRORS_SCHEMA] @@ -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); - expect(result[mockFilterConfig.paramName + minSuffix]).toEqual(['1990']); - expect(result[mockFilterConfig.paramName + maxSuffix]).toEqual(['1992']); + 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); - expect(result[mockFilterConfig.paramName + minSuffix]).toBeNull(); - expect(result[mockFilterConfig.paramName + maxSuffix]).toBeNull(); + 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' }); }); diff --git a/src/app/+search-page/search-filters/search-filter/search-range-filter/search-range-filter.component.ts b/src/app/+search-page/search-filters/search-filter/search-range-filter/search-range-filter.component.ts index e04628fc38..4a04d13308 100644 --- a/src/app/+search-page/search-filters/search-filter/search-range-filter/search-range-filter.component.ts +++ b/src/app/+search-page/search-filters/search-filter/search-range-filter/search-range-filter.component.ts @@ -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(); + } + } } diff --git a/src/app/+search-page/search-filters/search-filter/search-text-filter/search-text-filter.component.ts b/src/app/+search-page/search-filters/search-filter/search-text-filter/search-text-filter.component.ts index 7d269092db..bae124e1bf 100644 --- a/src/app/+search-page/search-filters/search-filter/search-text-filter/search-text-filter.component.ts +++ b/src/app/+search-page/search-filters/search-filter/search-text-filter/search-text-filter.component.ts @@ -23,5 +23,4 @@ import { renderFacetFor } from '../search-filter-type-decorator'; @renderFacetFor(FilterType.text) export class SearchTextFilterComponent extends SearchFacetFilterComponent implements OnInit { - currentPage: Observable; } diff --git a/src/app/+search-page/search-labels/search-labels.component.spec.ts b/src/app/+search-page/search-labels/search-labels.component.spec.ts index e3965e22a4..3df5b7788c 100644 --- a/src/app/+search-page/search-labels/search-labels.component.spec.ts +++ b/src/app/+search-page/search-labels/search-labels.component.spec.ts @@ -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, { diff --git a/src/app/+search-page/search-page.component.spec.ts b/src/app/+search-page/search-page.component.spec.ts index 96820e8a0b..a6b210b393 100644 --- a/src/app/+search-page/search-page.component.spec.ts +++ b/src/app/+search-page/search-page.component.spec.ts @@ -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'; @@ -76,11 +77,11 @@ describe('SearchPageComponent', () => { }, { provide: HostWindowService, useValue: jasmine.createSpyObj('hostWindowService', - { - isXs: Observable.of(true), - isSm: Observable.of(false), - isXsOrSm: Observable.of(true) - }) + { + isXs: Observable.of(true), + isSm: Observable.of(false), + isXsOrSm: Observable.of(true) + }) }, { provide: SearchSidebarService, @@ -91,13 +92,17 @@ describe('SearchPageComponent', () => { useValue: jasmine.createSpyObj('SearchFilterService', { getPaginatedSearchOptions: hot('a', { a: paginatedSearchOptions - }) + }), + getCurrentScope: hot('a', { + a: 'test-id' + }), + }) }, ], schemas: [NO_ERRORS_SCHEMA] }).overrideComponent(SearchPageComponent, { - set: { changeDetection: ChangeDetectionStrategy.Default } + set: { changeDetection: ChangeDetectionStrategy.Default } }).compileComponents(); })); diff --git a/src/app/+search-page/search-service/search.service.spec.ts b/src/app/+search-page/search-service/search.service.spec.ts index 083758e22b..c611b21a85 100644 --- a/src/app/+search-page/search-service/search.service.spec.ts +++ b/src/app/+search-page/search-service/search.service.spec.ts @@ -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 ], }); diff --git a/src/app/+search-page/search-settings/search-settings.component.spec.ts b/src/app/+search-page/search-settings/search-settings.component.spec.ts index 2330b62669..b21d41e305 100644 --- a/src/app/+search-page/search-settings/search-settings.component.spec.ts +++ b/src/app/+search-page/search-settings/search-settings.component.spec.ts @@ -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', () => { @@ -23,13 +25,21 @@ describe('SearchSettingsComponent', () => { pagination.currentPage = 1; pagination.pageSize = 10; const sort: SortOptions = new SortOptions('score', SortDirection.DESC); - const mockResults = [ 'test', 'data' ]; + const mockResults = ['test', 'data']; const searchServiceStub = { 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,12 +51,12 @@ describe('SearchSettingsComponent', () => { isCollapsed: Observable.of(true), collapse: () => this.isCollapsed = Observable.of(true), expand: () => this.isCollapsed = Observable.of(false) - } + }; beforeEach(async(() => { TestBed.configureTestingModule({ - imports: [ TranslateModule.forRoot(), RouterTestingModule.withRoutes([]) ], - declarations: [ SearchSettingsComponent, EnumKeysPipe ], + imports: [TranslateModule.forRoot(), RouterTestingModule.withRoutes([])], + declarations: [SearchSettingsComponent, EnumKeysPipe], providers: [ { provide: SearchService, useValue: searchServiceStub }, @@ -55,8 +65,20 @@ 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 ] + 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', () => { - 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); + (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(options.pagination.pageSizeOptions.length); + } + ) }); it('should have the proper order value selected by default', () => { diff --git a/src/app/+search-page/search-settings/search-settings.component.ts b/src/app/+search-page/search-settings/search-settings.component.ts index 56e2793b01..ba511bf15c 100644 --- a/src/app/+search-page/search-settings/search-settings.component.ts +++ b/src/app/+search-page/search-settings/search-settings.component.ts @@ -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); } - - } diff --git a/src/app/shared/loading/loading.component.spec.ts b/src/app/shared/loading/loading.component.spec.ts index aca9673282..3c58fa3e9e 100644 --- a/src/app/shared/loading/loading.component.spec.ts +++ b/src/app/shared/loading/loading.component.spec.ts @@ -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