fix: resolve issue with unit tests

This commit is contained in:
pcg-kk
2024-11-06 21:29:34 +01:00
parent d9d39b0cb5
commit 752eb4c57b
2 changed files with 26 additions and 6 deletions

View File

@@ -26,11 +26,13 @@ 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 { RemoteDataBuildService } from '../../cache/builders/remote-data-build.service';
import { ObjectCacheEntry } from '../../cache/object-cache.reducer';
import { ObjectCacheService } from '../../cache/object-cache.service';
import { HALEndpointService } from '../../shared/hal-endpoint.service';
import { HALLink } from '../../shared/hal-link.model';
import { HALResource } from '../../shared/hal-resource.model';
import { FindListOptions } from '../find-list-options.model';
import { RemoteData } from '../remote-data';
import { RequestService } from '../request.service';
@@ -417,6 +419,11 @@ describe('BaseDataService', () => {
c: remoteDataMocks.ResponsePending,
d: remoteDataMocks.Success,
}));
spyOn(service, 'getLinkDefinition').and.callFake((source, linkName) => {
return {
propertyName: linkName,
} as any as LinkDefinition<HALResource>;
});
const expected = '--b-c-d';
const values = {
b: remoteDataMocks.RequestPending,
@@ -625,16 +632,19 @@ 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);
});
});
});

View File

@@ -28,14 +28,19 @@ import {
isNotEmptyOperator,
} from '../../../shared/empty.util';
import { FollowLinkConfig } from '../../../shared/utils/follow-link-config.model';
import { getLinkDefinition } from '../../cache/builders/build-decorators';
import {
getLinkDefinition,
LinkDefinition,
} from '../../cache/builders/build-decorators';
import { RemoteDataBuildService } from '../../cache/builders/remote-data-build.service';
import { CacheableObject } from '../../cache/cacheable-object.model';
import { RequestParam } from '../../cache/models/request-param.model';
import { ObjectCacheEntry } from '../../cache/object-cache.reducer';
import { ObjectCacheService } from '../../cache/object-cache.service';
import { GenericConstructor } from '../../shared/generic-constructor';
import { HALEndpointService } from '../../shared/hal-endpoint.service';
import { HALLink } from '../../shared/hal-link.model';
import { HALResource } from '../../shared/hal-resource.model';
import { getFirstCompletedRemoteData } from '../../shared/operators';
import { URLCombiner } from '../../url-combiner/url-combiner';
import { FindListOptions } from '../find-list-options.model';
@@ -303,7 +308,7 @@ export class BaseDataService<T extends CacheableObject> implements HALDataServic
if (hasValue(remoteDataObject?.payload?._links)) {
for (const followLinkName of Object.keys(remoteDataObject.payload._links)) {
// only add the followLinks if they are embedded, and we get only links from the linkMap with the correct name
const linkDefinition = getLinkDefinition((remoteDataObject.payload as any).constructor, followLinkName);
const linkDefinition = this.getLinkDefinition((remoteDataObject.payload as any).constructor, followLinkName);
if (linkDefinition?.propertyName && hasValue(remoteDataObject.payload[linkDefinition.propertyName]) && followLinkName !== 'self') {
// followLink can be either an individual HALLink or a HALLink[]
const followLinksList: HALLink[] = [].concat(remoteDataObject.payload._links[followLinkName]);
@@ -359,8 +364,8 @@ export class BaseDataService<T extends CacheableObject> implements HALDataServic
if (hasValue(object?._links)) {
for (const followLinkName of Object.keys(object._links)) {
// only add the followLinks if they are embedded, and we get only links from the linkMap with the correct name
const linkDefinition = getLinkDefinition((remoteDataObject.payload as any).constructor, followLinkName);
if (linkDefinition?.propertyName && hasValue(remoteDataObject.payload[linkDefinition.propertyName]) && followLinkName !== 'self') {
const linkDefinition = this.getLinkDefinition((remoteDataObject.payload as any).constructor, 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]);
for (const individualFollowLink of followLinksList) {
@@ -515,4 +520,9 @@ export class BaseDataService<T extends CacheableObject> implements HALDataServic
return done$;
}
getLinkDefinition<D extends HALResource>(source: GenericConstructor<D>, linkName: keyof D['_links']): LinkDefinition<D> {
return getLinkDefinition(source, linkName);
}
}