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

113 lines
2.9 KiB
TypeScript
Raw Normal View History

import {
AsyncTestCompleter,
beforeEach,
ddescribe,
xdescribe,
describe,
dispatchEvent,
expect,
iit,
inject,
beforeEachBindings,
it,
xit,
TestComponentBuilder
} from 'angular2/testing_internal';
2015-08-26 14:41:41 -04:00
import {SpyRouter, SpyLocation} from './spies';
import {provide, Component, View} from 'angular2/core';
import {By} from 'angular2/src/core/debug';
2015-08-24 14:24:53 -04:00
import {
Location,
Router,
RouteRegistry,
RouterLink,
RouterOutlet,
Route,
RouteParams,
Instruction,
ComponentInstruction
2015-08-24 14:24:53 -04:00
} from 'angular2/router';
2015-08-20 17:28:25 -04:00
import {DOM} from 'angular2/src/core/dom/dom_adapter';
2015-10-08 11:22:11 -04:00
import {ComponentInstruction_} from "angular2/src/router/instruction";
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(() => [
provide(Location, {useValue: makeDummyLocation()}),
provide(Router, {useValue: 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.debugElement.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'?
testComponent.debugElement.query(By.css('a')).triggerEventHandler('click', null);
expect(router.spy('navigateByInstruction')).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 {
}
function makeDummyLocation() {
2015-08-26 14:41:41 -04:00
var dl = new SpyLocation();
dl.spy('prepareExternalUrl').andCallFake((url) => url);
return dl;
}
function makeDummyRouter() {
2015-08-26 14:41:41 -04:00
var dr = new SpyRouter();
dr.spy('generate').andCallFake((routeParams) => dummyInstruction);
dr.spy('isRouteActive').andCallFake((_) => false);
dr.spy('navigateInstruction');
return dr;
}