[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) { removeByHrefSubstring(href: string) {
// Do nothing // Do nothing
} },
}) as RequestService; }) as RequestService;
const rdbService = getMockRemoteDataBuildService(); 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 { VersionHistoryDataService } from '../../../core/data/version-history-data.service';
import { WorkspaceitemDataService } from '../../../core/submission/workspaceitem-data.service'; import { WorkspaceitemDataService } from '../../../core/submission/workspaceitem-data.service';
import { WorkflowItemDataService } from '../../../core/submission/workflowitem-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', () => { describe('ItemVersionsSharedService', () => {
let service: 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(() => { beforeEach(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
@@ -20,16 +28,29 @@ describe('ItemVersionsSharedService', () => {
{ provide: VersionDataService, useValue: {} }, { provide: VersionDataService, useValue: {} },
{ provide: VersionHistoryDataService, useValue: {} }, { provide: VersionHistoryDataService, useValue: {} },
{ provide: AuthService, useValue: {} }, { provide: AuthService, useValue: {} },
{ provide: NotificationsService, useValue: {} }, { provide: NotificationsService, useValue: notificationsServiceSpy },
{ provide: TranslateService, useValue: {} }, { provide: TranslateService, useValue: { get: () => undefined, } },
{ provide: WorkspaceitemDataService, useValue: {} }, { provide: WorkspaceitemDataService, useValue: {} },
{ provide: WorkflowItemDataService, useValue: {} }, { provide: WorkflowItemDataService, useValue: {} },
], ],
}); });
service = TestBed.inject(ItemVersionsSharedService); service = TestBed.inject(ItemVersionsSharedService);
notificationService = TestBed.inject(NotificationsService);
}); });
it('should be created', () => { it('should be created', () => {
expect(service).toBeTruthy(); 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 { Injectable } from '@angular/core';
import { NotificationsService } from '../../notifications/notifications.service'; import { NotificationsService } from '../../notifications/notifications.service';
import { TranslateService } from '@ngx-translate/core'; 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 { RemoteData } from '../../../core/data/remote-data';
import { Version } from '../../../core/shared/version.model'; import { Version } from '../../../core/shared/version.model';
@@ -16,7 +12,6 @@ export class ItemVersionsSharedService {
constructor( constructor(
private notificationsService: NotificationsService, private notificationsService: NotificationsService,
private translateService: TranslateService, 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 newVersionRD the new version that has been created
* @param summary the optional summary for the new version
*/ */
createNewVersionAndNotify(item: Item, summary: string): Observable<RemoteData<Version>> { public notifyCreateNewVersion(newVersionRD: RemoteData<Version>): void {
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>) {
const newVersionNumber = newVersionRD?.payload?.version; const newVersionNumber = newVersionRD?.payload?.version;
newVersionRD.hasSucceeded ? newVersionRD.hasSucceeded ?
this.notificationsService.success(null, this.translateService.get(ItemVersionsSharedService.msg('success'), {version: newVersionNumber})) : 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'))); 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')));
}
} }