Skip to content

Commit 7ca4d8f

Browse files
fix: ensure findListByHref correctly calls addDependency
1 parent e4b2ebe commit 7ca4d8f

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

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

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*
66
* http://www.dspace.org/license/
77
*/
8+
// eslint-disable-next-line max-classes-per-file
89
import {
910
fakeAsync,
1011
tick,
@@ -26,14 +27,20 @@ import { HALEndpointServiceStub } from '../../../shared/testing/hal-endpoint-ser
2627
import { ObjectCacheServiceStub } from '../../../shared/testing/object-cache-service.stub';
2728
import { createPaginatedList } from '../../../shared/testing/utils.test';
2829
import { followLink } from '../../../shared/utils/follow-link-config.model';
29-
import { LinkDefinition } from '../../cache/builders/build-decorators';
30+
import {
31+
link,
32+
typedObject,
33+
} from '../../cache/builders/build-decorators';
3034
import { RemoteDataBuildService } from '../../cache/builders/remote-data-build.service';
3135
import { ObjectCacheEntry } from '../../cache/object-cache.reducer';
3236
import { ObjectCacheService } from '../../cache/object-cache.service';
37+
import { BITSTREAM } from '../../shared/bitstream.resource-type';
38+
import { COLLECTION } from '../../shared/collection.resource-type';
3339
import { HALEndpointService } from '../../shared/hal-endpoint.service';
3440
import { HALLink } from '../../shared/hal-link.model';
35-
import { HALResource } from '../../shared/hal-resource.model';
41+
import { ResourceType } from '../../shared/resource-type';
3642
import { FindListOptions } from '../find-list-options.model';
43+
import { PaginatedList } from '../paginated-list.model';
3744
import { RemoteData } from '../remote-data';
3845
import { RequestService } from '../request.service';
3946
import { RequestEntryState } from '../request-entry-state.model';
@@ -58,6 +65,25 @@ class TestService extends BaseDataService<any> {
5865
}
5966
}
6067

68+
@typedObject
69+
class BaseData {
70+
static type = new ResourceType('test');
71+
72+
foo: string;
73+
74+
_links: {
75+
followLink1: HALLink;
76+
followLink2: HALLink[];
77+
self: HALLink;
78+
};
79+
80+
@link(COLLECTION)
81+
followLink1: Observable<any>;
82+
83+
@link(BITSTREAM, true, 'followLink2')
84+
followLink2CustomVariableName: Observable<PaginatedList<any>>;
85+
}
86+
6187
describe('BaseDataService', () => {
6288
let service: TestService;
6389
let requestService;
@@ -68,8 +94,8 @@ describe('BaseDataService', () => {
6894
let linksToFollow;
6995
let testScheduler;
7096
let remoteDataTimestamp: number;
71-
let remoteDataMocks: { [responseType: string]: RemoteData<any> };
72-
let remoteDataPageMocks: { [responseType: string]: RemoteData<any> };
97+
let remoteDataMocks: { [responseType: string]: RemoteData<BaseData> };
98+
let remoteDataPageMocks: { [responseType: string]: RemoteData<PaginatedList<BaseData>> };
7399

74100
function initTestService(): TestService {
75101
requestService = getMockRequestService();
@@ -92,10 +118,10 @@ describe('BaseDataService', () => {
92118
// as cached values.
93119
remoteDataTimestamp = new Date().getTime() + 60 * 1000;
94120
const msToLive = 15 * 60 * 1000;
95-
const payload = {
121+
const payload: BaseData = Object.assign(new BaseData(), {
96122
foo: 'bar',
97-
followLink1: {},
98-
followLink2: {},
123+
followLink1: observableOf({}),
124+
followLink2CustomVariableName: observableOf(createPaginatedList()),
99125
_links: {
100126
self: Object.assign(new HALLink(), {
101127
href: 'self-test-link',
@@ -112,7 +138,7 @@ describe('BaseDataService', () => {
112138
}),
113139
],
114140
},
115-
};
141+
});
116142
const statusCodeSuccess = 200;
117143
const statusCodeError = 404;
118144
const errorMessage = 'not found';
@@ -439,11 +465,6 @@ describe('BaseDataService', () => {
439465
spyOn(rdbService, 'buildSingle').and.returnValue(cold('a', {
440466
a: remoteDataMocks.Success,
441467
}));
442-
spyOn(service, 'getLinkDefinition').and.callFake((source, linkName) => {
443-
return {
444-
propertyName: linkName,
445-
} as any as LinkDefinition<HALResource>;
446-
});
447468
const expected = 'a';
448469
const values = {
449470
a: remoteDataMocks.Success,
@@ -670,19 +691,16 @@ describe('BaseDataService', () => {
670691
c: remoteDataPageMocks.ResponsePending,
671692
d: remoteDataPageMocks.Success,
672693
}));
673-
spyOn(service, 'getLinkDefinition').and.callFake((source, linkName) => {
674-
return {
675-
propertyName: linkName,
676-
} as any as LinkDefinition<HALResource>;
677-
});
678694
const expected = '--b-c-d';
679695
const values = {
680696
b: remoteDataPageMocks.RequestPending,
681697
c: remoteDataPageMocks.ResponsePending,
682698
d: remoteDataPageMocks.Success,
683699
};
700+
684701
expectObservable(service.findListByHref(selfLink, findListOptions, false, false, ...linksToFollow)).toBe(expected, values);
685702
flush();
703+
expect(objectCache.addDependency).toHaveBeenCalledTimes(3);
686704
});
687705
});
688706
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ export class BaseDataService<T extends CacheableObject> implements HALDataServic
365365
if (hasValue(object?._links)) {
366366
for (const followLinkName of Object.keys(object._links) as (keyof typeof object._links)[]) {
367367
// only add the followLinks if they are embedded, and we get only links from the linkMap with the correct name
368-
const linkDefinition: LinkDefinition<PaginatedList<T>> = getLinkDefinition(remoteDataObject.payload.constructor as GenericConstructor<PaginatedList<T>>, followLinkName);
368+
const linkDefinition: LinkDefinition<PaginatedList<T>> = getLinkDefinition(object.constructor as GenericConstructor<PaginatedList<T>>, followLinkName);
369369
if (linkDefinition?.propertyName && followLinkName !== 'self' && hasValue(object[linkDefinition.propertyName])) {
370370
// followLink can be either an individual HALLink or a HALLink[]
371371
const followLinksList: HALLink[] = [].concat(object._links[followLinkName]);

0 commit comments

Comments
 (0)