diff --git a/src/app/+collection-page/create-collection-page/create-collection-page.component.ts b/src/app/+collection-page/create-collection-page/create-collection-page.component.ts index 4d94a53035..6429e623bf 100644 --- a/src/app/+collection-page/create-collection-page/create-collection-page.component.ts +++ b/src/app/+collection-page/create-collection-page/create-collection-page.component.ts @@ -33,8 +33,7 @@ export class CreateCollectionPageComponent { private collectionDataService: CollectionDataService, private communityDataService: CommunityDataService, private routeService: RouteService, - private router: Router, - private objectCache: ObjectCacheService + private router: Router ) { this.parentUUID$ = this.routeService.getQueryParameterValue('parent'); this.parentUUID$.subscribe((uuid: string) => { @@ -56,11 +55,10 @@ export class CreateCollectionPageComponent { // TODO: metadata for news and provenance ] }); - this.collectionDataService.create(collection, uuid).pipe( - flatMap((rd: RemoteData) => this.objectCache.getByUUID(rd.payload.id)), - isNotEmptyOperator() - ).subscribe((col: NormalizedCollection) => { - this.router.navigate(['collections', col.id]); + this.collectionDataService.create(collection, uuid).subscribe((rd: RemoteData) => { + if (rd.hasSucceeded) { + this.router.navigate(['collections', rd.payload.id]); + } }); }); } diff --git a/src/app/core/data/data.service.spec.ts b/src/app/core/data/data.service.spec.ts index 7ebe536219..d16af124e4 100644 --- a/src/app/core/data/data.service.spec.ts +++ b/src/app/core/data/data.service.spec.ts @@ -18,6 +18,12 @@ import { DSpaceObject } from '../shared/dspace-object.model'; import { RemoteData } from './remote-data'; import { RequestEntry } from './request.reducer'; 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'; @@ -49,7 +55,7 @@ class TestService extends DataService { describe('DataService', () => { let service: TestService; let options: FindAllOptions; - const responseCache = getMockResponseCacheService(); + let responseCache = getMockResponseCacheService(); let rdbService = {} as RemoteDataBuildService; const authService = {} as AuthService; const notificationsService = {} as NotificationsService; @@ -57,12 +63,38 @@ describe('DataService', () => { const store = {} as Store; const endpoint = 'https://rest.api/core'; const halService = Object.assign({ - getEndpoint: () => Observable.of(endpoint) + getEndpoint: (linkpath) => Observable.of(endpoint) }); 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, responseCache$: Observable, payload$: Observable) => { + 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 { return new TestService( responseCache, @@ -136,13 +168,13 @@ describe('DataService', () => { }); it('should include all provided options in href', () => { - const sortOptions = new SortOptions('field1', SortDirection.DESC) + const sortOptions = new SortOptions('field1', SortDirection.DESC); options = { currentPage: 6, elementsPerPage: 10, sort: sortOptions, startsWith: 'ab' - } + }; const expected = `${endpoint}?page=${options.currentPage - 1}&size=${options.elementsPerPage}` + `&sort=${sortOptions.field},${sortOptions.direction}&startsWith=${options.startsWith}`; @@ -152,14 +184,12 @@ describe('DataService', () => { }) }); - fdescribe('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('create', () => { describe('when the request was successful', () => { beforeEach(() => { - rdbService = getMockRemoteDataBuildService(successfulRd$); + responseCache = initSuccessfulResponseCacheService(); + rdbService = initSuccessfulRemoteDataBuildService(); service = initTestService(); }); @@ -168,23 +198,25 @@ describe('DataService', () => { expect(rd.payload).toBe(dso); }); }); - }); - describe('when the request was unsuccessful', () => { - beforeEach(() => { - rdbService = getMockRemoteDataBuildService(failingRd$); - service = initTestService(); + it('should get the response from cache with the correct url when parent is empty', () => { + const expectedUrl = endpoint; + + service.create(dso, undefined).subscribe((value) => { + expect(responseCache.get).toHaveBeenCalledWith(expectedUrl); + }); }); - it('should not return anything', () => { - service.create(dso, undefined); + it('should get the response from cache with the correct url when parent is not empty', () => { + 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 - }); }); diff --git a/src/app/core/dspace-rest-v2/dspace-rest-v2.service.spec.ts b/src/app/core/dspace-rest-v2/dspace-rest-v2.service.spec.ts index 4893908627..256fffaa24 100644 --- a/src/app/core/dspace-rest-v2/dspace-rest-v2.service.spec.ts +++ b/src/app/core/dspace-rest-v2/dspace-rest-v2.service.spec.ts @@ -2,6 +2,7 @@ import { TestBed, inject } from '@angular/core/testing'; import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; import { DSpaceRESTv2Service } from './dspace-rest-v2.service'; +import { DSpaceObject } from '../shared/dspace-object.model'; describe('DSpaceRESTv2Service', () => { let dSpaceRESTv2Service: DSpaceRESTv2Service; @@ -65,4 +66,15 @@ describe('DSpaceRESTv2Service', () => { expect(req.request.method).toBe('GET'); 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); + }); + }); });