@@ -12,116 +12,143 @@ import {
1212 inject ,
1313 beforeEachProviders ,
1414 it ,
15- xit
15+ xit ,
16+ fakeAsync ,
17+ tick
1618} from 'angular2/testing_internal' ;
1719import { provide , Component , ComponentResolver } from 'angular2/core' ;
1820
21+
1922import {
2023 Router ,
2124 RouterOutletMap ,
2225 RouteSegment ,
2326 Route ,
2427 ROUTER_DIRECTIVES ,
2528 Routes ,
26- RouterUrlParser ,
27- DefaultRouterUrlParser ,
29+ RouterUrlSerializer ,
30+ DefaultRouterUrlSerializer ,
2831 OnActivate
2932} from 'angular2/alt_router' ;
33+ import { DOM } from 'angular2/src/platform/dom/dom_adapter' ;
3034
3135export function main ( ) {
3236 describe ( 'navigation' , ( ) => {
3337 beforeEachProviders ( ( ) => [
34- provide ( RouterUrlParser , { useClass : DefaultRouterUrlParser } ) ,
38+ provide ( RouterUrlSerializer , { useClass : DefaultRouterUrlSerializer } ) ,
3539 RouterOutletMap ,
3640 provide ( Router ,
3741 {
3842 useFactory : ( resolver , urlParser , outletMap ) =>
3943 new Router ( RootCmp , resolver , urlParser , outletMap ) ,
40- deps : [ ComponentResolver , RouterUrlParser , RouterOutletMap ]
44+ deps : [ ComponentResolver , RouterUrlSerializer , RouterOutletMap ]
4145 } )
4246 ] ) ;
4347
4448 it ( 'should support nested routes' ,
45- inject ( [ AsyncTestCompleter , Router , TestComponentBuilder ] , ( async , router , tcb ) => {
46- let fixture ;
47- compileRoot ( tcb )
48- . then ( ( rtc ) => { fixture = rtc } )
49- . then ( ( _ ) => router . navigateByUrl ( '/team/22/user/victor' ) )
50- . then ( ( _ ) => {
51- fixture . detectChanges ( ) ;
52- expect ( fixture . debugElement . nativeElement )
53- . toHaveText ( 'team 22 { hello victor, aux: }' ) ;
54- async . done ( ) ;
55- } ) ;
56- } ) ) ;
49+ fakeAsync ( inject ( [ Router , TestComponentBuilder ] , ( router , tcb ) => {
50+ let fixture = tcb . createFakeAsync ( RootCmp ) ;
51+
52+ router . navigateByUrl ( '/team/22/user/victor' ) ;
53+ advance ( fixture ) ;
54+
55+ expect ( fixture . debugElement . nativeElement ) . toHaveText ( 'team 22 { hello victor, aux: }' ) ;
56+ } ) ) ) ;
5757
5858 it ( 'should support aux routes' ,
59- inject ( [ AsyncTestCompleter , Router , TestComponentBuilder ] , ( async , router , tcb ) => {
60- let fixture ;
61- compileRoot ( tcb )
62- . then ( ( rtc ) => { fixture = rtc } )
63- . then ( ( _ ) => router . navigateByUrl ( '/team/22/user/victor(/simple)' ) )
64- . then ( ( _ ) => {
65- fixture . detectChanges ( ) ;
66- expect ( fixture . debugElement . nativeElement )
67- . toHaveText ( 'team 22 { hello victor, aux: simple }' ) ;
68- async . done ( ) ;
69- } ) ;
70- } ) ) ;
71-
72- it ( 'should unload outlets' ,
73- inject ( [ AsyncTestCompleter , Router , TestComponentBuilder ] , ( async , router , tcb ) => {
74- let fixture ;
75- compileRoot ( tcb )
76- . then ( ( rtc ) => { fixture = rtc } )
77- . then ( ( _ ) => router . navigateByUrl ( '/team/22/user/victor(/simple)' ) )
78- . then ( ( _ ) => router . navigateByUrl ( '/team/22/user/victor' ) )
79- . then ( ( _ ) => {
80- fixture . detectChanges ( ) ;
81- expect ( fixture . debugElement . nativeElement )
82- . toHaveText ( 'team 22 { hello victor, aux: }' ) ;
83- async . done ( ) ;
84- } ) ;
85- } ) ) ;
59+ fakeAsync ( inject ( [ Router , TestComponentBuilder ] , ( router , tcb ) => {
60+ let fixture = tcb . createFakeAsync ( RootCmp ) ;
61+
62+ router . navigateByUrl ( '/team/22/user/victor(/simple)' ) ;
63+ advance ( fixture ) ;
64+
65+ expect ( fixture . debugElement . nativeElement )
66+ . toHaveText ( 'team 22 { hello victor, aux: simple }' ) ;
67+ } ) ) ) ;
68+
69+ it ( 'should unload outlets' , fakeAsync ( inject ( [ Router , TestComponentBuilder ] , ( router , tcb ) => {
70+ let fixture = tcb . createFakeAsync ( RootCmp ) ;
71+
72+ router . navigateByUrl ( '/team/22/user/victor(/simple)' ) ;
73+ advance ( fixture ) ;
74+
75+ router . navigateByUrl ( '/team/22/user/victor' ) ;
76+ advance ( fixture ) ;
77+
78+ expect ( fixture . debugElement . nativeElement ) . toHaveText ( 'team 22 { hello victor, aux: }' ) ;
79+ } ) ) ) ;
8680
8781 it ( 'should unload nested outlets' ,
88- inject ( [ AsyncTestCompleter , Router , TestComponentBuilder ] , ( async , router , tcb ) => {
89- let fixture ;
90- compileRoot ( tcb )
91- . then ( ( rtc ) => { fixture = rtc } )
92- . then ( ( _ ) => router . navigateByUrl ( '/team/22/user/victor(/simple)' ) )
93- . then ( ( _ ) => router . navigateByUrl ( '/' ) )
94- . then ( ( _ ) => {
95- fixture . detectChanges ( ) ;
96- expect ( fixture . debugElement . nativeElement ) . toHaveText ( '' ) ;
97- async . done ( ) ;
98- } ) ;
99- } ) ) ;
82+ fakeAsync ( inject ( [ Router , TestComponentBuilder ] , ( router , tcb ) => {
83+ let fixture = tcb . createFakeAsync ( RootCmp ) ;
84+
85+ router . navigateByUrl ( '/team/22/user/victor(/simple)' ) ;
86+ advance ( fixture ) ;
87+
88+ router . navigateByUrl ( '/' ) ;
89+ advance ( fixture ) ;
90+
91+ expect ( fixture . debugElement . nativeElement ) . toHaveText ( '' ) ;
92+ } ) ) ) ;
10093
10194 it ( 'should update nested routes when url changes' ,
102- inject ( [ AsyncTestCompleter , Router , TestComponentBuilder ] , ( async , router , tcb ) => {
103- let fixture ;
104- let team1 ;
105- let team2 ;
106- compileRoot ( tcb )
107- . then ( ( rtc ) => { fixture = rtc } )
108- . then ( ( _ ) => router . navigateByUrl ( '/team/22/user/victor' ) )
109- . then ( ( _ ) => { team1 = fixture . debugElement . children [ 1 ] . componentInstance ; } )
110- . then ( ( _ ) => router . navigateByUrl ( '/team/22/user/fedor' ) )
111- . then ( ( _ ) => { team2 = fixture . debugElement . children [ 1 ] . componentInstance ; } )
112- . then ( ( _ ) => {
113- fixture . detectChanges ( ) ;
114- expect ( team1 ) . toBe ( team2 ) ;
115- expect ( fixture . debugElement . nativeElement )
116- . toHaveText ( 'team 22 { hello fedor, aux: }' ) ;
117- async . done ( ) ;
118- } ) ;
119- } ) ) ;
120-
121- // unload unused nodes
95+ fakeAsync ( inject ( [ Router , TestComponentBuilder ] , ( router , tcb ) => {
96+ let fixture = tcb . createFakeAsync ( RootCmp ) ;
97+
98+ router . navigateByUrl ( '/team/22/user/victor' ) ;
99+ advance ( fixture ) ;
100+ let team1 = fixture . debugElement . children [ 1 ] . componentInstance ;
101+
102+ router . navigateByUrl ( '/team/22/user/fedor' ) ;
103+ advance ( fixture ) ;
104+ let team2 = fixture . debugElement . children [ 1 ] . componentInstance ;
105+
106+ expect ( team1 ) . toBe ( team2 ) ;
107+ expect ( fixture . debugElement . nativeElement ) . toHaveText ( 'team 22 { hello fedor, aux: }' ) ;
108+ } ) ) ) ;
109+
110+ it ( "should support router links" ,
111+ fakeAsync ( inject ( [ Router , TestComponentBuilder ] , ( router , tcb ) => {
112+ let fixture = tcb . createFakeAsync ( RootCmp ) ;
113+ advance ( fixture ) ;
114+
115+ router . navigateByUrl ( '/team/22/link' ) ;
116+ advance ( fixture ) ;
117+ expect ( fixture . debugElement . nativeElement ) . toHaveText ( 'team 22 { link, aux: }' ) ;
118+
119+ let native = DOM . querySelector ( fixture . debugElement . nativeElement , "a" ) ;
120+ expect ( DOM . getAttribute ( native , "href" ) ) . toEqual ( "/team/33/simple" ) ;
121+ DOM . dispatchEvent ( native , DOM . createMouseEvent ( 'click' ) ) ;
122+ advance ( fixture ) ;
123+
124+ expect ( fixture . debugElement . nativeElement ) . toHaveText ( 'team 33 { simple, aux: }' ) ;
125+ } ) ) ) ;
126+
127+ it ( "should update router links when router changes" ,
128+ fakeAsync ( inject ( [ Router , TestComponentBuilder ] , ( router , tcb ) => {
129+ let fixture = tcb . createFakeAsync ( RootCmp ) ;
130+ advance ( fixture ) ;
131+
132+ router . navigateByUrl ( '/team/22/link(simple)' ) ;
133+ advance ( fixture ) ;
134+ expect ( fixture . debugElement . nativeElement ) . toHaveText ( 'team 22 { link, aux: simple }' ) ;
135+
136+ let native = DOM . querySelector ( fixture . debugElement . nativeElement , "a" ) ;
137+ expect ( DOM . getAttribute ( native , "href" ) ) . toEqual ( "/team/33/simple(aux:simple)" ) ;
138+
139+ router . navigateByUrl ( '/team/22/link(simple2)' ) ;
140+ advance ( fixture ) ;
141+
142+ expect ( DOM . getAttribute ( native , "href" ) ) . toEqual ( "/team/33/simple(aux:simple2)" ) ;
143+ } ) ) ) ;
122144 } ) ;
123145}
124146
147+ function advance ( fixture : ComponentFixture ) : void {
148+ tick ( ) ;
149+ fixture . detectChanges ( ) ;
150+ }
151+
125152function compileRoot ( tcb : TestComponentBuilder ) : Promise < ComponentFixture > {
126153 return tcb . createAsync ( RootCmp ) ;
127154}
@@ -136,14 +163,28 @@ class UserCmp implements OnActivate {
136163class SimpleCmp {
137164}
138165
166+ @Component ( { selector : 'simple2-cmp' , template : `simple2` } )
167+ class Simple2Cmp {
168+ }
169+
170+ @Component ( {
171+ selector : 'link-cmp' ,
172+ template : `<a [routerLink]="['team', '33', 'simple']">link</a>` ,
173+ directives : ROUTER_DIRECTIVES
174+ } )
175+ class LinkCmp {
176+ }
177+
139178@Component ( {
140179 selector : 'team-cmp' ,
141180 template : `team {{id}} { <router-outlet></router-outlet>, aux: <router-outlet name="aux"></router-outlet> }` ,
142181 directives : [ ROUTER_DIRECTIVES ]
143182} )
144183@Routes ( [
145184 new Route ( { path : 'user/:name' , component : UserCmp } ) ,
146- new Route ( { path : 'simple' , component : SimpleCmp } )
185+ new Route ( { path : 'simple' , component : SimpleCmp } ) ,
186+ new Route ( { path : 'simple2' , component : Simple2Cmp } ) ,
187+ new Route ( { path : 'link' , component : LinkCmp } )
147188] )
148189class TeamCmp implements OnActivate {
149190 id : string ;
0 commit comments