From 0d18d08d22ddad98119e9ee26bb373e08d71bba0 Mon Sep 17 00:00:00 2001 From: Lotte Hofstede Date: Thu, 8 Mar 2018 12:17:31 +0100 Subject: [PATCH] fixed some tests --- .../search-facet-filter.component.spec.ts | 4 +- .../search-filter.service.spec.ts | 4 +- .../search-service/search.service.spec.ts | 11 +++++ .../search-service/search.service.ts | 9 ++-- .../core/cache/object-cache.service.spec.ts | 44 +++++++------------ src/app/core/data/comcol-data.service.spec.ts | 2 +- .../pagination/pagination.component.spec.ts | 4 +- .../view-mode-switch.component.spec.ts | 15 ++++++- 8 files changed, 50 insertions(+), 43 deletions(-) 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 d6c6da3051..bf069eee60 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 @@ -96,8 +96,8 @@ describe('SearchFacetFilterComponent', () => { link = comp.getSearchLink(); }); - it('should return the value of the uiSearchRoute variable in the filter service', () => { - expect(link).toEqual(filterService.uiSearchRoute); + it('should return the value of the searchLink variable in the filter service', () => { + expect(link).toEqual(filterService.searchLink); }); }); 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 6dcf8d73b8..853f8b0f68 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 @@ -46,7 +46,7 @@ describe('SearchFilterService', () => { }; const searchServiceStub: any = { - searchLink: '/search' + uiSearchRoute: '/search' }; beforeEach(() => { @@ -199,7 +199,7 @@ describe('SearchFilterService', () => { }); it('should return the value of uiSearchRoute in the search service', () => { - expect(link).toEqual(searchServiceStub.searchLink); + expect(link).toEqual(searchServiceStub.uiSearchRoute); }); }); }); 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 65af3231f9..a0d4b1f049 100644 --- a/src/app/+search-page/search-service/search.service.spec.ts +++ b/src/app/+search-page/search-service/search.service.spec.ts @@ -8,6 +8,12 @@ import { SearchService } from './search.service'; import { ItemDataService } from './../../core/data/item-data.service'; import { ViewMode } from '../../+search-page/search-options.model'; import { RouteService } from '../../shared/route.service'; +import { GLOBAL_CONFIG } from '../../../config'; +import { RemoteDataBuildService } from '../../core/cache/builders/remote-data-build.service'; +import { ActivatedRoute } from '@angular/router'; +import { RequestService } from '../../core/data/request.service'; +import { ResponseCacheService } from '../../core/cache/response-cache.service'; +import { ActivatedRouteStub } from '../../shared/testing/active-router-stub'; @Component({ template: '' }) class DummyComponent { } @@ -29,6 +35,11 @@ describe('SearchService', () => { providers: [ { provide: ItemDataService, useValue: {} }, { provide: RouteService, useValue: {} }, + { provide: ResponseCacheService, useValue: {} }, + { provide: RequestService, useValue: {} }, + { provide: ActivatedRoute, useValue: new ActivatedRouteStub() }, + { provide: RemoteDataBuildService, useValue: {} }, + { provide: GLOBAL_CONFIG, useValue: {} }, SearchService ], }); diff --git a/src/app/+search-page/search-service/search.service.ts b/src/app/+search-page/search-service/search.service.ts index f343adc38b..2dac0bd340 100644 --- a/src/app/+search-page/search-service/search.service.ts +++ b/src/app/+search-page/search-service/search.service.ts @@ -6,12 +6,10 @@ import { ViewMode } from '../../+search-page/search-options.model'; import { GLOBAL_CONFIG } from '../../../config'; import { GlobalConfig } from '../../../config/global-config.interface'; import { RemoteDataBuildService } from '../../core/cache/builders/remote-data-build.service'; -import { NormalizedDSpaceObject } from '../../core/cache/models/normalized-dspace-object.model'; import { SortOptions } from '../../core/cache/models/sort-options.model'; import { SearchSuccessResponse } from '../../core/cache/response-cache.models'; import { ResponseCacheEntry } from '../../core/cache/response-cache.reducer'; import { ResponseCacheService } from '../../core/cache/response-cache.service'; -import { ItemDataService } from '../../core/data/item-data.service'; import { PaginatedList } from '../../core/data/paginated-list'; import { ResponseParsingService } from '../../core/data/parsing.service'; import { RemoteData } from '../../core/data/remote-data'; @@ -125,11 +123,10 @@ export class SearchService extends HALEndpointService implements OnDestroy { args.push(`sort=${searchOptions.sort.field},${searchOptions.sort.direction}`); } if (isNotEmpty(searchOptions.pagination)) { - args.push(`page=${searchOptions.pagination.currentPage}`); + args.push(`page=${searchOptions.pagination.currentPage - 1}`); args.push(`size=${searchOptions.pagination.pageSize}`); } } - if (isNotEmpty(args)) { url = new URLCombiner(url, `?${args.join('&')}`).toString(); } @@ -233,8 +230,8 @@ export class SearchService extends HALEndpointService implements OnDestroy { payload.push({ value: value, count: Math.floor(Math.random() * 20) + 20 * (totalFilters - i), // make sure first results have the highest (random) count - search: decodeURI(this.router.url) + (this.router.url.includes('?') ? '&' : '?') + filterConfig.paramName + '=' + value - }); + search: (decodeURI(this.router.url) + (this.router.url.includes('?') ? '&' : '?') + filterConfig.paramName + '=' + value)} + ); } } const requestPending = false; diff --git a/src/app/core/cache/object-cache.service.spec.ts b/src/app/core/cache/object-cache.service.spec.ts index 86b8a3efdf..80a9121544 100644 --- a/src/app/core/cache/object-cache.service.spec.ts +++ b/src/app/core/cache/object-cache.service.spec.ts @@ -2,22 +2,10 @@ import { Store } from '@ngrx/store'; import { Observable } from 'rxjs/Observable'; import { ObjectCacheService } from './object-cache.service'; -import { CacheableObject } from './object-cache.reducer'; import { AddToObjectCacheAction, RemoveFromObjectCacheAction } from './object-cache.actions'; import { CoreState } from '../core.reducers'; import { ResourceType } from '../shared/resource-type'; - -class TestClass implements CacheableObject { - constructor( - public self: string, - public foo: string, - public type = ResourceType.Item - ) { } - - test(): string { - return this.foo + this.self; - } -} +import { NormalizedItem } from './models/normalized-item.model'; describe('ObjectCacheService', () => { let service: ObjectCacheService; @@ -28,7 +16,7 @@ describe('ObjectCacheService', () => { const msToLive = 900000; const objectToCache = { self: selfLink, - foo: 'bar' + type: ResourceType.Item }; const cacheEntry = { data: objectToCache, @@ -65,13 +53,13 @@ describe('ObjectCacheService', () => { it('should return an observable of the cached object with the specified self link and type', () => { spyOn(store, 'select').and.returnValue(Observable.of(cacheEntry)); - let testObj: any; // due to the implementation of spyOn above, this subscribe will be synchronous - service.getBySelfLink(selfLink).take(1).subscribe((o) => testObj = o); - expect(testObj.self).toBe(selfLink); - expect(testObj.foo).toBe('bar'); - // this only works if testObj is an instance of TestClass - expect(testObj.test()).toBe('bar' + selfLink); + service.getBySelfLink(selfLink).take(1).subscribe((o) => { + expect(o.self).toBe(selfLink); + // this only works if testObj is an instance of TestClass + expect(o instanceof NormalizedItem).toBeTruthy(); + } + ); }); it('should not return a cached object that has exceeded its time to live', () => { @@ -86,16 +74,14 @@ describe('ObjectCacheService', () => { describe('getList', () => { it('should return an observable of the array of cached objects with the specified self link and type', () => { - spyOn(service, 'getBySelfLink').and.returnValue(Observable.of(new TestClass(selfLink, 'bar'))); + const item = new NormalizedItem(); + item.self = selfLink; + spyOn(service, 'getBySelfLink').and.returnValue(Observable.of(item)); - let testObjs: any[]; - service.getList([selfLink, selfLink]).take(1).subscribe((arr) => testObjs = arr); - expect(testObjs[0].self).toBe(selfLink); - expect(testObjs[0].foo).toBe('bar'); - expect(testObjs[0].test()).toBe('bar' + selfLink); - expect(testObjs[1].self).toBe(selfLink); - expect(testObjs[1].foo).toBe('bar'); - expect(testObjs[1].test()).toBe('bar' + selfLink); + service.getList([selfLink, selfLink]).take(1).subscribe((arr) => { + expect(arr[0].self).toBe(selfLink); + expect(arr[0] instanceof NormalizedItem).toBeTruthy(); + }); }); }); diff --git a/src/app/core/data/comcol-data.service.spec.ts b/src/app/core/data/comcol-data.service.spec.ts index 2c38325a4f..cf43482ba5 100644 --- a/src/app/core/data/comcol-data.service.spec.ts +++ b/src/app/core/data/comcol-data.service.spec.ts @@ -126,7 +126,7 @@ describe('ComColDataService', () => { it('should fetch the scope Community from the cache', () => { scheduler.schedule(() => service.getScopedEndpoint(scopeID).subscribe()); scheduler.flush(); - expect(objectCache.getByUUID).toHaveBeenCalledWith(scopeID, NormalizedCommunity); + expect(objectCache.getByUUID).toHaveBeenCalledWith(scopeID); }); it('should return the endpoint to fetch resources within the given scope', () => { diff --git a/src/app/shared/pagination/pagination.component.spec.ts b/src/app/shared/pagination/pagination.component.spec.ts index a4b9e5fcea..b108074893 100644 --- a/src/app/shared/pagination/pagination.component.spec.ts +++ b/src/app/shared/pagination/pagination.component.spec.ts @@ -271,7 +271,7 @@ describe('Pagination component', () => { changePage(testFixture, 3); tick(); - expect(routerStub.navigate).toHaveBeenCalledWith([], { queryParams: { pageId: 'test', page: 3, pageSize: 10, sortDirection: 0, sortField: 'name' } }); + expect(routerStub.navigate).toHaveBeenCalledWith([], { queryParams: { pageId: 'test', page: 3, pageSize: 10, sortDirection: 'ASC', sortField: 'dc.title' } }); })); @@ -282,7 +282,7 @@ describe('Pagination component', () => { changePageSize(testFixture, '20'); tick(); - expect(routerStub.navigate).toHaveBeenCalledWith([], { queryParams: { pageId: 'test', page: 1, pageSize: 20, sortDirection: 0, sortField: 'name' } }); + expect(routerStub.navigate).toHaveBeenCalledWith([], { queryParams: { pageId: 'test', page: 1, pageSize: 20, sortDirection: 'ASC', sortField: 'dc.title' } }); })); it('should set correct values', fakeAsync(() => { diff --git a/src/app/shared/view-mode-switch/view-mode-switch.component.spec.ts b/src/app/shared/view-mode-switch/view-mode-switch.component.spec.ts index 541b1ed4c3..3923fec0a6 100644 --- a/src/app/shared/view-mode-switch/view-mode-switch.component.spec.ts +++ b/src/app/shared/view-mode-switch/view-mode-switch.component.spec.ts @@ -11,6 +11,12 @@ import { ItemDataService } from './../../core/data/item-data.service'; import { ViewModeSwitchComponent } from './view-mode-switch.component'; import { ViewMode } from '../../+search-page/search-options.model'; import { RouteService } from '../route.service'; +import { ResponseCacheService } from '../../core/cache/response-cache.service'; +import { RequestService } from '../../core/data/request.service'; +import { RemoteDataBuildService } from '../../core/cache/builders/remote-data-build.service'; +import { ActivatedRoute } from '@angular/router'; +import { GLOBAL_CONFIG } from '../../../config'; +import { ActivatedRouteStub } from '../testing/active-router-stub'; @Component({ template: '' }) class DummyComponent { } @@ -21,7 +27,7 @@ describe('ViewModeSwitchComponent', () => { let searchService: SearchService; let listButton: HTMLElement; let gridButton: HTMLElement; - + let route = new ActivatedRouteStub(); beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ @@ -42,6 +48,11 @@ describe('ViewModeSwitchComponent', () => { providers: [ { provide: ItemDataService, useValue: {} }, { provide: RouteService, useValue: {} }, + { provide: ResponseCacheService, useValue: {} }, + { provide: RequestService, useValue: {} }, + { provide: ActivatedRoute, useValue: route }, + { provide: RemoteDataBuildService, useValue: {} }, + { provide: GLOBAL_CONFIG, useValue: {} }, SearchService ], }).compileComponents(); @@ -59,6 +70,7 @@ describe('ViewModeSwitchComponent', () => { it('should set list button as active when on list mode', fakeAsync(() => { searchService.setViewMode(ViewMode.List); + route = new ActivatedRouteStub([{view: ViewMode.List}]) tick(); fixture.detectChanges(); expect(comp.currentMode).toBe(ViewMode.List); @@ -68,6 +80,7 @@ describe('ViewModeSwitchComponent', () => { it('should set grid button as active when on grid mode', fakeAsync(() => { searchService.setViewMode(ViewMode.Grid); + route = new ActivatedRouteStub([{view: ViewMode.Grid}]) tick(); fixture.detectChanges(); expect(comp.currentMode).toBe(ViewMode.Grid);