mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 10:04:11 +00:00
[DSC-287] Add test to guard
This commit is contained in:
@@ -1,16 +1,55 @@
|
|||||||
import { TestBed } from '@angular/core/testing';
|
|
||||||
|
|
||||||
import { ServerCheckGuard } from './server-check.guard';
|
import { ServerCheckGuard } from './server-check.guard';
|
||||||
|
import { createFailedRemoteDataObject$, createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils';
|
||||||
|
import { first } from 'rxjs/operators';
|
||||||
|
import { getPageInternalServerErrorRoute } from '../../app-routing-paths';
|
||||||
|
|
||||||
describe('ServerCheckGuard', () => {
|
describe('ServerCheckGuard', () => {
|
||||||
let guard: ServerCheckGuard;
|
let guard: ServerCheckGuard;
|
||||||
|
let router;
|
||||||
|
let rootDataServiceStub: any;
|
||||||
|
|
||||||
|
rootDataServiceStub = jasmine.createSpyObj('RootDataService', {
|
||||||
|
findRoot: jasmine.createSpy('findRoot')
|
||||||
|
});
|
||||||
|
router = jasmine.createSpyObj('Router', {
|
||||||
|
navigateByUrl: jasmine.createSpy('navigateByUrl')
|
||||||
|
});
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
TestBed.configureTestingModule({});
|
guard = new ServerCheckGuard(router, rootDataServiceStub);
|
||||||
guard = TestBed.inject(ServerCheckGuard);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be created', () => {
|
it('should be created', () => {
|
||||||
expect(guard).toBeTruthy();
|
expect(guard).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('when root endpoint has succeeded', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
rootDataServiceStub.findRoot.and.returnValue(createSuccessfulRemoteDataObject$({}));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not redirect to error page', () => {
|
||||||
|
guard.canActivate({} as any, {} as any).pipe(
|
||||||
|
first()
|
||||||
|
).subscribe((canActivate: boolean) => {
|
||||||
|
expect(canActivate).toEqual(true);
|
||||||
|
expect(router.navigateByUrl).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when root endpoint has not succeeded', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
rootDataServiceStub.findRoot.and.returnValue(createFailedRemoteDataObject$());
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should redirect to error page', () => {
|
||||||
|
guard.canActivate({} as any, {} as any).pipe(
|
||||||
|
first()
|
||||||
|
).subscribe((canActivate: boolean) => {
|
||||||
|
expect(canActivate).toEqual(false);
|
||||||
|
expect(router.navigateByUrl).toHaveBeenCalledWith(getPageInternalServerErrorRoute());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@@ -1,26 +1,30 @@
|
|||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router';
|
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
|
||||||
|
|
||||||
import { Observable } from 'rxjs';
|
import { Observable } from 'rxjs';
|
||||||
import { RootDataService } from '../data/root-data.service';
|
|
||||||
import { map, tap } from 'rxjs/operators';
|
import { map, tap } from 'rxjs/operators';
|
||||||
|
|
||||||
|
import { RootDataService } from '../data/root-data.service';
|
||||||
import { RemoteData } from '../data/remote-data';
|
import { RemoteData } from '../data/remote-data';
|
||||||
import { getPageInternalServerErrorRoute } from '../../app-routing-paths';
|
import { getPageInternalServerErrorRoute } from '../../app-routing-paths';
|
||||||
import { Router } from '@angular/router';
|
import { getFirstCompletedRemoteData } from '../shared/operators';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
})
|
})
|
||||||
export class ServerCheckGuard implements CanActivate {
|
export class ServerCheckGuard implements CanActivate {
|
||||||
constructor(private router: Router,private rootDataService: RootDataService) {}
|
constructor(private router: Router, private rootDataService: RootDataService) {
|
||||||
|
}
|
||||||
|
|
||||||
canActivate(
|
canActivate(
|
||||||
route: ActivatedRouteSnapshot,
|
route: ActivatedRouteSnapshot,
|
||||||
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
state: RouterStateSnapshot): Observable<boolean> {
|
||||||
|
|
||||||
return this.rootDataService.findRoot().pipe(
|
return this.rootDataService.findRoot().pipe(
|
||||||
map( (res: RemoteData<any>) => res.hasSucceeded ),
|
getFirstCompletedRemoteData(),
|
||||||
tap( (responsehasSucceeded: boolean) => {
|
map((res: RemoteData<any>) => res.hasSucceeded),
|
||||||
if (!responsehasSucceeded) {
|
tap((hasSucceeded: boolean) => {
|
||||||
|
if (!hasSucceeded) {
|
||||||
this.router.navigateByUrl(getPageInternalServerErrorRoute());
|
this.router.navigateByUrl(getPageInternalServerErrorRoute());
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
Reference in New Issue
Block a user