Skip to content

Commit b42bf55

Browse files
[DURACOM-413] additional tests
1 parent 66bd310 commit b42bf55

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed

src/app/core/data/item-data.service.spec.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { HttpClient } from '@angular/common/http';
2+
import { createSuccessfulRemoteDataObject$ } from '@dspace/core/utilities/remote-data.utils';
23
import { Store } from '@ngrx/store';
34
import {
45
cold,
@@ -13,6 +14,7 @@ import { RestResponse } from '../cache/response.models';
1314
import { CoreState } from '../core-state.model';
1415
import { NotificationsService } from '../notification-system/notifications.service';
1516
import { ExternalSourceEntry } from '../shared/external-source-entry.model';
17+
import { Item } from '../shared/item.model';
1618
import { HALEndpointServiceStub } from '../testing/hal-endpoint-service.stub';
1719
import { getMockRemoteDataBuildService } from '../testing/remote-data-build.service.mock';
1820
import { 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
});

src/app/core/data/item-data.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ export abstract class BaseItemDataService extends IdentifiableDataService<Item>
442442
* {@link HALLink}s should be automatically resolved
443443
* @param projections List of {@link projections} used to pass as parameters
444444
*/
445-
private findByCustomUrl(id: string, useCachedVersionIfAvailable = true, reRequestOnStale = true, linksToFollow: FollowLinkConfig<Item>[], projections: string[] = []): Observable<RemoteData<Item>> {
445+
public findByCustomUrl(id: string, useCachedVersionIfAvailable = true, reRequestOnStale = true, linksToFollow: FollowLinkConfig<Item>[], projections: string[] = []): Observable<RemoteData<Item>> {
446446
const searchHref = 'findByCustomURL';
447447

448448
const options = Object.assign({}, {
@@ -471,7 +471,7 @@ export abstract class BaseItemDataService extends IdentifiableDataService<Item>
471471
* @param linksToFollow List of {@link FollowLinkConfig} that indicate which
472472
* {@link HALLink}s should be automatically resolved
473473
*/
474-
findById(id: string, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<Item>[]): Observable<RemoteData<Item>> {
474+
public findById(id: string, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<Item>[]): Observable<RemoteData<Item>> {
475475

476476
if (uuidValidate(id)) {
477477
const href$ = this.getIDHrefObs(encodeURIComponent(id), ...linksToFollow);

0 commit comments

Comments
 (0)