angular-cn/modules/angular2/test/router/router_link_spec.ts

127 lines
3.0 KiB
TypeScript
Raw Normal View History

import {
AsyncTestCompleter,
beforeEach,
ddescribe,
xdescribe,
describe,
dispatchEvent,
expect,
iit,
inject,
beforeEachBindings,
it,
xit,
TestComponentBuilder,
proxy,
SpyObject,
By
} from 'angular2/test_lib';
2015-08-20 17:28:25 -04:00
import {IMPLEMENTS} from 'angular2/src/core/facade/lang';
import {bind, Component, View} from 'angular2/angular2';
2015-08-24 14:24:53 -04:00
import {
Location,
Router,
RootRouter,
RouteRegistry,
Pipeline,
RouterLink,
RouterOutlet,
Route,
RouteParams
} from 'angular2/router';
import {Instruction, ComponentInstruction} from 'angular2/src/router/instruction';
2015-08-20 17:28:25 -04:00
import {DOM} from 'angular2/src/core/dom/dom_adapter';
var dummyInstruction = new Instruction(new ComponentInstruction('detail', [], null), null, {});
export function main() {
describe('router-link directive', function() {
2015-08-24 14:24:53 -04:00
var tcb: TestComponentBuilder;
beforeEachBindings(
() =>
[bind(Location).toValue(makeDummyLocation()), bind(Router).toValue(makeDummyRouter())]);
2015-08-24 14:24:53 -04:00
beforeEach(inject([TestComponentBuilder], (tcBuilder) => { tcb = tcBuilder; }));
2015-08-24 14:24:53 -04:00
it('should update a[href] attribute', inject([AsyncTestCompleter], (async) => {
tcb.createAsync(TestComponent)
.then((testComponent) => {
testComponent.detectChanges();
let anchorElement = testComponent.query(By.css('a')).nativeElement;
expect(DOM.getAttribute(anchorElement, 'href')).toEqual('/detail');
async.done();
});
}));
it('should call router.navigate when a link is clicked',
2015-08-24 14:24:53 -04:00
inject([AsyncTestCompleter, Router], (async, router) => {
tcb.createAsync(TestComponent)
.then((testComponent) => {
testComponent.detectChanges();
// TODO: shouldn't this be just 'click' rather than '^click'?
2015-08-24 14:24:53 -04:00
testComponent.query(By.css('a')).triggerEventHandler('^click', null);
expect(router.spy('navigateInstruction')).toHaveBeenCalledWith(dummyInstruction);
async.done();
});
}));
});
}
2015-08-24 14:24:53 -04:00
@Component({selector: 'my-comp'})
class MyComp {
name;
}
@Component({selector: 'user-cmp'})
@View({template: "hello {{user}}"})
class UserCmp {
user: string;
constructor(params: RouteParams) { this.user = params.get('name'); }
}
@Component({selector: 'test-component'})
@View({
template: `
<div>
<a [router-link]="['/detail']">detail view</a>
</div>`,
directives: [RouterLink]
})
class TestComponent {
}
@proxy
@IMPLEMENTS(Location)
class DummyLocation extends SpyObject {
noSuchMethod(m) { return super.noSuchMethod(m) }
}
function makeDummyLocation() {
var dl = new DummyLocation();
dl.spy('normalizeAbsolutely').andCallFake((url) => url);
return dl;
}
@proxy
@IMPLEMENTS(Router)
class DummyRouter extends SpyObject {
noSuchMethod(m) { return super.noSuchMethod(m) }
}
function makeDummyRouter() {
var dr = new DummyRouter();
dr.spy('generate').andCallFake((routeParams) => dummyInstruction);
dr.spy('navigateInstruction');
return dr;
}