mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 01:54:15 +00:00
[DURACOM-234] Fixes after migrating to functional guards
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
/* eslint-disable max-classes-per-file */
|
||||
import {
|
||||
ChangeDetectionStrategy,
|
||||
NO_ERRORS_SCHEMA,
|
||||
@@ -13,9 +12,9 @@ import { By } from '@angular/platform-browser';
|
||||
import {
|
||||
ActivatedRoute,
|
||||
ActivatedRouteSnapshot,
|
||||
CanActivateFn,
|
||||
RouterModule,
|
||||
RouterStateSnapshot,
|
||||
UrlTree,
|
||||
} from '@angular/router';
|
||||
import {
|
||||
TranslateLoader,
|
||||
@@ -31,29 +30,31 @@ import { TranslateLoaderMock } from '../../shared/mocks/translate-loader.mock';
|
||||
import { createSuccessfulRemoteDataObject } from '../../shared/remote-data.utils';
|
||||
import { EditItemPageComponent } from './edit-item-page.component';
|
||||
|
||||
describe('ItemPageComponent', () => {
|
||||
describe('EditItemPageComponent', () => {
|
||||
let comp: EditItemPageComponent;
|
||||
let fixture: ComponentFixture<EditItemPageComponent>;
|
||||
|
||||
class AcceptAllGuard {
|
||||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
||||
const AcceptAllGuard: CanActivateFn = (
|
||||
route: ActivatedRouteSnapshot,
|
||||
state: RouterStateSnapshot,
|
||||
): Observable<boolean> => {
|
||||
return observableOf(true);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class AcceptNoneGuard {
|
||||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
|
||||
const AcceptNoneGuard: CanActivateFn = (
|
||||
route: ActivatedRouteSnapshot,
|
||||
state: RouterStateSnapshot,
|
||||
): Observable<boolean> => {
|
||||
return observableOf(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const accesiblePages = ['accessible'];
|
||||
const inaccesiblePages = ['inaccessible', 'inaccessibleDoubleGuard'];
|
||||
const accessiblePages = ['accessible'];
|
||||
const inaccessiblePages = ['inaccessible', 'inaccessibleDoubleGuard'];
|
||||
const mockRoute = {
|
||||
snapshot: {
|
||||
firstChild: {
|
||||
routeConfig: {
|
||||
path: accesiblePages[0],
|
||||
path: accessiblePages[0],
|
||||
},
|
||||
},
|
||||
routerState: {
|
||||
@@ -63,13 +64,13 @@ describe('ItemPageComponent', () => {
|
||||
routeConfig: {
|
||||
children: [
|
||||
{
|
||||
path: accesiblePages[0],
|
||||
path: accessiblePages[0],
|
||||
canActivate: [AcceptAllGuard],
|
||||
}, {
|
||||
path: inaccesiblePages[0],
|
||||
path: inaccessiblePages[0],
|
||||
canActivate: [AcceptNoneGuard],
|
||||
}, {
|
||||
path: inaccesiblePages[1],
|
||||
path: inaccessiblePages[1],
|
||||
canActivate: [AcceptAllGuard, AcceptNoneGuard],
|
||||
},
|
||||
],
|
||||
@@ -77,13 +78,6 @@ describe('ItemPageComponent', () => {
|
||||
data: observableOf({ dso: createSuccessfulRemoteDataObject(new Item()) }),
|
||||
};
|
||||
|
||||
const mockRouter = {
|
||||
routerState: {
|
||||
snapshot: undefined,
|
||||
},
|
||||
events: observableOf(undefined),
|
||||
};
|
||||
|
||||
beforeEach(waitForAsync(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [
|
||||
@@ -98,8 +92,6 @@ describe('ItemPageComponent', () => {
|
||||
],
|
||||
providers: [
|
||||
{ provide: ActivatedRoute, useValue: mockRoute },
|
||||
AcceptAllGuard,
|
||||
AcceptNoneGuard,
|
||||
],
|
||||
schemas: [NO_ERRORS_SCHEMA],
|
||||
}).overrideComponent(EditItemPageComponent, {
|
||||
@@ -110,19 +102,19 @@ describe('ItemPageComponent', () => {
|
||||
beforeEach(waitForAsync(() => {
|
||||
fixture = TestBed.createComponent(EditItemPageComponent);
|
||||
comp = fixture.componentInstance;
|
||||
spyOn((comp as any).injector, 'get').and.callFake((a) => new a());
|
||||
// spyOn((comp as any).injector, 'get').and.callFake((a) => new a());
|
||||
fixture.detectChanges();
|
||||
}));
|
||||
|
||||
describe('ngOnInit', () => {
|
||||
it('should enable tabs that the user can activate', fakeAsync(() => {
|
||||
const enabledItems = fixture.debugElement.queryAll(By.css('a.nav-link'));
|
||||
expect(enabledItems.length).toBe(accesiblePages.length);
|
||||
expect(enabledItems.length).toBe(accessiblePages.length);
|
||||
}));
|
||||
|
||||
it('should disable tabs that the user can not activate', () => {
|
||||
const disabledItems = fixture.debugElement.queryAll(By.css('button.nav-link.disabled'));
|
||||
expect(disabledItems.length).toBe(inaccesiblePages.length);
|
||||
expect(disabledItems.length).toBe(inaccessiblePages.length);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@@ -9,6 +9,7 @@ import {
|
||||
Component,
|
||||
Injector,
|
||||
OnInit,
|
||||
runInInjectionContext,
|
||||
} from '@angular/core';
|
||||
import {
|
||||
ActivatedRoute,
|
||||
@@ -28,7 +29,6 @@ import {
|
||||
import { map } from 'rxjs/operators';
|
||||
|
||||
import { RemoteData } from '../../core/data/remote-data';
|
||||
import { GenericConstructor } from '../../core/shared/generic-constructor';
|
||||
import { Item } from '../../core/shared/item.model';
|
||||
import {
|
||||
fadeIn,
|
||||
@@ -88,15 +88,10 @@ export class EditItemPageComponent implements OnInit {
|
||||
.map((child: Route) => {
|
||||
let enabled = observableOf(true);
|
||||
if (isNotEmpty(child.canActivate)) {
|
||||
enabled = observableCombineLatest(child.canActivate.map((guardConstructor: GenericConstructor<{
|
||||
canActivate: CanActivateFn;
|
||||
}>) => {
|
||||
const guard: {
|
||||
canActivate: CanActivateFn;
|
||||
} = this.injector.get<{
|
||||
canActivate: CanActivateFn;
|
||||
}>(guardConstructor);
|
||||
return guard.canActivate(this.route.snapshot, this.router.routerState.snapshot);
|
||||
enabled = observableCombineLatest(child.canActivate.map((guardFn: CanActivateFn) => {
|
||||
return runInInjectionContext(this.injector, () => {
|
||||
return guardFn(this.route.snapshot, this.router.routerState.snapshot);
|
||||
});
|
||||
}),
|
||||
).pipe(
|
||||
map((canActivateOutcomes: any[]) => canActivateOutcomes.every((e) => e === true)),
|
||||
|
Reference in New Issue
Block a user