54472: DataService create method tests + buildFormData test pt2

This commit is contained in:
Kristof De Langhe
2018-09-17 10:33:20 +02:00
parent 6aee6e668c
commit 30284ea78f
3 changed files with 69 additions and 27 deletions

View File

@@ -33,8 +33,7 @@ export class CreateCollectionPageComponent {
private collectionDataService: CollectionDataService, private collectionDataService: CollectionDataService,
private communityDataService: CommunityDataService, private communityDataService: CommunityDataService,
private routeService: RouteService, private routeService: RouteService,
private router: Router, private router: Router
private objectCache: ObjectCacheService
) { ) {
this.parentUUID$ = this.routeService.getQueryParameterValue('parent'); this.parentUUID$ = this.routeService.getQueryParameterValue('parent');
this.parentUUID$.subscribe((uuid: string) => { this.parentUUID$.subscribe((uuid: string) => {
@@ -56,11 +55,10 @@ export class CreateCollectionPageComponent {
// TODO: metadata for news and provenance // TODO: metadata for news and provenance
] ]
}); });
this.collectionDataService.create(collection, uuid).pipe( this.collectionDataService.create(collection, uuid).subscribe((rd: RemoteData<Collection>) => {
flatMap((rd: RemoteData<Collection>) => this.objectCache.getByUUID(rd.payload.id)), if (rd.hasSucceeded) {
isNotEmptyOperator() this.router.navigate(['collections', rd.payload.id]);
).subscribe((col: NormalizedCollection) => { }
this.router.navigate(['collections', col.id]);
}); });
}); });
} }

View File

