@@ -303,7 +303,7 @@ describe('$route', function() {
303
303
event . preventDefault ( ) ;
304
304
} ) ;
305
305
306
- $rootScope . $on ( '$beforeRouteChange ' , function ( event ) {
306
+ $rootScope . $on ( '$routeChangeSuccess ' , function ( event ) {
307
307
throw new Error ( 'Should not get here' ) ;
308
308
} ) ;
309
309
@@ -1214,34 +1214,107 @@ describe('$route', function() {
1214
1214
} ) ;
1215
1215
1216
1216
describe ( 'reload' , function ( ) {
1217
+ var $location ;
1218
+ var $log ;
1219
+ var $rootScope ;
1220
+ var $route ;
1221
+ var routeChangeStartSpy ;
1222
+ var routeChangeSuccessSpy ;
1223
+
1224
+ beforeEach ( module ( function ( $routeProvider ) {
1225
+ $routeProvider . when ( '/bar/:barId' , {
1226
+ template : '' ,
1227
+ controller : controller ,
1228
+ reloadOnSearch : false
1229
+ } ) ;
1217
1230
1218
- it ( 'should reload even if reloadOnSearch is false' , function ( ) {
1219
- var routeChangeSpy = jasmine . createSpy ( 'route change' ) ;
1231
+ function controller ( $log ) {
1232
+ $log . debug ( 'initialized' ) ;
1233
+ }
1234
+ } ) ) ;
1235
+ beforeEach ( inject ( function ( $compile , _$location_ , _$log_ , _$rootScope_ , _$route_ ) {
1236
+ $location = _$location_ ;
1237
+ $log = _$log_ ;
1238
+ $rootScope = _$rootScope_ ;
1239
+ $route = _$route_ ;
1220
1240
1221
- module ( function ( $routeProvider ) {
1222
- $routeProvider . when ( '/bar/:barId' , { controller : angular . noop , reloadOnSearch : false } ) ;
1223
- } ) ;
1241
+ routeChangeStartSpy = jasmine . createSpy ( 'routeChangeStart' ) ;
1242
+ routeChangeSuccessSpy = jasmine . createSpy ( 'routeChangeSuccess' ) ;
1224
1243
1225
- inject ( function ( $route , $location , $rootScope , $routeParams ) {
1226
- $rootScope . $on ( '$routeChangeSuccess' , routeChangeSpy ) ;
1244
+ $rootScope . $on ( '$routeChangeStart' , routeChangeStartSpy ) ;
1245
+ $rootScope . $on ( '$routeChangeSuccess' , routeChangeSuccessSpy ) ;
1227
1246
1228
- $location . path ( '/bar/123' ) ;
1229
- $rootScope . $digest ( ) ;
1230
- expect ( $routeParams ) . toEqual ( { barId :'123' } ) ;
1231
- expect ( routeChangeSpy ) . toHaveBeenCalledOnce ( ) ;
1232
- routeChangeSpy . reset ( ) ;
1247
+ element = $compile ( '<div><div ng-view></div></div>' ) ( $rootScope ) ;
1248
+ } ) ) ;
1233
1249
1234
- $location . path ( '/bar/123' ) . search ( 'a=b' ) ;
1235
- $rootScope . $digest ( ) ;
1236
- expect ( $routeParams ) . toEqual ( { barId :'123' , a :'b' } ) ;
1237
- expect ( routeChangeSpy ) . not . toHaveBeenCalled ( ) ;
1250
+ it ( 'should reload the current route' , function ( ) {
1251
+ $location . path ( '/bar/123' ) ;
1252
+ $rootScope . $digest ( ) ;
1253
+ expect ( $location . path ( ) ) . toBe ( '/bar/123' ) ;
1254
+ expect ( routeChangeStartSpy ) . toHaveBeenCalledOnce ( ) ;
1255
+ expect ( routeChangeSuccessSpy ) . toHaveBeenCalledOnce ( ) ;
1256
+ expect ( $log . debug . logs ) . toEqual ( [ [ 'initialized' ] ] ) ;
1238
1257
1239
- $route . reload ( ) ;
1240
- $rootScope . $digest ( ) ;
1241
- expect ( $routeParams ) . toEqual ( { barId :'123' , a :'b' } ) ;
1242
- expect ( routeChangeSpy ) . toHaveBeenCalledOnce ( ) ;
1243
- } ) ;
1258
+ routeChangeStartSpy . reset ( ) ;
1259
+ routeChangeSuccessSpy . reset ( ) ;
1260
+ $log . reset ( ) ;
1261
+
1262
+ $route . reload ( ) ;
1263
+ $rootScope . $digest ( ) ;
1264
+ expect ( $location . path ( ) ) . toBe ( '/bar/123' ) ;
1265
+ expect ( routeChangeStartSpy ) . toHaveBeenCalledOnce ( ) ;
1266
+ expect ( routeChangeSuccessSpy ) . toHaveBeenCalledOnce ( ) ;
1267
+ expect ( $log . debug . logs ) . toEqual ( [ [ 'initialized' ] ] ) ;
1268
+
1269
+ $log . reset ( ) ;
1270
+ } ) ;
1271
+
1272
+ it ( 'should support preventing a route reload' , function ( ) {
1273
+ $location . path ( '/bar/123' ) ;
1274
+ $rootScope . $digest ( ) ;
1275
+ expect ( $location . path ( ) ) . toBe ( '/bar/123' ) ;
1276
+ expect ( routeChangeStartSpy ) . toHaveBeenCalledOnce ( ) ;
1277
+ expect ( routeChangeSuccessSpy ) . toHaveBeenCalledOnce ( ) ;
1278
+ expect ( $log . debug . logs ) . toEqual ( [ [ 'initialized' ] ] ) ;
1279
+
1280
+ routeChangeStartSpy . reset ( ) ;
1281
+ routeChangeSuccessSpy . reset ( ) ;
1282
+ $log . reset ( ) ;
1283
+
1284
+ routeChangeStartSpy . andCallFake ( function ( evt ) { evt . preventDefault ( ) ; } ) ;
1285
+
1286
+ $route . reload ( ) ;
1287
+ $rootScope . $digest ( ) ;
1288
+ expect ( $location . path ( ) ) . toBe ( '/bar/123' ) ;
1289
+ expect ( routeChangeStartSpy ) . toHaveBeenCalledOnce ( ) ;
1290
+ expect ( routeChangeSuccessSpy ) . not . toHaveBeenCalled ( ) ;
1291
+ expect ( $log . debug . logs ) . toEqual ( [ ] ) ;
1244
1292
} ) ;
1293
+
1294
+ it ( 'should reload even if reloadOnSearch is false' , inject ( function ( $routeParams ) {
1295
+ $location . path ( '/bar/123' ) ;
1296
+ $rootScope . $digest ( ) ;
1297
+ expect ( $routeParams ) . toEqual ( { barId : '123' } ) ;
1298
+ expect ( routeChangeSuccessSpy ) . toHaveBeenCalledOnce ( ) ;
1299
+ expect ( $log . debug . logs ) . toEqual ( [ [ 'initialized' ] ] ) ;
1300
+
1301
+ routeChangeSuccessSpy . reset ( ) ;
1302
+ $log . reset ( ) ;
1303
+
1304
+ $location . search ( 'a=b' ) ;
1305
+ $rootScope . $digest ( ) ;
1306
+ expect ( $routeParams ) . toEqual ( { barId : '123' , a : 'b' } ) ;
1307
+ expect ( routeChangeSuccessSpy ) . not . toHaveBeenCalled ( ) ;
1308
+ expect ( $log . debug . logs ) . toEqual ( [ ] ) ;
1309
+
1310
+ $route . reload ( ) ;
1311
+ $rootScope . $digest ( ) ;
1312
+ expect ( $routeParams ) . toEqual ( { barId : '123' , a : 'b' } ) ;
1313
+ expect ( routeChangeSuccessSpy ) . toHaveBeenCalledOnce ( ) ;
1314
+ expect ( $log . debug . logs ) . toEqual ( [ [ 'initialized' ] ] ) ;
1315
+
1316
+ $log . reset ( ) ;
1317
+ } ) ) ;
1245
1318
} ) ;
1246
1319
} ) ;
1247
1320
0 commit comments