2016-04-22 15:05:38 -04:00
|
|
|
import {
|
|
|
|
AsyncTestCompleter,
|
|
|
|
beforeEach,
|
|
|
|
ddescribe,
|
|
|
|
xdescribe,
|
|
|
|
describe,
|
|
|
|
expect,
|
|
|
|
iit,
|
|
|
|
inject,
|
|
|
|
beforeEachProviders,
|
|
|
|
it,
|
2016-05-02 13:36:58 -04:00
|
|
|
xit
|
|
|
|
} from '@angular/core/testing/testing_internal';
|
|
|
|
import {fakeAsync, tick} from '@angular/core/testing';
|
|
|
|
import {ComponentFixture, TestComponentBuilder} from '@angular/compiler/testing';
|
|
|
|
import {provide, Component, ComponentResolver} from '@angular/core';
|
|
|
|
import {PromiseWrapper} from '../src/facade/async';
|
2016-04-22 15:05:38 -04:00
|
|
|
|
2016-04-27 18:37:20 -04:00
|
|
|
|
2016-04-22 15:05:38 -04:00
|
|
|
import {
|
|
|
|
Router,
|
|
|
|
RouterOutletMap,
|
|
|
|
RouteSegment,
|
|
|
|
Route,
|
|
|
|
ROUTER_DIRECTIVES,
|
|
|
|
Routes,
|
2016-04-27 18:37:20 -04:00
|
|
|
RouterUrlSerializer,
|
|
|
|
DefaultRouterUrlSerializer,
|
2016-04-29 21:04:55 -04:00
|
|
|
OnActivate,
|
|
|
|
CanDeactivate
|
2016-05-02 13:36:58 -04:00
|
|
|
} from '@angular/router';
|
|
|
|
import {SpyLocation} from '@angular/common/testing';
|
|
|
|
import {Location} from '@angular/common';
|
|
|
|
import {getDOM} from '../platform_browser_private';
|
2016-04-22 15:05:38 -04:00
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('navigation', () => {
|
|
|
|
beforeEachProviders(() => [
|
2016-04-27 18:37:20 -04:00
|
|
|
provide(RouterUrlSerializer, {useClass: DefaultRouterUrlSerializer}),
|
2016-04-22 15:05:38 -04:00
|
|
|
RouterOutletMap,
|
2016-04-28 11:01:27 -04:00
|
|
|
provide(Location, {useClass: SpyLocation}),
|
2016-05-04 17:40:17 -04:00
|
|
|
provide(RouteSegment, {useFactory: (r) => r.routeTree.root, deps: [Router]}),
|
2016-04-22 15:05:38 -04:00
|
|
|
provide(Router,
|
|
|
|
{
|
2016-04-29 21:04:55 -04:00
|
|
|
useFactory: (resolver, urlParser, outletMap, location) => new Router(
|
|
|
|
"RootComponent", RootCmp, resolver, urlParser, outletMap, location),
|
2016-04-28 11:01:27 -04:00
|
|
|
deps: [ComponentResolver, RouterUrlSerializer, RouterOutletMap, Location]
|
2016-04-22 15:05:38 -04:00
|
|
|
})
|
|
|
|
]);
|
|
|
|
|
2016-04-28 11:01:27 -04:00
|
|
|
it('should update location when navigating',
|
|
|
|
fakeAsync(inject([Router, TestComponentBuilder, Location], (router, tcb, location) => {
|
|
|
|
let fixture = tcb.createFakeAsync(RootCmp);
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/22/user/victor');
|
|
|
|
advance(fixture);
|
|
|
|
expect(location.path()).toEqual('/team/22/user/victor');
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/33/simple');
|
|
|
|
advance(fixture);
|
2016-04-28 21:33:48 -04:00
|
|
|
|
2016-04-28 11:01:27 -04:00
|
|
|
expect(location.path()).toEqual('/team/33/simple');
|
|
|
|
})));
|
|
|
|
|
2016-04-30 14:57:02 -04:00
|
|
|
it('should navigate when locations changes',
|
|
|
|
fakeAsync(inject([Router, TestComponentBuilder, Location], (router, tcb, location) => {
|
|
|
|
let fixture = tcb.createFakeAsync(RootCmp);
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/22/user/victor');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
location.simulateHashChange("/team/22/user/fedor");
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('team 22 { hello fedor, aux: }');
|
|
|
|
})));
|
|
|
|
|
2016-04-22 15:05:38 -04:00
|
|
|
it('should support nested routes',
|
2016-04-27 18:37:20 -04:00
|
|
|
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
|
|
|
|
let fixture = tcb.createFakeAsync(RootCmp);
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/22/user/victor');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('team 22 { hello victor, aux: }');
|
|
|
|
})));
|
2016-04-25 19:57:27 -04:00
|
|
|
|
|
|
|
it('should support aux routes',
|
2016-04-27 18:37:20 -04:00
|
|
|
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
|
|
|
|
let fixture = tcb.createFakeAsync(RootCmp);
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/22/user/victor(/simple)');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(fixture.debugElement.nativeElement)
|
|
|
|
.toHaveText('team 22 { hello victor, aux: simple }');
|
|
|
|
})));
|
|
|
|
|
2016-05-04 18:01:27 -04:00
|
|
|
it('should deactivate outlets',
|
|
|
|
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
|
2016-04-27 18:37:20 -04:00
|
|
|
let fixture = tcb.createFakeAsync(RootCmp);
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/22/user/victor(/simple)');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/22/user/victor');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('team 22 { hello victor, aux: }');
|
|
|
|
})));
|
2016-04-25 19:57:27 -04:00
|
|
|
|
2016-05-04 13:59:20 -04:00
|
|
|
it('should deactivate nested outlets',
|
2016-04-27 18:37:20 -04:00
|
|
|
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
|
|
|
|
let fixture = tcb.createFakeAsync(RootCmp);
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/22/user/victor(/simple)');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
router.navigateByUrl('/');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('');
|
|
|
|
})));
|
2016-04-22 15:05:38 -04:00
|
|
|
|
|
|
|
it('should update nested routes when url changes',
|
2016-04-27 18:37:20 -04:00
|
|
|
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
|
|
|
|
let fixture = tcb.createFakeAsync(RootCmp);
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/22/user/victor');
|
|
|
|
advance(fixture);
|
|
|
|
let team1 = fixture.debugElement.children[1].componentInstance;
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/22/user/fedor');
|
|
|
|
advance(fixture);
|
|
|
|
let team2 = fixture.debugElement.children[1].componentInstance;
|
|
|
|
|
|
|
|
expect(team1).toBe(team2);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('team 22 { hello fedor, aux: }');
|
|
|
|
})));
|
|
|
|
|
2016-05-04 13:59:20 -04:00
|
|
|
it('should not deactivate the route if can deactivate returns false',
|
2016-04-30 13:33:14 -04:00
|
|
|
fakeAsync(inject([Router, TestComponentBuilder, Location], (router, tcb, location) => {
|
2016-04-29 21:04:55 -04:00
|
|
|
let fixture = tcb.createFakeAsync(RootCmp);
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/22/cannotDeactivate');
|
|
|
|
advance(fixture);
|
2016-04-27 18:37:20 -04:00
|
|
|
|
2016-04-29 21:04:55 -04:00
|
|
|
router.navigateByUrl('/team/22/user/fedor');
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(fixture.debugElement.nativeElement)
|
|
|
|
.toHaveText('team 22 { cannotDeactivate, aux: }');
|
2016-04-30 13:33:14 -04:00
|
|
|
|
|
|
|
expect(location.path()).toEqual('/team/22/cannotDeactivate');
|
2016-04-29 21:04:55 -04:00
|
|
|
})));
|
|
|
|
|
2016-05-02 13:36:58 -04:00
|
|
|
if (getDOM().supportsDOMEvents()) {
|
2016-04-29 21:04:55 -04:00
|
|
|
it("should support absolute router links",
|
2016-04-28 21:33:48 -04:00
|
|
|
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
|
|
|
|
let fixture = tcb.createFakeAsync(RootCmp);
|
|
|
|
advance(fixture);
|
2016-04-27 18:37:20 -04:00
|
|
|
|
2016-04-28 21:33:48 -04:00
|
|
|
router.navigateByUrl('/team/22/link');
|
|
|
|
advance(fixture);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('team 22 { link, aux: }');
|
2016-04-27 18:37:20 -04:00
|
|
|
|
2016-05-02 13:36:58 -04:00
|
|
|
let native = getDOM().querySelector(fixture.debugElement.nativeElement, "a");
|
|
|
|
expect(getDOM().getAttribute(native, "href")).toEqual("/team/33/simple");
|
|
|
|
getDOM().dispatchEvent(native, getDOM().createMouseEvent('click'));
|
2016-04-28 21:33:48 -04:00
|
|
|
advance(fixture);
|
2016-04-27 18:37:20 -04:00
|
|
|
|
2016-04-28 21:33:48 -04:00
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('team 33 { simple, aux: }');
|
|
|
|
})));
|
2016-04-27 18:37:20 -04:00
|
|
|
|
2016-04-29 21:04:55 -04:00
|
|
|
it("should support relative router links",
|
|
|
|
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
|
|
|
|
let fixture = tcb.createFakeAsync(RootCmp);
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/22/relativelink');
|
|
|
|
advance(fixture);
|
|
|
|
expect(fixture.debugElement.nativeElement)
|
|
|
|
.toHaveText('team 22 { relativelink { }, aux: }');
|
|
|
|
|
2016-05-02 13:36:58 -04:00
|
|
|
let native = getDOM().querySelector(fixture.debugElement.nativeElement, "a");
|
|
|
|
expect(getDOM().getAttribute(native, "href")).toEqual("/team/22/relativelink/simple");
|
|
|
|
getDOM().dispatchEvent(native, getDOM().createMouseEvent('click'));
|
2016-04-29 21:04:55 -04:00
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
expect(fixture.debugElement.nativeElement)
|
|
|
|
.toHaveText('team 22 { relativelink { simple }, aux: }');
|
|
|
|
})));
|
|
|
|
|
2016-05-01 20:07:10 -04:00
|
|
|
it("should set the router-link-active class",
|
|
|
|
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
|
|
|
|
let fixture = tcb.createFakeAsync(RootCmp);
|
|
|
|
advance(fixture);
|
|
|
|
|
|
|
|
router.navigateByUrl('/team/22/relativelink');
|
|
|
|
advance(fixture);
|
|
|
|
expect(fixture.debugElement.nativeElement)
|
|
|
|
.toHaveText('team 22 { relativelink { }, aux: }');
|
2016-05-02 13:36:58 -04:00
|
|
|
let link = getDOM().querySelector(fixture.debugElement.nativeElement, "a");
|
|
|
|
expect(getDOM().hasClass(link, "router-link-active")).toEqual(false);
|
2016-05-01 20:07:10 -04:00
|
|
|
|
2016-05-02 13:36:58 -04:00
|
|
|
getDOM().dispatchEvent(link, getDOM().createMouseEvent('click'));
|
2016-05-01 20:07:10 -04:00
|
|
|
advance(fixture);
|
|
|
|
|
2016-05-02 13:36:58 -04:00
|
|
|
expect(getDOM().hasClass(link, "router-link-active")).toEqual(true);
|
2016-05-01 20:07:10 -04:00
|
|
|
})));
|
|
|
|
|
2016-04-28 21:33:48 -04:00
|
|
|
it("should update router links when router changes",
|
|
|
|
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
|
|
|
|
let fixture = tcb.createFakeAsync(RootCmp);
|
|
|
|
advance(fixture);
|
2016-04-27 18:37:20 -04:00
|
|
|
|
2016-04-28 21:33:48 -04:00
|
|
|
router.navigateByUrl('/team/22/link(simple)');
|
|
|
|
advance(fixture);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('team 22 { link, aux: simple }');
|
2016-04-27 18:37:20 -04:00
|
|
|
|
2016-05-02 13:36:58 -04:00
|
|
|
let native = getDOM().querySelector(fixture.debugElement.nativeElement, "a");
|
|
|
|
expect(getDOM().getAttribute(native, "href")).toEqual("/team/33/simple(aux:simple)");
|
2016-04-27 18:37:20 -04:00
|
|
|
|
2016-04-28 21:33:48 -04:00
|
|
|
router.navigateByUrl('/team/22/link(simple2)');
|
|
|
|
advance(fixture);
|
|
|
|
|
2016-05-02 13:36:58 -04:00
|
|
|
expect(getDOM().getAttribute(native, "href")).toEqual("/team/33/simple(aux:simple2)");
|
2016-04-28 21:33:48 -04:00
|
|
|
})));
|
2016-05-04 17:40:17 -04:00
|
|
|
|
|
|
|
it("should support top-level link",
|
2016-05-04 18:01:27 -04:00
|
|
|
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
|
|
|
|
let fixture = tcb.createFakeAsync(LinkCmp);
|
|
|
|
advance(fixture);
|
|
|
|
expect(fixture.debugElement.nativeElement).toHaveText('link');
|
|
|
|
})));
|
2016-04-28 21:33:48 -04:00
|
|
|
}
|
2016-04-22 15:05:38 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-30 13:52:04 -04:00
|
|
|
function advance(fixture: ComponentFixture<any>): void {
|
2016-04-27 18:37:20 -04:00
|
|
|
tick();
|
|
|
|
fixture.detectChanges();
|
|
|
|
}
|
|
|
|
|
2016-04-30 13:52:04 -04:00
|
|
|
function compileRoot(tcb: TestComponentBuilder): Promise<ComponentFixture<any>> {
|
2016-04-22 15:05:38 -04:00
|
|
|
return tcb.createAsync(RootCmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'user-cmp', template: `hello {{user}}`})
|
|
|
|
class UserCmp implements OnActivate {
|
|
|
|
user: string;
|
|
|
|
routerOnActivate(s: RouteSegment, a?, b?, c?) { this.user = s.getParam('name'); }
|
|
|
|
}
|
|
|
|
|
2016-04-29 21:04:55 -04:00
|
|
|
@Component({selector: 'cannot-deactivate', template: `cannotDeactivate`})
|
|
|
|
class CanDeactivateCmp implements CanDeactivate {
|
|
|
|
routerCanDeactivate(a?, b?): Promise<boolean> { return PromiseWrapper.resolve(false); }
|
|
|
|
}
|
|
|
|
|
2016-04-25 19:57:27 -04:00
|
|
|
@Component({selector: 'simple-cmp', template: `simple`})
|
|
|
|
class SimpleCmp {
|
|
|
|
}
|
|
|
|
|
2016-04-27 18:37:20 -04:00
|
|
|
@Component({selector: 'simple2-cmp', template: `simple2`})
|
|
|
|
class Simple2Cmp {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'link-cmp',
|
2016-04-29 21:04:55 -04:00
|
|
|
template: `<a [routerLink]="['/team', '33', 'simple']">link</a>`,
|
2016-04-27 18:37:20 -04:00
|
|
|
directives: ROUTER_DIRECTIVES
|
|
|
|
})
|
|
|
|
class LinkCmp {
|
|
|
|
}
|
|
|
|
|
2016-04-29 21:04:55 -04:00
|
|
|
@Component({
|
|
|
|
selector: 'link-cmp',
|
|
|
|
template: `<a [routerLink]="['./simple']">relativelink</a> { <router-outlet></router-outlet> }`,
|
|
|
|
directives: ROUTER_DIRECTIVES
|
|
|
|
})
|
|
|
|
@Routes([new Route({path: 'simple', component: SimpleCmp})])
|
|
|
|
class RelativeLinkCmp {
|
|
|
|
}
|
|
|
|
|
2016-04-22 15:05:38 -04:00
|
|
|
@Component({
|
|
|
|
selector: 'team-cmp',
|
2016-04-25 19:57:27 -04:00
|
|
|
template: `team {{id}} { <router-outlet></router-outlet>, aux: <router-outlet name="aux"></router-outlet> }`,
|
2016-04-22 15:05:38 -04:00
|
|
|
directives: [ROUTER_DIRECTIVES]
|
|
|
|
})
|
2016-04-25 19:57:27 -04:00
|
|
|
@Routes([
|
|
|
|
new Route({path: 'user/:name', component: UserCmp}),
|
2016-04-27 18:37:20 -04:00
|
|
|
new Route({path: 'simple', component: SimpleCmp}),
|
|
|
|
new Route({path: 'simple2', component: Simple2Cmp}),
|
2016-04-29 21:04:55 -04:00
|
|
|
new Route({path: 'link', component: LinkCmp}),
|
|
|
|
new Route({path: 'relativelink', component: RelativeLinkCmp}),
|
|
|
|
new Route({path: 'cannotDeactivate', component: CanDeactivateCmp})
|
2016-04-25 19:57:27 -04:00
|
|
|
])
|
2016-04-22 15:05:38 -04:00
|
|
|
class TeamCmp implements OnActivate {
|
|
|
|
id: string;
|
|
|
|
routerOnActivate(s: RouteSegment, a?, b?, c?) { this.id = s.getParam('id'); }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'root-cmp',
|
|
|
|
template: `<router-outlet></router-outlet>`,
|
|
|
|
directives: [ROUTER_DIRECTIVES]
|
|
|
|
})
|
|
|
|
@Routes([new Route({path: 'team/:id', component: TeamCmp})])
|
|
|
|
class RootCmp {
|
|
|
|
}
|