fixed some tests

This commit is contained in:
Lotte Hofstede
2018-03-08 12:17:31 +01:00
parent acf85577ee
commit 0d18d08d22
8 changed files with 50 additions and 43 deletions

View File

@@ -96,8 +96,8 @@ describe('SearchFacetFilterComponent', () => {
link = comp.getSearchLink(); link = comp.getSearchLink();
}); });
it('should return the value of the uiSearchRoute variable in the filter service', () => { it('should return the value of the searchLink variable in the filter service', () => {
expect(link).toEqual(filterService.uiSearchRoute); expect(link).toEqual(filterService.searchLink);
}); });
}); });

View File

@@ -46,7 +46,7 @@ describe('SearchFilterService', () => {
}; };
const searchServiceStub: any = { const searchServiceStub: any = {
searchLink: '/search' uiSearchRoute: '/search'
}; };
beforeEach(() => { beforeEach(() => {
@@ -199,7 +199,7 @@ describe('SearchFilterService', () => {
}); });
it('should return the value of uiSearchRoute in the search service', () => { it('should return the value of uiSearchRoute in the search service', () => {
expect(link).toEqual(searchServiceStub.searchLink); expect(link).toEqual(searchServiceStub.uiSearchRoute);
}); });
}); });
}); });

View File

@@ -8,6 +8,12 @@ import { SearchService } from './search.service';
import { ItemDataService } from './../../core/data/item-data.service'; import { ItemDataService } from './../../core/data/item-data.service';
import { ViewMode } from '../../+search-page/search-options.model'; import { ViewMode } from '../../+search-page/search-options.model';
import { RouteService } from '../../shared/route.service'; 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: '' }) @Component({ template: '' })
class DummyComponent { } class DummyComponent { }
@@ -29,6 +35,11 @@ describe('SearchService', () => {
providers: [ providers: [
{ provide: ItemDataService, useValue: {} }, { provide: ItemDataService, useValue: {} },
{ provide: RouteService, useValue: {} }, { provide: RouteService, useValue: {} },
{ provide: ResponseCacheService, useValue: {} },
{ provide: RequestService, useValue: {} },
{ provide: ActivatedRoute, useValue: new ActivatedRouteStub() },
{ provide: RemoteDataBuildService, useValue: {} },
{ provide: GLOBAL_CONFIG, useValue: {} },
SearchService SearchService
], ],
}); });

View File

