fixes for existing tested

This commit is contained in:
Lotte Hofstede
2018-04-05 08:49:36 +02:00
parent a232127422
commit 79caf1533c
9 changed files with 56 additions and 168 deletions

View File

@@ -32,7 +32,7 @@
[action]="getCurrentUrl()"> [action]="getCurrentUrl()">
<input type="text" [(ngModel)]="filter" [name]="filterConfig.paramName" class="form-control" <input type="text" [(ngModel)]="filter" [name]="filterConfig.paramName" class="form-control"
aria-label="New filter input" aria-label="New filter input"
[placeholder]="'search.filters.filter.' + filterConfig.name + '.placeholder'| translate"/> [placeholder]="'search.filters.filter.' + filterConfig.name + '.placeholder'| translate" [ngModelOptions]="{standalone: true}"/>
<input type="submit" class="d-none"/> <input type="submit" class="d-none"/>
</form> </form>
</div> </div>

View File

@@ -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', () => { describe('when the getSelectedValuesForFilter method is called', () => {
beforeEach(() => { beforeEach(() => {
spyOn(routeServiceStub, 'getQueryParameterValues'); 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);
});
});
}); });

View File

@@ -4,6 +4,7 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing'; import { RouterTestingModule } from '@angular/router/testing';
import { TranslateModule } from '@ngx-translate/core'; import { TranslateModule } from '@ngx-translate/core';
import { NoopAnimationsModule } from '@angular/platform-browser/animations'; import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { SearchFilterService } from './search-filter/search-filter.service';
import { SearchFiltersComponent } from './search-filters.component'; import { SearchFiltersComponent } from './search-filters.component';
import { SearchService } from '../search-service/search.service'; import { SearchService } from '../search-service/search.service';
import { Observable } from 'rxjs/Observable'; import { Observable } from 'rxjs/Observable';
@@ -22,6 +23,9 @@ describe('SearchFiltersComponent', () => {
} }
/* tslint:enable:no-empty */ /* tslint:enable:no-empty */
}; };
const searchFilterServiceStub = jasmine.createSpyObj('SearchFilterService', {
getCurrentFilters: Observable.of({})
});
beforeEach(async(() => { beforeEach(async(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
@@ -29,6 +33,7 @@ describe('SearchFiltersComponent', () => {
declarations: [SearchFiltersComponent], declarations: [SearchFiltersComponent],
providers: [ providers: [
{ provide: SearchService, useValue: searchServiceStub }, { provide: SearchService, useValue: searchServiceStub },
{ provide: SearchFilterService, useValue: searchFilterServiceStub },
], ],
schemas: [NO_ERRORS_SCHEMA] schemas: [NO_ERRORS_SCHEMA]
@@ -44,17 +49,6 @@ describe('SearchFiltersComponent', () => {
searchService = (comp as any).searchService; 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', () => { describe('when the getSearchLink method is called', () => {
beforeEach(() => { beforeEach(() => {
spyOn(searchService, 'getSearchLink'); spyOn(searchService, 'getSearchLink');

View File

@@ -1,22 +1,22 @@
<div class="container"> <div class="container">
<div class="search-page row"> <div class="search-page row">
<ds-search-sidebar *ngIf="!(isMobileView | async)" class="col-3 sidebar-md-sticky" <ds-search-sidebar *ngIf="!(isMobileView$ | async)" class="col-3 sidebar-md-sticky"
id="search-sidebar" id="search-sidebar"
[resultCount]="(resultsRDObs | async)?.pageInfo?.totalElements"></ds-search-sidebar> [resultCount]="(resultsRD$ | async)?.pageInfo?.totalElements"></ds-search-sidebar>
<div class="col-12 col-md-9"> <div class="col-12 col-md-9">
<ds-search-form id="search-form" <ds-search-form id="search-form"
[query]="query" [query]="(searchOptions$ | async)?.query"
[scope]="(scopeObjectRDObs | async)?.payload" [scope]="(searchOptions$ | async)?.scope"
[currentParams]="currentParams" [currentParams]="currentParams"
[scopes]="(scopeListRDObs | async)?.payload?.page"> [scopes]="(scopeListRD$ | async)?.payload?.page">
</ds-search-form> </ds-search-form>
<div class="row"> <div class="row">
<div id="search-body" <div id="search-body"
class="row-offcanvas row-offcanvas-left" class="row-offcanvas row-offcanvas-left"
[@pushInOut]="(isSidebarCollapsed() | async) ? 'collapsed' : 'expanded'"> [@pushInOut]="(isSidebarCollapsed() | async) ? 'collapsed' : 'expanded'">
<ds-search-sidebar *ngIf="(isMobileView | async)" class="col-12" <ds-search-sidebar *ngIf="(isMobileView$ | async)" class="col-12"
id="search-sidebar-sm" id="search-sidebar-sm"
[resultCount]="(resultsRDObs | async)?.pageInfo?.totalElements" [resultCount]="(resultsRD$ | async)?.pageInfo?.totalElements"
(toggleSidebar)="closeSidebar()" (toggleSidebar)="closeSidebar()"
[ngClass]="{'active': !(isSidebarCollapsed() | async)}"> [ngClass]="{'active': !(isSidebarCollapsed() | async)}">
</ds-search-sidebar> </ds-search-sidebar>
@@ -29,8 +29,8 @@
| translate}} | translate}}
</button> </button>
</div> </div>
<ds-search-results [searchResults]="resultsRDObs | async" <ds-search-results [searchResults]="resultsRD$ | async"
[searchConfig]="searchOptions" [sortConfig]="sortConfig"></ds-search-results> [searchConfig]="searchOptions$ | async" [sortConfig]="sortConfig"></ds-search-results>
</div> </div>
</div> </div>
</div> </div>

View File

@@ -4,12 +4,14 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing'; import { RouterTestingModule } from '@angular/router/testing';
import { Store } from '@ngrx/store'; import { Store } from '@ngrx/store';
import { TranslateModule } from '@ngx-translate/core'; import { TranslateModule } from '@ngx-translate/core';
import { cold, hot } from 'jasmine-marbles';
import { Observable } from 'rxjs/Observable'; import { Observable } from 'rxjs/Observable';
import { SortDirection, SortOptions } from '../core/cache/models/sort-options.model'; import { SortDirection, SortOptions } from '../core/cache/models/sort-options.model';
import { CommunityDataService } from '../core/data/community-data.service'; import { CommunityDataService } from '../core/data/community-data.service';
import { Community } from '../core/shared/community.model'; import { Community } from '../core/shared/community.model';
import { HostWindowService } from '../shared/host-window.service'; import { HostWindowService } from '../shared/host-window.service';
import { PaginationComponentOptions } from '../shared/pagination/pagination-component-options.model'; import { PaginationComponentOptions } from '../shared/pagination/pagination-component-options.model';
import { PaginatedSearchOptions } from './paginated-search-options.model';
import { SearchPageComponent } from './search-page.component'; import { SearchPageComponent } from './search-page.component';
import { SearchService } from './search-service/search.service'; import { SearchService } from './search-service/search.service';
import { NoopAnimationsModule } from '@angular/platform-browser/animations'; import { NoopAnimationsModule } from '@angular/platform-browser/animations';
@@ -35,12 +37,17 @@ describe('SearchPageComponent', () => {
pagination.pageSize = 10; pagination.pageSize = 10;
const sort: SortOptions = new SortOptions(); const sort: SortOptions = new SortOptions();
const mockResults = Observable.of(['test', 'data']); const mockResults = Observable.of(['test', 'data']);
const searchServiceStub = { const searchServiceStub = jasmine.createSpyObj('SearchService', {
searchOptions:{ pagination: pagination, sort: sort }, search: mockResults
search: () => mockResults });
};
const queryParam = 'test query'; const queryParam = 'test query';
const scopeParam = '7669c72a-3f2a-451f-a3b9-9210e7a4c02f'; const scopeParam = '7669c72a-3f2a-451f-a3b9-9210e7a4c02f';
const paginatedSearchOptions = {
query: queryParam,
scope: scopeParam,
pagination,
sort
};
const activatedRouteStub = { const activatedRouteStub = {
queryParams: Observable.of({ queryParams: Observable.of({
query: queryParam, query: queryParam,
@@ -51,20 +58,8 @@ describe('SearchPageComponent', () => {
isCollapsed: Observable.of(true), isCollapsed: Observable.of(true),
collapse: () => this.isCollapsed = Observable.of(true), collapse: () => this.isCollapsed = Observable.of(true),
expand: () => this.isCollapsed = Observable.of(false) 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(() => { beforeEach(async(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [TranslateModule.forRoot(), RouterTestingModule.withRoutes([]), NoopAnimationsModule, NgbCollapseModule.forRoot()], imports: [TranslateModule.forRoot(), RouterTestingModule.withRoutes([]), NoopAnimationsModule, NgbCollapseModule.forRoot()],
@@ -92,7 +87,11 @@ describe('SearchPageComponent', () => {
}, },
{ {
provide: SearchFilterService, provide: SearchFilterService,
useValue: {} useValue: jasmine.createSpyObj('SearchFilterService', {
getPaginatedSearchOptions: hot('a', {
a: paginatedSearchOptions
})
})
}, },
], ],
schemas: [NO_ERRORS_SCHEMA] schemas: [NO_ERRORS_SCHEMA]
@@ -108,54 +107,10 @@ describe('SearchPageComponent', () => {
searchServiceObject = (comp as any).service; searchServiceObject = (comp as any).service;
}); });
it('should set the scope and query based on the route parameters', () => { it('should get the scope and query from the route parameters', () => {
expect(comp.query).toBe(queryParam); expect(comp.searchOptions$).toBeObservable(cold('b', {
expect((comp as any).scope).toBe(scopeParam); b: paginatedSearchOptions
}); }));
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);
});
}); });
describe('when the closeSidebar event is emitted clicked in mobile view', () => { describe('when the closeSidebar event is emitted clicked in mobile view', () => {

View File

@@ -1,6 +1,6 @@
import { ChangeDetectionStrategy, Component, OnDestroy, OnInit } from '@angular/core'; import { ChangeDetectionStrategy, Component, OnDestroy, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Observable } from 'rxjs/Observable'; import { Observable } from 'rxjs/Observable';
import { flatMap, } from 'rxjs/operators';
import { SortOptions } from '../core/cache/models/sort-options.model'; import { SortOptions } from '../core/cache/models/sort-options.model';
import { CommunityDataService } from '../core/data/community-data.service'; import { CommunityDataService } from '../core/data/community-data.service';
import { PaginatedList } from '../core/data/paginated-list'; 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 { Community } from '../core/shared/community.model';
import { DSpaceObject } from '../core/shared/dspace-object.model'; import { DSpaceObject } from '../core/shared/dspace-object.model';
import { pushInOut } from '../shared/animations/push'; import { pushInOut } from '../shared/animations/push';
import { isNotEmpty } from '../shared/empty.util';
import { HostWindowService } from '../shared/host-window.service'; import { HostWindowService } from '../shared/host-window.service';
import { PaginationComponentOptions } from '../shared/pagination/pagination-component-options.model'; import { PaginatedSearchOptions } from './paginated-search-options.model';
import { SearchOptions, ViewMode } from './search-options.model'; import { SearchFilterService } from './search-filters/search-filter/search-filter.service';
import { SearchResult } from './search-result.model'; import { SearchResult } from './search-result.model';
import { SearchService } from './search-service/search.service'; import { SearchService } from './search-service/search.service';
import { SearchSidebarService } from './search-sidebar/search-sidebar.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. * This component renders a simple item page.
@@ -30,19 +28,14 @@ import { SearchFilterService } from './search-filters/search-filter/search-filte
changeDetection: ChangeDetectionStrategy.OnPush, changeDetection: ChangeDetectionStrategy.OnPush,
animations: [pushInOut] animations: [pushInOut]
}) })
export class SearchPageComponent implements OnInit, OnDestroy { export class SearchPageComponent implements OnInit {
private sub; resultsRD$: Observable<RemoteData<PaginatedList<SearchResult<DSpaceObject>>>>;
private scope: string;
query: string;
scopeObjectRDObs: Observable<RemoteData<DSpaceObject>>;
resultsRDObs: Observable<RemoteData<PaginatedList<SearchResult<DSpaceObject>>>>;
currentParams = {}; currentParams = {};
searchOptions: SearchOptions; searchOptions$: Observable<PaginatedSearchOptions>;
sortConfig: SortOptions; sortConfig: SortOptions;
scopeListRDObs: Observable<RemoteData<PaginatedList<Community>>>; scopeListRD$: Observable<RemoteData<PaginatedList<Community>>>;
isMobileView: Observable<boolean>; isMobileView$: Observable<boolean>;
pageSize; pageSize;
pageSizeOptions; pageSizeOptions;
defaults = { defaults = {
@@ -58,27 +51,19 @@ export class SearchPageComponent implements OnInit, OnDestroy {
private sidebarService: SearchSidebarService, private sidebarService: SearchSidebarService,
private windowService: HostWindowService, private windowService: HostWindowService,
private filterService: SearchFilterService) { private filterService: SearchFilterService) {
this.isMobileView = Observable.combineLatest( this.isMobileView$ = Observable.combineLatest(
this.windowService.isXs(), this.windowService.isXs(),
this.windowService.isSm(), this.windowService.isSm(),
((isXs, isSm) => isXs || isSm) ((isXs, isSm) => isXs || isSm)
); );
this.scopeListRDObs = communityService.findAll(); this.scopeListRD$ = communityService.findAll();
} }
ngOnInit(): void { ngOnInit(): void {
this.sub = this.filterService.getPaginatedSearchOptions(this.defaults).subscribe((options) => { this.searchOptions$ = this.filterService.getPaginatedSearchOptions(this.defaults);
this.updateSearchResults(options); this.resultsRD$ = this.searchOptions$.pipe(
}); flatMap((searchOptions) => this.service.search(searchOptions))
} );
private updateSearchResults(searchOptions) {
this.resultsRDObs = this.service.search(searchOptions);
this.searchOptions = searchOptions;
}
ngOnDestroy() {
this.sub.unsubscribe();
} }
public closeSidebar(): void { public closeSidebar(): void {

View File

@@ -17,18 +17,6 @@ describe('HALEndpointService', () => {
}; };
const linkPath = 'test'; 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', () => { describe('getRootEndpointMap', () => {
beforeEach(() => { beforeEach(() => {
responseCache = jasmine.createSpyObj('responseCache', { responseCache = jasmine.createSpyObj('responseCache', {
@@ -45,7 +33,7 @@ describe('HALEndpointService', () => {
rest: { baseUrl: 'https://rest.api/' } rest: { baseUrl: 'https://rest.api/' }
} as any; } as any;
service = new TestService( service = new HALEndpointService(
responseCache, responseCache,
requestService, requestService,
envConfig envConfig
@@ -73,7 +61,7 @@ describe('HALEndpointService', () => {
rest: { baseUrl: 'https://rest.api/' } rest: { baseUrl: 'https://rest.api/' }
} as any; } as any;
service = new TestService( service = new HALEndpointService(
responseCache, responseCache,
requestService, requestService,
envConfig envConfig
@@ -100,7 +88,7 @@ describe('HALEndpointService', () => {
describe('isEnabledOnRestApi', () => { describe('isEnabledOnRestApi', () => {
beforeEach(() => { beforeEach(() => {
service = new TestService( service = new HALEndpointService(
responseCache, responseCache,
requestService, requestService,
envConfig envConfig

View File

@@ -69,7 +69,7 @@ describe('SearchFormComponent', () => {
fixture.detectChanges(); fixture.detectChanges();
const testCommunity = objects[1]; const testCommunity = objects[1];
comp.scope = testCommunity; comp.scope = testCommunity.id;
fixture.detectChanges(); fixture.detectChanges();
tick(); tick();

View File

@@ -22,10 +22,8 @@ export class SearchFormComponent {
@Input() scopes: DSpaceObject[]; @Input() scopes: DSpaceObject[];
@Input() @Input()
set scope(dso: DSpaceObject) { set scope(id: string) {
if (hasValue(dso)) { this.selectedId = id;
this.selectedId = dso.id;
}
} }
constructor(private router: Router) { constructor(private router: Router) {