[CST-4499] Version history - Test item-data.service and item-versions-shared.service

This commit is contained in:
Davide Negretti
2021-10-19 13:34:58 +02:00
parent e142a49479
commit 376d803b96
3 changed files with 37 additions and 28 deletions

View File

@@ -31,7 +31,7 @@ describe('ItemDataService', () => {
},
removeByHrefSubstring(href: string) {
// Do nothing
}
},
}) as RequestService;
const rdbService = getMockRemoteDataBuildService();
@@ -184,4 +184,14 @@ describe('ItemDataService', () => {
});
});
describe('when cache is invalidated', () => {
beforeEach(() => {
service = initTestService();
});
it('should call setStaleByHrefSubstring', () => {
service.invalidateItemCache('uuid');
expect(requestService.setStaleByHrefSubstring).toHaveBeenCalledWith('item/uuid');
});
});
});

View File

@@ -9,9 +9,17 @@ import { TranslateService } from '@ngx-translate/core';
import { VersionHistoryDataService } from '../../../core/data/version-history-data.service';
import { WorkspaceitemDataService } from '../../../core/submission/workspaceitem-data.service';
import { WorkflowItemDataService } from '../../../core/submission/workflowitem-data.service';
import { createFailedRemoteDataObject, createSuccessfulRemoteDataObject } from '../../remote-data.utils';
import { Version } from '../../../core/shared/version.model';
describe('ItemVersionsSharedService', () => {
let service: ItemVersionsSharedService;
let notificationService: NotificationsService;
const successfulVersionRD = createSuccessfulRemoteDataObject<Version>(new Version());
const failedVersionRD = createFailedRemoteDataObject<Version>();
const notificationsServiceSpy = jasmine.createSpyObj('notificationsServiceSpy', ['success', 'error']);
beforeEach(() => {
TestBed.configureTestingModule({
@@ -20,16 +28,29 @@ describe('ItemVersionsSharedService', () => {
{ provide: VersionDataService, useValue: {} },
{ provide: VersionHistoryDataService, useValue: {} },
{ provide: AuthService, useValue: {} },
{ provide: NotificationsService, useValue: {} },
{ provide: TranslateService, useValue: {} },
{ provide: NotificationsService, useValue: notificationsServiceSpy },
{ provide: TranslateService, useValue: { get: () => undefined, } },
{ provide: WorkspaceitemDataService, useValue: {} },
{ provide: WorkflowItemDataService, useValue: {} },
],
});
service = TestBed.inject(ItemVersionsSharedService);
notificationService = TestBed.inject(NotificationsService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
describe('when notifyCreateNewVersion is called', () => {
it('should notify when successful', () => {
service.notifyCreateNewVersion(successfulVersionRD);
expect(notificationService.success).toHaveBeenCalled();
});
it('should notify when not successful', () => {
service.notifyCreateNewVersion(failedVersionRD);
expect(notificationService.error).toHaveBeenCalled();
});
});
});

View File

@@ -1,10 +1,6 @@
import { Injectable } from '@angular/core';
import { NotificationsService } from '../../notifications/notifications.service';
import { TranslateService } from '@ngx-translate/core';
import { Item } from '../../../core/shared/item.model';
import { switchMap } from 'rxjs/operators';
import { VersionHistoryDataService } from '../../../core/data/version-history-data.service';
import { Observable, of } from 'rxjs';
import { RemoteData } from '../../../core/data/remote-data';
import { Version } from '../../../core/shared/version.model';
@@ -16,7 +12,6 @@ export class ItemVersionsSharedService {
constructor(
private notificationsService: NotificationsService,
private translateService: TranslateService,
private versionHistoryService: VersionHistoryDataService,
) {
}
@@ -26,32 +21,15 @@ export class ItemVersionsSharedService {
}
/**
* Create a new version, notify success/failure and return new version number
* Notify success/failure after creating a new version.
*
* @param item the item from which a new version will be created
* @param summary the optional summary for the new version
* @param newVersionRD the new version that has been created
*/
createNewVersionAndNotify(item: Item, summary: string): Observable<RemoteData<Version>> {
return this.versionHistoryService.createVersion(item._links.self.href, summary).pipe(
switchMap((postResult: RemoteData<Version>) => {
const newVersionNumber = postResult?.payload?.version;
this.notifyCreateNewVersionBak(postResult.hasSucceeded, postResult.statusCode, newVersionNumber);
return of(postResult);
})
);
}
public notifyCreateNewVersion(newVersionRD: RemoteData<Version>) {
public notifyCreateNewVersion(newVersionRD: RemoteData<Version>): void {
const newVersionNumber = newVersionRD?.payload?.version;
newVersionRD.hasSucceeded ?
this.notificationsService.success(null, this.translateService.get(ItemVersionsSharedService.msg('success'), {version: newVersionNumber})) :
this.notificationsService.error(null, this.translateService.get(ItemVersionsSharedService.msg(newVersionRD?.statusCode === 422 ? 'inProgress' : 'failure')));
}
private notifyCreateNewVersionBak(success: boolean, statusCode: number, newVersionNumber: number) { // TODO delete this
success ?
this.notificationsService.success(null, this.translateService.get(ItemVersionsSharedService.msg('success'), {version: newVersionNumber})) :
this.notificationsService.error(null, this.translateService.get(ItemVersionsSharedService.msg(statusCode === 422 ? 'inProgress' : 'failure')));
}
}