()
| 76 | import {timeout} from '@angular/private/testing'; |
| 77 | |
| 78 | export function guardsIntegrationSuite() { |
| 79 | describe('guards', () => { |
| 80 | describe('CanActivate', () => { |
| 81 | describe('guard completes before emitting a value', () => { |
| 82 | @Injectable({providedIn: 'root'}) |
| 83 | class CompletesBeforeEmitting { |
| 84 | private subject$ = new Subject<boolean>(); |
| 85 | |
| 86 | constructor(destroyRef: DestroyRef) { |
| 87 | destroyRef.onDestroy(() => this.subject$.complete()); |
| 88 | } |
| 89 | |
| 90 | // Note that this is a simple illustrative case of when an observable |
| 91 | // completes without emitting a value. In a real-world scenario, this |
| 92 | // might represent an HTTP request that never emits before the app is |
| 93 | // destroyed and then completes when the app is destroyed. |
| 94 | canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { |
| 95 | return this.subject$; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | it('should not thrown an unhandled promise rejection', async () => { |
| 100 | const router = TestBed.inject(Router); |
| 101 | const fixture = await createRoot(router, RootCmp); |
| 102 | |
| 103 | const onUnhandledrejection = jasmine.createSpy(); |
| 104 | window.addEventListener('unhandledrejection', onUnhandledrejection); |
| 105 | |
| 106 | router.resetConfig([ |
| 107 | {path: 'team/:id', component: TeamCmp, canActivate: [CompletesBeforeEmitting]}, |
| 108 | ]); |
| 109 | |
| 110 | router.navigateByUrl('/team/22'); |
| 111 | |
| 112 | // This was previously throwing an error `NG0205: Injector has already been destroyed`. |
| 113 | fixture.destroy(); |
| 114 | |
| 115 | // Wait until the event task is dispatched. |
| 116 | await new Promise((resolve) => setTimeout(resolve, 10)); |
| 117 | window.removeEventListener('unhandledrejection', onUnhandledrejection); |
| 118 | |
| 119 | expect(onUnhandledrejection).not.toHaveBeenCalled(); |
| 120 | }); |
| 121 | }); |
| 122 | |
| 123 | describe('should not activate a route when CanActivate returns false', () => { |
| 124 | it('works', async () => { |
| 125 | const router = TestBed.inject(Router); |
| 126 | const location = TestBed.inject(Location); |
| 127 | const fixture = await createRoot(router, RootCmp); |
| 128 | |
| 129 | const recordedEvents: Event[] = []; |
| 130 | router.events.forEach((e) => recordedEvents.push(e)); |
| 131 | |
| 132 | router.resetConfig([{path: 'team/:id', component: TeamCmp, canActivate: [() => false]}]); |
| 133 | |
| 134 | router.navigateByUrl('/team/22'); |
| 135 | await advance(fixture); |
no test coverage detected
searching dependent graphs…