MCPcopy Create free account
hub / github.com/angular/angular / guardsIntegrationSuite

Function guardsIntegrationSuite

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

Source from the content-addressed store, hash-verified

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

Callers 1

Calls 15

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

Tested by

no test coverage detected