mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 18:14:17 +00:00
start group tests - group data service
This commit is contained in:
187
src/app/core/eperson/group-data.service.spec.ts
Normal file
187
src/app/core/eperson/group-data.service.spec.ts
Normal file
@@ -0,0 +1,187 @@
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { HttpHeaders } from '@angular/common/http';
|
||||
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { Store, StoreModule } from '@ngrx/store';
|
||||
import { of as observableOf } from 'rxjs';
|
||||
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
|
||||
import { compare, Operation } from 'fast-json-patch';
|
||||
import {
|
||||
GroupRegistryCancelGroupAction,
|
||||
GroupRegistryEditGroupAction
|
||||
} from '../../+admin/admin-access-control/group-registry/group-registry.actions';
|
||||
import { getMockRemoteDataBuildServiceHrefMap } from '../../shared/mocks/mock-remote-data-build.service';
|
||||
import { getMockRequestService } from '../../shared/mocks/mock-request.service';
|
||||
import { MockTranslateLoader } from '../../shared/mocks/mock-translate-loader';
|
||||
import { EPersonMock, EPersonMock2 } from '../../shared/testing/eperson-mock';
|
||||
import { GroupMock, GroupMock2 } from '../../shared/testing/group-mock';
|
||||
import { HALEndpointServiceStub } from '../../shared/testing/hal-endpoint-service-stub';
|
||||
import { createSuccessfulRemoteDataObject$ } from '../../shared/testing/utils';
|
||||
import { SearchParam } from '../cache/models/search-param.model';
|
||||
import { CoreState } from '../core.reducers';
|
||||
import { ChangeAnalyzer } from '../data/change-analyzer';
|
||||
import { PaginatedList } from '../data/paginated-list';
|
||||
import { DeleteByIDRequest, DeleteRequest, FindListOptions, PostRequest } from '../data/request.models';
|
||||
import { RequestEntry } from '../data/request.reducer';
|
||||
import { RequestService } from '../data/request.service';
|
||||
import { HttpOptions } from '../dspace-rest-v2/dspace-rest-v2.service';
|
||||
import { Item } from '../shared/item.model';
|
||||
import { PageInfo } from '../shared/page-info.model';
|
||||
import { GroupDataService } from './group-data.service';
|
||||
|
||||
describe('GroupDataService', () => {
|
||||
let service: GroupDataService;
|
||||
let store: Store<CoreState>;
|
||||
let requestService: RequestService;
|
||||
|
||||
const restEndpointURL = 'https://dspace.4science.it/dspace-spring-rest/api/eperson';
|
||||
const groupsEndpoint = `${restEndpointURL}/groups`;
|
||||
const groups = [GroupMock, GroupMock2];
|
||||
const groups$ = createSuccessfulRemoteDataObject$(new PaginatedList(new PageInfo(), groups));
|
||||
const halService: any = new HALEndpointServiceStub(restEndpointURL);
|
||||
const rdbService = getMockRemoteDataBuildServiceHrefMap(undefined, { 'https://dspace.4science.it/dspace-spring-rest/api/eperson/groups': groups$ });
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
imports: [
|
||||
CommonModule,
|
||||
StoreModule.forRoot({}),
|
||||
TranslateModule.forRoot({
|
||||
loader: {
|
||||
provide: TranslateLoader,
|
||||
useClass: MockTranslateLoader
|
||||
}
|
||||
}),
|
||||
],
|
||||
declarations: [],
|
||||
providers: [],
|
||||
schemas: [CUSTOM_ELEMENTS_SCHEMA]
|
||||
});
|
||||
|
||||
const getRequestEntry$ = (successful: boolean) => {
|
||||
return observableOf({
|
||||
completed: true,
|
||||
response: { isSuccessful: successful, payload: groups } as any
|
||||
} as RequestEntry)
|
||||
};
|
||||
|
||||
function initTestService() {
|
||||
return new GroupDataService(
|
||||
new DummyChangeAnalyzer() as any,
|
||||
null,
|
||||
null,
|
||||
requestService,
|
||||
rdbService,
|
||||
store,
|
||||
null,
|
||||
halService
|
||||
);
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
requestService = getMockRequestService(getRequestEntry$(true));
|
||||
store = new Store<CoreState>(undefined, undefined, undefined);
|
||||
service = initTestService();
|
||||
spyOn(store, 'dispatch');
|
||||
});
|
||||
|
||||
describe('searchGroups', () => {
|
||||
beforeEach(() => {
|
||||
spyOn(service, 'searchBy');
|
||||
});
|
||||
|
||||
it('search with empty query', () => {
|
||||
service.searchGroups('');
|
||||
const options = Object.assign(new FindListOptions(), {
|
||||
searchParams: [Object.assign(new SearchParam('query', ''))]
|
||||
});
|
||||
expect(service.searchBy).toHaveBeenCalledWith('byMetadata', options);
|
||||
});
|
||||
|
||||
it('search with query', () => {
|
||||
service.searchGroups('test');
|
||||
const options = Object.assign(new FindListOptions(), {
|
||||
searchParams: [Object.assign(new SearchParam('query', 'test'))]
|
||||
});
|
||||
expect(service.searchBy).toHaveBeenCalledWith('byMetadata', options);
|
||||
});
|
||||
});
|
||||
|
||||
describe('deleteGroup', () => {
|
||||
beforeEach(() => {
|
||||
service.deleteGroup(GroupMock2).subscribe();
|
||||
});
|
||||
|
||||
it('should send DeleteRequest', () => {
|
||||
const expected = new DeleteByIDRequest(requestService.generateRequestId(), groupsEndpoint + '/' + GroupMock2.uuid, GroupMock2.uuid);
|
||||
expect(requestService.configure).toHaveBeenCalledWith(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('addSubGroupToGroup', () => {
|
||||
beforeEach(() => {
|
||||
service.addSubGroupToGroup(GroupMock, GroupMock2).subscribe();
|
||||
});
|
||||
it('should send PostRequest to eperson/groups/group-id/subgroups endpoint with new subgroup link in body', () => {
|
||||
let headers = new HttpHeaders();
|
||||
const options: HttpOptions = Object.create({});
|
||||
headers = headers.append('Content-Type', 'text/uri-list');
|
||||
options.headers = headers;
|
||||
const expected = new PostRequest(requestService.generateRequestId(), GroupMock.self + '/' + service.subgroupsEndpoint, GroupMock2.self, options);
|
||||
expect(requestService.configure).toHaveBeenCalledWith(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('deleteSubGroupFromGroup', () => {
|
||||
beforeEach(() => {
|
||||
service.deleteSubGroupFromGroup(GroupMock, GroupMock2).subscribe();
|
||||
});
|
||||
it('should send DeleteRequest to eperson/groups/group-id/subgroups/group-id endpoint', () => {
|
||||
const expected = new DeleteRequest(requestService.generateRequestId(), GroupMock.self + '/' + service.subgroupsEndpoint + '/' + GroupMock2.id);
|
||||
expect(requestService.configure).toHaveBeenCalledWith(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('addMemberToGroup', () => {
|
||||
beforeEach(() => {
|
||||
service.addMemberToGroup(GroupMock, EPersonMock2).subscribe();
|
||||
});
|
||||
it('should send PostRequest to eperson/groups/group-id/epersons endpoint with new eperson member in body', () => {
|
||||
let headers = new HttpHeaders();
|
||||
const options: HttpOptions = Object.create({});
|
||||
headers = headers.append('Content-Type', 'text/uri-list');
|
||||
options.headers = headers;
|
||||
const expected = new PostRequest(requestService.generateRequestId(), GroupMock.self + '/' + service.ePersonsEndpoint, EPersonMock2.self, options);
|
||||
expect(requestService.configure).toHaveBeenCalledWith(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('deleteMemberFromGroup', () => {
|
||||
beforeEach(() => {
|
||||
service.deleteMemberFromGroup(GroupMock, EPersonMock).subscribe();
|
||||
});
|
||||
it('should send DeleteRequest to eperson/groups/group-id/epersons/eperson-id endpoint', () => {
|
||||
const expected = new DeleteRequest(requestService.generateRequestId(), GroupMock.self + '/' + service.ePersonsEndpoint + '/' + EPersonMock.id);
|
||||
expect(requestService.configure).toHaveBeenCalledWith(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('editGroup', () => {
|
||||
it('should dispatch a EDIT_GROUP action with the groupp to start editing', () => {
|
||||
service.editGroup(GroupMock);
|
||||
expect(store.dispatch).toHaveBeenCalledWith(new GroupRegistryEditGroupAction(GroupMock));
|
||||
});
|
||||
});
|
||||
|
||||
describe('cancelEditGroup', () => {
|
||||
it('should dispatch a CANCEL_EDIT_GROUP action', () => {
|
||||
service.cancelEditGroup();
|
||||
expect(store.dispatch).toHaveBeenCalledWith(new GroupRegistryCancelGroupAction());
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
class DummyChangeAnalyzer implements ChangeAnalyzer<Item> {
|
||||
diff(object1: Item, object2: Item): Operation[] {
|
||||
return compare((object1 as any).metadata, (object2 as any).metadata);
|
||||
}
|
||||
}
|
@@ -45,8 +45,8 @@ const editGroupSelector = createSelector(groupRegistryStateSelector, (groupRegis
|
||||
export class GroupDataService extends DataService<Group> {
|
||||
protected linkPath = 'groups';
|
||||
protected browseEndpoint = '';
|
||||
protected ePersonsEndpoint = 'epersons';
|
||||
protected subgroupsEndpoint = 'subgroups';
|
||||
public ePersonsEndpoint = 'epersons';
|
||||
public subgroupsEndpoint = 'subgroups';
|
||||
|
||||
constructor(
|
||||
protected comparator: DSOChangeAnalyzer<Group>,
|
||||
@@ -130,7 +130,6 @@ export class GroupDataService extends DataService<Group> {
|
||||
/**
|
||||
* Create or Update a group
|
||||
* If the group contains an id, it is assumed the eperson already exists and is updated instead
|
||||
* //TODO
|
||||
* @param group The group to create or update
|
||||
*/
|
||||
public createOrUpdateGroup(group: Group): Observable<RemoteData<Group>> {
|
||||
|
@@ -1,14 +1,34 @@
|
||||
import { Group } from '../../core/eperson/models/group.model';
|
||||
import { EPersonMock } from './eperson-mock';
|
||||
|
||||
export const GroupMock2: Group = Object.assign(new Group(), {
|
||||
handle: null,
|
||||
groups: [],
|
||||
epersons: [],
|
||||
selfRegistered: false,
|
||||
_links: {
|
||||
self: {
|
||||
href: 'https://dspace.4science.it/dspace-spring-rest/api/eperson/groups/testgroupid2',
|
||||
},
|
||||
subgroups: { href: 'https://dspace.4science.it/dspace-spring-rest/api/eperson/groups/testgroupid2/subgroups' },
|
||||
epersons: { href: 'https://dspace.4science.it/dspace-spring-rest/api/eperson/groups/testgroupid2/epersons' }
|
||||
},
|
||||
id: 'testgroupid2',
|
||||
uuid: 'testgroupid2',
|
||||
type: 'group',
|
||||
});
|
||||
|
||||
export const GroupMock: Group = Object.assign(new Group(), {
|
||||
handle: null,
|
||||
groups: [],
|
||||
groups: [GroupMock2],
|
||||
epersons: [EPersonMock],
|
||||
selfRegistered: false,
|
||||
_links: {
|
||||
self: {
|
||||
href: 'https://dspace.4science.it/dspace-spring-rest/api/eperson/groups/testgroupid',
|
||||
},
|
||||
groups: { href: 'https://dspace.4science.it/dspace-spring-rest/api/eperson/groups/testgroupid/groups' }
|
||||
subgroups: { href: 'https://dspace.4science.it/dspace-spring-rest/api/eperson/groups/testgroupid/subgroups' },
|
||||
epersons: { href: 'https://dspace.4science.it/dspace-spring-rest/api/eperson/groups/testgroupid/epersons' }
|
||||
},
|
||||
id: 'testgroupid',
|
||||
uuid: 'testgroupid',
|
||||
|
Reference in New Issue
Block a user