MCPcopy Index your code
hub / github.com/angular/angular / guardsIntegrationSuite

Function guardsIntegrationSuite

packages/router/test/integration/guards.spec.ts:78–2492  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

76import {timeout} from '@angular/private/testing';
77
78export 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);

Callers 1

Calls 15

createRootFunction · 0.90
advanceFunction · 0.90
expectEventsFunction · 0.90
injectFunction · 0.90
provideRouterFunction · 0.90
withRouterConfigFunction · 0.90
timeoutFunction · 0.90
setTimeoutFunction · 0.85
beforeEachFunction · 0.85
configureRouterFunction · 0.85
delayPromiseFunction · 0.85
runSeriallyFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…