From 79caf1533c3e90a31ab8b15b360187f4df359dc9 Mon Sep 17 00:00:00 2001 From: Lotte Hofstede Date: Thu, 5 Apr 2018 08:49:36 +0200 Subject: [PATCH] fixes for existing tested --- .../search-facet-filter.component.html | 4 +- .../search-filter.service.spec.ts | 32 ------- .../search-filters.component.spec.ts | 16 ++-- .../+search-page/search-page.component.html | 18 ++-- .../search-page.component.spec.ts | 85 +++++-------------- src/app/+search-page/search-page.component.ts | 43 +++------- .../core/shared/hal-endpoint.service.spec.ts | 18 +--- .../search-form/search-form.component.spec.ts | 2 +- .../search-form/search-form.component.ts | 6 +- 9 files changed, 56 insertions(+), 168 deletions(-) diff --git a/src/app/+search-page/search-filters/search-filter/search-facet-filter/search-facet-filter.component.html b/src/app/+search-page/search-filters/search-filter/search-facet-filter/search-facet-filter.component.html index aa71c5c24c..8687aeb25b 100644 --- a/src/app/+search-page/search-filters/search-filter/search-facet-filter/search-facet-filter.component.html +++ b/src/app/+search-page/search-filters/search-filter/search-facet-filter/search-facet-filter.component.html @@ -32,7 +32,7 @@ [action]="getCurrentUrl()"> + [placeholder]="'search.filters.filter.' + filterConfig.name + '.placeholder'| translate" [ngModelOptions]="{standalone: true}"/> - \ No newline at end of file + 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 0ab1e4319d..26eb961c53 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 @@ -159,28 +159,6 @@ describe('SearchFilterService', () => { }); }); - describe('when the getQueryParamsWithout method is called', () => { - beforeEach(() => { - spyOn(routeServiceStub, 'removeQueryParameterValue'); - service.getQueryParamsWithout(mockFilterConfig, value1); - }); - - it('should call removeQueryParameterValue on the route service with the same parameters', () => { - expect(routeServiceStub.removeQueryParameterValue).toHaveBeenCalledWith(mockFilterConfig.paramName, value1); - }); - }); - - describe('when the getQueryParamsWith method is called', () => { - beforeEach(() => { - spyOn(routeServiceStub, 'addQueryParameterValue'); - service.getQueryParamsWith(mockFilterConfig, value1); - }); - - it('should call addQueryParameterValue on the route service with the same parameters', () => { - expect(routeServiceStub.addQueryParameterValue).toHaveBeenCalledWith(mockFilterConfig.paramName, value1); - }); - }); - describe('when the getSelectedValuesForFilter method is called', () => { beforeEach(() => { spyOn(routeServiceStub, 'getQueryParameterValues'); @@ -192,14 +170,4 @@ describe('SearchFilterService', () => { }); }); - describe('when the uiSearchRoute method is called', () => { - let link: string; - beforeEach(() => { - link = service.searchLink; - }); - - it('should return the value of uiSearchRoute in the search service', () => { - expect(link).toEqual(searchServiceStub.uiSearchRoute); - }); - }); }); diff --git a/src/app/+search-page/search-filters/search-filters.component.spec.ts b/src/app/+search-page/search-filters/search-filters.component.spec.ts index 0bdee94634..64c2ea5332 100644 --- a/src/app/+search-page/search-filters/search-filters.component.spec.ts +++ b/src/app/+search-page/search-filters/search-filters.component.spec.ts @@ -4,6 +4,7 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; import { TranslateModule } from '@ngx-translate/core'; import { NoopAnimationsModule } from '@angular/platform-browser/animations'; +import { SearchFilterService } from './search-filter/search-filter.service'; import { SearchFiltersComponent } from './search-filters.component'; import { SearchService } from '../search-service/search.service'; import { Observable } from 'rxjs/Observable'; @@ -22,6 +23,9 @@ describe('SearchFiltersComponent', () => { } /* tslint:enable:no-empty */ }; + const searchFilterServiceStub = jasmine.createSpyObj('SearchFilterService', { + getCurrentFilters: Observable.of({}) + }); beforeEach(async(() => { TestBed.configureTestingModule({ @@ -29,6 +33,7 @@ describe('SearchFiltersComponent', () => { declarations: [SearchFiltersComponent], providers: [ { provide: SearchService, useValue: searchServiceStub }, + { provide: SearchFilterService, useValue: searchFilterServiceStub }, ], schemas: [NO_ERRORS_SCHEMA] @@ -44,17 +49,6 @@ describe('SearchFiltersComponent', () => { searchService = (comp as any).searchService; }); - describe('when the getClearFiltersQueryParams method is called', () => { - beforeEach(() => { - spyOn(searchService, 'getClearFiltersQueryParams'); - comp.getClearFiltersQueryParams(); - }); - - it('should call getClearFiltersQueryParams on the searchService', () => { - expect(searchService.getClearFiltersQueryParams).toHaveBeenCalled() - }); - }); - describe('when the getSearchLink method is called', () => { beforeEach(() => { spyOn(searchService, 'getSearchLink'); diff --git a/src/app/+search-page/search-page.component.html b/src/app/+search-page/search-page.component.html index 81f0c78527..c50bb30696 100644 --- a/src/app/+search-page/search-page.component.html +++ b/src/app/+search-page/search-page.component.html @@ -1,22 +1,22 @@
- + [resultCount]="(resultsRD$ | async)?.pageInfo?.totalElements">
+ [scopes]="(scopeListRD$ | async)?.payload?.page">
- @@ -29,8 +29,8 @@ | translate}}
- +
diff --git a/src/app/+search-page/search-page.component.spec.ts b/src/app/+search-page/search-page.component.spec.ts index 8cd041eabb..cc53e18871 100644 --- a/src/app/+search-page/search-page.component.spec.ts +++ b/src/app/+search-page/search-page.component.spec.ts @@ -4,12 +4,14 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; import { Store } from '@ngrx/store'; import { TranslateModule } from '@ngx-translate/core'; +import { cold, hot } from 'jasmine-marbles'; import { Observable } from 'rxjs/Observable'; import { SortDirection, SortOptions } from '../core/cache/models/sort-options.model'; import { CommunityDataService } from '../core/data/community-data.service'; import { Community } from '../core/shared/community.model'; import { HostWindowService } from '../shared/host-window.service'; import { PaginationComponentOptions } from '../shared/pagination/pagination-component-options.model'; +import { PaginatedSearchOptions } from './paginated-search-options.model'; import { SearchPageComponent } from './search-page.component'; import { SearchService } from './search-service/search.service'; import { NoopAnimationsModule } from '@angular/platform-browser/animations'; @@ -35,12 +37,17 @@ describe('SearchPageComponent', () => { pagination.pageSize = 10; const sort: SortOptions = new SortOptions(); const mockResults = Observable.of(['test', 'data']); - const searchServiceStub = { - searchOptions:{ pagination: pagination, sort: sort }, - search: () => mockResults - }; + const searchServiceStub = jasmine.createSpyObj('SearchService', { + 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, @@ -51,20 +58,8 @@ describe('SearchPageComponent', () => { isCollapsed: Observable.of(true), collapse: () => this.isCollapsed = Observable.of(true), expand: () => this.isCollapsed = Observable.of(false) - } - - const mockCommunityList = []; - const communityDataServiceStub = { - findAll: () => Observable.of(mockCommunityList), - findById: () => Observable.of(new Community()) }; - class RouterStub { - navigateByUrl(url: string) { - return url; - } - } - beforeEach(async(() => { TestBed.configureTestingModule({ imports: [TranslateModule.forRoot(), RouterTestingModule.withRoutes([]), NoopAnimationsModule, NgbCollapseModule.forRoot()], @@ -92,7 +87,11 @@ describe('SearchPageComponent', () => { }, { provide: SearchFilterService, - useValue: {} + useValue: jasmine.createSpyObj('SearchFilterService', { + getPaginatedSearchOptions: hot('a', { + a: paginatedSearchOptions + }) + }) }, ], schemas: [NO_ERRORS_SCHEMA] @@ -108,54 +107,10 @@ describe('SearchPageComponent', () => { searchServiceObject = (comp as any).service; }); - it('should set the scope and query based on the route parameters', () => { - expect(comp.query).toBe(queryParam); - expect((comp as any).scope).toBe(scopeParam); - }); - - describe('when update search results is called', () => { - let paginationUpdate; - let sortUpdate; - beforeEach(() => { - paginationUpdate = Object.assign( - {}, - new PaginationComponentOptions(), - { - currentPage: 5, - pageSize: 15 - } - ); - sortUpdate = Object.assign({}, - new SortOptions(), - { - direction: SortDirection.Ascending, - field: 'test-field' - } - ); - }); - - it('should call the search function of the search service with the right parameters', () => { - spyOn(searchServiceObject, 'search').and.callThrough(); - - (comp as any).updateSearchResults({ - pagination: pagination, - sort: sort - }); - - expect(searchServiceObject.search).toHaveBeenCalledWith(queryParam, scopeParam, { - pagination: pagination, - sort: sort - }); - }); - - it('should update the results', () => { - spyOn(searchServiceObject, 'search').and.callThrough(); - - (comp as any).updateSearchResults({}); - - expect(comp.resultsRDObs as any).toBe(mockResults); - }); - + it('should get the scope and query from the route parameters', () => { + expect(comp.searchOptions$).toBeObservable(cold('b', { + b: paginatedSearchOptions + })); }); describe('when the closeSidebar event is emitted clicked in mobile view', () => { diff --git a/src/app/+search-page/search-page.component.ts b/src/app/+search-page/search-page.component.ts index 57fb02b730..2a903f884a 100644 --- a/src/app/+search-page/search-page.component.ts +++ b/src/app/+search-page/search-page.component.ts @@ -1,6 +1,6 @@ import { ChangeDetectionStrategy, Component, OnDestroy, OnInit } from '@angular/core'; -import { ActivatedRoute } from '@angular/router'; import { Observable } from 'rxjs/Observable'; +import { flatMap, } from 'rxjs/operators'; import { SortOptions } from '../core/cache/models/sort-options.model'; import { CommunityDataService } from '../core/data/community-data.service'; import { PaginatedList } from '../core/data/paginated-list'; @@ -8,14 +8,12 @@ import { RemoteData } from '../core/data/remote-data'; import { Community } from '../core/shared/community.model'; import { DSpaceObject } from '../core/shared/dspace-object.model'; import { pushInOut } from '../shared/animations/push'; -import { isNotEmpty } from '../shared/empty.util'; import { HostWindowService } from '../shared/host-window.service'; -import { PaginationComponentOptions } from '../shared/pagination/pagination-component-options.model'; -import { SearchOptions, ViewMode } from './search-options.model'; +import { PaginatedSearchOptions } from './paginated-search-options.model'; +import { SearchFilterService } from './search-filters/search-filter/search-filter.service'; import { SearchResult } from './search-result.model'; import { SearchService } from './search-service/search.service'; import { SearchSidebarService } from './search-sidebar/search-sidebar.service'; -import { SearchFilterService } from './search-filters/search-filter/search-filter.service'; /** * This component renders a simple item page. @@ -30,19 +28,14 @@ import { SearchFilterService } from './search-filters/search-filter/search-filte changeDetection: ChangeDetectionStrategy.OnPush, animations: [pushInOut] }) -export class SearchPageComponent implements OnInit, OnDestroy { +export class SearchPageComponent implements OnInit { - private sub; - private scope: string; - - query: string; - scopeObjectRDObs: Observable>; - resultsRDObs: Observable>>>; + resultsRD$: Observable>>>; currentParams = {}; - searchOptions: SearchOptions; + searchOptions$: Observable; sortConfig: SortOptions; - scopeListRDObs: Observable>>; - isMobileView: Observable; + scopeListRD$: Observable>>; + isMobileView$: Observable; pageSize; pageSizeOptions; defaults = { @@ -58,27 +51,19 @@ export class SearchPageComponent implements OnInit, OnDestroy { private sidebarService: SearchSidebarService, private windowService: HostWindowService, private filterService: SearchFilterService) { - this.isMobileView = Observable.combineLatest( + this.isMobileView$ = Observable.combineLatest( this.windowService.isXs(), this.windowService.isSm(), ((isXs, isSm) => isXs || isSm) ); - this.scopeListRDObs = communityService.findAll(); + this.scopeListRD$ = communityService.findAll(); } ngOnInit(): void { - this.sub = this.filterService.getPaginatedSearchOptions(this.defaults).subscribe((options) => { - this.updateSearchResults(options); - }); - } - - private updateSearchResults(searchOptions) { - this.resultsRDObs = this.service.search(searchOptions); - this.searchOptions = searchOptions; - } - - ngOnDestroy() { - this.sub.unsubscribe(); + this.searchOptions$ = this.filterService.getPaginatedSearchOptions(this.defaults); + this.resultsRD$ = this.searchOptions$.pipe( + flatMap((searchOptions) => this.service.search(searchOptions)) + ); } public closeSidebar(): void { diff --git a/src/app/core/shared/hal-endpoint.service.spec.ts b/src/app/core/shared/hal-endpoint.service.spec.ts index 479e15b52a..0c2afe938b 100644 --- a/src/app/core/shared/hal-endpoint.service.spec.ts +++ b/src/app/core/shared/hal-endpoint.service.spec.ts @@ -17,18 +17,6 @@ describe('HALEndpointService', () => { }; const linkPath = 'test'; - /* tslint:disable:no-shadowed-variable */ - class TestService extends HALEndpointService { - - constructor(private responseCache: ResponseCacheService, - private requestService: RequestService, - private EnvConfig: GlobalConfig) { - super(responseCache, requestService, EnvConfig); - } - } - - /* tslint:enable:no-shadowed-variable */ - describe('getRootEndpointMap', () => { beforeEach(() => { responseCache = jasmine.createSpyObj('responseCache', { @@ -45,7 +33,7 @@ describe('HALEndpointService', () => { rest: { baseUrl: 'https://rest.api/' } } as any; - service = new TestService( + service = new HALEndpointService( responseCache, requestService, envConfig @@ -73,7 +61,7 @@ describe('HALEndpointService', () => { rest: { baseUrl: 'https://rest.api/' } } as any; - service = new TestService( + service = new HALEndpointService( responseCache, requestService, envConfig @@ -100,7 +88,7 @@ describe('HALEndpointService', () => { describe('isEnabledOnRestApi', () => { beforeEach(() => { - service = new TestService( + service = new HALEndpointService( responseCache, requestService, envConfig diff --git a/src/app/shared/search-form/search-form.component.spec.ts b/src/app/shared/search-form/search-form.component.spec.ts index ee1a8cd8f5..d148429b01 100644 --- a/src/app/shared/search-form/search-form.component.spec.ts +++ b/src/app/shared/search-form/search-form.component.spec.ts @@ -69,7 +69,7 @@ describe('SearchFormComponent', () => { fixture.detectChanges(); const testCommunity = objects[1]; - comp.scope = testCommunity; + comp.scope = testCommunity.id; fixture.detectChanges(); tick(); diff --git a/src/app/shared/search-form/search-form.component.ts b/src/app/shared/search-form/search-form.component.ts index fb3c6ba5a2..478df3bb65 100644 --- a/src/app/shared/search-form/search-form.component.ts +++ b/src/app/shared/search-form/search-form.component.ts @@ -22,10 +22,8 @@ export class SearchFormComponent { @Input() scopes: DSpaceObject[]; @Input() - set scope(dso: DSpaceObject) { - if (hasValue(dso)) { - this.selectedId = dso.id; - } + set scope(id: string) { + this.selectedId = id; } constructor(private router: Router) {