mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 18:14:17 +00:00
Added unit tests.
This commit is contained in:
50
src/app/+lookup-by-id/lookup-guard.spec.ts
Normal file
50
src/app/+lookup-by-id/lookup-guard.spec.ts
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import {LookupGuard} from "./lookup-guard";
|
||||||
|
import {of as observableOf} from "rxjs";
|
||||||
|
import {IdentifierType} from "../core/index/index.reducer";
|
||||||
|
|
||||||
|
describe('LookupGuard', () => {
|
||||||
|
let dsoService: any;
|
||||||
|
let guard: any;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
dsoService = {
|
||||||
|
findById: jasmine.createSpy('findById').and.returnValue(observableOf({ hasFailed: false,
|
||||||
|
hasSucceeded: true }))
|
||||||
|
};
|
||||||
|
guard = new LookupGuard(dsoService);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should call findById with handle params', () => {
|
||||||
|
const scopedRoute = {
|
||||||
|
params: {
|
||||||
|
id: '1234',
|
||||||
|
idType: '123456789'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
guard.canActivate(scopedRoute as any, undefined);
|
||||||
|
expect(dsoService.findById).toHaveBeenCalledWith('123456789%2F1234', IdentifierType.HANDLE)
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should call findById with handle params', () => {
|
||||||
|
const scopedRoute = {
|
||||||
|
params: {
|
||||||
|
id: '123456789%2F1234',
|
||||||
|
idType: 'handle'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
guard.canActivate(scopedRoute as any, undefined);
|
||||||
|
expect(dsoService.findById).toHaveBeenCalledWith('123456789%2F1234', IdentifierType.HANDLE)
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should call findById with UUID params', () => {
|
||||||
|
const scopedRoute = {
|
||||||
|
params: {
|
||||||
|
id: '34cfed7c-f597-49ef-9cbe-ea351f0023c2',
|
||||||
|
idType: 'uuid'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
guard.canActivate(scopedRoute as any, undefined);
|
||||||
|
expect(dsoService.findById).toHaveBeenCalledWith('34cfed7c-f597-49ef-9cbe-ea351f0023c2', IdentifierType.UUID)
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
@@ -14,7 +14,7 @@ interface LookupParams {
|
|||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class LookupGuard implements CanActivate {
|
export class LookupGuard implements CanActivate {
|
||||||
constructor(private dsoService: DsoDataRedirectService, private router: Router) {
|
constructor(private dsoService: DsoDataRedirectService) {
|
||||||
}
|
}
|
||||||
|
|
||||||
canActivate(route: ActivatedRouteSnapshot, state:RouterStateSnapshot): Observable<boolean> {
|
canActivate(route: ActivatedRouteSnapshot, state:RouterStateSnapshot): Observable<boolean> {
|
||||||
|
@@ -19,7 +19,6 @@ describe('DsoDataRedirectService', () => {
|
|||||||
let halService: HALEndpointService;
|
let halService: HALEndpointService;
|
||||||
let requestService: RequestService;
|
let requestService: RequestService;
|
||||||
let rdbService: RemoteDataBuildService;
|
let rdbService: RemoteDataBuildService;
|
||||||
let objectCache: ObjectCacheService;
|
|
||||||
let router;
|
let router;
|
||||||
let remoteData;
|
let remoteData;
|
||||||
const dsoUUID = '9b4f22f4-164a-49db-8817-3316b6ee5746';
|
const dsoUUID = '9b4f22f4-164a-49db-8817-3316b6ee5746';
|
||||||
@@ -29,7 +28,13 @@ describe('DsoDataRedirectService', () => {
|
|||||||
const requestHandleURL = `https://rest.api/rest/api/pid/find?id=${encodedHandle}`;
|
const requestHandleURL = `https://rest.api/rest/api/pid/find?id=${encodedHandle}`;
|
||||||
const requestUUIDURL = `https://rest.api/rest/api/pid/find?id=${dsoUUID}`;
|
const requestUUIDURL = `https://rest.api/rest/api/pid/find?id=${dsoUUID}`;
|
||||||
const requestUUID = '34cfed7c-f597-49ef-9cbe-ea351f0023c2';
|
const requestUUID = '34cfed7c-f597-49ef-9cbe-ea351f0023c2';
|
||||||
|
const store = {} as Store<CoreState>;
|
||||||
|
const notificationsService = {} as NotificationsService;
|
||||||
|
const http = {} as HttpClient;
|
||||||
|
const comparator = {} as any;
|
||||||
|
const dataBuildService = {} as NormalizedObjectBuildService;
|
||||||
|
const objectCache = {} as ObjectCacheService;
|
||||||
|
let setup;
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
scheduler = getTestScheduler();
|
scheduler = getTestScheduler();
|
||||||
|
|
||||||
@@ -40,14 +45,10 @@ describe('DsoDataRedirectService', () => {
|
|||||||
generateRequestId: requestUUID,
|
generateRequestId: requestUUID,
|
||||||
configure: true
|
configure: true
|
||||||
});
|
});
|
||||||
rdbService = jasmine.createSpyObj('rdbService', {
|
|
||||||
buildSingle: cold('a', {
|
|
||||||
a: remoteData
|
|
||||||
})
|
|
||||||
});
|
|
||||||
router = {
|
router = {
|
||||||
navigate: jasmine.createSpy('navigate')
|
navigate: jasmine.createSpy('navigate')
|
||||||
};
|
};
|
||||||
|
|
||||||
remoteData = {
|
remoteData = {
|
||||||
isSuccessful: true,
|
isSuccessful: true,
|
||||||
error: undefined,
|
error: undefined,
|
||||||
@@ -58,13 +59,13 @@ describe('DsoDataRedirectService', () => {
|
|||||||
uuid: '123456789'
|
uuid: '123456789'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
objectCache = {} as ObjectCacheService;
|
|
||||||
const store = {} as Store<CoreState>;
|
|
||||||
const notificationsService = {} as NotificationsService;
|
|
||||||
const http = {} as HttpClient;
|
|
||||||
const comparator = {} as any;
|
|
||||||
const dataBuildService = {} as NormalizedObjectBuildService;
|
|
||||||
|
|
||||||
|
setup = () => {
|
||||||
|
rdbService = jasmine.createSpyObj('rdbService', {
|
||||||
|
buildSingle: cold('a', {
|
||||||
|
a: remoteData
|
||||||
|
})
|
||||||
|
});
|
||||||
service = new DsoDataRedirectService(
|
service = new DsoDataRedirectService(
|
||||||
requestService,
|
requestService,
|
||||||
rdbService,
|
rdbService,
|
||||||
@@ -77,10 +78,12 @@ describe('DsoDataRedirectService', () => {
|
|||||||
comparator,
|
comparator,
|
||||||
router
|
router
|
||||||
);
|
);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('findById', () => {
|
describe('findById', () => {
|
||||||
it('should call HALEndpointService with the path to the pid endpoint', () => {
|
it('should call HALEndpointService with the path to the pid endpoint', () => {
|
||||||
|
setup();
|
||||||
scheduler.schedule(() => service.findById(dsoUUID));
|
scheduler.schedule(() => service.findById(dsoUUID));
|
||||||
scheduler.flush();
|
scheduler.flush();
|
||||||
|
|
||||||
@@ -88,6 +91,7 @@ describe('DsoDataRedirectService', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should configure the proper FindByIDRequest for uuid', () => {
|
it('should configure the proper FindByIDRequest for uuid', () => {
|
||||||
|
setup();
|
||||||
scheduler.schedule(() => service.findById(dsoUUID, IdentifierType.UUID));
|
scheduler.schedule(() => service.findById(dsoUUID, IdentifierType.UUID));
|
||||||
scheduler.flush();
|
scheduler.flush();
|
||||||
|
|
||||||
@@ -95,13 +99,16 @@ describe('DsoDataRedirectService', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should configure the proper FindByIDRequest for handle', () => {
|
it('should configure the proper FindByIDRequest for handle', () => {
|
||||||
|
setup();
|
||||||
scheduler.schedule(() => service.findById(dsoHandle, IdentifierType.HANDLE));
|
scheduler.schedule(() => service.findById(dsoHandle, IdentifierType.HANDLE));
|
||||||
scheduler.flush();
|
scheduler.flush();
|
||||||
|
|
||||||
expect(requestService.configure).toHaveBeenCalledWith(new FindByIDRequest(requestUUID, requestHandleURL, dsoHandle, IdentifierType.HANDLE), false);
|
expect(requestService.configure).toHaveBeenCalledWith(new FindByIDRequest(requestUUID, requestHandleURL, dsoHandle, IdentifierType.HANDLE), false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should navigate to dso route', () => {
|
it('should navigate to item route', () => {
|
||||||
|
remoteData.payload.type = 'item';
|
||||||
|
setup();
|
||||||
const redir = service.findById(dsoHandle, IdentifierType.HANDLE);
|
const redir = service.findById(dsoHandle, IdentifierType.HANDLE);
|
||||||
// The framework would normally subscribe but do it here so we can test navigation.
|
// The framework would normally subscribe but do it here so we can test navigation.
|
||||||
redir.subscribe();
|
redir.subscribe();
|
||||||
@@ -109,5 +116,27 @@ describe('DsoDataRedirectService', () => {
|
|||||||
scheduler.flush();
|
scheduler.flush();
|
||||||
expect(router.navigate).toHaveBeenCalledWith([remoteData.payload.type + 's/' + remoteData.payload.uuid]);
|
expect(router.navigate).toHaveBeenCalledWith([remoteData.payload.type + 's/' + remoteData.payload.uuid]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should navigate to collections route', () => {
|
||||||
|
remoteData.payload.type = 'collection';
|
||||||
|
setup();
|
||||||
|
const redir = service.findById(dsoHandle, IdentifierType.HANDLE);
|
||||||
|
redir.subscribe();
|
||||||
|
scheduler.schedule(() => redir);
|
||||||
|
scheduler.flush();
|
||||||
|
expect(router.navigate).toHaveBeenCalledWith([remoteData.payload.type + 's/' + remoteData.payload.uuid]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should navigate to communities route', () => {
|
||||||
|
remoteData.payload.type = 'community';
|
||||||
|
setup();
|
||||||
|
const redir = service.findById(dsoHandle, IdentifierType.HANDLE);
|
||||||
|
redir.subscribe();
|
||||||
|
scheduler.schedule(() => redir);
|
||||||
|
scheduler.flush();
|
||||||
|
expect(router.navigate).toHaveBeenCalledWith(['communities/' + remoteData.payload.uuid]);
|
||||||
|
});
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user