11import { HttpClient } from '@angular/common/http' ;
2+ import { createSuccessfulRemoteDataObject$ } from '@dspace/core/utilities/remote-data.utils' ;
23import { Store } from '@ngrx/store' ;
34import {
45 cold ,
@@ -13,6 +14,7 @@ import { RestResponse } from '../cache/response.models';
1314import { CoreState } from '../core-state.model' ;
1415import { NotificationsService } from '../notification-system/notifications.service' ;
1516import { ExternalSourceEntry } from '../shared/external-source-entry.model' ;
17+ import { Item } from '../shared/item.model' ;
1618import { HALEndpointServiceStub } from '../testing/hal-endpoint-service.stub' ;
1719import { getMockRemoteDataBuildService } from '../testing/remote-data-build.service.mock' ;
1820import { getMockRequestService } from '../testing/request.service.mock' ;
@@ -209,4 +211,93 @@ describe('ItemDataService', () => {
209211 } ) ;
210212 } ) ;
211213
214+ describe ( 'findByCustomUrl' , ( ) => {
215+ let itemDataService : ItemDataService ;
216+ let searchData : any ;
217+ let findByHrefSpy : jasmine . Spy ;
218+ let getSearchByHrefSpy : jasmine . Spy ;
219+ const id = 'custom-id' ;
220+ const fakeHrefObs = of ( 'https://rest.api/core/items/search/findByCustomURL?q=custom-id' ) ;
221+ const linksToFollow = [ ] ;
222+ const projections = [ 'full' , 'detailed' ] ;
223+
224+ beforeEach ( ( ) => {
225+ searchData = jasmine . createSpyObj ( 'searchData' , [ 'getSearchByHref' ] ) ;
226+ getSearchByHrefSpy = searchData . getSearchByHref . and . returnValue ( fakeHrefObs ) ;
227+ itemDataService = new ItemDataService (
228+ requestService ,
229+ rdbService ,
230+ objectCache ,
231+ halEndpointService ,
232+ notificationsService ,
233+ comparator ,
234+ browseService ,
235+ bundleService ,
236+ ) ;
237+
238+ ( itemDataService as any ) . searchData = searchData ;
239+ findByHrefSpy = spyOn ( itemDataService , 'findByHref' ) . and . returnValue ( createSuccessfulRemoteDataObject$ ( new Item ( ) ) ) ;
240+ } ) ;
241+
242+ it ( 'should call searchData.getSearchByHref with correct parameters' , ( ) => {
243+ itemDataService . findByCustomUrl ( id , true , true , linksToFollow , projections ) . subscribe ( ) ;
244+
245+ expect ( getSearchByHrefSpy ) . toHaveBeenCalledWith (
246+ 'findByCustomURL' ,
247+ jasmine . objectContaining ( {
248+ searchParams : jasmine . arrayContaining ( [
249+ jasmine . objectContaining ( { fieldName : 'q' , fieldValue : id } ) ,
250+ jasmine . objectContaining ( { fieldName : 'projection' , fieldValue : 'full' } ) ,
251+ jasmine . objectContaining ( { fieldName : 'projection' , fieldValue : 'detailed' } ) ,
252+ ] ) ,
253+ } ) ,
254+ ...linksToFollow ,
255+ ) ;
256+ } ) ;
257+
258+ it ( 'should call findByHref with the href observable returned from getSearchByHref' , ( ) => {
259+ itemDataService . findByCustomUrl ( id , true , false , linksToFollow , projections ) . subscribe ( ) ;
260+
261+ expect ( findByHrefSpy ) . toHaveBeenCalledWith ( fakeHrefObs , true , false , ...linksToFollow ) ;
262+ } ) ;
263+ } ) ;
264+
265+ describe ( 'findById' , ( ) => {
266+ let itemDataService : ItemDataService ;
267+
268+ beforeEach ( ( ) => {
269+ itemDataService = new ItemDataService (
270+ requestService ,
271+ rdbService ,
272+ objectCache ,
273+ halEndpointService ,
274+ notificationsService ,
275+ comparator ,
276+ browseService ,
277+ bundleService ,
278+ ) ;
279+ spyOn ( itemDataService , 'findByCustomUrl' ) . and . returnValue ( createSuccessfulRemoteDataObject$ ( new Item ( ) ) ) ;
280+ spyOn ( itemDataService , 'findByHref' ) . and . returnValue ( createSuccessfulRemoteDataObject$ ( new Item ( ) ) ) ;
281+ spyOn ( itemDataService as any , 'getIDHrefObs' ) . and . returnValue ( of ( 'uuid-href' ) ) ;
282+ } ) ;
283+
284+ it ( 'should call findByHref when given a valid UUID' , ( ) => {
285+ const validUuid = '4af28e99-6a9c-4036-a199-e1b587046d39' ;
286+ itemDataService . findById ( validUuid ) . subscribe ( ) ;
287+
288+ expect ( ( itemDataService as any ) . getIDHrefObs ) . toHaveBeenCalledWith ( encodeURIComponent ( validUuid ) ) ;
289+ expect ( itemDataService . findByHref ) . toHaveBeenCalled ( ) ;
290+ expect ( itemDataService . findByCustomUrl ) . not . toHaveBeenCalled ( ) ;
291+ } ) ;
292+
293+ it ( 'should call findByCustomUrl when given a non-UUID id' , ( ) => {
294+ const nonUuid = 'custom-url' ;
295+ itemDataService . findById ( nonUuid ) . subscribe ( ) ;
296+
297+ expect ( itemDataService . findByCustomUrl ) . toHaveBeenCalledWith ( nonUuid , true , true , [ ] ) ;
298+ expect ( itemDataService . findByHref ) . not . toHaveBeenCalled ( ) ;
299+ } ) ;
300+ } ) ;
301+
302+
212303} ) ;
0 commit comments