2015-07-02 20:38:19 -04:00
|
|
|
import {
|
|
|
|
AsyncTestCompleter,
|
|
|
|
beforeEach,
|
|
|
|
ddescribe,
|
|
|
|
xdescribe,
|
|
|
|
describe,
|
|
|
|
dispatchEvent,
|
|
|
|
expect,
|
|
|
|
iit,
|
|
|
|
inject,
|
|
|
|
beforeEachBindings,
|
|
|
|
it,
|
|
|
|
xit,
|
|
|
|
TestComponentBuilder,
|
|
|
|
By
|
|
|
|
} from 'angular2/test_lib';
|
|
|
|
|
2015-08-26 14:41:41 -04:00
|
|
|
import {SpyRouter, SpyLocation} from './spies';
|
2015-07-02 20:38:19 -04:00
|
|
|
|
|
|
|
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';
|
2015-07-17 16:36:53 -04:00
|
|
|
import {Instruction, ComponentInstruction} from 'angular2/src/router/instruction';
|
2015-07-02 20:38:19 -04:00
|
|
|
|
2015-08-20 17:28:25 -04:00
|
|
|
import {DOM} from 'angular2/src/core/dom/dom_adapter';
|
2015-07-02 20:38:19 -04:00
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
var dummyInstruction = new Instruction(new ComponentInstruction('detail', [], null), null, {});
|
2015-07-02 20:38:19 -04:00
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
export function main() {
|
2015-07-02 20:38:19 -04:00
|
|
|
describe('router-link directive', function() {
|
2015-08-24 14:24:53 -04:00
|
|
|
var tcb: TestComponentBuilder;
|
2015-07-02 20:38:19 -04:00
|
|
|
|
|
|
|
beforeEachBindings(
|
|
|
|
() =>
|
|
|
|
[bind(Location).toValue(makeDummyLocation()), bind(Router).toValue(makeDummyRouter())]);
|
|
|
|
|
2015-08-24 14:24:53 -04:00
|
|
|
beforeEach(inject([TestComponentBuilder], (tcBuilder) => { tcb = tcBuilder; }));
|
2015-07-02 20:38:19 -04:00
|
|
|
|
2015-08-24 14:24:53 -04:00
|
|
|
it('should update a[href] attribute', inject([AsyncTestCompleter], (async) => {
|
2015-07-02 20:38:19 -04:00
|
|
|
|
|
|
|
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) => {
|
2015-07-02 20:38:19 -04:00
|
|
|
|
|
|
|
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);
|
2015-07-17 16:36:53 -04:00
|
|
|
expect(router.spy('navigateInstruction')).toHaveBeenCalledWith(dummyInstruction);
|
2015-07-02 20:38:19 -04:00
|
|
|
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'); }
|
|
|
|
}
|
2015-07-02 20:38:19 -04:00
|
|
|
|
|
|
|
@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();
|
2015-07-02 20:38:19 -04:00
|
|
|
dl.spy('normalizeAbsolutely').andCallFake((url) => url);
|
|
|
|
return dl;
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeDummyRouter() {
|
2015-08-26 14:41:41 -04:00
|
|
|
var dr = new SpyRouter();
|
2015-07-17 16:36:53 -04:00
|
|
|
dr.spy('generate').andCallFake((routeParams) => dummyInstruction);
|
|
|
|
dr.spy('navigateInstruction');
|
2015-07-02 20:38:19 -04:00
|
|
|
return dr;
|
|
|
|
}
|