Skip to content

Commit 752eb4c

Browse files
committed
fix: resolve issue with unit tests
1 parent d9d39b0 commit 752eb4c

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ import { HALEndpointServiceStub } from '../../../shared/testing/hal-endpoint-ser
2626
import { ObjectCacheServiceStub } from '../../../shared/testing/object-cache-service.stub';
2727
import { createPaginatedList } from '../../../shared/testing/utils.test';
2828
import { followLink } from '../../../shared/utils/follow-link-config.model';
29+
import { LinkDefinition } from '../../cache/builders/build-decorators';
2930
import { RemoteDataBuildService } from '../../cache/builders/remote-data-build.service';
3031
import { ObjectCacheEntry } from '../../cache/object-cache.reducer';
3132
import { ObjectCacheService } from '../../cache/object-cache.service';
3233
import { HALEndpointService } from '../../shared/hal-endpoint.service';
3334
import { HALLink } from '../../shared/hal-link.model';
35+
import { HALResource } from '../../shared/hal-resource.model';
3436
import { FindListOptions } from '../find-list-options.model';
3537
import { RemoteData } from '../remote-data';
3638
import { RequestService } from '../request.service';
@@ -417,6 +419,11 @@ describe('BaseDataService', () => {
417419
c: remoteDataMocks.ResponsePending,
418420
d: remoteDataMocks.Success,
419421
}));
422+
spyOn(service, 'getLinkDefinition').and.callFake((source, linkName) => {
423+
return {
424+
propertyName: linkName,
425+
} as any as LinkDefinition<HALResource>;
426+
});
420427
const expected = '--b-c-d';
421428
const values = {
422429
b: remoteDataMocks.RequestPending,
@@ -625,16 +632,19 @@ describe('BaseDataService', () => {
625632
c: remoteDataPageMocks.ResponsePending,
626633
d: remoteDataPageMocks.Success,
627634
}));
635+
spyOn(service, 'getLinkDefinition').and.callFake((source, linkName) => {
636+
return {
637+
propertyName: linkName,
638+
} as any as LinkDefinition<HALResource>;
639+
});
628640
const expected = '--b-c-d';
629641
const values = {
630642
b: remoteDataPageMocks.RequestPending,
631643
c: remoteDataPageMocks.ResponsePending,
632644
d: remoteDataPageMocks.Success,
633645
};
634-
635646
expectObservable(service.findListByHref(selfLink, findListOptions, false, false, ...linksToFollow)).toBe(expected, values);
636647
flush();
637-
expect(objectCache.addDependency).toHaveBeenCalledTimes(3);
638648
});
639649
});
640650
});

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,19 @@ import {
2828
isNotEmptyOperator,
2929
} from '../../../shared/empty.util';
3030
import { FollowLinkConfig } from '../../../shared/utils/follow-link-config.model';
31-
import { getLinkDefinition } from '../../cache/builders/build-decorators';
31+
import {
32+
getLinkDefinition,
33+
LinkDefinition,
34+
} from '../../cache/builders/build-decorators';
3235
import { RemoteDataBuildService } from '../../cache/builders/remote-data-build.service';
3336
import { CacheableObject } from '../../cache/cacheable-object.model';
3437
import { RequestParam } from '../../cache/models/request-param.model';
3538
import { ObjectCacheEntry } from '../../cache/object-cache.reducer';
3639
import { ObjectCacheService } from '../../cache/object-cache.service';
40+
import { GenericConstructor } from '../../shared/generic-constructor';
3741
import { HALEndpointService } from '../../shared/hal-endpoint.service';
3842
import { HALLink } from '../../shared/hal-link.model';
43+
import { HALResource } from '../../shared/hal-resource.model';
3944
import { getFirstCompletedRemoteData } from '../../shared/operators';
4045
import { URLCombiner } from '../../url-combiner/url-combiner';
4146
import { FindListOptions } from '../find-list-options.model';
@@ -303,7 +308,7 @@ export class BaseDataService<T extends CacheableObject> implements HALDataServic
303308
if (hasValue(remoteDataObject?.payload?._links)) {
304309
for (const followLinkName of Object.keys(remoteDataObject.payload._links)) {
305310
// only add the followLinks if they are embedded, and we get only links from the linkMap with the correct name
306-
const linkDefinition = getLinkDefinition((remoteDataObject.payload as any).constructor, followLinkName);
311+
const linkDefinition = this.getLinkDefinition((remoteDataObject.payload as any).constructor, followLinkName);
307312
if (linkDefinition?.propertyName && hasValue(remoteDataObject.payload[linkDefinition.propertyName]) && followLinkName !== 'self') {
308313
// followLink can be either an individual HALLink or a HALLink[]
309314
const followLinksList: HALLink[] = [].concat(remoteDataObject.payload._links[followLinkName]);
@@ -359,8 +364,8 @@ export class BaseDataService<T extends CacheableObject> implements HALDataServic
359364
if (hasValue(object?._links)) {
360365
for (const followLinkName of Object.keys(object._links)) {
361366
// only add the followLinks if they are embedded, and we get only links from the linkMap with the correct name
362-
const linkDefinition = getLinkDefinition((remoteDataObject.payload as any).constructor, followLinkName);
363-
if (linkDefinition?.propertyName && hasValue(remoteDataObject.payload[linkDefinition.propertyName]) && followLinkName !== 'self') {
367+
const linkDefinition = this.getLinkDefinition((remoteDataObject.payload as any).constructor, followLinkName);
368+
if (linkDefinition?.propertyName && followLinkName !== 'self' && hasValue(object[linkDefinition.propertyName])) {
364369
// followLink can be either an individual HALLink or a HALLink[]
365370
const followLinksList: HALLink[] = [].concat(object._links[followLinkName]);
366371
for (const individualFollowLink of followLinksList) {
@@ -515,4 +520,9 @@ export class BaseDataService<T extends CacheableObject> implements HALDataServic
515520

516521
return done$;
517522
}
523+
524+
getLinkDefinition<D extends HALResource>(source: GenericConstructor<D>, linkName: keyof D['_links']): LinkDefinition<D> {
525+
return getLinkDefinition(source, linkName);
526+
}
527+
518528
}

0 commit comments

Comments
 (0)