2015-04-17 12:59:56 -04:00
|
|
|
import {
|
|
|
|
AsyncTestCompleter,
|
|
|
|
beforeEach,
|
|
|
|
ddescribe,
|
|
|
|
xdescribe,
|
|
|
|
describe,
|
|
|
|
el,
|
|
|
|
expect,
|
|
|
|
iit,
|
|
|
|
inject,
|
|
|
|
beforeEachBindings,
|
|
|
|
it,
|
|
|
|
xit
|
|
|
|
} from 'angular2/test_lib';
|
|
|
|
|
|
|
|
import {TestBed} from 'angular2/test';
|
|
|
|
|
|
|
|
import {Injector, bind} from 'angular2/di';
|
2015-04-29 18:07:55 -04:00
|
|
|
import {Component} from 'angular2/src/core/annotations_impl/annotations';
|
2015-04-28 21:17:00 -04:00
|
|
|
import {View} from 'angular2/src/core/annotations_impl/view';
|
2015-04-17 12:59:56 -04:00
|
|
|
|
|
|
|
import {RootRouter} from 'angular2/src/router/router';
|
|
|
|
import {Pipeline} from 'angular2/src/router/pipeline';
|
|
|
|
import {Router, RouterOutlet, RouterLink, RouteConfig, RouteParams} from 'angular2/router';
|
|
|
|
|
|
|
|
import {DOM} from 'angular2/src/dom/dom_adapter';
|
|
|
|
|
2015-05-01 18:43:54 -04:00
|
|
|
import {SpyLocation} from 'angular2/src/mock/location_mock';
|
2015-05-01 08:53:38 -04:00
|
|
|
import {Location} from 'angular2/src/router/location';
|
|
|
|
import {RouteRegistry} from 'angular2/src/router/route_registry';
|
|
|
|
import {DirectiveMetadataReader} from 'angular2/src/core/compiler/directive_metadata_reader';
|
2015-04-21 14:23:23 -04:00
|
|
|
|
2015-04-17 12:59:56 -04:00
|
|
|
export function main() {
|
|
|
|
describe('Outlet Directive', () => {
|
|
|
|
|
2015-05-01 08:53:38 -04:00
|
|
|
var ctx, tb, view, rtr;
|
2015-04-17 12:59:56 -04:00
|
|
|
|
2015-05-01 08:53:38 -04:00
|
|
|
beforeEachBindings(() => [
|
|
|
|
Pipeline,
|
|
|
|
RouteRegistry,
|
|
|
|
DirectiveMetadataReader,
|
2015-05-01 18:43:54 -04:00
|
|
|
bind(Location).toClass(SpyLocation),
|
2015-05-01 08:53:38 -04:00
|
|
|
bind(Router).toFactory((registry, pipeline, location) => {
|
|
|
|
return new RootRouter(registry, pipeline, location, MyComp);
|
|
|
|
}, [RouteRegistry, Pipeline, Location])
|
|
|
|
]);
|
|
|
|
|
|
|
|
beforeEach(inject([TestBed, Router], (testBed, router) => {
|
2015-04-17 12:59:56 -04:00
|
|
|
tb = testBed;
|
|
|
|
ctx = new MyComp();
|
2015-05-01 08:53:38 -04:00
|
|
|
rtr = router;
|
2015-04-17 12:59:56 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
function compile(template:string = "<router-outlet></router-outlet>") {
|
|
|
|
tb.overrideView(MyComp, new View({template: ('<div>' + template + '</div>'), directives: [RouterOutlet, RouterLink]}));
|
|
|
|
return tb.createView(MyComp, {context: ctx}).then((v) => {
|
|
|
|
view = v;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should work in a simple case', inject([AsyncTestCompleter], (async) => {
|
|
|
|
compile()
|
2015-05-01 08:53:38 -04:00
|
|
|
.then((_) => rtr.config({'path': '/test', 'component': HelloCmp}))
|
|
|
|
.then((_) => rtr.navigate('/test'))
|
2015-04-17 12:59:56 -04:00
|
|
|
.then((_) => {
|
|
|
|
view.detectChanges();
|
|
|
|
expect(view.rootNodes).toHaveText('hello');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
it('should navigate between components with different parameters', inject([AsyncTestCompleter], (async) => {
|
|
|
|
compile()
|
2015-05-01 08:53:38 -04:00
|
|
|
.then((_) => rtr.config({'path': '/user/:name', 'component': UserCmp}))
|
|
|
|
.then((_) => rtr.navigate('/user/brian'))
|
2015-04-17 12:59:56 -04:00
|
|
|
.then((_) => {
|
|
|
|
view.detectChanges();
|
|
|
|
expect(view.rootNodes).toHaveText('hello brian');
|
|
|
|
})
|
2015-05-01 08:53:38 -04:00
|
|
|
.then((_) => rtr.navigate('/user/igor'))
|
2015-04-17 12:59:56 -04:00
|
|
|
.then((_) => {
|
|
|
|
view.detectChanges();
|
|
|
|
expect(view.rootNodes).toHaveText('hello igor');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
it('should work with child routers', inject([AsyncTestCompleter], (async) => {
|
|
|
|
compile('outer { <router-outlet></router-outlet> }')
|
2015-05-01 08:53:38 -04:00
|
|
|
.then((_) => rtr.config({'path': '/a', 'component': ParentCmp}))
|
|
|
|
.then((_) => rtr.navigate('/a/b'))
|
2015-04-17 12:59:56 -04:00
|
|
|
.then((_) => {
|
|
|
|
view.detectChanges();
|
|
|
|
expect(view.rootNodes).toHaveText('outer { inner { hello } }');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
it('should generate link hrefs', inject([AsyncTestCompleter], (async) => {
|
|
|
|
ctx.name = 'brian';
|
|
|
|
compile('<a href="hello" router-link="user" [router-params]="{name: name}">{{name}}</a>')
|
2015-05-01 08:53:38 -04:00
|
|
|
.then((_) => rtr.config({'path': '/user/:name', 'component': UserCmp, 'alias': 'user'}))
|
|
|
|
.then((_) => rtr.navigate('/a/b'))
|
2015-04-17 12:59:56 -04:00
|
|
|
.then((_) => {
|
|
|
|
view.detectChanges();
|
|
|
|
expect(view.rootNodes).toHaveText('brian');
|
|
|
|
expect(DOM.getAttribute(view.rootNodes[0].childNodes[0], 'href')).toEqual('/user/brian');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'hello-cmp'
|
|
|
|
})
|
|
|
|
@View({
|
|
|
|
template: "{{greeting}}"
|
|
|
|
})
|
|
|
|
class HelloCmp {
|
|
|
|
greeting:string;
|
|
|
|
constructor() {
|
|
|
|
this.greeting = "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@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]
|
|
|
|
})
|
2015-04-29 18:47:12 -04:00
|
|
|
@RouteConfig([{
|
2015-04-17 12:59:56 -04:00
|
|
|
path: '/b',
|
|
|
|
component: HelloCmp
|
2015-04-29 18:47:12 -04:00
|
|
|
}])
|
2015-04-17 12:59:56 -04:00
|
|
|
class ParentCmp {
|
|
|
|
constructor() {}
|
|
|
|
}
|
|
|
|
|
2015-04-27 18:14:30 -04:00
|
|
|
@Component({
|
|
|
|
selector: 'my-comp'
|
|
|
|
})
|
2015-04-17 12:59:56 -04:00
|
|
|
class MyComp {
|
|
|
|
name;
|
|
|
|
}
|