@@ -18,6 +18,12 @@ import { DSpaceObject } from '../shared/dspace-object.model';
import { RemoteData } from './remote-data'; import { RemoteData } from './remote-data';
import { RequestEntry } from './request.reducer'; import { RequestEntry } from './request.reducer';
import { getMockRemoteDataBuildService } from '../../shared/mocks/mock-remote-data-build.service'; import { getMockRemoteDataBuildService } from '../../shared/mocks/mock-remote-data-build.service';
import { EmptyError } from 'rxjs/util/EmptyError';
import { ResponseCacheEntry } from '../cache/response-cache.reducer';
import { ErrorResponse, RestResponse } from '../cache/response-cache.models';
import { hasValue } from '../../shared/empty.util';
import { map } from 'rxjs/operators';
import { RemoteDataError } from './remote-data-error';
const LINK_NAME = 'test'; const LINK_NAME = 'test';
@@ -49,7 +55,7 @@ class TestService extends DataService<NormalizedTestObject, any> {
describe('DataService', () => { describe('DataService', () => {
let service: TestService; let service: TestService;
let options: FindAllOptions; let options: FindAllOptions;
const responseCache = getMockResponseCacheService(); let responseCache = getMockResponseCacheService();
let rdbService = {} as RemoteDataBuildService; let rdbService = {} as RemoteDataBuildService;
const authService = {} as AuthService; const authService = {} as AuthService;
const notificationsService = {} as NotificationsService; const notificationsService = {} as NotificationsService;
@@ -57,12 +63,38 @@ describe('DataService', () => {
const store = {} as Store<CoreState>; const store = {} as Store<CoreState>;
const endpoint = 'https://rest.api/core'; const endpoint = 'https://rest.api/core';
const halService = Object.assign({ const halService = Object.assign({
getEndpoint: () => Observable.of(endpoint) getEndpoint: (linkpath) => Observable.of(endpoint)
}); });
const requestService = Object.assign(getMockRequestService(), { const requestService = Object.assign(getMockRequestService(), {
getByUUID: () => Observable.of(new RequestEntry()) getByUUID: () => Observable.of(new RequestEntry()),
configure: (request) => request
}); });
const dso = new DSpaceObject();
const successfulRd$ = Observable.of(new RemoteData(false, false, true, undefined, dso));
const successfulResponseCacheEntry = {
response: {
isSuccessful: true,
payload: dso,
toCache: true,
statusCode: '200'
} as RestResponse
} as ResponseCacheEntry;
function initSuccessfulRemoteDataBuildService(): RemoteDataBuildService {
return {
toRemoteDataObservable: (requestEntry$: Observable<RequestEntry>, responseCache$: Observable<ResponseCacheEntry>, payload$: Observable<any>) => {
requestEntry$.subscribe();
responseCache$.subscribe();
payload$.subscribe();
return successfulRd$;
}
} as RemoteDataBuildService;
}
function initSuccessfulResponseCacheService(): ResponseCacheService {
return getMockResponseCacheService(Observable.of(new ResponseCacheEntry()), Observable.of(successfulResponseCacheEntry));
}
function initTestService(): TestService { function initTestService(): TestService {
return new TestService( return new TestService(
responseCache, responseCache,
@@ -136,13 +168,13 @@ describe('DataService', () => {
}); });
it('should include all provided options in href', () => { it('should include all provided options in href', () => {
const sortOptions = new SortOptions('field1', SortDirection.DESC) const sortOptions = new SortOptions('field1', SortDirection.DESC);
options = { options = {
currentPage: 6, currentPage: 6,
elementsPerPage: 10, elementsPerPage: 10,
sort: sortOptions, sort: sortOptions,
startsWith: 'ab' startsWith: 'ab'
} };
const expected = `${endpoint}?page=${options.currentPage - 1}&size=${options.elementsPerPage}` + const expected = `${endpoint}?page=${options.currentPage - 1}&size=${options.elementsPerPage}` +
`&sort=${sortOptions.field},${sortOptions.direction}&startsWith=${options.startsWith}`; `&sort=${sortOptions.field},${sortOptions.direction}&startsWith=${options.startsWith}`;
@@ -152,14 +184,12 @@ describe('DataService', () => {
}) })
}); });
fdescribe('create', () => { describe('create', () => {
const dso = new DSpaceObject();
const successfulRd$ = Observable.of(new RemoteData(false, false, true, undefined, dso));
const failingRd$ = Observable.of(new RemoteData(false, false, false, undefined, dso));
describe('when the request was successful', () => { describe('when the request was successful', () => {
beforeEach(() => { beforeEach(() => {
rdbService = getMockRemoteDataBuildService(successfulRd$); responseCache = initSuccessfulResponseCacheService();
rdbService = initSuccessfulRemoteDataBuildService();
service = initTestService(); service = initTestService();
}); });
@@ -168,23 +198,25 @@ describe('DataService', () => {
expect(rd.payload).toBe(dso); expect(rd.payload).toBe(dso);
}); });
}); });
});
describe('when the request was unsuccessful', () => { it('should get the response from cache with the correct url when parent is empty', () => {
beforeEach(() => { const expectedUrl = endpoint;
rdbService = getMockRemoteDataBuildService(failingRd$);
service = initTestService(); service.create(dso, undefined).subscribe((value) => {
expect(responseCache.get).toHaveBeenCalledWith(expectedUrl);
});
}); });
it('should not return anything', () => { it('should get the response from cache with the correct url when parent is not empty', () => {
service.create(dso, undefined); const parent = 'fake-parent-uuid';
const expectedUrl = `${endpoint}?parent=${parent}`;
// TODO: Expect create to emit nothing service.create(dso, parent).subscribe((value) => {
expect(responseCache.get).toHaveBeenCalledWith(expectedUrl);
});
}); });
}); });
// TODO: Create tests with passing parent
}); });
}); });

View File

@@ -2,6 +2,7 @@ import { TestBed, inject } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { DSpaceRESTv2Service } from './dspace-rest-v2.service'; import { DSpaceRESTv2Service } from './dspace-rest-v2.service';
import { DSpaceObject } from '../shared/dspace-object.model';
describe('DSpaceRESTv2Service', () => { describe('DSpaceRESTv2Service', () => {
let dSpaceRESTv2Service: DSpaceRESTv2Service; let dSpaceRESTv2Service: DSpaceRESTv2Service;
@@ -65,4 +66,15 @@ describe('DSpaceRESTv2Service', () => {
expect(req.request.method).toBe('GET'); expect(req.request.method).toBe('GET');
req.error(mockError); req.error(mockError);
}); });
fdescribe('buildFormData', () => {
it('should return the correct data', () => {
const name = 'testname';
const dso: DSpaceObject = {
name: name
} as DSpaceObject;
const formdata = dSpaceRESTv2Service.buildFormData(dso);
expect(formdata.get('name')).toBe(name);
});
});
}); });