Skip to content

Commit ca0f057

Browse files
atscottalxhub
authored andcommitted
test(router): refactor tests to not use deprecated loadChildren (#43578)
Many of the tests in the router code use the deprecated loadChildren as a string. This has been deprecated for years and can easily be changed to just a function that returns the module. PR Close #43578
1 parent e3e424e commit ca0f057

5 files changed

Lines changed: 827 additions & 908 deletions

File tree

‎aio/content/examples/testing/src/app/app.component.router.spec.ts‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,10 @@ describe('AppComponent & RouterTestingModule', () => {
8181

8282

8383
///////////////
84-
import { NgModuleFactoryLoader } from '@angular/core';
85-
import { SpyNgModuleFactoryLoader } from '@angular/router/testing';
8684

8785
import { HeroModule } from './hero/hero.module'; // should be lazy loaded
8886
import { HeroListComponent } from './hero/hero-list.component';
8987

90-
let loader: SpyNgModuleFactoryLoader;
91-
9288
///////// Can't get lazy loaded Heroes to work yet
9389
xdescribe('AppComponent & Lazy Loading (not working yet)', () => {
9490
beforeEach(waitForAsync(() => {
@@ -104,9 +100,7 @@ xdescribe('AppComponent & Lazy Loading (not working yet)', () => {
104100

105101
beforeEach(fakeAsync(() => {
106102
createComponent();
107-
loader = TestBed.inject(NgModuleFactoryLoader) as SpyNgModuleFactoryLoader;
108-
loader.stubbedModules = {expected: HeroModule};
109-
router.resetConfig([{path: 'heroes', loadChildren: 'expected'}]);
103+
router.resetConfig([{path: 'heroes', loadChildren: () => HeroModule}]);
110104
}));
111105

112106
it('should navigate to "Heroes" on click', waitForAsync(() => {

‎packages/router/test/apply_redirects.spec.ts‎

Lines changed: 74 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ describe('applyRedirects', () => {
199199
return of(loadedConfig);
200200
}
201201
};
202-
const config: Routes = [{path: 'a', component: ComponentA, loadChildren: 'children'}];
202+
const config: Routes =
203+
[{path: 'a', component: ComponentA, loadChildren: jasmine.createSpy('children')}];
203204

204205
applyRedirects(testModule.injector, <any>loader, serializer, tree('a/b'), config)
205206
.forEach(r => {
@@ -212,7 +213,8 @@ describe('applyRedirects', () => {
212213
const loader = {
213214
load: (p: any) => new Observable<any>((obs: any) => obs.error(new Error('Loading Error')))
214215
};
215-
const config = [{path: 'a', component: ComponentA, loadChildren: 'children'}];
216+
const config =
217+
[{path: 'a', component: ComponentA, loadChildren: jasmine.createSpy('children')}];
216218

217219
applyRedirects(testModule.injector, <any>loader, serializer, tree('a/b'), config)
218220
.subscribe(() => {}, (e) => {
@@ -229,9 +231,12 @@ describe('applyRedirects', () => {
229231
get: (token: any) => token === 'guard1' || token === 'guard2' ? guard : {injector}
230232
};
231233

232-
const config = [
233-
{path: 'a', component: ComponentA, canLoad: ['guard1', 'guard2'], loadChildren: 'children'}
234-
];
234+
const config = [{
235+
path: 'a',
236+
component: ComponentA,
237+
canLoad: ['guard1', 'guard2'],
238+
loadChildren: jasmine.createSpy('children')
239+
}];
235240

236241
applyRedirects(<any>injector, <any>loader, serializer, tree('a/b'), config).forEach(r => {
237242
expectTreeToBe(r, '/a/b');
@@ -257,9 +262,12 @@ describe('applyRedirects', () => {
257262
}
258263
};
259264

260-
const config = [
261-
{path: 'a', component: ComponentA, canLoad: ['guard1', 'guard2'], loadChildren: 'children'}
262-
];
265+
const config = [{
266+
path: 'a',
267+
component: ComponentA,
268+
canLoad: ['guard1', 'guard2'],
269+
loadChildren: jasmine.createSpy('children')
270+
}];
263271

264272
applyRedirects(<any>injector, <any>loader, serializer, tree('a/b'), config)
265273
.subscribe(
@@ -291,9 +299,12 @@ describe('applyRedirects', () => {
291299
}
292300
};
293301

294-
const config = [
295-
{path: 'a', component: ComponentA, canLoad: ['guard1', 'guard2'], loadChildren: 'children'}
296-
];
302+
const config = [{
303+
path: 'a',
304+
component: ComponentA,
305+
canLoad: ['guard1', 'guard2'],
306+
loadChildren: jasmine.createSpy('children')
307+
}];
297308

298309
applyRedirects(<any>injector, <any>loader, serializer, tree('a/b'), config)
299310
.subscribe(
@@ -312,8 +323,12 @@ describe('applyRedirects', () => {
312323
const guard = {canLoad: () => Promise.resolve(true)};
313324
const injector = {get: (token: any) => token === 'guard' ? guard : {injector}};
314325

315-
const config =
316-
[{path: 'a', component: ComponentA, canLoad: ['guard'], loadChildren: 'children'}];
326+
const config = [{
327+
path: 'a',
328+
component: ComponentA,
329+
canLoad: ['guard'],
330+
loadChildren: jasmine.createSpy('children')
331+
}];
317332

318333
applyRedirects(<any>injector, <any>loader, serializer, tree('a/b'), config)
319334
.subscribe(
@@ -337,8 +352,12 @@ describe('applyRedirects', () => {
337352
};
338353
const injector = {get: (token: any) => token === 'guard' ? guard : {injector}};
339354

340-
const config =
341-
[{path: 'a', component: ComponentA, canLoad: ['guard'], loadChildren: 'children'}];
355+
const config = [{
356+
path: 'a',
357+
component: ComponentA,
358+
canLoad: ['guard'],
359+
loadChildren: jasmine.createSpy('children')
360+
}];
342361

343362
applyRedirects(<any>injector, <any>loader, serializer, tree('a/b'), config)
344363
.subscribe(
@@ -367,8 +386,12 @@ describe('applyRedirects', () => {
367386
};
368387
const injector = {get: (token: any) => token === 'guard' ? guard : {injector}};
369388

370-
const config =
371-
[{path: 'a', component: ComponentA, canLoad: ['guard'], loadChildren: 'children'}];
389+
const config = [{
390+
path: 'a',
391+
component: ComponentA,
392+
canLoad: ['guard'],
393+
loadChildren: jasmine.createSpy('children')
394+
}];
372395

373396
applyRedirects(<any>injector, <any>loader, serializer, tree('a/b'), config)
374397
.subscribe(
@@ -388,8 +411,10 @@ describe('applyRedirects', () => {
388411

389412
const loader = {load: (injector: any, p: any) => of(loadedConfig)};
390413

391-
const config: Routes =
392-
[{path: '', pathMatch: 'full', redirectTo: '/a'}, {path: 'a', loadChildren: 'children'}];
414+
const config: Routes = [
415+
{path: '', pathMatch: 'full', redirectTo: '/a'},
416+
{path: 'a', loadChildren: jasmine.createSpy('children')}
417+
];
393418

394419
applyRedirects(testModule.injector, <any>loader, serializer, tree(''), config).forEach(r => {
395420
expectTreeToBe(r, 'a');
@@ -409,7 +434,7 @@ describe('applyRedirects', () => {
409434
}
410435
};
411436

412-
const config: Routes = [{path: 'a', loadChildren: 'children'}];
437+
const config: Routes = [{path: 'a', loadChildren: jasmine.createSpy('children')}];
413438

414439
applyRedirects(testModule.injector, <any>loader, serializer, tree('a?k1'), config)
415440
.subscribe(r => {});
@@ -430,7 +455,7 @@ describe('applyRedirects', () => {
430455

431456
const loader = {load: (injector: any, p: any) => of(loadedConfig)};
432457

433-
const config: Routes = [{path: '**', loadChildren: 'children'}];
458+
const config: Routes = [{path: '**', loadChildren: jasmine.createSpy('children')}];
434459

435460
applyRedirects(testModule.injector, <any>loader, serializer, tree('xyz'), config)
436461
.forEach(r => {
@@ -445,14 +470,14 @@ describe('applyRedirects', () => {
445470
loader.load.and.returnValue(of(loadedConfig).pipe(delay(0)));
446471

447472
const config: Routes = [
448-
{path: '', loadChildren: 'matchChildren'},
449-
{path: '**', loadChildren: 'children'},
473+
{path: '', loadChildren: jasmine.createSpy('matchChildren')},
474+
{path: '**', loadChildren: jasmine.createSpy('children')},
450475
];
451476

452477
applyRedirects(testModule.injector, <any>loader, serializer, tree(''), config).forEach(r => {
453478
expect(loader.load.calls.count()).toEqual(1);
454479
expect(loader.load.calls.first().args).not.toContain(jasmine.objectContaining({
455-
loadChildren: 'children'
480+
loadChildren: jasmine.createSpy('children')
456481
}));
457482
});
458483
});
@@ -462,8 +487,10 @@ describe('applyRedirects', () => {
462487

463488
const loader = {load: (injector: any, p: any) => of(loadedConfig)};
464489

465-
const config: Routes =
466-
[{path: 'not-found', loadChildren: 'children'}, {path: '**', redirectTo: 'not-found'}];
490+
const config: Routes = [
491+
{path: 'not-found', loadChildren: jasmine.createSpy('children')},
492+
{path: '**', redirectTo: 'not-found'}
493+
];
467494

468495
applyRedirects(testModule.injector, <any>loader, serializer, tree('xyz'), config)
469496
.forEach(r => {
@@ -476,8 +503,10 @@ describe('applyRedirects', () => {
476503

477504
const loader = {load: (injector: any, p: any) => of(loadedConfig)};
478505

479-
const config: Routes =
480-
[{path: 'not-found', loadChildren: 'children'}, {path: '**', redirectTo: '/not-found'}];
506+
const config: Routes = [
507+
{path: 'not-found', loadChildren: jasmine.createSpy('children')},
508+
{path: '**', redirectTo: '/not-found'}
509+
];
481510

482511
applyRedirects(testModule.injector, <any>loader, serializer, tree('xyz'), config)
483512
.forEach(r => {
@@ -497,13 +526,15 @@ describe('applyRedirects', () => {
497526
return of(loadedConfig)
498527
.pipe(
499528
delay(100 * loadCalls),
500-
tap(() => loaded.push(p.loadChildren! as string)),
529+
tap(() => loaded.push((p.loadChildren as jasmine.Spy).and.identity)),
501530
);
502531
}
503532
};
504533

505-
const config: Routes =
506-
[{path: '', loadChildren: 'root'}, {path: '', loadChildren: 'aux', outlet: 'popup'}];
534+
const config: Routes = [
535+
{path: '', loadChildren: jasmine.createSpy('root')},
536+
{path: '', loadChildren: jasmine.createSpy('aux'), outlet: 'popup'}
537+
];
507538

508539
applyRedirects(testModule.injector, <any>loader, serializer, tree(''), config).subscribe();
509540
expect(loadCalls).toBe(1);
@@ -526,13 +557,13 @@ describe('applyRedirects', () => {
526557
return of(loadedConfig)
527558
.pipe(
528559
delay(100 * loadCalls),
529-
tap(() => loaded.push(p.loadChildren! as string)),
560+
tap(() => loaded.push((p.loadChildren as jasmine.Spy).and.identity)),
530561
);
531562
}
532563
};
533564

534565
const config: Routes = [
535-
{path: '**', loadChildren: 'children'},
566+
{path: '**', loadChildren: jasmine.createSpy('children')},
536567
];
537568

538569
applyRedirects(testModule.injector, <any>loader, serializer, tree('xyz/a'), config)
@@ -562,13 +593,15 @@ describe('applyRedirects', () => {
562593
loadCalls++;
563594
return of(loadedConfig)
564595
.pipe(
565-
tap(() => loaded.push(p.loadChildren! as string)),
596+
tap(() => loaded.push((p.loadChildren as jasmine.Spy).and.identity)),
566597
);
567598
}
568599
};
569600

570-
const config: Routes =
571-
[{path: 'a', loadChildren: 'first'}, {path: 'a', loadChildren: 'second'}];
601+
const config: Routes = [
602+
{path: 'a', loadChildren: jasmine.createSpy('first')},
603+
{path: 'a', loadChildren: jasmine.createSpy('second')}
604+
];
572605

573606
applyRedirects(testModule.injector, <any>loader, serializer, tree('a'), config).subscribe();
574607
expect(loadCalls).toBe(1);
@@ -584,19 +617,20 @@ describe('applyRedirects', () => {
584617
const auxDelay = 1;
585618
const loader = {
586619
load: (injector: any, p: Route) => {
587-
const delayMs = p.loadChildren! as string === 'aux' ? auxDelay : rootDelay;
620+
const delayMs =
621+
(p.loadChildren! as jasmine.Spy).and.identity === 'aux' ? auxDelay : rootDelay;
588622
return of(loadedConfig)
589623
.pipe(
590624
delay(delayMs),
591-
tap(() => loaded.push(p.loadChildren! as string)),
625+
tap(() => loaded.push((p.loadChildren as jasmine.Spy).and.identity)),
592626
);
593627
}
594628
};
595629

596630
const config: Routes = [
597631
// Define aux route first so it matches before the primary outlet
598-
{path: 'modal', loadChildren: 'aux', outlet: 'popup'},
599-
{path: '', loadChildren: 'root'},
632+
{path: 'modal', loadChildren: jasmine.createSpy('aux'), outlet: 'popup'},
633+
{path: '', loadChildren: jasmine.createSpy('root')},
600634
];
601635

602636
applyRedirects(testModule.injector, <any>loader, serializer, tree('(popup:modal)'), config)

‎packages/router/test/config.spec.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ describe('config', () => {
7676

7777
it('should throw when redirectTo and loadChildren are used together', () => {
7878
expect(() => {
79-
validateConfig([{path: 'a', redirectTo: 'b', loadChildren: 'value'}]);
79+
validateConfig([{path: 'a', redirectTo: 'b', loadChildren: jasmine.createSpy('value')}]);
8080
})
8181
.toThrowError(
8282
`Invalid configuration of route 'a': redirectTo and loadChildren cannot be used together`);
8383
});
8484

8585
it('should throw when children and loadChildren are used together', () => {
8686
expect(() => {
87-
validateConfig([{path: 'a', children: [], loadChildren: 'value'}]);
87+
validateConfig([{path: 'a', children: [], loadChildren: jasmine.createSpy('value')}]);
8888
})
8989
.toThrowError(
9090
`Invalid configuration of route 'a': children and loadChildren cannot be used together`);
@@ -168,7 +168,7 @@ describe('config', () => {
168168
validateConfig([{path: 'a', outlet: 'aux', children: []}]);
169169
}).not.toThrow();
170170
expect(() => {
171-
validateConfig([{path: 'a', outlet: 'aux', loadChildren: 'child'}]);
171+
validateConfig([{path: 'a', outlet: 'aux', loadChildren: jasmine.createSpy('child')}]);
172172
}).not.toThrow();
173173
});
174174
});

0 commit comments

Comments
 (0)