Skip to content

Commit 0142ae1

Browse files
atscottthePunderWoman
authored andcommitted
refactor(router): Update tests to not use deprecated string tokens (angular#60378)
This replaces string tokens in the router tests with functional guads. PR Close angular#60378
1 parent c57951d commit 0142ae1

13 files changed

+418
-561
lines changed

packages/router/test/apply_redirects.spec.ts

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {EnvironmentInjector, inject, Injectable, NgModuleRef} from '@angular/core';
9+
import {EnvironmentInjector, inject, Injectable} from '@angular/core';
1010
import {fakeAsync, TestBed, tick} from '@angular/core/testing';
11-
import {provideRouter, withRouterConfig} from '@angular/router';
12-
import {firstValueFrom, Observable, of} from 'rxjs';
13-
import {delay, map, tap} from 'rxjs/operators';
11+
import {Observable, of} from 'rxjs';
12+
import {delay, tap} from 'rxjs/operators';
1413

1514
import {Route, Routes} from '../src/models';
1615
import {recognize} from '../src/recognize';
@@ -584,24 +583,28 @@ describe('redirects', () => {
584583

585584
let passedUrlSegments: UrlSegment[];
586585

587-
const guard = {
588-
canLoad: (route: Route, urlSegments: UrlSegment[]) => {
589-
passedUrlSegments = urlSegments;
590-
return true;
591-
},
592-
};
593-
const injector = {get: (token: any) => (token === 'guard' ? guard : {injector})};
594-
595586
const config = [
596587
{
597588
path: 'a',
598589
component: ComponentA,
599-
canLoad: ['guard'],
590+
canLoad: [
591+
(route: Route, urlSegments: UrlSegment[]) => {
592+
passedUrlSegments = urlSegments;
593+
return true;
594+
},
595+
],
600596
loadChildren: jasmine.createSpy('children'),
601597
},
602598
];
603599

604-
recognize(<any>injector, <any>loader, null, config, tree('a/b'), serializer).subscribe(
600+
recognize(
601+
TestBed.inject(EnvironmentInjector),
602+
<any>loader,
603+
null,
604+
config,
605+
tree('a/b'),
606+
serializer,
607+
).subscribe(
605608
({tree: r}) => {
606609
expectTreeToBe(r, '/a/b');
607610
expect(passedUrlSegments.length).toBe(2);
@@ -710,7 +713,7 @@ describe('redirects', () => {
710713
});
711714
});
712715

713-
it('should not load the configuration of a wildcard route if there is a match', () => {
716+
it('should not load the configuration of a wildcard route if there is a match', async () => {
714717
const loadedConfig = {
715718
routes: [{path: '', component: ComponentB}],
716719
injector: TestBed.inject(EnvironmentInjector),
@@ -727,20 +730,23 @@ describe('redirects', () => {
727730
{path: '**', loadChildren: jasmine.createSpy('children')},
728731
];
729732

730-
recognize(
731-
TestBed.inject(EnvironmentInjector),
732-
<any>loader,
733-
null,
734-
config,
735-
tree(''),
736-
serializer,
737-
).forEach(({tree: r}) => {
738-
expect(loader.loadChildren.calls.count()).toEqual(1);
739-
expect(loader.loadChildren.calls.first().args).not.toContain(
740-
jasmine.objectContaining({
741-
loadChildren: jasmine.createSpy('children'),
742-
}),
743-
);
733+
await new Promise<void>((resolve) => {
734+
recognize(
735+
TestBed.inject(EnvironmentInjector),
736+
<any>loader,
737+
null,
738+
config,
739+
tree(''),
740+
serializer,
741+
).forEach(({tree: r}) => {
742+
expect(loader.loadChildren.calls.count()).toEqual(1);
743+
expect(loader.loadChildren.calls.first().args).not.toContain(
744+
jasmine.objectContaining({
745+
loadChildren: jasmine.createSpy('children'),
746+
}),
747+
);
748+
resolve();
749+
});
744750
});
745751
});
746752

packages/router/test/computed_state_restoration.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ describe('`restoredState#ɵrouterPageId`', () => {
7070
TestBed.configureTestingModule({
7171
imports: [TestModule],
7272
providers: [
73-
{provide: 'alwaysFalse', useValue: (a: any) => false},
7473
{provide: Location, useClass: SpyLocation},
7574
provideRouter(
7675
[
@@ -106,7 +105,7 @@ describe('`restoredState#ɵrouterPageId`', () => {
106105
{
107106
path: 'loaded',
108107
loadChildren: () => of(ModuleWithSimpleCmpAsRoute),
109-
canLoad: ['alwaysFalse'],
108+
canLoad: [() => false],
110109
},
111110
],
112111
withRouterConfig({

packages/router/test/helpers.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@ export class Logger {
2323
}
2424
}
2525

26-
export function provideTokenLogger(token: string, returnValue = true as boolean | UrlTree) {
27-
return {
28-
provide: token,
29-
useFactory: (logger: Logger) => () => (logger.add(token), returnValue),
30-
deps: [Logger],
31-
};
32-
}
33-
3426
export declare type ARSArgs = {
3527
url?: UrlSegment[];
3628
params?: Params;

packages/router/test/integration/duplicate_in_flight_navigations.spec.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,7 @@ export function duplicateInFlightNavigationsIntegrationSuite() {
3434

3535
beforeEach(() => {
3636
TestBed.configureTestingModule({
37-
providers: [
38-
{
39-
provide: 'in1Second',
40-
useValue: (c: any, a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => {
41-
let res: any = null;
42-
const p = new Promise((_) => (res = _));
43-
setTimeout(() => res(true), 1000);
44-
return p;
45-
},
46-
},
47-
RedirectingGuard,
48-
],
37+
providers: [RedirectingGuard],
4938
});
5039
});
5140

@@ -54,7 +43,13 @@ export function duplicateInFlightNavigationsIntegrationSuite() {
5443
const location = TestBed.inject(Location);
5544
const fixture = createRoot(router, RootCmp);
5645

57-
router.resetConfig([{path: 'simple', component: SimpleCmp, canActivate: ['in1Second']}]);
46+
router.resetConfig([
47+
{
48+
path: 'simple',
49+
component: SimpleCmp,
50+
canActivate: [() => new Promise((resolve) => setTimeout(resolve, 1000))],
51+
},
52+
]);
5853

5954
// Trigger two location changes to the same URL.
6055
// Because of the guard the order will look as follows:
@@ -162,8 +157,16 @@ export function duplicateInFlightNavigationsIntegrationSuite() {
162157
it('should accurately track currentNavigation', fakeAsync(() => {
163158
const router = TestBed.inject(Router);
164159
router.resetConfig([
165-
{path: 'one', component: SimpleCmp, canActivate: ['in1Second']},
166-
{path: 'two', component: BlankCmp, canActivate: ['in1Second']},
160+
{
161+
path: 'one',
162+
component: SimpleCmp,
163+
canActivate: [() => new Promise((resolve) => setTimeout(resolve, 1000))],
164+
},
165+
{
166+
path: 'two',
167+
component: BlankCmp,
168+
canActivate: [() => new Promise((resolve) => setTimeout(resolve, 1000))],
169+
},
167170
]);
168171

169172
router.events.subscribe((e) => {

packages/router/test/integration/eager_url_update_strategy.spec.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,6 @@ export function eagerUrlUpdateStrategyIntegrationSuite() {
5353
const serializer = new DefaultUrlSerializer();
5454
TestBed.configureTestingModule({
5555
providers: [
56-
{
57-
provide: 'authGuardFail',
58-
useValue: (a: any, b: any) => {
59-
return new Promise((res) => {
60-
setTimeout(() => res(serializer.parse('/login')), 1);
61-
});
62-
},
63-
},
6456
AuthGuard,
6557
DelayedGuard,
6658
provideRouter([], withRouterConfig({urlUpdateStrategy: 'eager'})),
@@ -102,7 +94,16 @@ export function eagerUrlUpdateStrategyIntegrationSuite() {
10294
advance(fixture);
10395

10496
router.resetConfig([
105-
{path: 'team/:id', component: SimpleCmp, canActivate: ['authGuardFail']},
97+
{
98+
path: 'team/:id',
99+
component: SimpleCmp,
100+
canActivate: [
101+
() =>
102+
new Promise((res) => {
103+
setTimeout(() => res(new DefaultUrlSerializer().parse('/login')), 1);
104+
}),
105+
],
106+
},
106107
{path: 'login', component: AbsoluteSimpleLinkCmp},
107108
]);
108109

0 commit comments

Comments
 (0)