2016-08-10 18:53:57 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-08-16 23:21:05 -04:00
|
|
|
import {CommonModule, Location} from '@angular/common';
|
2016-08-10 18:53:57 -04:00
|
|
|
import {Component, NgModule, NgModuleFactoryLoader} from '@angular/core';
|
2016-10-20 13:58:53 -04:00
|
|
|
import {ComponentFixture, TestBed, async, fakeAsync, inject, tick} from '@angular/core/testing';
|
2016-08-10 18:53:57 -04:00
|
|
|
import {expect} from '@angular/platform-browser/testing/matchers';
|
|
|
|
import {Observable} from 'rxjs/Observable';
|
2016-08-30 17:25:46 -04:00
|
|
|
import {map} from 'rxjs/operator/map';
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-10-20 13:44:44 -04:00
|
|
|
import {ActivatedRoute, ActivatedRouteSnapshot, CanActivate, CanDeactivate, Event, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, PRIMARY_OUTLET, Params, PreloadAllModules, PreloadingStrategy, Resolve, Router, RouterModule, RouterStateSnapshot, RoutesRecognized, UrlHandlingStrategy, UrlSegmentGroup, UrlTree} from '../index';
|
2016-09-16 18:08:15 -04:00
|
|
|
import {RouterPreloader} from '../src/router_preloader';
|
2016-10-20 13:44:44 -04:00
|
|
|
import {forEach} from '../src/utils/collection';
|
2016-08-10 18:53:57 -04:00
|
|
|
import {RouterTestingModule, SpyNgModuleFactoryLoader} from '../testing';
|
|
|
|
|
2016-08-16 23:21:05 -04:00
|
|
|
|
2016-08-10 18:53:57 -04:00
|
|
|
describe('Integration', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
TestBed.configureTestingModule({
|
2016-11-08 16:36:59 -05:00
|
|
|
imports:
|
|
|
|
[RouterTestingModule.withRoutes([{path: 'simple', component: SimpleCmp}]), TestModule]
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should navigate with a provided config',
|
2016-08-16 16:40:28 -04:00
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/simple');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(location.path()).toEqual('/simple');
|
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-10-28 18:17:00 -04:00
|
|
|
describe('should execute navigations serially', () => {
|
2016-10-28 17:56:08 -04:00
|
|
|
let log: any[] = [];
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
log = [];
|
|
|
|
|
|
|
|
TestBed.configureTestingModule({
|
|
|
|
providers: [
|
|
|
|
{
|
|
|
|
provide: 'trueRightAway',
|
|
|
|
useValue: () => {
|
|
|
|
log.push('trueRightAway');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
provide: 'trueIn2Seconds',
|
|
|
|
useValue: () => {
|
|
|
|
log.push('trueIn2Seconds-start');
|
|
|
|
let res: any = null;
|
|
|
|
const p = new Promise(r => res = r);
|
|
|
|
setTimeout(() => {
|
|
|
|
log.push('trueIn2Seconds-end');
|
|
|
|
res(true);
|
|
|
|
}, 2000);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-11-03 19:26:10 -04:00
|
|
|
describe('should advance the parent route after deactivating its children', () => {
|
|
|
|
let log: string[] = [];
|
|
|
|
|
|
|
|
@Component({template: '<router-outlet></router-outlet>'})
|
|
|
|
class Parent {
|
|
|
|
constructor(route: ActivatedRoute) {
|
|
|
|
route.params.subscribe((s: any) => { log.push(s); });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({template: 'child1'})
|
|
|
|
class Child1 {
|
|
|
|
ngOnDestroy() { log.push('child1 destroy'); }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({template: 'child2'})
|
|
|
|
class Child2 {
|
|
|
|
constructor() { log.push('child2 constructor'); }
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [Parent, Child1, Child2],
|
|
|
|
entryComponents: [Parent, Child1, Child2],
|
|
|
|
imports: [RouterModule]
|
|
|
|
})
|
|
|
|
class TestModule {
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
log = [];
|
|
|
|
TestBed.configureTestingModule({imports: [TestModule]});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work',
|
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig([{
|
|
|
|
path: 'parent/:id',
|
|
|
|
component: Parent,
|
|
|
|
children:
|
|
|
|
[{path: 'child1', component: Child1}, {path: 'child2', component: Child2}]
|
|
|
|
}]);
|
|
|
|
|
|
|
|
router.navigateByUrl('/parent/1/child1');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
router.navigateByUrl('/parent/2/child2');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(location.path()).toEqual('/parent/2/child2');
|
|
|
|
expect(log).toEqual([{id: '1'}, 'child1 destroy', {id: '2'}, 'child2 constructor']);
|
|
|
|
})));
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2016-10-28 17:56:08 -04:00
|
|
|
it('should execute navigations serialy',
|
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig([
|
|
|
|
{path: 'a', component: SimpleCmp, canActivate: ['trueRightAway', 'trueIn2Seconds']},
|
|
|
|
{path: 'b', component: SimpleCmp, canActivate: ['trueRightAway', 'trueIn2Seconds']}
|
|
|
|
]);
|
|
|
|
|
|
|
|
router.navigateByUrl('/a');
|
|
|
|
tick(100);
|
|
|
|
fixture.detectChanges();
|
|
|
|
|
|
|
|
router.navigateByUrl('/b');
|
|
|
|
tick(100); // 200
|
|
|
|
fixture.detectChanges();
|
|
|
|
|
|
|
|
expect(log).toEqual(['trueRightAway', 'trueIn2Seconds-start']);
|
|
|
|
|
|
|
|
tick(2000); // 2200
|
|
|
|
fixture.detectChanges();
|
|
|
|
|
|
|
|
expect(log).toEqual([
|
|
|
|
'trueRightAway', 'trueIn2Seconds-start', 'trueIn2Seconds-end', 'trueRightAway',
|
|
|
|
'trueIn2Seconds-start'
|
|
|
|
]);
|
|
|
|
|
|
|
|
tick(2000); // 4200
|
|
|
|
fixture.detectChanges();
|
|
|
|
|
|
|
|
expect(log).toEqual([
|
|
|
|
'trueRightAway', 'trueIn2Seconds-start', 'trueIn2Seconds-end', 'trueRightAway',
|
|
|
|
'trueIn2Seconds-start', 'trueIn2Seconds-end'
|
|
|
|
]);
|
|
|
|
})));
|
|
|
|
});
|
|
|
|
|
2016-11-08 16:36:59 -05:00
|
|
|
it('should not error when no url left and no children are matching',
|
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig([{
|
|
|
|
path: 'team/:id',
|
|
|
|
component: TeamCmp,
|
|
|
|
children: [{path: 'simple', component: SimpleCmp}]
|
|
|
|
}]);
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/33/simple');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(location.path()).toEqual('/team/33/simple');
|
|
|
|
expect(fixture.nativeElement).toHaveText('team 33 [ simple, right: ]');
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/33');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(location.path()).toEqual('/team/33');
|
|
|
|
expect(fixture.nativeElement).toHaveText('team 33 [ , right: ]');
|
|
|
|
})));
|
|
|
|
|
2016-08-10 18:53:57 -04:00
|
|
|
it('should work when an outlet is in an ngIf',
|
2016-08-16 16:40:28 -04:00
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig([{
|
|
|
|
path: 'child',
|
|
|
|
component: LinkInNgIf,
|
|
|
|
children: [{path: 'simple', component: SimpleCmp}]
|
|
|
|
}]);
|
|
|
|
|
|
|
|
router.navigateByUrl('/child/simple');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(location.path()).toEqual('/child/simple');
|
|
|
|
})));
|
|
|
|
|
|
|
|
it('should work when an outlet is in an ngIf (and is removed)', fakeAsync(() => {
|
|
|
|
@Component({
|
|
|
|
selector: 'someRoot',
|
2016-08-16 23:21:05 -04:00
|
|
|
template: `<div *ngIf="cond"><router-outlet></router-outlet></div>`
|
2016-08-16 16:40:28 -04:00
|
|
|
})
|
|
|
|
class RootCmpWithLink {
|
|
|
|
cond: boolean = true;
|
|
|
|
}
|
|
|
|
TestBed.configureTestingModule({declarations: [RootCmpWithLink]});
|
|
|
|
|
2016-08-16 23:21:05 -04:00
|
|
|
const router: Router = TestBed.get(Router);
|
|
|
|
const location: Location = TestBed.get(Location);
|
2016-08-16 16:40:28 -04:00
|
|
|
|
|
|
|
const fixture = createRoot(router, RootCmpWithLink);
|
|
|
|
|
|
|
|
router.resetConfig(
|
|
|
|
[{path: 'simple', component: SimpleCmp}, {path: 'blank', component: BlankCmp}]);
|
|
|
|
|
|
|
|
router.navigateByUrl('/simple');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/simple');
|
|
|
|
|
|
|
|
const instance = fixture.componentInstance;
|
|
|
|
instance.cond = false;
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
let recordedError: any = null;
|
|
|
|
router.navigateByUrl('/blank').catch(e => recordedError = e);
|
|
|
|
advance(fixture);
|
2016-10-20 13:44:44 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(recordedError.message).toEqual('Cannot find primary outlet to load \'BlankCmp\'');
|
|
|
|
}));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-25 05:35:46 -04:00
|
|
|
it('should update location when navigating', fakeAsync(() => {
|
|
|
|
@Component({template: `record`})
|
|
|
|
class RecordLocationCmp {
|
|
|
|
private storedPath: string;
|
|
|
|
constructor(loc: Location) { this.storedPath = loc.path(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({declarations: [RecordLocationCmp], entryComponents: [RecordLocationCmp]})
|
|
|
|
class TestModule {
|
|
|
|
}
|
|
|
|
|
|
|
|
TestBed.configureTestingModule({imports: [TestModule]});
|
|
|
|
|
|
|
|
const router = TestBed.get(Router);
|
|
|
|
const location = TestBed.get(Location);
|
2016-08-16 16:40:28 -04:00
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-25 05:35:46 -04:00
|
|
|
router.resetConfig([{path: 'record/:id', component: RecordLocationCmp}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-25 05:35:46 -04:00
|
|
|
router.navigateByUrl('/record/22');
|
2016-08-16 16:40:28 -04:00
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-25 05:35:46 -04:00
|
|
|
const c = fixture.debugElement.children[1].componentInstance;
|
|
|
|
expect(location.path()).toEqual('/record/22');
|
|
|
|
expect(c.storedPath).toEqual('/record/22');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-25 05:35:46 -04:00
|
|
|
router.navigateByUrl('/record/33');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/record/33');
|
|
|
|
}));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
|
|
|
it('should skip location update when using NavigationExtras.skipLocationChange with navigateByUrl',
|
2016-08-16 16:40:28 -04:00
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = TestBed.createComponent(RootCmp);
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig([{path: 'team/:id', component: TeamCmp}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/22');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/22');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('team 22 [ , right: ]');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/33', {skipLocationChange: true});
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(location.path()).toEqual('/team/22');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('team 33 [ , right: ]');
|
2016-08-16 16:40:28 -04:00
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
|
|
|
it('should skip location update when using NavigationExtras.skipLocationChange with navigate',
|
2016-08-16 16:40:28 -04:00
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = TestBed.createComponent(RootCmp);
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig([{path: 'team/:id', component: TeamCmp}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigate(['/team/22']);
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/22');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('team 22 [ , right: ]');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigate(['/team/33'], {skipLocationChange: true});
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(location.path()).toEqual('/team/22');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('team 33 [ , right: ]');
|
2016-08-16 16:40:28 -04:00
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
|
|
|
it('should navigate back and forward',
|
2016-08-16 16:40:28 -04:00
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig([{
|
|
|
|
path: 'team/:id',
|
|
|
|
component: TeamCmp,
|
|
|
|
children:
|
|
|
|
[{path: 'simple', component: SimpleCmp}, {path: 'user/:name', component: UserCmp}]
|
|
|
|
}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/33/simple');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/33/simple');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/22/user/victor');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
location.back();
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/33/simple');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
location.forward();
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/22/user/victor');
|
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
|
|
|
it('should navigate when locations changes',
|
2016-08-16 16:40:28 -04:00
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig([{
|
|
|
|
path: 'team/:id',
|
|
|
|
component: TeamCmp,
|
|
|
|
children: [{path: 'user/:name', component: UserCmp}]
|
|
|
|
}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
const recordedEvents: any[] = [];
|
|
|
|
router.events.forEach(e => recordedEvents.push(e));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/22/user/victor');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
(<any>location).simulateHashChange('/team/22/user/fedor');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
(<any>location).simulateUrlPop('/team/22/user/fedor');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('team 22 [ user fedor, right: ]');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
expectEvents(recordedEvents, [
|
|
|
|
[NavigationStart, '/team/22/user/victor'], [RoutesRecognized, '/team/22/user/victor'],
|
|
|
|
[NavigationEnd, '/team/22/user/victor'],
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
[NavigationStart, '/team/22/user/fedor'], [RoutesRecognized, '/team/22/user/fedor'],
|
|
|
|
[NavigationEnd, '/team/22/user/fedor']
|
|
|
|
]);
|
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
|
|
|
it('should update the location when the matched route does not change',
|
2016-08-16 16:40:28 -04:00
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig([{path: '**', component: CollectParamsCmp}]);
|
|
|
|
|
|
|
|
router.navigateByUrl('/one/two');
|
|
|
|
advance(fixture);
|
|
|
|
const cmp = fixture.debugElement.children[1].componentInstance;
|
|
|
|
expect(location.path()).toEqual('/one/two');
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('collect-params');
|
2016-08-16 16:40:28 -04:00
|
|
|
|
|
|
|
expect(cmp.recordedUrls()).toEqual(['one/two']);
|
|
|
|
|
|
|
|
router.navigateByUrl('/three/four');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/three/four');
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('collect-params');
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(cmp.recordedUrls()).toEqual(['one/two', 'three/four']);
|
|
|
|
})));
|
|
|
|
|
|
|
|
it('should support secondary routes', fakeAsync(inject([Router], (router: Router) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig([{
|
|
|
|
path: 'team/:id',
|
|
|
|
component: TeamCmp,
|
|
|
|
children: [
|
|
|
|
{path: 'user/:name', component: UserCmp},
|
|
|
|
{path: 'simple', component: SimpleCmp, outlet: 'right'}
|
|
|
|
]
|
|
|
|
}]);
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/22/(user/victor//right:simple)');
|
|
|
|
advance(fixture);
|
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('team 22 [ user victor, right: simple ]');
|
2016-08-16 16:40:28 -04:00
|
|
|
})));
|
|
|
|
|
2016-10-20 13:59:08 -04:00
|
|
|
it('should support secondary routes in seperate commands',
|
|
|
|
fakeAsync(inject([Router], (router: Router) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig([{
|
|
|
|
path: 'team/:id',
|
|
|
|
component: TeamCmp,
|
|
|
|
children: [
|
|
|
|
{path: 'user/:name', component: UserCmp},
|
|
|
|
{path: 'simple', component: SimpleCmp, outlet: 'right'}
|
|
|
|
]
|
|
|
|
}]);
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/22/user/victor');
|
|
|
|
advance(fixture);
|
|
|
|
router.navigate(['team/22', {outlets: {right: 'simple'}}]);
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(fixture.nativeElement).toHaveText('team 22 [ user victor, right: simple ]');
|
|
|
|
})));
|
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
it('should deactivate outlets', fakeAsync(inject([Router], (router: Router) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig([{
|
|
|
|
path: 'team/:id',
|
|
|
|
component: TeamCmp,
|
|
|
|
children: [
|
|
|
|
{path: 'user/:name', component: UserCmp},
|
|
|
|
{path: 'simple', component: SimpleCmp, outlet: 'right'}
|
|
|
|
]
|
|
|
|
}]);
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/22/(user/victor//right:simple)');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/22/user/victor');
|
|
|
|
advance(fixture);
|
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('team 22 [ user victor, right: ]');
|
2016-08-16 16:40:28 -04:00
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
it('should deactivate nested outlets', fakeAsync(inject([Router], (router: Router) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig([
|
|
|
|
{
|
|
|
|
path: 'team/:id',
|
|
|
|
component: TeamCmp,
|
|
|
|
children: [
|
|
|
|
{path: 'user/:name', component: UserCmp},
|
|
|
|
{path: 'simple', component: SimpleCmp, outlet: 'right'}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{path: '', component: BlankCmp}
|
|
|
|
]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/22/(user/victor//right:simple)');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('');
|
2016-08-16 16:40:28 -04:00
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
it('should set query params and fragment', fakeAsync(inject([Router], (router: Router) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig([{path: 'query', component: QueryParamsAndFragmentCmp}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/query?name=1#fragment1');
|
|
|
|
advance(fixture);
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('query: 1 fragment: fragment1');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/query?name=2#fragment2');
|
|
|
|
advance(fixture);
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('query: 2 fragment: fragment2');
|
2016-08-16 16:40:28 -04:00
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
it('should push params only when they change', fakeAsync(inject([Router], (router: Router) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig([{
|
|
|
|
path: 'team/:id',
|
|
|
|
component: TeamCmp,
|
|
|
|
children: [{path: 'user/:name', component: UserCmp}]
|
|
|
|
}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/22/user/victor');
|
|
|
|
advance(fixture);
|
|
|
|
const team = fixture.debugElement.children[1].componentInstance;
|
|
|
|
const user = fixture.debugElement.children[1].children[1].componentInstance;
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(team.recordedParams).toEqual([{id: '22'}]);
|
|
|
|
expect(user.recordedParams).toEqual([{name: 'victor'}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/22/user/fedor');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(team.recordedParams).toEqual([{id: '22'}]);
|
|
|
|
expect(user.recordedParams).toEqual([{name: 'victor'}, {name: 'fedor'}]);
|
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
it('should work when navigating to /', fakeAsync(inject([Router], (router: Router) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig([
|
|
|
|
{path: '', pathMatch: 'full', component: SimpleCmp},
|
|
|
|
{path: 'user/:name', component: UserCmp}
|
|
|
|
]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/user/victor');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('user victor');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('simple');
|
2016-08-16 16:40:28 -04:00
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
it('should cancel in-flight navigations', fakeAsync(inject([Router], (router: Router) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig([{path: 'user/:name', component: UserCmp}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
const recordedEvents: any[] = [];
|
|
|
|
router.events.forEach(e => recordedEvents.push(e));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/user/init');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
const user = fixture.debugElement.children[1].componentInstance;
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
let r1: any, r2: any;
|
|
|
|
router.navigateByUrl('/user/victor').then(_ => r1 = _);
|
|
|
|
router.navigateByUrl('/user/fedor').then(_ => r2 = _);
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(r1).toEqual(false); // returns false because it was canceled
|
|
|
|
expect(r2).toEqual(true); // returns true because it was successful
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('user fedor');
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(user.recordedParams).toEqual([{name: 'init'}, {name: 'fedor'}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
expectEvents(recordedEvents, [
|
|
|
|
[NavigationStart, '/user/init'], [RoutesRecognized, '/user/init'],
|
|
|
|
[NavigationEnd, '/user/init'],
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-10-28 17:56:08 -04:00
|
|
|
[NavigationStart, '/user/victor'], [NavigationCancel, '/user/victor'],
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-10-28 17:56:08 -04:00
|
|
|
[NavigationStart, '/user/fedor'], [RoutesRecognized, '/user/fedor'],
|
2016-08-16 16:40:28 -04:00
|
|
|
[NavigationEnd, '/user/fedor']
|
|
|
|
]);
|
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
it('should handle failed navigations gracefully', fakeAsync(inject([Router], (router: Router) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig([{path: 'user/:name', component: UserCmp}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
const recordedEvents: any[] = [];
|
|
|
|
router.events.forEach(e => recordedEvents.push(e));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
let e: any;
|
|
|
|
router.navigateByUrl('/invalid').catch(_ => e = _);
|
|
|
|
advance(fixture);
|
|
|
|
expect(e.message).toContain('Cannot match any routes');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/user/fedor');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('user fedor');
|
2016-08-16 16:40:28 -04:00
|
|
|
|
|
|
|
expectEvents(recordedEvents, [
|
|
|
|
[NavigationStart, '/invalid'], [NavigationError, '/invalid'],
|
|
|
|
|
|
|
|
[NavigationStart, '/user/fedor'], [RoutesRecognized, '/user/fedor'],
|
|
|
|
[NavigationEnd, '/user/fedor']
|
|
|
|
]);
|
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-25 10:56:30 -04:00
|
|
|
it('should support custom error handlers', fakeAsync(inject([Router], (router: Router) => {
|
|
|
|
router.errorHandler = (error) => 'resolvedValue';
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig([{path: 'user/:name', component: UserCmp}]);
|
|
|
|
|
|
|
|
const recordedEvents: any[] = [];
|
|
|
|
router.events.forEach(e => recordedEvents.push(e));
|
|
|
|
|
|
|
|
let e: any;
|
|
|
|
router.navigateByUrl('/invalid').then(_ => e = _);
|
|
|
|
advance(fixture);
|
|
|
|
expect(e).toEqual('resolvedValue');
|
|
|
|
|
|
|
|
expectEvents(recordedEvents, [[NavigationStart, '/invalid'], [NavigationError, '/invalid']]);
|
|
|
|
})));
|
|
|
|
|
2016-08-10 18:53:57 -04:00
|
|
|
it('should replace state when path is equal to current path',
|
2016-08-16 16:40:28 -04:00
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig([{
|
|
|
|
path: 'team/:id',
|
|
|
|
component: TeamCmp,
|
|
|
|
children:
|
|
|
|
[{path: 'simple', component: SimpleCmp}, {path: 'user/:name', component: UserCmp}]
|
|
|
|
}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/33/simple');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/22/user/victor');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/22/user/victor');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
location.back();
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/33/simple');
|
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
|
|
|
it('should handle componentless paths',
|
2016-08-16 16:40:28 -04:00
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmpWithTwoOutlets);
|
|
|
|
|
|
|
|
router.resetConfig([
|
|
|
|
{
|
|
|
|
path: 'parent/:id',
|
|
|
|
children: [
|
|
|
|
{path: 'simple', component: SimpleCmp},
|
|
|
|
{path: 'user/:name', component: UserCmp, outlet: 'right'}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{path: 'user/:name', component: UserCmp}
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
// navigate to a componentless route
|
|
|
|
router.navigateByUrl('/parent/11/(simple//right:user/victor)');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/parent/11/(simple//right:user/victor)');
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('primary [simple] right [user victor]');
|
2016-08-16 16:40:28 -04:00
|
|
|
|
|
|
|
// navigate to the same route with different params (reuse)
|
|
|
|
router.navigateByUrl('/parent/22/(simple//right:user/fedor)');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/parent/22/(simple//right:user/fedor)');
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('primary [simple] right [user fedor]');
|
2016-08-16 16:40:28 -04:00
|
|
|
|
|
|
|
// navigate to a normal route (check deactivation)
|
|
|
|
router.navigateByUrl('/user/victor');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/user/victor');
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('primary [user victor] right []');
|
2016-08-16 16:40:28 -04:00
|
|
|
|
|
|
|
// navigate back to a componentless route
|
|
|
|
router.navigateByUrl('/parent/11/(simple//right:user/victor)');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/parent/11/(simple//right:user/victor)');
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('primary [simple] right [user victor]');
|
2016-08-16 16:40:28 -04:00
|
|
|
})));
|
|
|
|
|
2016-10-24 15:50:00 -04:00
|
|
|
it('should not deactivate aux routes when navigating from a componentless routes',
|
|
|
|
fakeAsync(inject(
|
|
|
|
[Router, Location, NgModuleFactoryLoader],
|
|
|
|
(router: Router, location: Location, loader: SpyNgModuleFactoryLoader) => {
|
|
|
|
const fixture = createRoot(router, TwoOutletsCmp);
|
|
|
|
|
|
|
|
router.resetConfig([
|
|
|
|
{path: 'simple', component: SimpleCmp},
|
|
|
|
{path: 'componentless', children: [{path: 'simple', component: SimpleCmp}]},
|
|
|
|
{path: 'user/:name', outlet: 'aux', component: UserCmp}
|
|
|
|
]);
|
|
|
|
|
|
|
|
router.navigateByUrl('/componentless/simple(aux:user/victor)');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/componentless/simple(aux:user/victor)');
|
|
|
|
expect(fixture.nativeElement).toHaveText('[ simple, aux: user victor ]');
|
|
|
|
|
|
|
|
router.navigateByUrl('/simple(aux:user/victor)');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/simple(aux:user/victor)');
|
|
|
|
expect(fixture.nativeElement).toHaveText('[ simple, aux: user victor ]');
|
|
|
|
})));
|
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
it('should emit an event when an outlet gets activated', fakeAsync(() => {
|
|
|
|
@Component({
|
|
|
|
selector: 'container',
|
|
|
|
template:
|
|
|
|
`<router-outlet (activate)="recordActivate($event)" (deactivate)="recordDeactivate($event)"></router-outlet>`
|
|
|
|
})
|
|
|
|
class Container {
|
|
|
|
activations: any[] = [];
|
|
|
|
deactivations: any[] = [];
|
|
|
|
|
|
|
|
recordActivate(component: any): void { this.activations.push(component); }
|
|
|
|
|
|
|
|
recordDeactivate(component: any): void { this.deactivations.push(component); }
|
|
|
|
}
|
|
|
|
|
|
|
|
TestBed.configureTestingModule({declarations: [Container]});
|
|
|
|
|
2016-08-16 23:21:05 -04:00
|
|
|
const router: Router = TestBed.get(Router);
|
2016-08-16 16:40:28 -04:00
|
|
|
|
|
|
|
const fixture = createRoot(router, Container);
|
2016-09-09 15:04:38 -04:00
|
|
|
const cmp = fixture.componentInstance;
|
2016-08-16 16:40:28 -04:00
|
|
|
|
|
|
|
router.resetConfig(
|
|
|
|
[{path: 'blank', component: BlankCmp}, {path: 'simple', component: SimpleCmp}]);
|
|
|
|
|
|
|
|
cmp.activations = [];
|
|
|
|
cmp.deactivations = [];
|
|
|
|
|
|
|
|
router.navigateByUrl('/blank');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(cmp.activations.length).toEqual(1);
|
|
|
|
expect(cmp.activations[0] instanceof BlankCmp).toBe(true);
|
|
|
|
|
2016-11-08 16:36:59 -05:00
|
|
|
router.navigateByUrl('/simple').catch(e => console.log(e));
|
2016-08-16 16:40:28 -04:00
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(cmp.activations.length).toEqual(2);
|
|
|
|
expect(cmp.activations[1] instanceof SimpleCmp).toBe(true);
|
2016-11-08 16:36:59 -05:00
|
|
|
expect(cmp.deactivations.length).toEqual(1);
|
|
|
|
expect(cmp.deactivations[0] instanceof BlankCmp).toBe(true);
|
2016-08-16 16:40:28 -04:00
|
|
|
}));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
|
|
|
it('should update url and router state before activating components',
|
2016-08-16 16:40:28 -04:00
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig([{path: 'cmp', component: ComponentRecordingRoutePathAndUrl}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/cmp');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
const cmp = fixture.debugElement.children[1].componentInstance;
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(cmp.url).toBe('/cmp');
|
|
|
|
expect(cmp.path.length).toEqual(2);
|
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
|
|
|
describe('data', () => {
|
|
|
|
class ResolveSix implements Resolve<TeamCmp> {
|
|
|
|
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): number { return 6; }
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2016-08-16 23:21:05 -04:00
|
|
|
TestBed.configureTestingModule({
|
|
|
|
providers: [
|
|
|
|
{provide: 'resolveTwo', useValue: (a: any, b: any) => 2},
|
|
|
|
{provide: 'resolveFour', useValue: (a: any, b: any) => 4},
|
2016-10-24 17:32:11 -04:00
|
|
|
{provide: 'resolveSix', useClass: ResolveSix},
|
|
|
|
{provide: 'resolveError', useValue: (a: any, b: any) => Promise.reject('error')},
|
2016-10-28 18:17:00 -04:00
|
|
|
{provide: 'numberOfUrlSegments', useValue: (a: any, b: any) => a.url.length}
|
2016-08-16 23:21:05 -04:00
|
|
|
]
|
|
|
|
});
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should provide resolved data',
|
2016-08-16 16:40:28 -04:00
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmpWithTwoOutlets);
|
|
|
|
|
|
|
|
router.resetConfig([{
|
|
|
|
path: 'parent/:id',
|
|
|
|
data: {one: 1},
|
|
|
|
resolve: {two: 'resolveTwo'},
|
|
|
|
children: [
|
|
|
|
{path: '', data: {three: 3}, resolve: {four: 'resolveFour'}, component: RouteCmp}, {
|
|
|
|
path: '',
|
|
|
|
data: {five: 5},
|
|
|
|
resolve: {six: 'resolveSix'},
|
|
|
|
component: RouteCmp,
|
|
|
|
outlet: 'right'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/parent/1');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
const primaryCmp = fixture.debugElement.children[1].componentInstance;
|
|
|
|
const rightCmp = fixture.debugElement.children[3].componentInstance;
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(primaryCmp.route.snapshot.data).toEqual({one: 1, two: 2, three: 3, four: 4});
|
|
|
|
expect(rightCmp.route.snapshot.data).toEqual({one: 1, two: 2, five: 5, six: 6});
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
let primaryRecorded: any[] = [];
|
|
|
|
primaryCmp.route.data.forEach((rec: any) => primaryRecorded.push(rec));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
let rightRecorded: any[] = [];
|
|
|
|
rightCmp.route.data.forEach((rec: any) => rightRecorded.push(rec));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/parent/2');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(primaryRecorded).toEqual([
|
|
|
|
{one: 1, three: 3, two: 2, four: 4}, {one: 1, three: 3, two: 2, four: 4}
|
|
|
|
]);
|
|
|
|
expect(rightRecorded).toEqual([
|
|
|
|
{one: 1, five: 5, two: 2, six: 6}, {one: 1, five: 5, two: 2, six: 6}
|
|
|
|
]);
|
|
|
|
})));
|
2016-10-24 17:32:11 -04:00
|
|
|
|
|
|
|
it('should handle errors',
|
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig(
|
|
|
|
[{path: 'simple', component: SimpleCmp, resolve: {error: 'resolveError'}}]);
|
|
|
|
|
|
|
|
let recordedEvents: any[] = [];
|
|
|
|
router.events.subscribe(e => recordedEvents.push(e));
|
|
|
|
|
|
|
|
let e: any = null;
|
|
|
|
router.navigateByUrl('/simple').catch(error => e = error);
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expectEvents(recordedEvents, [
|
|
|
|
[NavigationStart, '/simple'], [RoutesRecognized, '/simple'],
|
|
|
|
[NavigationError, '/simple']
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(e).toEqual('error');
|
|
|
|
})));
|
2016-10-25 17:33:18 -04:00
|
|
|
|
|
|
|
it('should preserve resolved data',
|
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig([{
|
|
|
|
path: 'parent',
|
|
|
|
resolve: {two: 'resolveTwo'},
|
|
|
|
children: [
|
|
|
|
{path: 'child1', component: CollectParamsCmp},
|
|
|
|
{path: 'child2', component: CollectParamsCmp}
|
|
|
|
]
|
|
|
|
}]);
|
|
|
|
|
|
|
|
let e: any = null;
|
|
|
|
router.navigateByUrl('/parent/child1');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
router.navigateByUrl('/parent/child2');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
const cmp = fixture.debugElement.children[1].componentInstance;
|
|
|
|
expect(cmp.route.snapshot.data).toEqual({two: 2});
|
|
|
|
})));
|
2016-10-28 18:17:00 -04:00
|
|
|
|
|
|
|
it('should rerun resolvers when the urls segments of a wildcard route change',
|
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig([{
|
|
|
|
path: '**',
|
|
|
|
component: CollectParamsCmp,
|
|
|
|
resolve: {numberOfUrlSegments: 'numberOfUrlSegments'}
|
|
|
|
}]);
|
|
|
|
|
|
|
|
let e: any = null;
|
|
|
|
router.navigateByUrl('/one/two');
|
|
|
|
advance(fixture);
|
|
|
|
const cmp = fixture.debugElement.children[1].componentInstance;
|
|
|
|
|
|
|
|
expect(cmp.route.snapshot.data).toEqual({numberOfUrlSegments: 2});
|
|
|
|
|
|
|
|
router.navigateByUrl('/one/two/three');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(cmp.route.snapshot.data).toEqual({numberOfUrlSegments: 3});
|
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('router links', () => {
|
2016-08-16 16:40:28 -04:00
|
|
|
it('should support string router links', fakeAsync(inject([Router], (router: Router) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig([{
|
|
|
|
path: 'team/:id',
|
|
|
|
component: TeamCmp,
|
|
|
|
children: [
|
|
|
|
{path: 'link', component: StringLinkCmp}, {path: 'simple', component: SimpleCmp}
|
|
|
|
]
|
|
|
|
}]);
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/22/link');
|
|
|
|
advance(fixture);
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('team 22 [ link, right: ]');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
const native = fixture.nativeElement.querySelector('a');
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(native.getAttribute('href')).toEqual('/team/33/simple');
|
|
|
|
native.click();
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('team 33 [ simple, right: ]');
|
2016-08-16 16:40:28 -04:00
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
it('should not preserve query params and fragment by default', fakeAsync(() => {
|
|
|
|
@Component({
|
|
|
|
selector: 'someRoot',
|
2016-08-16 23:21:05 -04:00
|
|
|
template: `<router-outlet></router-outlet><a routerLink="/home">Link</a>`
|
2016-08-16 16:40:28 -04:00
|
|
|
})
|
|
|
|
class RootCmpWithLink {
|
|
|
|
}
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
TestBed.configureTestingModule({declarations: [RootCmpWithLink]});
|
2016-08-16 23:21:05 -04:00
|
|
|
const router: Router = TestBed.get(Router);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
const fixture = createRoot(router, RootCmpWithLink);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig([{path: 'home', component: SimpleCmp}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
const native = fixture.nativeElement.querySelector('a');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/home?q=123#fragment');
|
|
|
|
advance(fixture);
|
|
|
|
expect(native.getAttribute('href')).toEqual('/home');
|
|
|
|
}));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
it('should update hrefs when query params or fragment change', fakeAsync(() => {
|
2016-08-10 18:53:57 -04:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'someRoot',
|
|
|
|
template:
|
2016-08-16 23:21:05 -04:00
|
|
|
`<router-outlet></router-outlet><a routerLink="/home" preserveQueryParams preserveFragment>Link</a>`
|
2016-08-10 18:53:57 -04:00
|
|
|
})
|
|
|
|
class RootCmpWithLink {
|
|
|
|
}
|
2016-08-16 16:40:28 -04:00
|
|
|
TestBed.configureTestingModule({declarations: [RootCmpWithLink]});
|
2016-08-16 23:21:05 -04:00
|
|
|
const router: Router = TestBed.get(Router);
|
2016-08-16 16:40:28 -04:00
|
|
|
const fixture = createRoot(router, RootCmpWithLink);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
|
|
|
router.resetConfig([{path: 'home', component: SimpleCmp}]);
|
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
const native = fixture.nativeElement.querySelector('a');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
|
|
|
router.navigateByUrl('/home?q=123');
|
|
|
|
advance(fixture);
|
|
|
|
expect(native.getAttribute('href')).toEqual('/home?q=123');
|
|
|
|
|
|
|
|
router.navigateByUrl('/home?q=456');
|
|
|
|
advance(fixture);
|
|
|
|
expect(native.getAttribute('href')).toEqual('/home?q=456');
|
|
|
|
|
|
|
|
router.navigateByUrl('/home?q=456#1');
|
|
|
|
advance(fixture);
|
|
|
|
expect(native.getAttribute('href')).toEqual('/home?q=456#1');
|
2016-08-16 16:40:28 -04:00
|
|
|
}));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
it('should support using links on non-a tags', fakeAsync(inject([Router], (router: Router) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig([{
|
|
|
|
path: 'team/:id',
|
|
|
|
component: TeamCmp,
|
|
|
|
children: [
|
|
|
|
{path: 'link', component: StringLinkButtonCmp},
|
|
|
|
{path: 'simple', component: SimpleCmp}
|
|
|
|
]
|
|
|
|
}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/22/link');
|
|
|
|
advance(fixture);
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('team 22 [ link, right: ]');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
const native = fixture.nativeElement.querySelector('button');
|
2016-08-16 16:40:28 -04:00
|
|
|
native.click();
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('team 33 [ simple, right: ]');
|
2016-08-16 16:40:28 -04:00
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
it('should support absolute router links', fakeAsync(inject([Router], (router: Router) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig([{
|
|
|
|
path: 'team/:id',
|
|
|
|
component: TeamCmp,
|
|
|
|
children: [
|
|
|
|
{path: 'link', component: AbsoluteLinkCmp}, {path: 'simple', component: SimpleCmp}
|
|
|
|
]
|
|
|
|
}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/22/link');
|
|
|
|
advance(fixture);
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('team 22 [ link, right: ]');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
const native = fixture.nativeElement.querySelector('a');
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(native.getAttribute('href')).toEqual('/team/33/simple');
|
|
|
|
native.click();
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('team 33 [ simple, right: ]');
|
2016-08-16 16:40:28 -04:00
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
it('should support relative router links', fakeAsync(inject([Router], (router: Router) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig([{
|
|
|
|
path: 'team/:id',
|
|
|
|
component: TeamCmp,
|
|
|
|
children: [
|
|
|
|
{path: 'link', component: RelativeLinkCmp}, {path: 'simple', component: SimpleCmp}
|
|
|
|
]
|
|
|
|
}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/22/link');
|
|
|
|
advance(fixture);
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('team 22 [ link, right: ]');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
const native = fixture.nativeElement.querySelector('a');
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(native.getAttribute('href')).toEqual('/team/22/simple');
|
|
|
|
native.click();
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('team 22 [ simple, right: ]');
|
2016-08-16 16:40:28 -04:00
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
it('should support top-level link', fakeAsync(inject([Router], (router: Router) => {
|
|
|
|
const fixture = createRoot(router, RelativeLinkInIfCmp);
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig(
|
|
|
|
[{path: 'simple', component: SimpleCmp}, {path: '', component: BlankCmp}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/');
|
|
|
|
advance(fixture);
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText(' ');
|
|
|
|
const cmp = fixture.componentInstance;
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
cmp.show = true;
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('link ');
|
|
|
|
const native = fixture.nativeElement.querySelector('a');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(native.getAttribute('href')).toEqual('/simple');
|
|
|
|
native.click();
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('link simple');
|
2016-08-16 16:40:28 -04:00
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
|
|
|
it('should support query params and fragments',
|
2016-08-16 16:40:28 -04:00
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig([{
|
|
|
|
path: 'team/:id',
|
|
|
|
component: TeamCmp,
|
|
|
|
children: [
|
|
|
|
{path: 'link', component: LinkWithQueryParamsAndFragment},
|
|
|
|
{path: 'simple', component: SimpleCmp}
|
|
|
|
]
|
|
|
|
}]);
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/22/link');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
const native = fixture.nativeElement.querySelector('a');
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(native.getAttribute('href')).toEqual('/team/22/simple?q=1#f');
|
|
|
|
native.click();
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('team 22 [ simple, right: ]');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(location.path()).toEqual('/team/22/simple?q=1#f');
|
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('redirects', () => {
|
2016-08-16 16:40:28 -04:00
|
|
|
it('should work', fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig([
|
|
|
|
{path: 'old/team/:id', redirectTo: 'team/:id'}, {path: 'team/:id', component: TeamCmp}
|
|
|
|
]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('old/team/22');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(location.path()).toEqual('/team/22');
|
|
|
|
})));
|
2016-08-12 17:30:51 -04:00
|
|
|
|
|
|
|
it('should not break the back button when trigger by location change',
|
2016-08-16 16:40:28 -04:00
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = TestBed.createComponent(RootCmp);
|
|
|
|
advance(fixture);
|
|
|
|
router.resetConfig([
|
|
|
|
{path: 'initial', component: BlankCmp}, {path: 'old/team/:id', redirectTo: 'team/:id'},
|
|
|
|
{path: 'team/:id', component: TeamCmp}
|
|
|
|
]);
|
2016-08-12 17:30:51 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
location.go('initial');
|
|
|
|
location.go('old/team/22');
|
2016-08-12 17:30:51 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
// initial navigation
|
|
|
|
router.initialNavigation();
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/22');
|
2016-08-12 17:30:51 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
location.back();
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/initial');
|
2016-08-12 17:30:51 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
// location change
|
|
|
|
(<any>location).go('/old/team/33');
|
2016-08-12 17:30:51 -04:00
|
|
|
|
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/33');
|
2016-08-12 17:30:51 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
location.back();
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/initial');
|
|
|
|
})));
|
2016-08-12 17:30:51 -04:00
|
|
|
|
|
|
|
// should not break the back button when trigger by initial navigation
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('guards', () => {
|
|
|
|
describe('CanActivate', () => {
|
|
|
|
describe('should not activate a route when CanActivate returns false', () => {
|
|
|
|
beforeEach(() => {
|
2016-08-16 23:21:05 -04:00
|
|
|
TestBed.configureTestingModule(
|
|
|
|
{providers: [{provide: 'alwaysFalse', useValue: (a: any, b: any) => false}]});
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
it('works', fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-10-19 17:32:03 -04:00
|
|
|
const recordedEvents: any[] = [];
|
|
|
|
router.events.forEach(e => recordedEvents.push(e));
|
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig(
|
|
|
|
[{path: 'team/:id', component: TeamCmp, canActivate: ['alwaysFalse']}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/22');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(location.path()).toEqual('/');
|
2016-10-19 17:32:03 -04:00
|
|
|
expectEvents(recordedEvents, [
|
|
|
|
[NavigationStart, '/team/22'], [RoutesRecognized, '/team/22'],
|
|
|
|
[NavigationCancel, '/team/22']
|
|
|
|
]);
|
2016-08-16 16:40:28 -04:00
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe(
|
|
|
|
'should not activate a route when CanActivate returns false (componentless route)',
|
|
|
|
() => {
|
|
|
|
beforeEach(() => {
|
2016-08-16 23:21:05 -04:00
|
|
|
TestBed.configureTestingModule(
|
|
|
|
{providers: [{provide: 'alwaysFalse', useValue: (a: any, b: any) => false}]});
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
it('works',
|
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig([{
|
|
|
|
path: 'parent',
|
|
|
|
canActivate: ['alwaysFalse'],
|
|
|
|
children: [{path: 'team/:id', component: TeamCmp}]
|
|
|
|
}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('parent/team/22');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(location.path()).toEqual('/');
|
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('should activate a route when CanActivate returns true', () => {
|
|
|
|
beforeEach(() => {
|
2016-08-16 23:21:05 -04:00
|
|
|
TestBed.configureTestingModule({
|
|
|
|
providers: [{
|
|
|
|
provide: 'alwaysTrue',
|
|
|
|
useValue: (a: ActivatedRouteSnapshot, s: RouterStateSnapshot) => true
|
|
|
|
}]
|
|
|
|
});
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
it('works', fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig(
|
|
|
|
[{path: 'team/:id', component: TeamCmp, canActivate: ['alwaysTrue']}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/22');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(location.path()).toEqual('/team/22');
|
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('should work when given a class', () => {
|
|
|
|
class AlwaysTrue implements CanActivate {
|
|
|
|
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 23:21:05 -04:00
|
|
|
beforeEach(() => { TestBed.configureTestingModule({providers: [AlwaysTrue]}); });
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
it('works', fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig(
|
|
|
|
[{path: 'team/:id', component: TeamCmp, canActivate: [AlwaysTrue]}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/22');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(location.path()).toEqual('/team/22');
|
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('should work when returns an observable', () => {
|
|
|
|
beforeEach(() => {
|
2016-08-16 23:21:05 -04:00
|
|
|
TestBed.configureTestingModule({
|
|
|
|
providers: [{
|
|
|
|
provide: 'CanActivate',
|
2016-11-03 00:56:04 -04:00
|
|
|
useValue: (a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => {
|
|
|
|
return Observable.create((observer: any) => { observer.next(false); });
|
|
|
|
}
|
2016-08-16 23:21:05 -04:00
|
|
|
}]
|
|
|
|
});
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
it('works', fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig(
|
|
|
|
[{path: 'team/:id', component: TeamCmp, canActivate: ['CanActivate']}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/22');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/');
|
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('should work when returns a promise', () => {
|
|
|
|
beforeEach(() => {
|
2016-08-16 23:21:05 -04:00
|
|
|
TestBed.configureTestingModule({
|
|
|
|
providers: [{
|
|
|
|
provide: 'CanActivate',
|
|
|
|
useValue: (a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => {
|
|
|
|
if (a.params['id'] == '22') {
|
|
|
|
return Promise.resolve(true);
|
|
|
|
} else {
|
|
|
|
return Promise.resolve(false);
|
|
|
|
}
|
2016-08-10 18:53:57 -04:00
|
|
|
}
|
2016-08-16 23:21:05 -04:00
|
|
|
}]
|
|
|
|
});
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
it('works', fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig(
|
|
|
|
[{path: 'team/:id', component: TeamCmp, canActivate: ['CanActivate']}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/22');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/22');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/33');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/22');
|
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
2016-11-02 14:30:00 -04:00
|
|
|
|
|
|
|
describe('should reset the location when cancleling a navigation', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
TestBed.configureTestingModule({
|
|
|
|
providers: [{
|
|
|
|
provide: 'alwaysFalse',
|
|
|
|
useValue: (a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => { return false; }
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('works', fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig([
|
|
|
|
{path: 'one', component: SimpleCmp},
|
|
|
|
{path: 'two', component: SimpleCmp, canActivate: ['alwaysFalse']}
|
|
|
|
]);
|
|
|
|
|
|
|
|
router.navigateByUrl('/one');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/one');
|
|
|
|
|
|
|
|
location.go('/two');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/one');
|
|
|
|
|
|
|
|
})));
|
|
|
|
});
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('CanDeactivate', () => {
|
|
|
|
describe('should not deactivate a route when CanDeactivate returns false', () => {
|
2016-10-20 16:32:38 -04:00
|
|
|
let log: any;
|
|
|
|
|
2016-08-10 18:53:57 -04:00
|
|
|
beforeEach(() => {
|
2016-10-20 16:32:38 -04:00
|
|
|
log = [];
|
|
|
|
|
2016-08-16 23:21:05 -04:00
|
|
|
TestBed.configureTestingModule({
|
|
|
|
providers: [
|
|
|
|
{
|
|
|
|
provide: 'CanDeactivateParent',
|
|
|
|
useValue: (c: any, a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => {
|
|
|
|
return a.params['id'] === '22';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
provide: 'CanDeactivateTeam',
|
|
|
|
useValue: (c: any, a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => {
|
|
|
|
return c.route.snapshot.params['id'] === '22';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
provide: 'CanDeactivateUser',
|
|
|
|
useValue: (c: any, a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => {
|
|
|
|
return a.params['name'] === 'victor';
|
|
|
|
}
|
2016-10-04 18:57:37 -04:00
|
|
|
},
|
2016-10-20 16:32:38 -04:00
|
|
|
{
|
|
|
|
provide: 'RecordingDeactivate',
|
|
|
|
useValue: (c: any, a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => {
|
2016-11-01 15:11:42 -04:00
|
|
|
log.push({path: a.routeConfig.path, component: c});
|
2016-10-20 16:32:38 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
},
|
2016-08-16 23:21:05 -04:00
|
|
|
]
|
|
|
|
});
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
it('works', fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig(
|
|
|
|
[{path: 'team/:id', component: TeamCmp, canDeactivate: ['CanDeactivateTeam']}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/22');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/22');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
let successStatus: boolean;
|
|
|
|
router.navigateByUrl('/team/33').then(res => successStatus = res);
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/33');
|
|
|
|
expect(successStatus).toEqual(true);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
let canceledStatus: boolean;
|
|
|
|
router.navigateByUrl('/team/44').then(res => canceledStatus = res);
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/33');
|
|
|
|
expect(canceledStatus).toEqual(false);
|
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-10-20 16:32:38 -04:00
|
|
|
it('works with componentless routes',
|
2016-08-16 16:40:28 -04:00
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-10-20 16:32:38 -04:00
|
|
|
router.resetConfig([
|
|
|
|
{
|
|
|
|
path: 'grandparent',
|
|
|
|
canDeactivate: ['RecordingDeactivate'],
|
|
|
|
children: [{
|
|
|
|
path: 'parent',
|
|
|
|
canDeactivate: ['RecordingDeactivate'],
|
|
|
|
children: [{
|
|
|
|
path: 'child',
|
|
|
|
canDeactivate: ['RecordingDeactivate'],
|
2016-11-01 15:11:42 -04:00
|
|
|
children: [{
|
|
|
|
path: 'simple',
|
|
|
|
component: SimpleCmp,
|
|
|
|
canDeactivate: ['RecordingDeactivate']
|
|
|
|
}]
|
2016-10-20 16:32:38 -04:00
|
|
|
}]
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
{path: 'simple', component: SimpleCmp}
|
|
|
|
]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-10-20 16:32:38 -04:00
|
|
|
router.navigateByUrl('/grandparent/parent/child/simple');
|
2016-08-16 16:40:28 -04:00
|
|
|
advance(fixture);
|
2016-10-20 16:32:38 -04:00
|
|
|
expect(location.path()).toEqual('/grandparent/parent/child/simple');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-10-20 16:32:38 -04:00
|
|
|
router.navigateByUrl('/simple');
|
2016-08-16 16:40:28 -04:00
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-11-01 15:11:42 -04:00
|
|
|
const child = fixture.debugElement.children[1].componentInstance;
|
|
|
|
|
|
|
|
expect(log.map((a: any) => a.path)).toEqual([
|
|
|
|
'simple', 'child', 'parent', 'grandparent'
|
2016-10-20 16:32:38 -04:00
|
|
|
]);
|
2016-11-01 15:11:42 -04:00
|
|
|
expect(log.map((a: any) => a.component)).toEqual([child, null, null, null]);
|
2016-08-16 16:40:28 -04:00
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-10-20 17:00:02 -04:00
|
|
|
it('works with aux routes',
|
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig([{
|
|
|
|
path: 'two-outlets',
|
|
|
|
component: TwoOutletsCmp,
|
|
|
|
children: [
|
|
|
|
{path: 'a', component: BlankCmp}, {
|
|
|
|
path: 'b',
|
|
|
|
canDeactivate: ['RecordingDeactivate'],
|
|
|
|
component: SimpleCmp,
|
|
|
|
outlet: 'aux'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}]);
|
|
|
|
|
|
|
|
router.navigateByUrl('/two-outlets/(a//aux:b)');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/two-outlets/(a//aux:b)');
|
|
|
|
|
|
|
|
router.navigate(['two-outlets', {outlets: {aux: null}}]);
|
|
|
|
advance(fixture);
|
|
|
|
|
2016-11-01 15:11:42 -04:00
|
|
|
expect(log.map((a: any) => a.path)).toEqual(['b']);
|
2016-10-20 17:00:02 -04:00
|
|
|
expect(location.path()).toEqual('/two-outlets/(a)');
|
|
|
|
})));
|
|
|
|
|
2016-08-10 18:53:57 -04:00
|
|
|
it('works with a nested route',
|
2016-08-16 16:40:28 -04:00
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig([{
|
|
|
|
path: 'team/:id',
|
|
|
|
component: TeamCmp,
|
|
|
|
children: [
|
|
|
|
{path: '', pathMatch: 'full', component: SimpleCmp},
|
|
|
|
{path: 'user/:name', component: UserCmp, canDeactivate: ['CanDeactivateUser']}
|
|
|
|
]
|
|
|
|
}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/22/user/victor');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
// this works because we can deactivate victor
|
|
|
|
router.navigateByUrl('/team/33');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/33');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/33/user/fedor');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
// this doesn't work cause we cannot deactivate fedor
|
|
|
|
router.navigateByUrl('/team/44');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/33/user/fedor');
|
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('should work when given a class', () => {
|
|
|
|
class AlwaysTrue implements CanDeactivate<TeamCmp> {
|
|
|
|
canDeactivate(
|
|
|
|
component: TeamCmp, route: ActivatedRouteSnapshot,
|
|
|
|
state: RouterStateSnapshot): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 23:21:05 -04:00
|
|
|
beforeEach(() => { TestBed.configureTestingModule({providers: [AlwaysTrue]}); });
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
it('works', fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig(
|
|
|
|
[{path: 'team/:id', component: TeamCmp, canDeactivate: [AlwaysTrue]}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/22');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/22');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/33');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/33');
|
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('should work when returns an observable', () => {
|
|
|
|
beforeEach(() => {
|
2016-08-16 23:21:05 -04:00
|
|
|
TestBed.configureTestingModule({
|
|
|
|
providers: [{
|
|
|
|
provide: 'CanDeactivate',
|
|
|
|
useValue: (c: TeamCmp, a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => {
|
2016-11-03 00:56:04 -04:00
|
|
|
return Observable.create((observer: any) => { observer.next(false); });
|
2016-08-16 23:21:05 -04:00
|
|
|
}
|
|
|
|
}]
|
|
|
|
});
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
it('works', fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig(
|
|
|
|
[{path: 'team/:id', component: TeamCmp, canDeactivate: ['CanDeactivate']}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/22');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/22');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/33');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/22');
|
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('CanActivateChild', () => {
|
|
|
|
describe('should be invoked when activating a child', () => {
|
|
|
|
beforeEach(() => {
|
2016-08-16 23:21:05 -04:00
|
|
|
TestBed.configureTestingModule({
|
|
|
|
providers: [{
|
|
|
|
provide: 'alwaysFalse',
|
2016-10-04 18:57:37 -04:00
|
|
|
useValue: (a: any, b: any) => a.params.id === '22',
|
2016-08-16 23:21:05 -04:00
|
|
|
}]
|
|
|
|
});
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
it('works', fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.resetConfig([{
|
|
|
|
path: '',
|
|
|
|
canActivateChild: ['alwaysFalse'],
|
|
|
|
children: [{path: 'team/:id', component: TeamCmp}]
|
|
|
|
}]);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/22');
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(location.path()).toEqual('/team/22');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/33').catch(() => {});
|
|
|
|
advance(fixture);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(location.path()).toEqual('/team/22');
|
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('CanLoad', () => {
|
|
|
|
describe('should not load children when CanLoad returns false', () => {
|
|
|
|
beforeEach(() => {
|
2016-08-16 23:21:05 -04:00
|
|
|
TestBed.configureTestingModule({
|
|
|
|
providers: [
|
2016-08-29 20:51:38 -04:00
|
|
|
{provide: 'alwaysFalse', useValue: (a: any) => false}, {
|
|
|
|
provide: 'returnFalseAndNavigate',
|
|
|
|
useFactory: (router: any) => (a: any) => {
|
|
|
|
router.navigate(['blank']);
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
deps: [Router]
|
|
|
|
},
|
2016-08-16 23:21:05 -04:00
|
|
|
{provide: 'alwaysTrue', useValue: (a: any) => true}
|
|
|
|
]
|
|
|
|
});
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('works',
|
|
|
|
fakeAsync(inject(
|
2016-08-16 16:40:28 -04:00
|
|
|
[Router, Location, NgModuleFactoryLoader],
|
|
|
|
(router: Router, location: Location, loader: SpyNgModuleFactoryLoader) => {
|
2016-08-10 18:53:57 -04:00
|
|
|
|
|
|
|
@Component({selector: 'lazy', template: 'lazy-loaded'})
|
|
|
|
class LazyLoadedComponent {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [LazyLoadedComponent],
|
|
|
|
imports:
|
2016-08-16 23:21:05 -04:00
|
|
|
[RouterModule.forChild([{path: 'loaded', component: LazyLoadedComponent}])]
|
2016-08-10 18:53:57 -04:00
|
|
|
})
|
|
|
|
class LoadedModule {
|
|
|
|
}
|
|
|
|
|
|
|
|
loader.stubbedModules = {lazyFalse: LoadedModule, lazyTrue: LoadedModule};
|
2016-08-16 16:40:28 -04:00
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
|
|
|
router.resetConfig([
|
|
|
|
{path: 'lazyFalse', canLoad: ['alwaysFalse'], loadChildren: 'lazyFalse'},
|
|
|
|
{path: 'lazyTrue', canLoad: ['alwaysTrue'], loadChildren: 'lazyTrue'}
|
|
|
|
]);
|
|
|
|
|
|
|
|
const recordedEvents: any[] = [];
|
|
|
|
router.events.forEach(e => recordedEvents.push(e));
|
|
|
|
|
|
|
|
|
|
|
|
// failed navigation
|
2016-08-24 13:20:44 -04:00
|
|
|
router.navigateByUrl('/lazyFalse/loaded');
|
2016-08-10 18:53:57 -04:00
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(location.path()).toEqual('/');
|
|
|
|
|
|
|
|
expectEvents(recordedEvents, [
|
2016-08-24 13:20:44 -04:00
|
|
|
[NavigationStart, '/lazyFalse/loaded'],
|
|
|
|
[NavigationCancel, '/lazyFalse/loaded']
|
2016-08-10 18:53:57 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
recordedEvents.splice(0);
|
|
|
|
|
|
|
|
// successful navigation
|
|
|
|
router.navigateByUrl('/lazyTrue/loaded');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(location.path()).toEqual('/lazyTrue/loaded');
|
|
|
|
|
|
|
|
expectEvents(recordedEvents, [
|
|
|
|
[NavigationStart, '/lazyTrue/loaded'], [RoutesRecognized, '/lazyTrue/loaded'],
|
|
|
|
[NavigationEnd, '/lazyTrue/loaded']
|
|
|
|
]);
|
|
|
|
})));
|
2016-08-29 20:51:38 -04:00
|
|
|
|
|
|
|
it('should support navigating from within the guard',
|
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig([
|
|
|
|
{path: 'lazyFalse', canLoad: ['returnFalseAndNavigate'], loadChildren: 'lazyFalse'},
|
|
|
|
{path: 'blank', component: BlankCmp}
|
|
|
|
]);
|
|
|
|
|
|
|
|
const recordedEvents: any[] = [];
|
|
|
|
router.events.forEach(e => recordedEvents.push(e));
|
|
|
|
|
|
|
|
|
|
|
|
router.navigateByUrl('/lazyFalse/loaded');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(location.path()).toEqual('/blank');
|
|
|
|
|
|
|
|
expectEvents(recordedEvents, [
|
2016-10-28 17:56:08 -04:00
|
|
|
[NavigationStart, '/lazyFalse/loaded'], [NavigationCancel, '/lazyFalse/loaded'],
|
|
|
|
[NavigationStart, '/blank'], [RoutesRecognized, '/blank'],
|
2016-08-29 20:51:38 -04:00
|
|
|
[NavigationEnd, '/blank']
|
|
|
|
]);
|
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
});
|
2016-08-25 06:57:39 -04:00
|
|
|
|
|
|
|
describe('order', () => {
|
|
|
|
|
|
|
|
class Logger {
|
|
|
|
logs: string[] = [];
|
|
|
|
add(thing: string) { this.logs.push(thing); }
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
TestBed.configureTestingModule({
|
|
|
|
providers: [
|
|
|
|
Logger, {
|
|
|
|
provide: 'canActivateChild_parent',
|
|
|
|
useFactory: (logger: Logger) => () => (logger.add('canActivateChild_parent'), true),
|
|
|
|
deps: [Logger]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
provide: 'canActivate_team',
|
|
|
|
useFactory: (logger: Logger) => () => (logger.add('canActivate_team'), true),
|
|
|
|
deps: [Logger]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
provide: 'canDeactivate_team',
|
|
|
|
useFactory: (logger: Logger) => () => (logger.add('canDeactivate_team'), true),
|
|
|
|
deps: [Logger]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call guards in the right order',
|
|
|
|
fakeAsync(inject(
|
|
|
|
[Router, Location, Logger], (router: Router, location: Location, logger: Logger) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig([{
|
|
|
|
path: '',
|
|
|
|
canActivateChild: ['canActivateChild_parent'],
|
|
|
|
children: [{
|
|
|
|
path: 'team/:id',
|
|
|
|
canActivate: ['canActivate_team'],
|
|
|
|
canDeactivate: ['canDeactivate_team'],
|
|
|
|
component: TeamCmp
|
|
|
|
}]
|
|
|
|
}]);
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/22');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/33');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(logger.logs).toEqual([
|
|
|
|
'canActivateChild_parent', 'canActivate_team',
|
|
|
|
|
|
|
|
'canDeactivate_team', 'canActivateChild_parent', 'canActivate_team'
|
|
|
|
]);
|
|
|
|
})));
|
|
|
|
});
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('routerActiveLink', () => {
|
|
|
|
it('should set the class when the link is active (a tag)',
|
2016-08-16 16:40:28 -04:00
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig([{
|
|
|
|
path: 'team/:id',
|
|
|
|
component: TeamCmp,
|
|
|
|
children: [{
|
|
|
|
path: 'link',
|
|
|
|
component: DummyLinkCmp,
|
|
|
|
children:
|
|
|
|
[{path: 'simple', component: SimpleCmp}, {path: '', component: BlankCmp}]
|
|
|
|
}]
|
|
|
|
}]);
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/22/link;exact=true');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/22/link;exact=true');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
const nativeLink = fixture.nativeElement.querySelector('a');
|
|
|
|
const nativeButton = fixture.nativeElement.querySelector('button');
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(nativeLink.className).toEqual('active');
|
|
|
|
expect(nativeButton.className).toEqual('active');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/22/link/simple');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/22/link/simple');
|
|
|
|
expect(nativeLink.className).toEqual('');
|
|
|
|
expect(nativeButton.className).toEqual('');
|
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
it('should not set the class until the first navigation succeeds', fakeAsync(() => {
|
|
|
|
@Component({
|
|
|
|
template:
|
2016-09-16 18:08:15 -04:00
|
|
|
'<router-outlet></router-outlet><a routerLink="/" routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}" ></a>'
|
2016-08-16 16:40:28 -04:00
|
|
|
})
|
|
|
|
class RootCmpWithLink {
|
|
|
|
}
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
TestBed.configureTestingModule({declarations: [RootCmpWithLink]});
|
2016-08-16 23:21:05 -04:00
|
|
|
const router: Router = TestBed.get(Router);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
const f = TestBed.createComponent(RootCmpWithLink);
|
|
|
|
advance(f);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
const link = f.nativeElement.querySelector('a');
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(link.className).toEqual('');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.initialNavigation();
|
|
|
|
advance(f);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(link.className).toEqual('active');
|
|
|
|
}));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
|
|
|
|
|
|
|
it('should set the class on a parent element when the link is active',
|
2016-08-16 16:40:28 -04:00
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig([{
|
|
|
|
path: 'team/:id',
|
|
|
|
component: TeamCmp,
|
|
|
|
children: [{
|
|
|
|
path: 'link',
|
|
|
|
component: DummyLinkWithParentCmp,
|
|
|
|
children:
|
|
|
|
[{path: 'simple', component: SimpleCmp}, {path: '', component: BlankCmp}]
|
|
|
|
}]
|
|
|
|
}]);
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/22/link;exact=true');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/22/link;exact=true');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
const native = fixture.nativeElement.querySelector('#link-parent');
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(native.className).toEqual('active');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/22/link/simple');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/22/link/simple');
|
|
|
|
expect(native.className).toEqual('');
|
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
|
|
|
it('should set the class when the link is active',
|
2016-08-16 16:40:28 -04:00
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig([{
|
|
|
|
path: 'team/:id',
|
|
|
|
component: TeamCmp,
|
|
|
|
children: [{
|
|
|
|
path: 'link',
|
|
|
|
component: DummyLinkCmp,
|
|
|
|
children:
|
|
|
|
[{path: 'simple', component: SimpleCmp}, {path: '', component: BlankCmp}]
|
|
|
|
}]
|
|
|
|
}]);
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/22/link');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/22/link');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-09-09 15:04:38 -04:00
|
|
|
const native = fixture.nativeElement.querySelector('a');
|
2016-08-16 16:40:28 -04:00
|
|
|
expect(native.className).toEqual('active');
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
router.navigateByUrl('/team/22/link/simple');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/22/link/simple');
|
|
|
|
expect(native.className).toEqual('active');
|
|
|
|
})));
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-10-25 15:51:24 -04:00
|
|
|
|
|
|
|
it('should expose an isActive property', fakeAsync(() => {
|
|
|
|
@Component({
|
|
|
|
template: `<a routerLink="/team" routerLinkActive #rla="routerLinkActive"></a>
|
|
|
|
<p>{{rla.isActive}}</p>
|
|
|
|
<router-outlet></router-outlet>`
|
|
|
|
})
|
|
|
|
class ComponentWithRouterLink {
|
|
|
|
}
|
|
|
|
|
|
|
|
TestBed.configureTestingModule({declarations: [ComponentWithRouterLink]});
|
|
|
|
const router: Router = TestBed.get(Router);
|
|
|
|
|
|
|
|
router.resetConfig([
|
|
|
|
{
|
|
|
|
path: 'team',
|
|
|
|
component: TeamCmp,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: 'otherteam',
|
|
|
|
component: TeamCmp,
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
|
|
|
const f = TestBed.createComponent(ComponentWithRouterLink);
|
|
|
|
router.navigateByUrl('/team');
|
|
|
|
advance(f);
|
|
|
|
|
|
|
|
const paragraph = f.nativeElement.querySelector('p');
|
|
|
|
expect(paragraph.textContent).toEqual('true');
|
|
|
|
|
|
|
|
router.navigateByUrl('/otherteam');
|
|
|
|
advance(f);
|
|
|
|
|
|
|
|
expect(paragraph.textContent).toEqual('false');
|
|
|
|
}));
|
|
|
|
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('lazy loading', () => {
|
2016-09-09 15:04:38 -04:00
|
|
|
it('works',
|
|
|
|
fakeAsync(inject(
|
|
|
|
[Router, Location, NgModuleFactoryLoader],
|
|
|
|
(router: Router, location: Location, loader: SpyNgModuleFactoryLoader) => {
|
|
|
|
@Component({
|
|
|
|
selector: 'lazy',
|
|
|
|
template: 'lazy-loaded-parent [<router-outlet></router-outlet>]'
|
|
|
|
})
|
|
|
|
class ParentLazyLoadedComponent {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'lazy', template: 'lazy-loaded-child'})
|
|
|
|
class ChildLazyLoadedComponent {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [ParentLazyLoadedComponent, ChildLazyLoadedComponent],
|
|
|
|
imports: [RouterModule.forChild([{
|
|
|
|
path: 'loaded',
|
|
|
|
component: ParentLazyLoadedComponent,
|
|
|
|
children: [{path: 'child', component: ChildLazyLoadedComponent}]
|
|
|
|
}])]
|
|
|
|
})
|
|
|
|
class LoadedModule {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
loader.stubbedModules = {expected: LoadedModule};
|
|
|
|
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig([{path: 'lazy', loadChildren: 'expected'}]);
|
|
|
|
|
|
|
|
router.navigateByUrl('/lazy/loaded/child');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(location.path()).toEqual('/lazy/loaded/child');
|
|
|
|
expect(fixture.nativeElement).toHaveText('lazy-loaded-parent [lazy-loaded-child]');
|
|
|
|
})));
|
2016-09-16 18:08:15 -04:00
|
|
|
|
2016-08-23 14:57:58 -04:00
|
|
|
it('throws an error when forRoot() is used in a lazy context',
|
|
|
|
fakeAsync(inject(
|
|
|
|
[Router, Location, NgModuleFactoryLoader],
|
|
|
|
(router: Router, location: Location, loader: SpyNgModuleFactoryLoader) => {
|
|
|
|
@Component({selector: 'lazy', template: 'should not show'})
|
|
|
|
class LazyLoadedComponent {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [LazyLoadedComponent],
|
|
|
|
imports: [RouterModule.forRoot([{path: 'loaded', component: LazyLoadedComponent}])]
|
|
|
|
})
|
|
|
|
class LoadedModule {
|
|
|
|
}
|
2016-08-10 18:53:57 -04:00
|
|
|
|
2016-08-23 14:57:58 -04:00
|
|
|
loader.stubbedModules = {expected: LoadedModule};
|
|
|
|
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig([{path: 'lazy', loadChildren: 'expected'}]);
|
|
|
|
|
|
|
|
let recordedError: any = null;
|
|
|
|
router.navigateByUrl('/lazy/loaded').catch(err => recordedError = err);
|
|
|
|
advance(fixture);
|
|
|
|
expect(recordedError.message)
|
|
|
|
.toEqual(
|
|
|
|
`RouterModule.forRoot() called twice. Lazy loaded modules should use RouterModule.forChild() instead.`);
|
|
|
|
})));
|
2016-10-19 15:23:00 -04:00
|
|
|
|
2016-08-10 18:53:57 -04:00
|
|
|
it('should combine routes from multiple modules into a single configuration',
|
|
|
|
fakeAsync(inject(
|
2016-08-16 16:40:28 -04:00
|
|
|
[Router, Location, NgModuleFactoryLoader],
|
|
|
|
(router: Router, location: Location, loader: SpyNgModuleFactoryLoader) => {
|
2016-08-10 18:53:57 -04:00
|
|
|
@Component({selector: 'lazy', template: 'lazy-loaded-2'})
|
|
|
|
class LazyComponent2 {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [LazyComponent2],
|
2016-08-16 23:21:05 -04:00
|
|
|
imports: [RouterModule.forChild([{path: 'loaded', component: LazyComponent2}])]
|
2016-08-10 18:53:57 -04:00
|
|
|
})
|
|
|
|
class SiblingOfLoadedModule {
|
|
|
|
}
|
|
|
|
|
2016-08-16 23:21:05 -04:00
|
|
|
@Component({selector: 'lazy', template: 'lazy-loaded-1'})
|
2016-08-10 18:53:57 -04:00
|
|
|
class LazyComponent1 {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [LazyComponent1],
|
|
|
|
imports: [
|
|
|
|
RouterModule.forChild([{path: 'loaded', component: LazyComponent1}]),
|
|
|
|
SiblingOfLoadedModule
|
2016-08-16 23:21:05 -04:00
|
|
|
]
|
2016-08-10 18:53:57 -04:00
|
|
|
})
|
|
|
|
class LoadedModule {
|
|
|
|
}
|
|
|
|
|
|
|
|
loader.stubbedModules = {expected1: LoadedModule, expected2: SiblingOfLoadedModule};
|
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
|
|
|
router.resetConfig([
|
|
|
|
{path: 'lazy1', loadChildren: 'expected1'},
|
|
|
|
{path: 'lazy2', loadChildren: 'expected2'}
|
|
|
|
]);
|
|
|
|
|
|
|
|
router.navigateByUrl('/lazy1/loaded');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/lazy1/loaded');
|
|
|
|
|
|
|
|
router.navigateByUrl('/lazy2/loaded');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/lazy2/loaded');
|
|
|
|
})));
|
|
|
|
|
|
|
|
it('should use the injector of the lazily-loaded configuration',
|
|
|
|
fakeAsync(inject(
|
2016-08-16 16:40:28 -04:00
|
|
|
[Router, Location, NgModuleFactoryLoader],
|
|
|
|
(router: Router, location: Location, loader: SpyNgModuleFactoryLoader) => {
|
2016-08-10 18:53:57 -04:00
|
|
|
class LazyLoadedServiceDefinedInModule {}
|
|
|
|
class LazyLoadedServiceDefinedInCmp {}
|
|
|
|
|
|
|
|
@Component({selector: 'lazy', template: 'lazy-loaded'})
|
|
|
|
class LazyLoadedChildComponent {
|
|
|
|
constructor(service: LazyLoadedServiceDefinedInCmp) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'lazy',
|
|
|
|
template: '<router-outlet></router-outlet>',
|
|
|
|
providers: [LazyLoadedServiceDefinedInCmp]
|
|
|
|
})
|
|
|
|
class LazyLoadedParentComponent {
|
|
|
|
constructor(service: LazyLoadedServiceDefinedInModule) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [LazyLoadedParentComponent, LazyLoadedChildComponent],
|
|
|
|
imports: [RouterModule.forChild([{
|
|
|
|
path: '',
|
|
|
|
children: [{
|
|
|
|
path: 'loaded',
|
|
|
|
component: LazyLoadedParentComponent,
|
|
|
|
children: [{path: 'child', component: LazyLoadedChildComponent}]
|
|
|
|
}]
|
|
|
|
}])],
|
|
|
|
providers: [LazyLoadedServiceDefinedInModule]
|
|
|
|
})
|
|
|
|
class LoadedModule {
|
|
|
|
}
|
|
|
|
|
|
|
|
loader.stubbedModules = {expected: LoadedModule};
|
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
|
|
|
router.resetConfig([{path: 'lazy', loadChildren: 'expected'}]);
|
|
|
|
|
|
|
|
router.navigateByUrl('/lazy/loaded/child');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(location.path()).toEqual('/lazy/loaded/child');
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('lazy-loaded');
|
2016-08-10 18:53:57 -04:00
|
|
|
})));
|
|
|
|
|
2016-08-16 00:11:09 -04:00
|
|
|
it('works when given a callback',
|
|
|
|
fakeAsync(inject(
|
2016-08-16 16:40:28 -04:00
|
|
|
[Router, Location, NgModuleFactoryLoader], (router: Router, location: Location) => {
|
2016-08-16 00:11:09 -04:00
|
|
|
@Component({selector: 'lazy', template: 'lazy-loaded'})
|
|
|
|
class LazyLoadedComponent {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [LazyLoadedComponent],
|
|
|
|
imports: [RouterModule.forChild([{path: 'loaded', component: LazyLoadedComponent}])],
|
|
|
|
})
|
|
|
|
class LoadedModule {
|
|
|
|
}
|
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-16 00:11:09 -04:00
|
|
|
|
|
|
|
router.resetConfig([{path: 'lazy', loadChildren: () => LoadedModule}]);
|
|
|
|
|
|
|
|
router.navigateByUrl('/lazy/loaded');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(location.path()).toEqual('/lazy/loaded');
|
2016-09-09 15:04:38 -04:00
|
|
|
expect(fixture.nativeElement).toHaveText('lazy-loaded');
|
2016-08-16 00:11:09 -04:00
|
|
|
})));
|
|
|
|
|
2016-08-10 18:53:57 -04:00
|
|
|
it('error emit an error when cannot load a config',
|
|
|
|
fakeAsync(inject(
|
2016-08-16 16:40:28 -04:00
|
|
|
[Router, Location, NgModuleFactoryLoader],
|
|
|
|
(router: Router, location: Location, loader: SpyNgModuleFactoryLoader) => {
|
2016-08-10 18:53:57 -04:00
|
|
|
loader.stubbedModules = {};
|
2016-08-16 16:40:28 -04:00
|
|
|
const fixture = createRoot(router, RootCmp);
|
2016-08-10 18:53:57 -04:00
|
|
|
|
|
|
|
router.resetConfig([{path: 'lazy', loadChildren: 'invalid'}]);
|
|
|
|
|
|
|
|
const recordedEvents: any[] = [];
|
|
|
|
router.events.forEach(e => recordedEvents.push(e));
|
|
|
|
|
|
|
|
router.navigateByUrl('/lazy/loaded').catch(s => {});
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(location.path()).toEqual('/');
|
|
|
|
|
|
|
|
expectEvents(
|
|
|
|
recordedEvents,
|
|
|
|
[[NavigationStart, '/lazy/loaded'], [NavigationError, '/lazy/loaded']]);
|
|
|
|
})));
|
2016-09-16 18:08:15 -04:00
|
|
|
|
2016-10-19 15:23:00 -04:00
|
|
|
it('should work with complex redirect rules',
|
|
|
|
fakeAsync(inject(
|
|
|
|
[Router, Location, NgModuleFactoryLoader],
|
|
|
|
(router: Router, location: Location, loader: SpyNgModuleFactoryLoader) => {
|
|
|
|
@Component({selector: 'lazy', template: 'lazy-loaded'})
|
|
|
|
class LazyLoadedComponent {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [LazyLoadedComponent],
|
|
|
|
imports: [RouterModule.forChild([{path: 'loaded', component: LazyLoadedComponent}])],
|
|
|
|
})
|
|
|
|
class LoadedModule {
|
|
|
|
}
|
|
|
|
|
|
|
|
loader.stubbedModules = {lazy: LoadedModule};
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig(
|
|
|
|
[{path: 'lazy', loadChildren: 'lazy'}, {path: '**', redirectTo: 'lazy'}]);
|
|
|
|
|
|
|
|
router.navigateByUrl('/lazy/loaded');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(location.path()).toEqual('/lazy/loaded');
|
|
|
|
})));
|
|
|
|
|
2016-09-16 18:08:15 -04:00
|
|
|
describe('preloading', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
TestBed.configureTestingModule(
|
|
|
|
{providers: [{provide: PreloadingStrategy, useExisting: PreloadAllModules}]});
|
|
|
|
const preloader = TestBed.get(RouterPreloader);
|
|
|
|
preloader.setUpPreloading();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work',
|
|
|
|
fakeAsync(inject(
|
|
|
|
[Router, Location, NgModuleFactoryLoader],
|
|
|
|
(router: Router, location: Location, loader: SpyNgModuleFactoryLoader) => {
|
|
|
|
@Component({selector: 'lazy', template: 'should not show'})
|
|
|
|
class LazyLoadedComponent {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [LazyLoadedComponent],
|
|
|
|
imports: [RouterModule.forChild(
|
|
|
|
[{path: 'LoadedModule2', component: LazyLoadedComponent}])]
|
|
|
|
})
|
|
|
|
class LoadedModule2 {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
imports:
|
|
|
|
[RouterModule.forChild([{path: 'LoadedModule1', loadChildren: 'expected2'}])]
|
|
|
|
})
|
|
|
|
class LoadedModule1 {
|
|
|
|
}
|
|
|
|
|
|
|
|
loader.stubbedModules = {expected: LoadedModule1, expected2: LoadedModule2};
|
|
|
|
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig([
|
|
|
|
{path: 'blank', component: BlankCmp}, {path: 'lazy', loadChildren: 'expected'}
|
|
|
|
]);
|
|
|
|
|
|
|
|
router.navigateByUrl('/blank');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
const config: any = router.config;
|
|
|
|
const firstConfig = config[1]._loadedConfig;
|
2016-10-28 17:56:08 -04:00
|
|
|
|
2016-09-16 18:08:15 -04:00
|
|
|
expect(firstConfig).toBeDefined();
|
|
|
|
expect(firstConfig.routes[0].path).toEqual('LoadedModule1');
|
|
|
|
|
|
|
|
const secondConfig = firstConfig.routes[0]._loadedConfig;
|
|
|
|
expect(secondConfig).toBeDefined();
|
|
|
|
expect(secondConfig.routes[0].path).toEqual('LoadedModule2');
|
|
|
|
})));
|
|
|
|
|
|
|
|
});
|
2016-10-20 13:44:44 -04:00
|
|
|
|
|
|
|
describe('custom url handling strategies', () => {
|
|
|
|
class CustomUrlHandlingStrategy implements UrlHandlingStrategy {
|
|
|
|
shouldProcessUrl(url: UrlTree): boolean {
|
|
|
|
return url.toString().startsWith('/include') || url.toString() === '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
extract(url: UrlTree): UrlTree {
|
|
|
|
const oldRoot = url.root;
|
2016-10-28 17:56:08 -04:00
|
|
|
const children: any = {};
|
|
|
|
if (oldRoot.children[PRIMARY_OUTLET]) {
|
|
|
|
children[PRIMARY_OUTLET] = oldRoot.children[PRIMARY_OUTLET];
|
|
|
|
}
|
|
|
|
const root = new UrlSegmentGroup(oldRoot.segments, children);
|
2016-10-20 13:44:44 -04:00
|
|
|
return new UrlTree(root, url.queryParams, url.fragment);
|
|
|
|
}
|
|
|
|
|
|
|
|
merge(newUrlPart: UrlTree, wholeUrl: UrlTree): UrlTree {
|
|
|
|
const oldRoot = newUrlPart.root;
|
|
|
|
|
|
|
|
const children: any = {};
|
|
|
|
if (oldRoot.children[PRIMARY_OUTLET]) {
|
|
|
|
children[PRIMARY_OUTLET] = oldRoot.children[PRIMARY_OUTLET];
|
|
|
|
}
|
|
|
|
|
|
|
|
forEach(wholeUrl.root.children, (v: any, k: any) => {
|
|
|
|
if (k !== PRIMARY_OUTLET) {
|
|
|
|
children[k] = v;
|
|
|
|
}
|
|
|
|
v.parent = this;
|
|
|
|
});
|
|
|
|
const root = new UrlSegmentGroup(oldRoot.segments, children);
|
|
|
|
return new UrlTree(root, newUrlPart.queryParams, newUrlPart.fragment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
TestBed.configureTestingModule(
|
|
|
|
{providers: [{provide: UrlHandlingStrategy, useClass: CustomUrlHandlingStrategy}]});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should work',
|
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig([{
|
|
|
|
path: 'include',
|
|
|
|
component: TeamCmp,
|
|
|
|
children: [
|
|
|
|
{path: 'user/:name', component: UserCmp}, {path: 'simple', component: SimpleCmp}
|
|
|
|
]
|
|
|
|
}]);
|
|
|
|
|
|
|
|
let events: any[] = [];
|
|
|
|
router.events.subscribe(e => events.push(e));
|
|
|
|
|
|
|
|
// supported URL
|
|
|
|
router.navigateByUrl('/include/user/kate');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(location.path()).toEqual('/include/user/kate');
|
|
|
|
expectEvents(events, [
|
|
|
|
[NavigationStart, '/include/user/kate'], [RoutesRecognized, '/include/user/kate'],
|
|
|
|
[NavigationEnd, '/include/user/kate']
|
|
|
|
]);
|
|
|
|
expect(fixture.nativeElement).toHaveText('team [ user kate, right: ]');
|
|
|
|
events.splice(0);
|
|
|
|
|
|
|
|
// unsupported URL
|
|
|
|
router.navigateByUrl('/exclude/one');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(location.path()).toEqual('/exclude/one');
|
|
|
|
expect(Object.keys(router.routerState.root.children).length).toEqual(0);
|
|
|
|
expect(fixture.nativeElement).toHaveText('');
|
|
|
|
expectEvents(
|
|
|
|
events, [[NavigationStart, '/exclude/one'], [NavigationEnd, '/exclude/one']]);
|
|
|
|
events.splice(0);
|
|
|
|
|
|
|
|
// another unsupported URL
|
|
|
|
location.go('/exclude/two');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(location.path()).toEqual('/exclude/two');
|
|
|
|
expectEvents(events, []);
|
|
|
|
|
|
|
|
// back to a supported URL
|
|
|
|
location.go('/include/simple');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(location.path()).toEqual('/include/simple');
|
|
|
|
expect(fixture.nativeElement).toHaveText('team [ simple, right: ]');
|
|
|
|
|
|
|
|
expectEvents(events, [
|
|
|
|
[NavigationStart, '/include/simple'], [RoutesRecognized, '/include/simple'],
|
|
|
|
[NavigationEnd, '/include/simple']
|
|
|
|
]);
|
|
|
|
})));
|
|
|
|
|
|
|
|
it('should handle the case when the router takes only the primary url',
|
|
|
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
|
|
const fixture = createRoot(router, RootCmp);
|
|
|
|
|
|
|
|
router.resetConfig([{
|
|
|
|
path: 'include',
|
|
|
|
component: TeamCmp,
|
|
|
|
children: [
|
|
|
|
{path: 'user/:name', component: UserCmp}, {path: 'simple', component: SimpleCmp}
|
|
|
|
]
|
|
|
|
}]);
|
|
|
|
|
|
|
|
let events: any[] = [];
|
|
|
|
router.events.subscribe(e => events.push(e));
|
|
|
|
|
|
|
|
location.go('/include/user/kate(aux:excluded)');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(location.path()).toEqual('/include/user/kate(aux:excluded)');
|
|
|
|
expectEvents(events, [
|
|
|
|
[NavigationStart, '/include/user/kate'], [RoutesRecognized, '/include/user/kate'],
|
|
|
|
[NavigationEnd, '/include/user/kate']
|
|
|
|
]);
|
|
|
|
events.splice(0);
|
|
|
|
|
|
|
|
location.go('/include/user/kate(aux:excluded2)');
|
|
|
|
advance(fixture);
|
|
|
|
expectEvents(events, []);
|
|
|
|
|
|
|
|
router.navigateByUrl('/include/simple');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(location.path()).toEqual('/include/simple(aux:excluded2)');
|
|
|
|
expectEvents(events, [
|
|
|
|
[NavigationStart, '/include/simple'], [RoutesRecognized, '/include/simple'],
|
|
|
|
[NavigationEnd, '/include/simple']
|
|
|
|
]);
|
|
|
|
})));
|
|
|
|
});
|
2016-08-10 18:53:57 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function expectEvents(events: Event[], pairs: any[]) {
|
2016-10-20 13:44:44 -04:00
|
|
|
expect(events.length).toEqual(pairs.length);
|
2016-08-10 18:53:57 -04:00
|
|
|
for (let i = 0; i < events.length; ++i) {
|
|
|
|
expect((<any>events[i].constructor).name).toBe(pairs[i][0].name);
|
|
|
|
expect((<any>events[i]).url).toBe(pairs[i][1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 23:21:05 -04:00
|
|
|
@Component({selector: 'link-cmp', template: `<a routerLink="/team/33/simple">link</a>`})
|
2016-08-10 18:53:57 -04:00
|
|
|
class StringLinkCmp {
|
|
|
|
}
|
|
|
|
|
2016-10-20 13:44:44 -04:00
|
|
|
@Component({selector: 'link-cmp', template: `<button routerLink="/team/33/simple">link</button>`})
|
2016-08-10 18:53:57 -04:00
|
|
|
class StringLinkButtonCmp {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'link-cmp',
|
2016-08-16 23:21:05 -04:00
|
|
|
template: `<router-outlet></router-outlet><a [routerLink]="['/team/33/simple']">link</a>`
|
2016-08-10 18:53:57 -04:00
|
|
|
})
|
|
|
|
class AbsoluteLinkCmp {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'link-cmp',
|
|
|
|
template:
|
|
|
|
`<router-outlet></router-outlet><a routerLinkActive="active" [routerLinkActiveOptions]="{exact: exact}" [routerLink]="['./']">link</a>
|
|
|
|
<button routerLinkActive="active" [routerLinkActiveOptions]="{exact: exact}" [routerLink]="['./']">button</button>
|
2016-08-16 23:21:05 -04:00
|
|
|
`
|
2016-08-10 18:53:57 -04:00
|
|
|
})
|
|
|
|
class DummyLinkCmp {
|
|
|
|
private exact: boolean;
|
|
|
|
constructor(route: ActivatedRoute) { this.exact = (<any>route.snapshot.params).exact === 'true'; }
|
|
|
|
}
|
|
|
|
|
2016-08-16 23:21:05 -04:00
|
|
|
@Component({selector: 'link-cmp', template: `<a [routerLink]="['../simple']">link</a>`})
|
2016-08-10 18:53:57 -04:00
|
|
|
class RelativeLinkCmp {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'link-cmp',
|
2016-08-16 23:21:05 -04:00
|
|
|
template: `<a [routerLink]="['../simple']" [queryParams]="{q: '1'}" fragment="f">link</a>`
|
2016-08-10 18:53:57 -04:00
|
|
|
})
|
|
|
|
class LinkWithQueryParamsAndFragment {
|
|
|
|
}
|
|
|
|
|
2016-08-16 23:21:05 -04:00
|
|
|
@Component({selector: 'simple-cmp', template: `simple`})
|
2016-08-10 18:53:57 -04:00
|
|
|
class SimpleCmp {
|
|
|
|
}
|
|
|
|
|
2016-08-16 23:21:05 -04:00
|
|
|
@Component({selector: 'collect-params-cmp', template: `collect-params`})
|
2016-08-10 18:53:57 -04:00
|
|
|
class CollectParamsCmp {
|
|
|
|
private params: any = [];
|
|
|
|
private urls: any = [];
|
|
|
|
|
2016-10-25 17:33:18 -04:00
|
|
|
constructor(private route: ActivatedRoute) {
|
|
|
|
route.params.forEach(p => this.params.push(p));
|
|
|
|
route.url.forEach(u => this.urls.push(u));
|
2016-08-10 18:53:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
recordedUrls(): string[] {
|
|
|
|
return this.urls.map((a: any) => a.map((p: any) => p.path).join('/'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 23:21:05 -04:00
|
|
|
@Component({selector: 'blank-cmp', template: ``})
|
2016-08-10 18:53:57 -04:00
|
|
|
class BlankCmp {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'team-cmp',
|
|
|
|
template:
|
2016-08-16 23:21:05 -04:00
|
|
|
`team {{id | async}} [ <router-outlet></router-outlet>, right: <router-outlet name="right"></router-outlet> ]`
|
2016-08-10 18:53:57 -04:00
|
|
|
})
|
|
|
|
class TeamCmp {
|
|
|
|
id: Observable<string>;
|
|
|
|
recordedParams: Params[] = [];
|
|
|
|
|
|
|
|
constructor(public route: ActivatedRoute) {
|
2016-08-30 17:25:46 -04:00
|
|
|
this.id = map.call(route.params, (p: any) => p['id']);
|
2016-08-10 18:53:57 -04:00
|
|
|
route.params.forEach(_ => this.recordedParams.push(_));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-20 17:00:02 -04:00
|
|
|
@Component({
|
|
|
|
selector: 'two-outlets-cmp',
|
|
|
|
template: `[ <router-outlet></router-outlet>, aux: <router-outlet name="aux"></router-outlet> ]`
|
|
|
|
})
|
|
|
|
class TwoOutletsCmp {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-16 23:21:05 -04:00
|
|
|
@Component({selector: 'user-cmp', template: `user {{name | async}}`})
|
2016-08-10 18:53:57 -04:00
|
|
|
class UserCmp {
|
|
|
|
name: Observable<string>;
|
|
|
|
recordedParams: Params[] = [];
|
|
|
|
|
|
|
|
constructor(route: ActivatedRoute) {
|
2016-08-30 17:25:46 -04:00
|
|
|
this.name = map.call(route.params, (p: any) => p['name']);
|
2016-08-10 18:53:57 -04:00
|
|
|
route.params.forEach(_ => this.recordedParams.push(_));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 23:21:05 -04:00
|
|
|
@Component({selector: 'wrapper', template: `<router-outlet></router-outlet>`})
|
2016-08-10 18:53:57 -04:00
|
|
|
class WrapperCmp {
|
|
|
|
}
|
|
|
|
|
2016-08-16 23:21:05 -04:00
|
|
|
@Component(
|
|
|
|
{selector: 'query-cmp', template: `query: {{name | async}} fragment: {{fragment | async}}`})
|
2016-08-10 18:53:57 -04:00
|
|
|
class QueryParamsAndFragmentCmp {
|
|
|
|
name: Observable<string>;
|
|
|
|
fragment: Observable<string>;
|
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
constructor(route: ActivatedRoute) {
|
2016-08-30 17:25:46 -04:00
|
|
|
this.name = map.call(route.queryParams, (p: any) => p['name']);
|
2016-08-16 16:40:28 -04:00
|
|
|
this.fragment = route.fragment;
|
2016-08-10 18:53:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 23:21:05 -04:00
|
|
|
@Component({selector: 'route-cmp', template: `route`})
|
2016-08-10 18:53:57 -04:00
|
|
|
class RouteCmp {
|
|
|
|
constructor(public route: ActivatedRoute) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'link-cmp',
|
|
|
|
template:
|
2016-08-16 23:21:05 -04:00
|
|
|
`<div *ngIf="show"><a [routerLink]="['./simple']">link</a></div> <router-outlet></router-outlet>`
|
2016-08-10 18:53:57 -04:00
|
|
|
})
|
|
|
|
class RelativeLinkInIfCmp {
|
|
|
|
show: boolean = false;
|
|
|
|
}
|
|
|
|
|
2016-08-16 23:21:05 -04:00
|
|
|
@Component(
|
|
|
|
{selector: 'child', template: '<div *ngIf="alwaysTrue"><router-outlet></router-outlet></div>'})
|
2016-08-10 18:53:57 -04:00
|
|
|
class LinkInNgIf {
|
|
|
|
alwaysTrue = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'link-cmp',
|
|
|
|
template: `<router-outlet></router-outlet>
|
2016-08-23 13:52:40 -04:00
|
|
|
<div id="link-parent" routerLinkActive="active" [routerLinkActiveOptions]="{exact: exact}">
|
|
|
|
<div ngClass="{one: 'true'}"><a [routerLink]="['./']">link</a></div>
|
|
|
|
</div>`
|
2016-08-10 18:53:57 -04:00
|
|
|
})
|
|
|
|
class DummyLinkWithParentCmp {
|
|
|
|
private exact: boolean;
|
|
|
|
constructor(route: ActivatedRoute) { this.exact = (<any>route.snapshot.params).exact === 'true'; }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'cmp', template: ''})
|
|
|
|
class ComponentRecordingRoutePathAndUrl {
|
|
|
|
private path: any;
|
|
|
|
private url: any;
|
|
|
|
|
|
|
|
constructor(router: Router, route: ActivatedRoute) {
|
|
|
|
this.path = router.routerState.pathFromRoot(route);
|
|
|
|
this.url = router.url.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 23:21:05 -04:00
|
|
|
@Component({selector: 'root-cmp', template: `<router-outlet></router-outlet>`})
|
2016-08-10 18:53:57 -04:00
|
|
|
class RootCmp {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'root-cmp',
|
|
|
|
template:
|
2016-08-16 23:21:05 -04:00
|
|
|
`primary [<router-outlet></router-outlet>] right [<router-outlet name="right"></router-outlet>]`
|
2016-08-10 18:53:57 -04:00
|
|
|
})
|
|
|
|
class RootCmpWithTwoOutlets {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function advance(fixture: ComponentFixture<any>): void {
|
|
|
|
tick();
|
|
|
|
fixture.detectChanges();
|
|
|
|
}
|
|
|
|
|
2016-08-16 16:40:28 -04:00
|
|
|
function createRoot(router: Router, type: any): ComponentFixture<any> {
|
|
|
|
const f = TestBed.createComponent(type);
|
2016-08-10 18:53:57 -04:00
|
|
|
advance(f);
|
|
|
|
router.initialNavigation();
|
|
|
|
advance(f);
|
|
|
|
return f;
|
|
|
|
}
|
2016-08-16 23:21:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
imports: [RouterTestingModule, CommonModule],
|
|
|
|
entryComponents: [
|
|
|
|
BlankCmp,
|
|
|
|
SimpleCmp,
|
2016-10-20 17:00:02 -04:00
|
|
|
TwoOutletsCmp,
|
2016-08-16 23:21:05 -04:00
|
|
|
TeamCmp,
|
|
|
|
UserCmp,
|
|
|
|
StringLinkCmp,
|
|
|
|
DummyLinkCmp,
|
|
|
|
AbsoluteLinkCmp,
|
|
|
|
RelativeLinkCmp,
|
|
|
|
DummyLinkWithParentCmp,
|
|
|
|
LinkWithQueryParamsAndFragment,
|
|
|
|
CollectParamsCmp,
|
|
|
|
QueryParamsAndFragmentCmp,
|
|
|
|
StringLinkButtonCmp,
|
|
|
|
WrapperCmp,
|
|
|
|
LinkInNgIf,
|
|
|
|
ComponentRecordingRoutePathAndUrl,
|
|
|
|
RouteCmp,
|
|
|
|
RootCmp,
|
|
|
|
RelativeLinkInIfCmp,
|
|
|
|
RootCmpWithTwoOutlets
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
|
|
exports: [
|
|
|
|
BlankCmp,
|
|
|
|
SimpleCmp,
|
2016-10-20 17:00:02 -04:00
|
|
|
TwoOutletsCmp,
|
2016-08-16 23:21:05 -04:00
|
|
|
TeamCmp,
|
|
|
|
UserCmp,
|
|
|
|
StringLinkCmp,
|
|
|
|
DummyLinkCmp,
|
|
|
|
AbsoluteLinkCmp,
|
|
|
|
RelativeLinkCmp,
|
|
|
|
DummyLinkWithParentCmp,
|
|
|
|
LinkWithQueryParamsAndFragment,
|
|
|
|
CollectParamsCmp,
|
|
|
|
QueryParamsAndFragmentCmp,
|
|
|
|
StringLinkButtonCmp,
|
|
|
|
WrapperCmp,
|
|
|
|
LinkInNgIf,
|
|
|
|
ComponentRecordingRoutePathAndUrl,
|
|
|
|
RouteCmp,
|
|
|
|
RootCmp,
|
|
|
|
RelativeLinkInIfCmp,
|
|
|
|
RootCmpWithTwoOutlets
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
declarations: [
|
|
|
|
BlankCmp,
|
|
|
|
SimpleCmp,
|
|
|
|
TeamCmp,
|
2016-10-20 17:00:02 -04:00
|
|
|
TwoOutletsCmp,
|
2016-08-16 23:21:05 -04:00
|
|
|
UserCmp,
|
|
|
|
StringLinkCmp,
|
|
|
|
DummyLinkCmp,
|
|
|
|
AbsoluteLinkCmp,
|
|
|
|
RelativeLinkCmp,
|
|
|
|
DummyLinkWithParentCmp,
|
|
|
|
LinkWithQueryParamsAndFragment,
|
|
|
|
CollectParamsCmp,
|
|
|
|
QueryParamsAndFragmentCmp,
|
|
|
|
StringLinkButtonCmp,
|
|
|
|
WrapperCmp,
|
|
|
|
LinkInNgIf,
|
|
|
|
ComponentRecordingRoutePathAndUrl,
|
|
|
|
RouteCmp,
|
|
|
|
RootCmp,
|
|
|
|
RelativeLinkInIfCmp,
|
|
|
|
RootCmpWithTwoOutlets
|
|
|
|
]
|
|
|
|
})
|
|
|
|
class TestModule {
|
2016-10-25 15:51:24 -04:00
|
|
|
}
|