feat(router): implement router link DSL

Closes #5557

Closes #5562
This commit is contained in:
vsavkin
2015-12-03 16:14:34 -08:00
committed by Victor Savkin
parent e67e1952d0
commit 4ea5b6e57f
8 changed files with 365 additions and 8 deletions
@@ -41,6 +41,8 @@ import {
import {RootRouter} from 'angular2/src/router/router';
import {DOM} from 'angular2/src/platform/dom/dom_adapter';
import {TEMPLATE_TRANSFORMS} from 'angular2/compiler';
import {RouterLinkTransform} from 'angular2/src/router/router_link_transform';
export function main() {
describe('router-link directive', function() {
@@ -53,7 +55,8 @@ export function main() {
DirectiveResolver,
provide(Location, {useClass: SpyLocation}),
provide(ROUTER_PRIMARY_COMPONENT, {useValue: MyComp}),
provide(Router, {useClass: RootRouter})
provide(Router, {useClass: RootRouter}),
provide(TEMPLATE_TRANSFORMS, {useClass: RouterLinkTransform, multi: true})
]);
beforeEach(inject([TestComponentBuilder, Router, Location], (tcBuilder, rtr, loc) => {
@@ -320,6 +323,25 @@ export function main() {
router.navigateByUrl('/child-with-grandchild/grandchild');
});
}));
describe("router link dsl", () => {
it('should generate link hrefs with params', inject([AsyncTestCompleter], (async) => {
compile('<a href="hello" [router-link]="route:./User(name: name)">{{name}}</a>')
.then((_) => router.config(
[new Route({path: '/user/:name', component: UserCmp, name: 'User'})]))
.then((_) => router.navigateByUrl('/a/b'))
.then((_) => {
fixture.debugElement.componentInstance.name = 'brian';
fixture.detectChanges();
expect(fixture.debugElement.nativeElement).toHaveText('brian');
expect(DOM.getAttribute(
fixture.debugElement.componentViewChildren[0].nativeElement, 'href'))
.toEqual('/user/brian');
async.done();
});
}));
});
});
describe('when clicked', () => {
@@ -360,6 +382,7 @@ export function main() {
.then((_) => {
fixture.detectChanges();
var dispatchedEvent = clickOnElement(fixture);
expect(DOM.isPrevented(dispatchedEvent)).toBe(true);
@@ -0,0 +1,68 @@
import {
AsyncTestCompleter,
describe,
proxy,
it,
iit,
ddescribe,
expect,
inject,
beforeEach,
beforeEachBindings,
SpyObject
} from 'angular2/testing_internal';
import {Injector, provide} from 'angular2/core';
import {CONST_EXPR} from 'angular2/src/facade/lang';
import {parseRouterLinkExpression} from 'angular2/src/router/router_link_transform';
import {Unparser} from '../core/change_detection/parser/unparser';
import {Parser} from 'angular2/src/core/change_detection/parser/parser';
export function main() {
function check(parser: Parser, input: string, expectedValue: string) {
let ast = parseRouterLinkExpression(parser, input);
expect(new Unparser().unparse(ast)).toEqual(expectedValue);
}
describe('parseRouterLinkExpression', () => {
it("should parse simple routes", inject([Parser], (p) => {
check(p, `User`, `["User"]`);
check(p, `/User`, `["/User"]`);
check(p, `./User`, `["./User"]`);
check(p, `../../User`, `["../../User"]`);
}));
it("should trim the string", inject([Parser], (p) => { check(p, ` User `, `["User"]`); }));
it("should parse parameters", inject([Parser], (p) => {
check(p, `./User(id: value, name: 'Bob')`, `["./User", {id: value, name: "Bob"}]`);
}));
it("should parse nested routes", inject([Parser], (p) => {
check(p, `User/Modal`, `["User", "Modal"]`);
check(p, `/User/Modal`, `["/User", "Modal"]`);
}));
it("should parse auxiliary routes", inject([Parser], (p) => {
check(p, `User[Modal]`, `["User", ["Modal"]]`);
check(p, `User[Modal1][Modal2]`, `["User", ["Modal1"], ["Modal2"]]`);
check(p, `User[Modal1[Modal2]]`, `["User", ["Modal1", ["Modal2"]]]`);
}));
it("should parse combinations", inject([Parser], (p) => {
check(p, `./User(id: value)/Post(title: 'blog')`, `["./User", {id: value}, "Post", {title: "blog"}]`);
check(p, `./User[Modal(param: value)]`, `["./User", ["Modal", {param: value}]]`);
}));
it("should error on empty fixed parts", inject([Parser], (p) => {
expect(() => parseRouterLinkExpression(p, `./(id: value, name: 'Bob')`))
.toThrowErrorWith("Invalid router link");
}));
it("should error on multiple slashes", inject([Parser], (p) => {
expect(() => parseRouterLinkExpression(p, `//User`))
.toThrowErrorWith("Invalid router link");
}));
});
}