mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-11 03:53:02 +00:00
added search service test
This commit is contained in:
@@ -16,6 +16,22 @@ import { ResponseCacheService } from '../../core/cache/response-cache.service';
|
|||||||
import { ActivatedRouteStub } from '../../shared/testing/active-router-stub';
|
import { ActivatedRouteStub } from '../../shared/testing/active-router-stub';
|
||||||
import { RouterStub } from '../../shared/testing/router-stub';
|
import { RouterStub } from '../../shared/testing/router-stub';
|
||||||
import { HALEndpointService } from '../../core/shared/hal-endpoint.service';
|
import { HALEndpointService } from '../../core/shared/hal-endpoint.service';
|
||||||
|
import { Observable } from 'rxjs/Observable';
|
||||||
|
import { PaginatedSearchOptions } from '../paginated-search-options.model';
|
||||||
|
import { RemoteData } from '../../core/data/remote-data';
|
||||||
|
import { PaginatedList } from '../../core/data/paginated-list';
|
||||||
|
import { SearchResult } from '../search-result.model';
|
||||||
|
import { DSpaceObject } from '../../core/shared/dspace-object.model';
|
||||||
|
import { ResponseCacheEntry } from '../../core/cache/response-cache.reducer';
|
||||||
|
import { RequestEntry } from '../../core/data/request.reducer';
|
||||||
|
import { getMockRequestService } from '../../shared/mocks/mock-request.service';
|
||||||
|
import { getMockResponseCacheService } from '../../shared/mocks/mock-response-cache.service';
|
||||||
|
import {
|
||||||
|
FacetConfigSuccessResponse, RestResponse,
|
||||||
|
SearchSuccessResponse
|
||||||
|
} from '../../core/cache/response-cache.models';
|
||||||
|
import { SearchQueryResponse } from './search-query-response.model';
|
||||||
|
import { SearchFilterConfig } from './search-filter-config.model';
|
||||||
|
|
||||||
@Component({ template: '' })
|
@Component({ template: '' })
|
||||||
class DummyComponent {
|
class DummyComponent {
|
||||||
@@ -40,8 +56,8 @@ describe('SearchService', () => {
|
|||||||
providers: [
|
providers: [
|
||||||
{ provide: Router, useValue: router },
|
{ provide: Router, useValue: router },
|
||||||
{ provide: ActivatedRoute, useValue: route },
|
{ provide: ActivatedRoute, useValue: route },
|
||||||
{ provide: ResponseCacheService, useValue: {} },
|
{ provide: ResponseCacheService, useValue: getMockResponseCacheService() },
|
||||||
{ provide: RequestService, useValue: {} },
|
{ provide: RequestService, useValue: getMockRequestService() },
|
||||||
{ provide: RemoteDataBuildService, useValue: {} },
|
{ provide: RemoteDataBuildService, useValue: {} },
|
||||||
{ provide: HALEndpointService, useValue: {} },
|
{ provide: HALEndpointService, useValue: {} },
|
||||||
SearchService
|
SearchService
|
||||||
@@ -60,6 +76,26 @@ describe('SearchService', () => {
|
|||||||
let searchService: SearchService;
|
let searchService: SearchService;
|
||||||
const router = new RouterStub();
|
const router = new RouterStub();
|
||||||
const route = new ActivatedRouteStub();
|
const route = new ActivatedRouteStub();
|
||||||
|
|
||||||
|
const halService = {
|
||||||
|
/* tslint:disable:no-empty */
|
||||||
|
getEndpoint: () => {}
|
||||||
|
/* tslint:enable:no-empty */
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
const remoteDataBuildService = {
|
||||||
|
toRemoteDataObservable: (requestEntryObs: Observable<RequestEntry>, responseCacheObs: Observable<ResponseCacheEntry>, payloadObs: Observable<any>) => {
|
||||||
|
return Observable.combineLatest(requestEntryObs,
|
||||||
|
responseCacheObs, payloadObs, (req, res, pay) => {
|
||||||
|
return { req, res, pay };
|
||||||
|
});
|
||||||
|
},
|
||||||
|
aggregate: (input: Array<Observable<RemoteData<any>>>): Observable<RemoteData<any[]>> => {
|
||||||
|
return Observable.of(new RemoteData(false, false, true, null, []));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({
|
||||||
imports: [
|
imports: [
|
||||||
@@ -74,10 +110,10 @@ describe('SearchService', () => {
|
|||||||
providers: [
|
providers: [
|
||||||
{ provide: Router, useValue: router },
|
{ provide: Router, useValue: router },
|
||||||
{ provide: ActivatedRoute, useValue: route },
|
{ provide: ActivatedRoute, useValue: route },
|
||||||
{ provide: ResponseCacheService, useValue: {} },
|
{ provide: ResponseCacheService, useValue: getMockResponseCacheService() },
|
||||||
{ provide: RequestService, useValue: {} },
|
{ provide: RequestService, useValue: getMockRequestService() },
|
||||||
{ provide: RemoteDataBuildService, useValue: {} },
|
{ provide: RemoteDataBuildService, useValue: remoteDataBuildService },
|
||||||
{ provide: HALEndpointService, useValue: {} },
|
{ provide: HALEndpointService, useValue: halService },
|
||||||
SearchService
|
SearchService
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
@@ -113,5 +149,95 @@ describe('SearchService', () => {
|
|||||||
searchService.getViewMode().subscribe((mode) => viewMode = mode);
|
searchService.getViewMode().subscribe((mode) => viewMode = mode);
|
||||||
expect(viewMode).toEqual(ViewMode.Grid);
|
expect(viewMode).toEqual(ViewMode.Grid);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('when search is called', () => {
|
||||||
|
const endPoint = 'http://endpoint.com/test/test';
|
||||||
|
const searchOptions = new PaginatedSearchOptions();
|
||||||
|
const queryResponse = Object.assign(new SearchQueryResponse(), { objects: [] });
|
||||||
|
const response = new SearchSuccessResponse(queryResponse, '200');
|
||||||
|
const responseEntry = Object.assign(new ResponseCacheEntry(), { response: response });
|
||||||
|
beforeEach(() => {
|
||||||
|
spyOn((searchService as any).halService, 'getEndpoint').and.returnValue(Observable.of(endPoint));
|
||||||
|
(searchService as any).responseCache.get.and.returnValue(Observable.of(responseEntry));
|
||||||
|
/* tslint:disable:no-empty */
|
||||||
|
searchService.search(searchOptions).subscribe((t) => {}); // subscribe to make sure all methods are called
|
||||||
|
/* tslint:enable:no-empty */
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should call getEndpoint on the halService', () => {
|
||||||
|
expect((searchService as any).halService.getEndpoint).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should send out the request on the request service', () => {
|
||||||
|
expect((searchService as any).requestService.configure).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should call getByHref on the request service with the correct request url', () => {
|
||||||
|
expect((searchService as any).requestService.getByHref).toHaveBeenCalledWith(endPoint);
|
||||||
|
});
|
||||||
|
it('should call get on the request service with the correct request url', () => {
|
||||||
|
expect((searchService as any).responseCache.get).toHaveBeenCalledWith(endPoint);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when getConfig is called without a scope', () => {
|
||||||
|
const endPoint = 'http://endpoint.com/test/config';
|
||||||
|
const filterConfig = [new SearchFilterConfig()];
|
||||||
|
const response = new FacetConfigSuccessResponse(filterConfig, '200');
|
||||||
|
const responseEntry = Object.assign(new ResponseCacheEntry(), { response: response });
|
||||||
|
beforeEach(() => {
|
||||||
|
spyOn((searchService as any).halService, 'getEndpoint').and.returnValue(Observable.of(endPoint));
|
||||||
|
(searchService as any).responseCache.get.and.returnValue(Observable.of(responseEntry));
|
||||||
|
/* tslint:disable:no-empty */
|
||||||
|
searchService.getConfig(null).subscribe((t) => {}); // subscribe to make sure all methods are called
|
||||||
|
/* tslint:enable:no-empty */
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should call getEndpoint on the halService', () => {
|
||||||
|
expect((searchService as any).halService.getEndpoint).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should send out the request on the request service', () => {
|
||||||
|
expect((searchService as any).requestService.configure).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should call getByHref on the request service with the correct request url', () => {
|
||||||
|
expect((searchService as any).requestService.getByHref).toHaveBeenCalledWith(endPoint);
|
||||||
|
});
|
||||||
|
it('should call get on the request service with the correct request url', () => {
|
||||||
|
expect((searchService as any).responseCache.get).toHaveBeenCalledWith(endPoint);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when getConfig is called with a scope', () => {
|
||||||
|
const endPoint = 'http://endpoint.com/test/config';
|
||||||
|
const scope = 'test';
|
||||||
|
const requestUrl = endPoint + '?scope=' + scope;
|
||||||
|
const filterConfig = [new SearchFilterConfig()];
|
||||||
|
const response = new FacetConfigSuccessResponse(filterConfig, '200');
|
||||||
|
const responseEntry = Object.assign(new ResponseCacheEntry(), { response: response });
|
||||||
|
beforeEach(() => {
|
||||||
|
spyOn((searchService as any).halService, 'getEndpoint').and.returnValue(Observable.of(endPoint));
|
||||||
|
(searchService as any).responseCache.get.and.returnValue(Observable.of(responseEntry));
|
||||||
|
/* tslint:disable:no-empty */
|
||||||
|
searchService.getConfig(scope).subscribe((t) => {}); // subscribe to make sure all methods are called
|
||||||
|
/* tslint:enable:no-empty */
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should call getEndpoint on the halService', () => {
|
||||||
|
expect((searchService as any).halService.getEndpoint).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should send out the request on the request service', () => {
|
||||||
|
expect((searchService as any).requestService.configure).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should call getByHref on the request service with the correct request url', () => {
|
||||||
|
expect((searchService as any).requestService.getByHref).toHaveBeenCalledWith(requestUrl);
|
||||||
|
});
|
||||||
|
it('should call get on the request service with the correct request url', () => {
|
||||||
|
expect((searchService as any).responseCache.get).toHaveBeenCalledWith(requestUrl);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@@ -96,6 +96,7 @@ export class SearchService implements OnDestroy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
search(searchOptions?: PaginatedSearchOptions): Observable<RemoteData<PaginatedList<SearchResult<DSpaceObject>>>> {
|
search(searchOptions?: PaginatedSearchOptions): Observable<RemoteData<PaginatedList<SearchResult<DSpaceObject>>>> {
|
||||||
|
// this.halService.getEndpoint(this.searchLinkPath).subscribe((t) => console.log(t));
|
||||||
const requestObs = this.halService.getEndpoint(this.searchLinkPath).pipe(
|
const requestObs = this.halService.getEndpoint(this.searchLinkPath).pipe(
|
||||||
map((url: string) => {
|
map((url: string) => {
|
||||||
if (hasValue(searchOptions)) {
|
if (hasValue(searchOptions)) {
|
||||||
@@ -110,7 +111,6 @@ export class SearchService implements OnDestroy {
|
|||||||
}),
|
}),
|
||||||
tap((request: RestRequest) => this.requestService.configure(request)),
|
tap((request: RestRequest) => this.requestService.configure(request)),
|
||||||
);
|
);
|
||||||
|
|
||||||
const requestEntryObs = requestObs.pipe(
|
const requestEntryObs = requestObs.pipe(
|
||||||
flatMap((request: RestRequest) => this.requestService.getByHref(request.href))
|
flatMap((request: RestRequest) => this.requestService.getByHref(request.href))
|
||||||
);
|
);
|
||||||
|
@@ -1,8 +1,10 @@
|
|||||||
import { RequestService } from '../../core/data/request.service';
|
import { RequestService } from '../../core/data/request.service';
|
||||||
|
import { RequestEntry } from '../../core/data/request.reducer';
|
||||||
|
|
||||||
export function getMockRequestService(): RequestService {
|
export function getMockRequestService(): RequestService {
|
||||||
return jasmine.createSpyObj('requestService', {
|
return jasmine.createSpyObj('requestService', {
|
||||||
configure: () => false,
|
configure: () => false,
|
||||||
generateRequestId: () => 'clients/b186e8ce-e99c-4183-bc9a-42b4821bdb78'
|
generateRequestId: () => 'clients/b186e8ce-e99c-4183-bc9a-42b4821bdb78',
|
||||||
|
getByHref: (uuid: string) => new RequestEntry()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@@ -1,10 +1,12 @@
|
|||||||
import { ResponseCacheService } from '../../core/cache/response-cache.service';
|
import { ResponseCacheService } from '../../core/cache/response-cache.service';
|
||||||
|
import { ResponseCacheEntry } from '../../core/cache/response-cache.reducer';
|
||||||
|
import { RestResponse } from '../../core/cache/response-cache.models';
|
||||||
|
|
||||||
export function getMockResponseCacheService(): ResponseCacheService {
|
export function getMockResponseCacheService(): ResponseCacheService {
|
||||||
return jasmine.createSpyObj('ResponseCacheService', [
|
return jasmine.createSpyObj('ResponseCacheService', {
|
||||||
'add',
|
add: (key: string, response: RestResponse, msToLive: number) => new ResponseCacheEntry(),
|
||||||
'get',
|
get: (key: string) => new ResponseCacheEntry(),
|
||||||
'has',
|
has: (key: string) => false,
|
||||||
]);
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user