@@ -6,12 +6,10 @@ import { ViewMode } from '../../+search-page/search-options.model';
import { GLOBAL_CONFIG } from '../../../config'; import { GLOBAL_CONFIG } from '../../../config';
import { GlobalConfig } from '../../../config/global-config.interface'; import { GlobalConfig } from '../../../config/global-config.interface';
import { RemoteDataBuildService } from '../../core/cache/builders/remote-data-build.service'; 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 { SortOptions } from '../../core/cache/models/sort-options.model';
import { SearchSuccessResponse } from '../../core/cache/response-cache.models'; import { SearchSuccessResponse } from '../../core/cache/response-cache.models';
import { ResponseCacheEntry } from '../../core/cache/response-cache.reducer'; import { ResponseCacheEntry } from '../../core/cache/response-cache.reducer';
import { ResponseCacheService } from '../../core/cache/response-cache.service'; import { ResponseCacheService } from '../../core/cache/response-cache.service';
import { ItemDataService } from '../../core/data/item-data.service';
import { PaginatedList } from '../../core/data/paginated-list'; import { PaginatedList } from '../../core/data/paginated-list';
import { ResponseParsingService } from '../../core/data/parsing.service'; import { ResponseParsingService } from '../../core/data/parsing.service';
import { RemoteData } from '../../core/data/remote-data'; 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}`); args.push(`sort=${searchOptions.sort.field},${searchOptions.sort.direction}`);
} }
if (isNotEmpty(searchOptions.pagination)) { if (isNotEmpty(searchOptions.pagination)) {
args.push(`page=${searchOptions.pagination.currentPage}`); args.push(`page=${searchOptions.pagination.currentPage - 1}`);
args.push(`size=${searchOptions.pagination.pageSize}`); args.push(`size=${searchOptions.pagination.pageSize}`);
} }
} }
if (isNotEmpty(args)) { if (isNotEmpty(args)) {
url = new URLCombiner(url, `?${args.join('&')}`).toString(); url = new URLCombiner(url, `?${args.join('&')}`).toString();
} }
@@ -233,8 +230,8 @@ export class SearchService extends HALEndpointService implements OnDestroy {
payload.push({ payload.push({
value: value, value: value,
count: Math.floor(Math.random() * 20) + 20 * (totalFilters - i), // make sure first results have the highest (random) count 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; const requestPending = false;

View File

@@ -2,22 +2,10 @@ import { Store } from '@ngrx/store';
import { Observable } from 'rxjs/Observable'; import { Observable } from 'rxjs/Observable';
import { ObjectCacheService } from './object-cache.service'; import { ObjectCacheService } from './object-cache.service';
import { CacheableObject } from './object-cache.reducer';
import { AddToObjectCacheAction, RemoveFromObjectCacheAction } from './object-cache.actions'; import { AddToObjectCacheAction, RemoveFromObjectCacheAction } from './object-cache.actions';
import { CoreState } from '../core.reducers'; import { CoreState } from '../core.reducers';
import { ResourceType } from '../shared/resource-type'; import { ResourceType } from '../shared/resource-type';
import { NormalizedItem } from './models/normalized-item.model';
class TestClass implements CacheableObject {
constructor(
public self: string,
public foo: string,
public type = ResourceType.Item
) { }
test(): string {
return this.foo + this.self;
}
}
describe('ObjectCacheService', () => { describe('ObjectCacheService', () => {
let service: ObjectCacheService; let service: ObjectCacheService;
@@ -28,7 +16,7 @@ describe('ObjectCacheService', () => {
const msToLive = 900000; const msToLive = 900000;
const objectToCache = { const objectToCache = {
self: selfLink, self: selfLink,
foo: 'bar' type: ResourceType.Item
}; };
const cacheEntry = { const cacheEntry = {
data: objectToCache, data: objectToCache,
@@ -65,13 +53,13 @@ describe('ObjectCacheService', () => {
it('should return an observable of the cached object with the specified self link and type', () => { it('should return an observable of the cached object with the specified self link and type', () => {
spyOn(store, 'select').and.returnValue(Observable.of(cacheEntry)); spyOn(store, 'select').and.returnValue(Observable.of(cacheEntry));
let testObj: any;
// due to the implementation of spyOn above, this subscribe will be synchronous // due to the implementation of spyOn above, this subscribe will be synchronous
service.getBySelfLink(selfLink).take(1).subscribe((o) => testObj = o); service.getBySelfLink(selfLink).take(1).subscribe((o) => {
expect(testObj.self).toBe(selfLink); expect(o.self).toBe(selfLink);
expect(testObj.foo).toBe('bar'); // this only works if testObj is an instance of TestClass
// this only works if testObj is an instance of TestClass expect(o instanceof NormalizedItem).toBeTruthy();
expect(testObj.test()).toBe('bar' + selfLink); }
);
}); });
it('should not return a cached object that has exceeded its time to live', () => { it('should not return a cached object that has exceeded its time to live', () => {
@@ -86,16 +74,14 @@ describe('ObjectCacheService', () => {
describe('getList', () => { describe('getList', () => {
it('should return an observable of the array of cached objects with the specified self link and type', () => { 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) => {
service.getList([selfLink, selfLink]).take(1).subscribe((arr) => testObjs = arr); expect(arr[0].self).toBe(selfLink);
expect(testObjs[0].self).toBe(selfLink); expect(arr[0] instanceof NormalizedItem).toBeTruthy();
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);
}); });
}); });

View File

@@ -126,7 +126,7 @@ describe('ComColDataService', () => {
it('should fetch the scope Community from the cache', () => { it('should fetch the scope Community from the cache', () => {
scheduler.schedule(() => service.getScopedEndpoint(scopeID).subscribe()); scheduler.schedule(() => service.getScopedEndpoint(scopeID).subscribe());
scheduler.flush(); 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', () => { it('should return the endpoint to fetch resources within the given scope', () => {

View File

@@ -271,7 +271,7 @@ describe('Pagination component', () => {
changePage(testFixture, 3); changePage(testFixture, 3);
tick(); 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'); changePageSize(testFixture, '20');
tick(); 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(() => { it('should set correct values', fakeAsync(() => {

View File

@@ -11,6 +11,12 @@ import { ItemDataService } from './../../core/data/item-data.service';
import { ViewModeSwitchComponent } from './view-mode-switch.component'; import { ViewModeSwitchComponent } from './view-mode-switch.component';
import { ViewMode } from '../../+search-page/search-options.model'; import { ViewMode } from '../../+search-page/search-options.model';
import { RouteService } from '../route.service'; 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: '' }) @Component({ template: '' })
class DummyComponent { } class DummyComponent { }
@@ -21,7 +27,7 @@ describe('ViewModeSwitchComponent', () => {
let searchService: SearchService; let searchService: SearchService;
let listButton: HTMLElement; let listButton: HTMLElement;
let gridButton: HTMLElement; let gridButton: HTMLElement;
let route = new ActivatedRouteStub();
beforeEach(async(() => { beforeEach(async(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ imports: [
@@ -42,6 +48,11 @@ describe('ViewModeSwitchComponent', () => {
providers: [ providers: [
{ provide: ItemDataService, useValue: {} }, { provide: ItemDataService, useValue: {} },
{ provide: RouteService, useValue: {} }, { provide: RouteService, useValue: {} },
{ provide: ResponseCacheService, useValue: {} },
{ provide: RequestService, useValue: {} },
{ provide: ActivatedRoute, useValue: route },
{ provide: RemoteDataBuildService, useValue: {} },
{ provide: GLOBAL_CONFIG, useValue: {} },
SearchService SearchService
], ],
}).compileComponents(); }).compileComponents();
@@ -59,6 +70,7 @@ describe('ViewModeSwitchComponent', () => {
it('should set list button as active when on list mode', fakeAsync(() => { it('should set list button as active when on list mode', fakeAsync(() => {
searchService.setViewMode(ViewMode.List); searchService.setViewMode(ViewMode.List);
route = new ActivatedRouteStub([{view: ViewMode.List}])
tick(); tick();
fixture.detectChanges(); fixture.detectChanges();
expect(comp.currentMode).toBe(ViewMode.List); expect(comp.currentMode).toBe(ViewMode.List);
@@ -68,6 +80,7 @@ describe('ViewModeSwitchComponent', () => {
it('should set grid button as active when on grid mode', fakeAsync(() => { it('should set grid button as active when on grid mode', fakeAsync(() => {
searchService.setViewMode(ViewMode.Grid); searchService.setViewMode(ViewMode.Grid);
route = new ActivatedRouteStub([{view: ViewMode.Grid}])
tick(); tick();
fixture.detectChanges(); fixture.detectChanges();
expect(comp.currentMode).toBe(ViewMode.Grid); expect(comp.currentMode).toBe(ViewMode.Grid);