DURACOM-235 Update comcol-metadata.component.ts and tests

This commit is contained in:
Mattia Vianelli
2024-02-26 12:18:35 +01:00
parent 9d490451d1
commit a059c31bad
2 changed files with 68 additions and 37 deletions

View File

@@ -17,7 +17,7 @@ import {
} from '../../../../remote-data.utils'; } from '../../../../remote-data.utils';
import { ComcolMetadataComponent } from './comcol-metadata.component'; import { ComcolMetadataComponent } from './comcol-metadata.component';
describe('ComColMetadataComponent', () => { fdescribe('ComColMetadataComponent', () => {
let comp: ComcolMetadataComponent<any>; let comp: ComcolMetadataComponent<any>;
let fixture: ComponentFixture<ComcolMetadataComponent<any>>; let fixture: ComponentFixture<ComcolMetadataComponent<any>>;
let dsoDataService; let dsoDataService;
@@ -29,8 +29,6 @@ describe('ComColMetadataComponent', () => {
let routerStub; let routerStub;
let routeStub; let routeStub;
const logoEndpoint = 'rest/api/logo/endpoint';
function initializeVars() { function initializeVars() {
community = Object.assign(new Community(), { community = Object.assign(new Community(), {
uuid: 'a20da287-e174-466a-9926-f66b9300d347', uuid: 'a20da287-e174-466a-9926-f66b9300d347',
@@ -51,7 +49,6 @@ describe('ComColMetadataComponent', () => {
communityDataServiceStub = { communityDataServiceStub = {
update: (com, uuid?) => createSuccessfulRemoteDataObject$(newCommunity), update: (com, uuid?) => createSuccessfulRemoteDataObject$(newCommunity),
patch: () => null, patch: () => null,
getLogoEndpoint: () => observableOf(logoEndpoint)
}; };
routerStub = { routerStub = {
@@ -118,7 +115,6 @@ describe('ComColMetadataComponent', () => {
} }
/* eslint-enable no-empty,@typescript-eslint/no-empty-function */ /* eslint-enable no-empty,@typescript-eslint/no-empty-function */
}, },
deleteLogo: false,
}; };
spyOn(router, 'navigate'); spyOn(router, 'navigate');
}); });
@@ -150,48 +146,84 @@ describe('ComColMetadataComponent', () => {
}); });
}); });
describe('with at least one item in the uploader\'s queue', () => { describe('with an empty operations array', () => {
beforeEach(() => { beforeEach(() => {
data = { data = {
dso: Object.assign(new Community(), { operations: [],
metadata: [{ dso: new Community(),
key: 'dc.title',
value: 'test'
}]
}),
uploader: { uploader: {
options: { options: {
url: '' url: ''
}, },
queue: [ queue: [],
{}
],
/* eslint-disable no-empty,@typescript-eslint/no-empty-function */ /* eslint-disable no-empty,@typescript-eslint/no-empty-function */
uploadAll: () => { uploadAll: () => {
} }
/* eslint-enable no-empty, @typescript-eslint/no-empty-function */ /* eslint-enable no-empty,@typescript-eslint/no-empty-function */
} },
}; };
});
it('should not navigate', () => {
spyOn(router, 'navigate'); spyOn(router, 'navigate');
comp.onSubmit(data);
fixture.detectChanges();
expect(router.navigate).not.toHaveBeenCalled();
}); });
it('should set the uploader\'s url to the logo\'s endpoint', () => { it('should navigate', () => {
comp.onSubmit(data); comp.onSubmit(data);
fixture.detectChanges(); fixture.detectChanges();
expect(data.uploader.options.url).toEqual(logoEndpoint); expect(router.navigate).toHaveBeenCalled();
});
});
describe('with a not empty operations array', () => {
beforeEach(() => {
data = {
operations: [
{
op: 'replace',
path: '/metadata/dc.title',
value: {
value: 'test',
language: null,
},
},
],
dso: new Community(),
uploader: {
options: {
url: ''
},
queue: [],
/* eslint-disable no-empty,@typescript-eslint/no-empty-function */
uploadAll: () => {
}
/* eslint-enable no-empty,@typescript-eslint/no-empty-function */
},
};
spyOn(router, 'navigate');
}); });
it('should call the uploader\'s uploadAll', () => { describe('when successful', () => {
spyOn(data.uploader, 'uploadAll');
comp.onSubmit(data); beforeEach(() => {
fixture.detectChanges(); spyOn(dsoDataService, 'patch').and.returnValue(createSuccessfulRemoteDataObject$({}));
expect(data.uploader.uploadAll).toHaveBeenCalled(); });
it('should navigate', () => {
comp.onSubmit(data);
fixture.detectChanges();
expect(router.navigate).toHaveBeenCalled();
});
});
describe('on failure', () => {
beforeEach(() => {
spyOn(dsoDataService, 'patch').and.returnValue(createFailedRemoteDataObject$('Error', 500));
});
it('should not navigate', () => {
comp.onSubmit(data);
fixture.detectChanges();
expect(router.navigate).not.toHaveBeenCalled();
});
}); });
}); });
}); });

View File

@@ -18,15 +18,14 @@ import { Collection } from '../../../../../core/shared/collection.model';
template: '' template: ''
}) })
export class ComcolMetadataComponent<TDomain extends Community | Collection> implements OnInit { export class ComcolMetadataComponent<TDomain extends Community | Collection> implements OnInit {
/**
* Frontend endpoint for this type of DSO
*/
protected frontendURL: string;
/** /**
* The initial DSO object * The initial DSO object
*/ */
public dsoRD$: Observable<RemoteData<TDomain>>; public dsoRD$: Observable<RemoteData<TDomain>>;
/**
* Frontend endpoint for this type of DSO
*/
protected frontendURL: string;
/** /**
* The type of the dso * The type of the dso
*/ */
@@ -54,7 +53,7 @@ export class ComcolMetadataComponent<TDomain extends Community | Collection> imp
this.dsoDataService.patch(event.dso, event.operations).pipe(getFirstCompletedRemoteData()) this.dsoDataService.patch(event.dso, event.operations).pipe(getFirstCompletedRemoteData())
.subscribe(async (response: RemoteData<DSpaceObject>) => { .subscribe(async (response: RemoteData<DSpaceObject>) => {
if (response.hasSucceeded) { if (response.hasSucceeded) {
await this.router.navigate([this.frontendURL + event.dso.uuid]); await this.router.navigate([this.frontendURL, event.dso.uuid]);
this.notificationsService.success(null, this.translate.get(`${this.type.value}.edit.notifications.success`)); this.notificationsService.success(null, this.translate.get(`${this.type.value}.edit.notifications.success`));
} else if (response.statusCode === 403) { } else if (response.statusCode === 403) {
this.notificationsService.error(null, this.translate.get(`${this.type.value}.edit.notifications.unauthorized`)); this.notificationsService.error(null, this.translate.get(`${this.type.value}.edit.notifications.unauthorized`));
@@ -63,7 +62,7 @@ export class ComcolMetadataComponent<TDomain extends Community | Collection> imp
} }
}); });
} else { } else {
this.router.navigate([this.frontendURL + event.dso.uuid]); this.router.navigate([this.frontendURL, event.dso.uuid]);
} }
} }