angular-cn/modules/@angular/router-deprecated/test/directives/router_link_spec.ts

144 lines
4.5 KiB
TypeScript
Raw Normal View History

import {
beforeEach,
ddescribe,
xdescribe,
describe,
expect,
iit,
inject,
beforeEachProviders,
it,
xit,
} from '@angular/core/testing/testing_internal';
import {TestComponentBuilder} from '@angular/compiler/testing';
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
import {SpyRouter, SpyLocation} from '../spies';
import {provide, Component} from '@angular/core';
import {Location} from '@angular/common';
import {
Router,
RouteRegistry,
RouterLink,
RouterOutlet,
Route,
RouteParams,
ComponentInstruction
2016-05-02 13:36:58 -04:00
} from '@angular/router-deprecated';
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
2016-05-02 13:36:58 -04:00
import {ResolvedInstruction} from '@angular/router-deprecated/src/instruction';
import {By} from '@angular/platform-browser/src/dom/debug/by';
let dummyInstruction = new ResolvedInstruction(
new ComponentInstruction('detail', [], null, null, true, '0', null, 'Detail'), null, {});
export function main() {
describe('routerLink directive', function() {
2015-08-24 14:24:53 -04:00
var tcb: TestComponentBuilder;
beforeEachProviders(() => [
{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.detail-view')).nativeElement;
expect(getDOM().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.detail-view'))
.triggerEventHandler('click', null);
expect(router.spy('navigateByInstruction')).toHaveBeenCalledWith(dummyInstruction);
async.done();
});
}));
it('should call router.navigate when a link is clicked if target is _self',
inject([AsyncTestCompleter, Router], (async, router) => {
tcb.createAsync(TestComponent)
.then((testComponent) => {
testComponent.detectChanges();
testComponent.debugElement.query(By.css('a.detail-view-self'))
.triggerEventHandler('click', null);
expect(router.spy('navigateByInstruction')).toHaveBeenCalledWith(dummyInstruction);
async.done();
});
}));
it('should NOT call router.navigate when a link is clicked if target is set to other than _self',
inject([AsyncTestCompleter, Router], (async, router) => {
tcb.createAsync(TestComponent)
.then((testComponent) => {
testComponent.detectChanges();
testComponent.debugElement.query(By.css('a.detail-view-blank'))
.triggerEventHandler('click', null);
expect(router.spy('navigateByInstruction')).not.toHaveBeenCalled();
async.done();
});
}));
});
}
@Component({selector: 'user-cmp', template: "hello {{user}}"})
2015-08-24 14:24:53 -04:00
class UserCmp {
user: string;
constructor(params: RouteParams) { this.user = params.get('name'); }
}
@Component({
selector: 'test-component',
template: `
<div>
<a [routerLink]="['/Detail']"
class="detail-view">
detail view
</a>
<a [routerLink]="['/Detail']"
class="detail-view-self"
target="_self">
detail view with _self target
</a>
<a [routerLink]="['/Detail']"
class="detail-view-blank"
target="_blank">
detail view with _blank target
</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;
}