63469: JSDocs + tests

This commit is contained in:
Kristof De Langhe
2019-07-08 14:30:19 +02:00
parent 8265942f18
commit 34e75a46e5
3 changed files with 50 additions and 23 deletions

View File

@@ -227,10 +227,8 @@ describe('ItemRelationshipsComponent', () => {
comp.submit(); comp.submit();
}); });
it('it should delete the correct relationship and de-cache the current item', () => { it('it should delete the correct relationship', () => {
expect(relationshipService.deleteRelationship).toHaveBeenCalledWith(relationships[1].uuid); expect(relationshipService.deleteRelationship).toHaveBeenCalledWith(relationships[1].uuid);
expect(objectCache.remove).toHaveBeenCalledWith(item.self);
expect(requestService.removeByHrefSubstring).toHaveBeenCalledWith(item.self);
}); });
}); });
}); });

View File

@@ -13,6 +13,8 @@ import { Item } from '../shared/item.model';
import { PaginatedList } from './paginated-list'; import { PaginatedList } from './paginated-list';
import { PageInfo } from '../shared/page-info.model'; import { PageInfo } from '../shared/page-info.model';
import { DeleteRequest } from './request.models'; import { DeleteRequest } from './request.models';
import { ObjectCacheService } from '../cache/object-cache.service';
import { Observable } from 'rxjs/internal/Observable';
describe('RelationshipService', () => { describe('RelationshipService', () => {
let service: RelationshipService; let service: RelationshipService;
@@ -22,6 +24,11 @@ describe('RelationshipService', () => {
const relationshipsEndpointURL = `${restEndpointURL}/relationships`; const relationshipsEndpointURL = `${restEndpointURL}/relationships`;
const halService: any = new HALEndpointServiceStub(restEndpointURL); const halService: any = new HALEndpointServiceStub(restEndpointURL);
const rdbService = getMockRemoteDataBuildService(); const rdbService = getMockRemoteDataBuildService();
const objectCache = Object.assign({
/* tslint:disable:no-empty */
remove: () => {}
/* tslint:enable:no-empty */
}) as ObjectCacheService;
const relationshipType = Object.assign(new RelationshipType(), { const relationshipType = Object.assign(new RelationshipType(), {
type: ResourceType.RelationshipType, type: ResourceType.RelationshipType,
@@ -31,24 +38,20 @@ describe('RelationshipService', () => {
rightLabel: 'isPublicationOfAuthor' rightLabel: 'isPublicationOfAuthor'
}); });
const relationships = [ const relationship1 = Object.assign(new Relationship(), {
Object.assign(new Relationship(), { self: relationshipsEndpointURL + '/2',
self: relationshipsEndpointURL + '/2', id: '2',
id: '2', uuid: '2',
uuid: '2', relationshipType: observableOf(new RemoteData(false, false, true, undefined, relationshipType))
leftId: 'author1', });
rightId: 'publication', const relationship2 = Object.assign(new Relationship(), {
relationshipType: observableOf(new RemoteData(false, false, true, undefined, relationshipType)) self: relationshipsEndpointURL + '/3',
}), id: '3',
Object.assign(new Relationship(), { uuid: '3',
self: relationshipsEndpointURL + '/3', relationshipType: observableOf(new RemoteData(false, false, true, undefined, relationshipType))
id: '3', });
uuid: '3',
leftId: 'author2', const relationships = [ relationship1, relationship2 ];
rightId: 'publication',
relationshipType: observableOf(new RemoteData(false, false, true, undefined, relationshipType))
})
];
const item = Object.assign(new Item(), { const item = Object.assign(new Item(), {
self: 'fake-item-url/publication', self: 'fake-item-url/publication',
@@ -65,6 +68,10 @@ describe('RelationshipService', () => {
id: 'author2', id: 'author2',
uuid: 'author2' uuid: 'author2'
}); });
relationship1.leftItem = getRemotedataObservable(relatedItem1);
relationship1.rightItem = getRemotedataObservable(item);
relationship2.leftItem = getRemotedataObservable(relatedItem2);
relationship2.rightItem = getRemotedataObservable(item);
const relatedItems = [relatedItem1, relatedItem2]; const relatedItems = [relatedItem1, relatedItem2];
const itemService = jasmine.createSpyObj('itemService', { const itemService = jasmine.createSpyObj('itemService', {
@@ -76,7 +83,8 @@ describe('RelationshipService', () => {
requestService, requestService,
halService, halService,
rdbService, rdbService,
itemService itemService,
objectCache
); );
} }
@@ -93,13 +101,22 @@ describe('RelationshipService', () => {
describe('deleteRelationship', () => { describe('deleteRelationship', () => {
beforeEach(() => { beforeEach(() => {
spyOn(service, 'findById').and.returnValue(getRemotedataObservable(relationship1));
spyOn(objectCache, 'remove');
service.deleteRelationship(relationships[0].uuid).subscribe(); service.deleteRelationship(relationships[0].uuid).subscribe();
}); });
it('should send a DeleteRequest', () => { it('should send a DeleteRequest', () => {
const expected = new DeleteRequest(requestService.generateRequestId(), relationshipsEndpointURL + '/' + relationships[0].uuid); const expected = new DeleteRequest(requestService.generateRequestId(), relationshipsEndpointURL + '/' + relationship1.uuid);
expect(requestService.configure).toHaveBeenCalledWith(expected, undefined); expect(requestService.configure).toHaveBeenCalledWith(expected, undefined);
}); });
it('should clear the related items their cache', () => {
expect(objectCache.remove).toHaveBeenCalledWith(relatedItem1.self);
expect(objectCache.remove).toHaveBeenCalledWith(item.self);
expect(requestService.removeByHrefSubstring).toHaveBeenCalledWith(relatedItem1.self);
expect(requestService.removeByHrefSubstring).toHaveBeenCalledWith(item.self);
});
}); });
describe('getItemRelationshipsArray', () => { describe('getItemRelationshipsArray', () => {
@@ -135,3 +152,7 @@ describe('RelationshipService', () => {
}) })
}); });
function getRemotedataObservable(obj: any): Observable<RemoteData<any>> {
return observableOf(new RemoteData(false, false, true, undefined, obj));
}

View File

@@ -51,6 +51,10 @@ export class RelationshipService {
); );
} }
/**
* Find a relationship by its UUID
* @param uuid
*/
findById(uuid: string): Observable<RemoteData<Relationship>> { findById(uuid: string): Observable<RemoteData<Relationship>> {
const href$ = this.getRelationshipEndpoint(uuid); const href$ = this.getRelationshipEndpoint(uuid);
return this.rdbService.buildSingle<Relationship>(href$); return this.rdbService.buildSingle<Relationship>(href$);
@@ -211,6 +215,10 @@ export class RelationshipService {
); );
} }
/**
* Clear object and request caches of the items related to a relationship (left and right items)
* @param uuid
*/
clearRelatedCache(uuid: string) { clearRelatedCache(uuid: string) {
this.findById(uuid).pipe( this.findById(uuid).pipe(
getSucceededRemoteData(), getSucceededRemoteData(),