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()
|
||||
export class LookupGuard implements CanActivate {
|
||||
constructor(private dsoService: DsoDataRedirectService, private router: Router) {
|
||||
constructor(private dsoService: DsoDataRedirectService) {
|
||||
}
|
||||
|
||||
canActivate(route: ActivatedRouteSnapshot, state:RouterStateSnapshot): Observable<boolean> {
|
||||
|
@@ -19,7 +19,6 @@ describe('DsoDataRedirectService', () => {
|
||||
let halService: HALEndpointService;
|
||||
let requestService: RequestService;
|
||||
let rdbService: RemoteDataBuildService;
|
||||
let objectCache: ObjectCacheService;
|
||||
let router;
|
||||
let remoteData;
|
||||
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 requestUUIDURL = `https://rest.api/rest/api/pid/find?id=${dsoUUID}`;
|
||||
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(() => {
|
||||
scheduler = getTestScheduler();
|
||||
|
||||
@@ -40,14 +45,10 @@ describe('DsoDataRedirectService', () => {
|
||||
generateRequestId: requestUUID,
|
||||
configure: true
|
||||
});
|
||||
rdbService = jasmine.createSpyObj('rdbService', {
|
||||
buildSingle: cold('a', {
|
||||
a: remoteData
|
||||
})
|
||||
});
|
||||
router = {
|
||||
navigate: jasmine.createSpy('navigate')
|
||||
};
|
||||
|
||||
remoteData = {
|
||||
isSuccessful: true,
|
||||
error: undefined,
|
||||
@@ -58,13 +59,13 @@ describe('DsoDataRedirectService', () => {
|
||||
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(
|
||||
requestService,
|
||||
rdbService,
|
||||
@@ -77,10 +78,12 @@ describe('DsoDataRedirectService', () => {
|
||||
comparator,
|
||||
router
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
describe('findById', () => {
|
||||
it('should call HALEndpointService with the path to the pid endpoint', () => {
|
||||
setup();
|
||||
scheduler.schedule(() => service.findById(dsoUUID));
|
||||
scheduler.flush();
|
||||
|
||||
@@ -88,6 +91,7 @@ describe('DsoDataRedirectService', () => {
|
||||
});
|
||||
|
||||
it('should configure the proper FindByIDRequest for uuid', () => {
|
||||
setup();
|
||||
scheduler.schedule(() => service.findById(dsoUUID, IdentifierType.UUID));
|
||||
scheduler.flush();
|
||||
|
||||
@@ -95,13 +99,16 @@ describe('DsoDataRedirectService', () => {
|
||||
});
|
||||
|
||||
it('should configure the proper FindByIDRequest for handle', () => {
|
||||
setup();
|
||||
scheduler.schedule(() => service.findById(dsoHandle, IdentifierType.HANDLE));
|
||||
scheduler.flush();
|
||||
|
||||
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);
|
||||
// The framework would normally subscribe but do it here so we can test navigation.
|
||||
redir.subscribe();
|
||||
@@ -109,5 +116,27 @@ describe('DsoDataRedirectService', () => {
|
||||
scheduler.flush();
|
||||
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