Skip to content

Commit

Permalink
fix: ensure findListByHref correctly calls addDependency
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrevryghem committed Dec 7, 2024
1 parent e4b2ebe commit 7ca4d8f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
54 changes: 36 additions & 18 deletions src/app/core/data/base/base-data.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*
* http://www.dspace.org/license/
*/
// eslint-disable-next-line max-classes-per-file
import {
fakeAsync,
tick,
Expand All @@ -26,14 +27,20 @@ import { HALEndpointServiceStub } from '../../../shared/testing/hal-endpoint-ser
import { ObjectCacheServiceStub } from '../../../shared/testing/object-cache-service.stub';
import { createPaginatedList } from '../../../shared/testing/utils.test';
import { followLink } from '../../../shared/utils/follow-link-config.model';
import { LinkDefinition } from '../../cache/builders/build-decorators';
import {
link,
typedObject,
} from '../../cache/builders/build-decorators';
import { RemoteDataBuildService } from '../../cache/builders/remote-data-build.service';
import { ObjectCacheEntry } from '../../cache/object-cache.reducer';
import { ObjectCacheService } from '../../cache/object-cache.service';
import { BITSTREAM } from '../../shared/bitstream.resource-type';
import { COLLECTION } from '../../shared/collection.resource-type';
import { HALEndpointService } from '../../shared/hal-endpoint.service';
import { HALLink } from '../../shared/hal-link.model';
import { HALResource } from '../../shared/hal-resource.model';
import { ResourceType } from '../../shared/resource-type';
import { FindListOptions } from '../find-list-options.model';
import { PaginatedList } from '../paginated-list.model';
import { RemoteData } from '../remote-data';
import { RequestService } from '../request.service';
import { RequestEntryState } from '../request-entry-state.model';
Expand All @@ -58,6 +65,25 @@ class TestService extends BaseDataService<any> {
}
}

@typedObject
class BaseData {
static type = new ResourceType('test');

foo: string;

_links: {
followLink1: HALLink;
followLink2: HALLink[];
self: HALLink;
};

@link(COLLECTION)
followLink1: Observable<any>;

@link(BITSTREAM, true, 'followLink2')
followLink2CustomVariableName: Observable<PaginatedList<any>>;
}

describe('BaseDataService', () => {
let service: TestService;
let requestService;
Expand All @@ -68,8 +94,8 @@ describe('BaseDataService', () => {
let linksToFollow;
let testScheduler;
let remoteDataTimestamp: number;
let remoteDataMocks: { [responseType: string]: RemoteData<any> };
let remoteDataPageMocks: { [responseType: string]: RemoteData<any> };
let remoteDataMocks: { [responseType: string]: RemoteData<BaseData> };
let remoteDataPageMocks: { [responseType: string]: RemoteData<PaginatedList<BaseData>> };

function initTestService(): TestService {
requestService = getMockRequestService();
Expand All @@ -92,10 +118,10 @@ describe('BaseDataService', () => {
// as cached values.
remoteDataTimestamp = new Date().getTime() + 60 * 1000;
const msToLive = 15 * 60 * 1000;
const payload = {
const payload: BaseData = Object.assign(new BaseData(), {
foo: 'bar',
followLink1: {},
followLink2: {},
followLink1: observableOf({}),
followLink2CustomVariableName: observableOf(createPaginatedList()),
_links: {
self: Object.assign(new HALLink(), {
href: 'self-test-link',
Expand All @@ -112,7 +138,7 @@ describe('BaseDataService', () => {
}),
],
},
};
});
const statusCodeSuccess = 200;
const statusCodeError = 404;
const errorMessage = 'not found';
Expand Down Expand Up @@ -439,11 +465,6 @@ describe('BaseDataService', () => {
spyOn(rdbService, 'buildSingle').and.returnValue(cold('a', {
a: remoteDataMocks.Success,
}));
spyOn(service, 'getLinkDefinition').and.callFake((source, linkName) => {
return {
propertyName: linkName,
} as any as LinkDefinition<HALResource>;
});
const expected = 'a';
const values = {
a: remoteDataMocks.Success,
Expand Down Expand Up @@ -670,19 +691,16 @@ describe('BaseDataService', () => {
c: remoteDataPageMocks.ResponsePending,
d: remoteDataPageMocks.Success,
}));
spyOn(service, 'getLinkDefinition').and.callFake((source, linkName) => {
return {
propertyName: linkName,
} as any as LinkDefinition<HALResource>;
});
const expected = '--b-c-d';
const values = {
b: remoteDataPageMocks.RequestPending,
c: remoteDataPageMocks.ResponsePending,
d: remoteDataPageMocks.Success,
};

expectObservable(service.findListByHref(selfLink, findListOptions, false, false, ...linksToFollow)).toBe(expected, values);
flush();
expect(objectCache.addDependency).toHaveBeenCalledTimes(3);
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/data/base/base-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ export class BaseDataService<T extends CacheableObject> implements HALDataServic
if (hasValue(object?._links)) {
for (const followLinkName of Object.keys(object._links) as (keyof typeof object._links)[]) {
// only add the followLinks if they are embedded, and we get only links from the linkMap with the correct name
const linkDefinition: LinkDefinition<PaginatedList<T>> = getLinkDefinition(remoteDataObject.payload.constructor as GenericConstructor<PaginatedList<T>>, followLinkName);
const linkDefinition: LinkDefinition<PaginatedList<T>> = getLinkDefinition(object.constructor as GenericConstructor<PaginatedList<T>>, followLinkName);
if (linkDefinition?.propertyName && followLinkName !== 'self' && hasValue(object[linkDefinition.propertyName])) {
// followLink can be either an individual HALLink or a HALLink[]
const followLinksList: HALLink[] = [].concat(object._links[followLinkName]);
Expand Down

0 comments on commit 7ca4d8f

Please sign in to comment.