@@ -27,7 +27,8 @@ import {
2727 RouteParams ,
2828 Router ,
2929 appBaseHrefToken ,
30- routerDirectives
30+ routerDirectives ,
31+ HashLocationStrategy
3132} from 'angular2/router' ;
3233
3334import { LocationStrategy } from 'angular2/src/router/location_strategy' ;
@@ -81,6 +82,57 @@ export function main() {
8182 } ) ) ;
8283 } ) ;
8384
85+ describe ( 'back button app' , ( ) => {
86+ beforeEachBindings ( ( ) => { return [ bind ( appComponentTypeToken ) . toValue ( HierarchyAppCmp ) ] ; } ) ;
87+
88+ it ( 'should change the url without pushing a new history state for back navigations' ,
89+ inject ( [ AsyncTestCompleter , TestComponentBuilder ] , ( async , tcb : TestComponentBuilder ) => {
90+
91+ tcb . createAsync ( HierarchyAppCmp )
92+ . then ( ( rootTC ) => {
93+ var router = rootTC . componentInstance . router ;
94+ var position = 0 ;
95+ var flipped = false ;
96+ var history =
97+ [
98+ [ '/parent/child' , 'root { parent { hello } }' , '/super-parent/child' ] ,
99+ [ '/super-parent/child' , 'root { super-parent { hello2 } }' , '/parent/child' ] ,
100+ [ '/parent/child' , 'root { parent { hello } }' , false ]
101+ ]
102+
103+ router . subscribe ( ( _ ) => {
104+ var location = rootTC . componentInstance . location ;
105+ var element = rootTC . nativeElement ;
106+ var path = location . path ( ) ;
107+
108+ var entry = history [ position ] ;
109+
110+ expect ( path ) . toEqual ( entry [ 0 ] ) ;
111+ expect ( element ) . toHaveText ( entry [ 1 ] ) ;
112+
113+ var nextUrl = entry [ 2 ] ;
114+ if ( nextUrl == false ) {
115+ flipped = true ;
116+ }
117+
118+ if ( flipped && position == 0 ) {
119+ async . done ( ) ;
120+ return ;
121+ }
122+
123+ position = position + ( flipped ? - 1 : 1 ) ;
124+ if ( flipped ) {
125+ location . back ( ) ;
126+ } else {
127+ router . navigate ( nextUrl ) ;
128+ }
129+ } ) ;
130+
131+ router . navigate ( history [ 0 ] [ 0 ] ) ;
132+ } ) ;
133+ } ) ) ;
134+ } ) ;
135+
84136 describe ( 'hierarchical app' , ( ) => {
85137 beforeEachBindings ( ( ) => { return [ bind ( appComponentTypeToken ) . toValue ( HierarchyAppCmp ) ] ; } ) ;
86138
@@ -153,6 +205,11 @@ export function main() {
153205class HelloCmp {
154206}
155207
208+ @Component ( { selector : 'hello2-cmp' } )
209+ @View ( { template : 'hello2' } )
210+ class Hello2Cmp {
211+ }
212+
156213@Component ( { selector : 'app-cmp' } )
157214@View ( { template : "outer { <router-outlet></router-outlet> }" , directives : routerDirectives } )
158215@RouteConfig ( [ new Route ( { path : '/' , component : HelloCmp } ) ] )
@@ -166,9 +223,18 @@ class AppCmp {
166223class ParentCmp {
167224}
168225
226+ @Component ( { selector : 'super-parent-cmp' } )
227+ @View ( { template : `super-parent { <router-outlet></router-outlet> }` , directives : routerDirectives } )
228+ @RouteConfig ( [ new Route ( { path : '/child' , component : Hello2Cmp } ) ] )
229+ class SuperParentCmp {
230+ }
231+
169232@Component ( { selector : 'app-cmp' } )
170233@View ( { template : `root { <router-outlet></router-outlet> }` , directives : routerDirectives } )
171- @RouteConfig ( [ new Route ( { path : '/parent/...' , component : ParentCmp } ) ] )
234+ @RouteConfig ( [
235+ new Route ( { path : '/parent/...' , component : ParentCmp } ) ,
236+ new Route ( { path : '/super-parent/...' , component : SuperParentCmp } )
237+ ] )
172238class HierarchyAppCmp {
173239 constructor ( public router : Router , public location : LocationStrategy ) { }
174240}
0 commit comments