2016-06-08 19:38:52 -04:00
|
|
|
import {beforeEach, ddescribe, xdescribe, describe, expect, iit, inject, beforeEachProviders, it, xit,} from '@angular/core/testing/testing_internal';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {TestComponentBuilder} from '@angular/compiler/testing';
|
|
|
|
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
|
2015-07-02 20:38:19 -04:00
|
|
|
|
2016-02-09 14:12:41 -05:00
|
|
|
import {SpyRouter, SpyLocation} from '../spies';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {provide, Component} from '@angular/core';
|
|
|
|
import {Location} from '@angular/common';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {Router, RouteRegistry, RouterLink, RouterOutlet, Route, RouteParams, ComponentInstruction} from '@angular/router-deprecated';
|
2016-04-28 20:50:03 -04:00
|
|
|
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';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {By} from '@angular/platform-browser/src/dom/debug/by';
|
2015-07-02 20:38:19 -04:00
|
|
|
|
2015-12-18 13:05:55 -05:00
|
|
|
let dummyInstruction = new ResolvedInstruction(
|
2016-03-30 14:26:31 -04:00
|
|
|
new ComponentInstruction('detail', [], null, null, true, '0', null, 'Detail'), null, {});
|
2015-07-02 20:38:19 -04:00
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
export function main() {
|
2015-11-23 19:02:19 -05:00
|
|
|
describe('routerLink directive', function() {
|
2015-08-24 14:24:53 -04:00
|
|
|
var tcb: TestComponentBuilder;
|
2015-07-02 20:38:19 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
beforeEachProviders(() => [{provide: Location, useValue: makeDummyLocation()}, {
|
|
|
|
provide: Router,
|
|
|
|
useValue: makeDummyRouter()
|
|
|
|
}]);
|
2015-07-02 20:38:19 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
beforeEach(
|
|
|
|
inject([TestComponentBuilder], (tcBuilder: any /** TODO #9100 */) => { tcb = tcBuilder; }));
|
2015-07-02 20:38:19 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should update a[href] attribute',
|
|
|
|
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
2015-07-02 20:38:19 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
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();
|
|
|
|
});
|
2015-07-02 20:38:19 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
it('should call router.navigate when a link is clicked',
|
2016-06-08 19:38:52 -04:00
|
|
|
inject(
|
|
|
|
[AsyncTestCompleter, Router],
|
|
|
|
(async: AsyncTestCompleter, router: any /** TODO #9100 */) => {
|
2015-07-02 20:38:19 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
tcb.createAsync(TestComponent).then((testComponent) => {
|
2016-04-12 12:40:37 -04:00
|
|
|
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();
|
|
|
|
});
|
2016-06-08 19:38:52 -04:00
|
|
|
}));
|
2015-11-02 18:11:57 -05:00
|
|
|
|
|
|
|
it('should call router.navigate when a link is clicked if target is _self',
|
2016-06-08 19:38:52 -04:00
|
|
|
inject(
|
|
|
|
[AsyncTestCompleter, Router],
|
|
|
|
(async: AsyncTestCompleter, router: any /** TODO #9100 */) => {
|
2015-11-02 18:11:57 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
tcb.createAsync(TestComponent).then((testComponent) => {
|
2016-04-12 12:40:37 -04:00
|
|
|
testComponent.detectChanges();
|
|
|
|
testComponent.debugElement.query(By.css('a.detail-view-self'))
|
|
|
|
.triggerEventHandler('click', null);
|
|
|
|
expect(router.spy('navigateByInstruction')).toHaveBeenCalledWith(dummyInstruction);
|
|
|
|
async.done();
|
|
|
|
});
|
2016-06-08 19:38:52 -04:00
|
|
|
}));
|
2015-11-02 18:11:57 -05:00
|
|
|
|
|
|
|
it('should NOT call router.navigate when a link is clicked if target is set to other than _self',
|
2016-06-08 19:38:52 -04:00
|
|
|
inject(
|
|
|
|
[AsyncTestCompleter, Router],
|
|
|
|
(async: AsyncTestCompleter, router: any /** TODO #9100 */) => {
|
2015-11-02 18:11:57 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
tcb.createAsync(TestComponent).then((testComponent) => {
|
2016-04-12 12:40:37 -04:00
|
|
|
testComponent.detectChanges();
|
|
|
|
testComponent.debugElement.query(By.css('a.detail-view-blank'))
|
|
|
|
.triggerEventHandler('click', null);
|
|
|
|
expect(router.spy('navigateByInstruction')).not.toHaveBeenCalled();
|
|
|
|
async.done();
|
|
|
|
});
|
2016-06-08 19:38:52 -04:00
|
|
|
}));
|
2015-07-02 20:38:19 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
@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'); }
|
|
|
|
}
|
2015-07-02 20:38:19 -04:00
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'test-component',
|
2015-07-02 20:38:19 -04:00
|
|
|
template: `
|
|
|
|
<div>
|
2015-11-23 19:02:19 -05:00
|
|
|
<a [routerLink]="['/Detail']"
|
2015-11-02 18:11:57 -05:00
|
|
|
class="detail-view">
|
|
|
|
detail view
|
|
|
|
</a>
|
2015-11-23 19:02:19 -05:00
|
|
|
<a [routerLink]="['/Detail']"
|
2015-11-02 18:11:57 -05:00
|
|
|
class="detail-view-self"
|
|
|
|
target="_self">
|
|
|
|
detail view with _self target
|
|
|
|
</a>
|
2015-11-23 19:02:19 -05:00
|
|
|
<a [routerLink]="['/Detail']"
|
2015-11-02 18:11:57 -05:00
|
|
|
class="detail-view-blank"
|
|
|
|
target="_blank">
|
|
|
|
detail view with _blank target
|
|
|
|
</a>
|
2015-07-02 20:38:19 -04:00
|
|
|
</div>`,
|
|
|
|
directives: [RouterLink]
|
|
|
|
})
|
|
|
|
class TestComponent {
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeDummyLocation() {
|
2015-08-26 14:41:41 -04:00
|
|
|
var dl = new SpyLocation();
|
2016-06-08 18:45:15 -04:00
|
|
|
dl.spy('prepareExternalUrl').andCallFake((url: any /** TODO #9100 */) => url);
|
2015-07-02 20:38:19 -04:00
|
|
|
return dl;
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeDummyRouter() {
|
2015-08-26 14:41:41 -04:00
|
|
|
var dr = new SpyRouter();
|
2016-06-08 18:45:15 -04:00
|
|
|
dr.spy('generate').andCallFake((routeParams: any /** TODO #9100 */) => dummyInstruction);
|
|
|
|
dr.spy('isRouteActive').andCallFake((_: any /** TODO #9100 */) => false);
|
2015-07-17 16:36:53 -04:00
|
|
|
dr.spy('navigateInstruction');
|
2015-07-02 20:38:19 -04:00
|
|
|
return dr;
|
|
|
|
}
|