Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,10 @@ describe('AppComponent & RouterTestingModule', () => {


///////////////
import { NgModuleFactoryLoader } from '@angular/core';
import { SpyNgModuleFactoryLoader } from '@angular/router/testing';

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

let loader: SpyNgModuleFactoryLoader;

///////// Can't get lazy loaded Heroes to work yet
xdescribe('AppComponent & Lazy Loading (not working yet)', () => {
beforeEach(waitForAsync(() => {
Expand All @@ -104,9 +100,7 @@ xdescribe('AppComponent & Lazy Loading (not working yet)', () => {

beforeEach(fakeAsync(() => {
createComponent();
loader = TestBed.inject(NgModuleFactoryLoader) as SpyNgModuleFactoryLoader;
loader.stubbedModules = {expected: HeroModule};
router.resetConfig([{path: 'heroes', loadChildren: 'expected'}]);
router.resetConfig([{path: 'heroes', loadChildren: () => HeroModule}]);
}));

it('should navigate to "Heroes" on click', waitForAsync(() => {
Expand Down
114 changes: 74 additions & 40 deletions packages/router/test/apply_redirects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ describe('applyRedirects', () => {
return of(loadedConfig);
}
};
const config: Routes = [{path: 'a', component: ComponentA, loadChildren: 'children'}];
const config: Routes =
[{path: 'a', component: ComponentA, loadChildren: jasmine.createSpy('children')}];

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

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

const config = [
{path: 'a', component: ComponentA, canLoad: ['guard1', 'guard2'], loadChildren: 'children'}
];
const config = [{
path: 'a',
component: ComponentA,
canLoad: ['guard1', 'guard2'],
loadChildren: jasmine.createSpy('children')
}];

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

const config = [
{path: 'a', component: ComponentA, canLoad: ['guard1', 'guard2'], loadChildren: 'children'}
];
const config = [{
path: 'a',
component: ComponentA,
canLoad: ['guard1', 'guard2'],
loadChildren: jasmine.createSpy('children')
}];

applyRedirects(<any>injector, <any>loader, serializer, tree('a/b'), config)
.subscribe(
Expand Down Expand Up @@ -291,9 +299,12 @@ describe('applyRedirects', () => {
}
};

const config = [
{path: 'a', component: ComponentA, canLoad: ['guard1', 'guard2'], loadChildren: 'children'}
];
const config = [{
path: 'a',
component: ComponentA,
canLoad: ['guard1', 'guard2'],
loadChildren: jasmine.createSpy('children')
}];

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

const config =
[{path: 'a', component: ComponentA, canLoad: ['guard'], loadChildren: 'children'}];
const config = [{
path: 'a',
component: ComponentA,
canLoad: ['guard'],
loadChildren: jasmine.createSpy('children')
}];

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

const config =
[{path: 'a', component: ComponentA, canLoad: ['guard'], loadChildren: 'children'}];
const config = [{
path: 'a',
component: ComponentA,
canLoad: ['guard'],
loadChildren: jasmine.createSpy('children')
}];

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

const config =
[{path: 'a', component: ComponentA, canLoad: ['guard'], loadChildren: 'children'}];
const config = [{
path: 'a',
component: ComponentA,
canLoad: ['guard'],
loadChildren: jasmine.createSpy('children')
}];

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

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

const config: Routes =
[{path: '', pathMatch: 'full', redirectTo: '/a'}, {path: 'a', loadChildren: 'children'}];
const config: Routes = [
{path: '', pathMatch: 'full', redirectTo: '/a'},
{path: 'a', loadChildren: jasmine.createSpy('children')}
];

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

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

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

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

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

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

const config: Routes = [
{path: '', loadChildren: 'matchChildren'},
{path: '**', loadChildren: 'children'},
{path: '', loadChildren: jasmine.createSpy('matchChildren')},
{path: '**', loadChildren: jasmine.createSpy('children')},
];

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

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

const config: Routes =
[{path: 'not-found', loadChildren: 'children'}, {path: '**', redirectTo: 'not-found'}];
const config: Routes = [
{path: 'not-found', loadChildren: jasmine.createSpy('children')},
{path: '**', redirectTo: 'not-found'}
];

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

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

const config: Routes =
[{path: 'not-found', loadChildren: 'children'}, {path: '**', redirectTo: '/not-found'}];
const config: Routes = [
{path: 'not-found', loadChildren: jasmine.createSpy('children')},
{path: '**', redirectTo: '/not-found'}
];

applyRedirects(testModule.injector, <any>loader, serializer, tree('xyz'), config)
.forEach(r => {
Expand All @@ -497,13 +526,15 @@ describe('applyRedirects', () => {
return of(loadedConfig)
.pipe(
delay(100 * loadCalls),
tap(() => loaded.push(p.loadChildren! as string)),
tap(() => loaded.push((p.loadChildren as jasmine.Spy).and.identity)),
);
}
};

const config: Routes =
[{path: '', loadChildren: 'root'}, {path: '', loadChildren: 'aux', outlet: 'popup'}];
const config: Routes = [
{path: '', loadChildren: jasmine.createSpy('root')},
{path: '', loadChildren: jasmine.createSpy('aux'), outlet: 'popup'}
];

applyRedirects(testModule.injector, <any>loader, serializer, tree(''), config).subscribe();
expect(loadCalls).toBe(1);
Expand All @@ -526,13 +557,13 @@ describe('applyRedirects', () => {
return of(loadedConfig)
.pipe(
delay(100 * loadCalls),
tap(() => loaded.push(p.loadChildren! as string)),
tap(() => loaded.push((p.loadChildren as jasmine.Spy).and.identity)),
);
}
};

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

applyRedirects(testModule.injector, <any>loader, serializer, tree('xyz/a'), config)
Expand Down Expand Up @@ -562,13 +593,15 @@ describe('applyRedirects', () => {
loadCalls++;
return of(loadedConfig)
.pipe(
tap(() => loaded.push(p.loadChildren! as string)),
tap(() => loaded.push((p.loadChildren as jasmine.Spy).and.identity)),
);
}
};

const config: Routes =
[{path: 'a', loadChildren: 'first'}, {path: 'a', loadChildren: 'second'}];
const config: Routes = [
{path: 'a', loadChildren: jasmine.createSpy('first')},
{path: 'a', loadChildren: jasmine.createSpy('second')}
];

applyRedirects(testModule.injector, <any>loader, serializer, tree('a'), config).subscribe();
expect(loadCalls).toBe(1);
Expand All @@ -584,19 +617,20 @@ describe('applyRedirects', () => {
const auxDelay = 1;
const loader = {
load: (injector: any, p: Route) => {
const delayMs = p.loadChildren! as string === 'aux' ? auxDelay : rootDelay;
const delayMs =
(p.loadChildren! as jasmine.Spy).and.identity === 'aux' ? auxDelay : rootDelay;
return of(loadedConfig)
.pipe(
delay(delayMs),
tap(() => loaded.push(p.loadChildren! as string)),
tap(() => loaded.push((p.loadChildren as jasmine.Spy).and.identity)),
);
}
};

const config: Routes = [
// Define aux route first so it matches before the primary outlet
{path: 'modal', loadChildren: 'aux', outlet: 'popup'},
{path: '', loadChildren: 'root'},
{path: 'modal', loadChildren: jasmine.createSpy('aux'), outlet: 'popup'},
{path: '', loadChildren: jasmine.createSpy('root')},
];

applyRedirects(testModule.injector, <any>loader, serializer, tree('(popup:modal)'), config)
Expand Down
6 changes: 3 additions & 3 deletions packages/router/test/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ describe('config', () => {

it('should throw when redirectTo and loadChildren are used together', () => {
expect(() => {
validateConfig([{path: 'a', redirectTo: 'b', loadChildren: 'value'}]);
validateConfig([{path: 'a', redirectTo: 'b', loadChildren: jasmine.createSpy('value')}]);
})
.toThrowError(
`Invalid configuration of route 'a': redirectTo and loadChildren cannot be used together`);
});

it('should throw when children and loadChildren are used together', () => {
expect(() => {
validateConfig([{path: 'a', children: [], loadChildren: 'value'}]);
validateConfig([{path: 'a', children: [], loadChildren: jasmine.createSpy('value')}]);
})
.toThrowError(
`Invalid configuration of route 'a': children and loadChildren cannot be used together`);
Expand Down Expand Up @@ -168,7 +168,7 @@ describe('config', () => {
validateConfig([{path: 'a', outlet: 'aux', children: []}]);
}).not.toThrow();
expect(() => {
validateConfig([{path: 'a', outlet: 'aux', loadChildren: 'child'}]);
validateConfig([{path: 'a', outlet: 'aux', loadChildren: jasmine.createSpy('child')}]);
}).not.toThrow();
});
});
Expand Down
Loading