2015-05-29 17:58:41 -04:00
|
|
|
import {
|
|
|
|
AsyncTestCompleter,
|
2015-06-26 19:04:19 -04:00
|
|
|
TestComponentBuilder,
|
|
|
|
asNativeElements,
|
2015-05-29 17:58:41 -04:00
|
|
|
beforeEach,
|
|
|
|
ddescribe,
|
|
|
|
xdescribe,
|
|
|
|
describe,
|
|
|
|
el,
|
|
|
|
expect,
|
|
|
|
iit,
|
|
|
|
inject,
|
|
|
|
beforeEachBindings,
|
|
|
|
it,
|
|
|
|
xit
|
|
|
|
} from 'angular2/test_lib';
|
|
|
|
|
|
|
|
import {Injector, bind} from 'angular2/di';
|
|
|
|
import {Component, View} from 'angular2/src/core/annotations/decorators';
|
|
|
|
import * as annotations from 'angular2/src/core/annotations_impl/view';
|
|
|
|
import {CONST} from 'angular2/src/facade/lang';
|
|
|
|
|
|
|
|
import {RootRouter} from 'angular2/src/router/router';
|
|
|
|
import {Pipeline} from 'angular2/src/router/pipeline';
|
|
|
|
import {Router, RouterOutlet, RouterLink, RouteParams} from 'angular2/router';
|
|
|
|
import {RouteConfig} from 'angular2/src/router/route_config_decorator';
|
|
|
|
|
|
|
|
import {DOM} from 'angular2/src/dom/dom_adapter';
|
|
|
|
|
|
|
|
import {SpyLocation} from 'angular2/src/mock/location_mock';
|
|
|
|
import {Location} from 'angular2/src/router/location';
|
|
|
|
import {RouteRegistry} from 'angular2/src/router/route_registry';
|
|
|
|
import {DirectiveResolver} from 'angular2/src/core/compiler/directive_resolver';
|
|
|
|
|
|
|
|
var teamCmpCount;
|
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('Outlet Directive', () => {
|
|
|
|
|
2015-06-26 19:04:19 -04:00
|
|
|
var tcb: TestComponentBuilder;
|
|
|
|
var rootTC, rtr, location;
|
2015-05-29 17:58:41 -04:00
|
|
|
|
|
|
|
beforeEachBindings(() => [
|
|
|
|
Pipeline,
|
2015-06-30 16:18:51 -04:00
|
|
|
bind(RouteRegistry).toFactory(() => new RouteRegistry(MyComp)),
|
2015-05-29 17:58:41 -04:00
|
|
|
DirectiveResolver,
|
|
|
|
bind(Location).toClass(SpyLocation),
|
2015-06-03 16:42:57 -04:00
|
|
|
bind(Router)
|
|
|
|
.toFactory((registry, pipeline,
|
|
|
|
location) => { return new RootRouter(registry, pipeline, location, MyComp); },
|
|
|
|
[RouteRegistry, Pipeline, Location])
|
2015-05-29 17:58:41 -04:00
|
|
|
]);
|
|
|
|
|
2015-06-26 19:04:19 -04:00
|
|
|
beforeEach(inject([TestComponentBuilder, Router, Location], (tcBuilder, router, loc) => {
|
|
|
|
tcb = tcBuilder;
|
2015-05-29 17:58:41 -04:00
|
|
|
rtr = router;
|
|
|
|
location = loc;
|
|
|
|
teamCmpCount = 0;
|
|
|
|
}));
|
|
|
|
|
|
|
|
function compile(template: string = "<router-outlet></router-outlet>") {
|
2015-06-26 19:04:19 -04:00
|
|
|
return tcb.overrideView(MyComp, new annotations.View({
|
|
|
|
template: ('<div>' + template + '</div>'),
|
|
|
|
directives: [RouterOutlet, RouterLink]
|
|
|
|
}))
|
|
|
|
.createAsync(MyComp)
|
|
|
|
.then((tc) => { rootTC = tc; });
|
2015-05-29 17:58:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
it('should work in a simple case', inject([AsyncTestCompleter], (async) => {
|
|
|
|
compile()
|
|
|
|
.then((_) => rtr.config({'path': '/test', 'component': HelloCmp}))
|
|
|
|
.then((_) => rtr.navigate('/test'))
|
|
|
|
.then((_) => {
|
2015-06-26 19:04:19 -04:00
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.nativeElement).toHaveText('hello');
|
2015-05-29 17:58:41 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
it('should navigate between components with different parameters',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
compile()
|
|
|
|
.then((_) => rtr.config({'path': '/user/:name', 'component': UserCmp}))
|
|
|
|
.then((_) => rtr.navigate('/user/brian'))
|
2015-06-03 16:42:57 -04:00
|
|
|
.then((_) => {
|
2015-06-26 19:04:19 -04:00
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.nativeElement).toHaveText('hello brian');
|
2015-06-03 16:42:57 -04:00
|
|
|
})
|
2015-05-29 17:58:41 -04:00
|
|
|
.then((_) => rtr.navigate('/user/igor'))
|
|
|
|
.then((_) => {
|
2015-06-26 19:04:19 -04:00
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.nativeElement).toHaveText('hello igor');
|
2015-05-29 17:58:41 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
it('should work with child routers', inject([AsyncTestCompleter], (async) => {
|
|
|
|
compile('outer { <router-outlet></router-outlet> }')
|
2015-06-17 14:57:38 -04:00
|
|
|
.then((_) => rtr.config({'path': '/a/...', 'component': ParentCmp}))
|
2015-05-29 17:58:41 -04:00
|
|
|
.then((_) => rtr.navigate('/a/b'))
|
|
|
|
.then((_) => {
|
2015-06-26 19:04:19 -04:00
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.nativeElement).toHaveText('outer { inner { hello } }');
|
2015-05-29 17:58:41 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
it('should work with redirects', inject([AsyncTestCompleter, Location], (async, location) => {
|
|
|
|
compile()
|
|
|
|
.then((_) => rtr.config({'path': '/original', 'redirectTo': '/redirected'}))
|
|
|
|
.then((_) => rtr.config({'path': '/redirected', 'component': A}))
|
|
|
|
.then((_) => rtr.navigate('/original'))
|
|
|
|
.then((_) => {
|
2015-06-26 19:04:19 -04:00
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.nativeElement).toHaveText('A');
|
2015-05-29 17:58:41 -04:00
|
|
|
expect(location.urlChanges).toEqual(['/redirected']);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-06-26 19:04:19 -04:00
|
|
|
function getHref(tc) {
|
|
|
|
return DOM.getAttribute(tc.componentViewChildren[0].nativeElement, 'href');
|
|
|
|
}
|
2015-05-29 17:58:41 -04:00
|
|
|
|
|
|
|
it('should generate absolute hrefs that include the base href',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
location.setBaseHref('/my/base');
|
2015-06-30 16:18:51 -04:00
|
|
|
compile('<a href="hello" [router-link]="[\'./user\']"></a>')
|
2015-05-29 17:58:41 -04:00
|
|
|
.then((_) => rtr.config({'path': '/user', 'component': UserCmp, 'as': 'user'}))
|
|
|
|
.then((_) => rtr.navigate('/a/b'))
|
|
|
|
.then((_) => {
|
2015-06-26 19:04:19 -04:00
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(getHref(rootTC)).toEqual('/my/base/user');
|
2015-05-29 17:58:41 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
it('should generate link hrefs without params', inject([AsyncTestCompleter], (async) => {
|
2015-06-30 16:18:51 -04:00
|
|
|
compile('<a href="hello" [router-link]="[\'./user\']"></a>')
|
2015-05-29 17:58:41 -04:00
|
|
|
.then((_) => rtr.config({'path': '/user', 'component': UserCmp, 'as': 'user'}))
|
|
|
|
.then((_) => rtr.navigate('/a/b'))
|
|
|
|
.then((_) => {
|
2015-06-26 19:04:19 -04:00
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(getHref(rootTC)).toEqual('/user');
|
2015-05-29 17:58:41 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
it('should reuse common parent components', inject([AsyncTestCompleter], (async) => {
|
|
|
|
compile()
|
2015-06-17 14:57:38 -04:00
|
|
|
.then((_) => rtr.config({'path': '/team/:id/...', 'component': TeamCmp}))
|
2015-05-29 17:58:41 -04:00
|
|
|
.then((_) => rtr.navigate('/team/angular/user/rado'))
|
2015-06-03 16:42:57 -04:00
|
|
|
.then((_) => {
|
2015-06-26 19:04:19 -04:00
|
|
|
rootTC.detectChanges();
|
2015-06-03 16:42:57 -04:00
|
|
|
expect(teamCmpCount).toBe(1);
|
2015-06-26 19:04:19 -04:00
|
|
|
expect(rootTC.nativeElement).toHaveText('team angular { hello rado }');
|
2015-06-03 16:42:57 -04:00
|
|
|
})
|
2015-05-29 17:58:41 -04:00
|
|
|
.then((_) => rtr.navigate('/team/angular/user/victor'))
|
|
|
|
.then((_) => {
|
2015-06-26 19:04:19 -04:00
|
|
|
rootTC.detectChanges();
|
2015-05-29 17:58:41 -04:00
|
|
|
expect(teamCmpCount).toBe(1);
|
2015-06-26 19:04:19 -04:00
|
|
|
expect(rootTC.nativeElement).toHaveText('team angular { hello victor }');
|
2015-05-29 17:58:41 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
it('should generate link hrefs with params', inject([AsyncTestCompleter], (async) => {
|
2015-06-30 16:18:51 -04:00
|
|
|
compile('<a href="hello" [router-link]="[\'./user\', {name: name}]">{{name}}</a>')
|
2015-05-29 17:58:41 -04:00
|
|
|
.then((_) => rtr.config({'path': '/user/:name', 'component': UserCmp, 'as': 'user'}))
|
|
|
|
.then((_) => rtr.navigate('/a/b'))
|
|
|
|
.then((_) => {
|
2015-06-26 19:04:19 -04:00
|
|
|
rootTC.componentInstance.name = 'brian';
|
|
|
|
rootTC.detectChanges();
|
|
|
|
expect(rootTC.nativeElement).toHaveText('brian');
|
|
|
|
expect(DOM.getAttribute(rootTC.componentViewChildren[0].nativeElement, 'href'))
|
2015-05-29 17:58:41 -04:00
|
|
|
.toEqual('/user/brian');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe('when clicked', () => {
|
|
|
|
|
|
|
|
var clickOnElement = function(view) {
|
2015-06-26 19:04:19 -04:00
|
|
|
var anchorEl = rootTC.componentViewChildren[0].nativeElement;
|
2015-05-29 17:58:41 -04:00
|
|
|
var dispatchedEvent = DOM.createMouseEvent('click');
|
|
|
|
DOM.dispatchEvent(anchorEl, dispatchedEvent);
|
|
|
|
return dispatchedEvent;
|
|
|
|
};
|
|
|
|
|
|
|
|
it('should navigate to link hrefs without params', inject([AsyncTestCompleter], (async) => {
|
2015-06-30 16:18:51 -04:00
|
|
|
compile('<a href="hello" [router-link]="[\'./user\']"></a>')
|
2015-05-29 17:58:41 -04:00
|
|
|
.then((_) => rtr.config({'path': '/user', 'component': UserCmp, 'as': 'user'}))
|
|
|
|
.then((_) => rtr.navigate('/a/b'))
|
|
|
|
.then((_) => {
|
2015-06-26 19:04:19 -04:00
|
|
|
rootTC.detectChanges();
|
2015-05-29 17:58:41 -04:00
|
|
|
|
2015-06-26 19:04:19 -04:00
|
|
|
var dispatchedEvent = clickOnElement(rootTC);
|
2015-05-29 17:58:41 -04:00
|
|
|
expect(dispatchedEvent.defaultPrevented || !dispatchedEvent.returnValue)
|
|
|
|
.toBe(true);
|
|
|
|
|
|
|
|
// router navigation is async.
|
|
|
|
rtr.subscribe((_) => {
|
|
|
|
expect(location.urlChanges).toEqual(['/user']);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should navigate to link hrefs in presence of base href',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
location.setBaseHref('/base');
|
2015-06-30 16:18:51 -04:00
|
|
|
compile('<a href="hello" [router-link]="[\'./user\']"></a>')
|
2015-05-29 17:58:41 -04:00
|
|
|
.then((_) => rtr.config({'path': '/user', 'component': UserCmp, 'as': 'user'}))
|
|
|
|
.then((_) => rtr.navigate('/a/b'))
|
|
|
|
.then((_) => {
|
2015-06-26 19:04:19 -04:00
|
|
|
rootTC.detectChanges();
|
2015-05-29 17:58:41 -04:00
|
|
|
|
2015-06-26 19:04:19 -04:00
|
|
|
var dispatchedEvent = clickOnElement(rootTC);
|
2015-05-29 17:58:41 -04:00
|
|
|
expect(dispatchedEvent.defaultPrevented || !dispatchedEvent.returnValue)
|
|
|
|
.toBe(true);
|
|
|
|
|
|
|
|
// router navigation is async.
|
|
|
|
rtr.subscribe((_) => {
|
|
|
|
expect(location.urlChanges).toEqual(['/base/user']);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Component({selector: 'hello-cmp'})
|
|
|
|
@View({template: "{{greeting}}"})
|
|
|
|
class HelloCmp {
|
|
|
|
greeting: string;
|
|
|
|
constructor() { this.greeting = "hello"; }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Component({selector: 'a-cmp'})
|
|
|
|
@View({template: "A"})
|
|
|
|
class A {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Component({selector: 'b-cmp'})
|
|
|
|
@View({template: "B"})
|
|
|
|
class B {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Component({selector: 'user-cmp'})
|
|
|
|
@View({template: "hello {{user}}"})
|
|
|
|
class UserCmp {
|
|
|
|
user: string;
|
|
|
|
constructor(params: RouteParams) { this.user = params.get('name'); }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Component({selector: 'parent-cmp'})
|
|
|
|
@View({template: "inner { <router-outlet></router-outlet> }", directives: [RouterOutlet]})
|
|
|
|
@RouteConfig([{path: '/b', component: HelloCmp}])
|
|
|
|
class ParentCmp {
|
|
|
|
constructor() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Component({selector: 'team-cmp'})
|
|
|
|
@View({template: "team {{id}} { <router-outlet></router-outlet> }", directives: [RouterOutlet]})
|
|
|
|
@RouteConfig([{path: '/user/:name', component: UserCmp}])
|
|
|
|
class TeamCmp {
|
|
|
|
id: string;
|
|
|
|
constructor(params: RouteParams) {
|
|
|
|
this.id = params.get('id');
|
|
|
|
teamCmpCount += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Component({selector: 'my-comp'})
|
|
|
|
class MyComp {
|
|
|
|
name;
|
|
|
|
}
|