From b8c26a71c33484aa692f72e28bc6dffce331f56e Mon Sep 17 00:00:00 2001 From: Giuseppe Digilio Date: Tue, 2 Apr 2024 18:45:57 +0200 Subject: [PATCH] [DURACOM-234] Fixes after migrating to functional guards --- .../edit-item-page.component.spec.ts | 54 ++++++++----------- .../edit-item-page.component.ts | 15 ++---- 2 files changed, 28 insertions(+), 41 deletions(-) diff --git a/src/app/item-page/edit-item-page/edit-item-page.component.spec.ts b/src/app/item-page/edit-item-page/edit-item-page.component.spec.ts index 9746c654d7..da92f53c1d 100644 --- a/src/app/item-page/edit-item-page/edit-item-page.component.spec.ts +++ b/src/app/item-page/edit-item-page/edit-item-page.component.spec.ts @@ -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; - class AcceptAllGuard { - canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | Promise | boolean | UrlTree { - return observableOf(true); - } - } + const AcceptAllGuard: CanActivateFn = ( + route: ActivatedRouteSnapshot, + state: RouterStateSnapshot, + ): Observable => { + return observableOf(true); + }; - class AcceptNoneGuard { - canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | Promise | boolean | UrlTree { - return observableOf(false); - } - } + const AcceptNoneGuard: CanActivateFn = ( + route: ActivatedRouteSnapshot, + state: RouterStateSnapshot, + ): Observable => { + 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); }); }); }); diff --git a/src/app/item-page/edit-item-page/edit-item-page.component.ts b/src/app/item-page/edit-item-page/edit-item-page.component.ts index ee71a52a4e..cd3e1ff215 100644 --- a/src/app/item-page/edit-item-page/edit-item-page.component.ts +++ b/src/app/item-page/edit-item-page/edit-item-page.component.ts @@ -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)),