This repository was archived by the owner on Apr 12, 2024. It is now read-only.
File tree 3 files changed +19
-12
lines changed
3 files changed +19
-12
lines changed Original file line number Diff line number Diff line change @@ -43,11 +43,14 @@ angular.mock.$Browser = function() {
43
43
var outstandingRequestCallbacks = [ ] ;
44
44
self . $$incOutstandingRequestCount = function ( ) { outstandingRequestCount ++ ; } ;
45
45
self . $$completeOutstandingRequest = function ( fn ) {
46
- fn ( ) ;
47
- outstandingRequestCount -- ;
48
- if ( ! outstandingRequestCount ) {
49
- while ( outstandingRequestCallbacks . length ) {
50
- outstandingRequestCallbacks . pop ( ) ( ) ;
46
+ try {
47
+ fn ( ) ;
48
+ } finally {
49
+ outstandingRequestCount -- ;
50
+ if ( ! outstandingRequestCount ) {
51
+ while ( outstandingRequestCallbacks . length ) {
52
+ outstandingRequestCallbacks . pop ( ) ( ) ;
53
+ }
51
54
}
52
55
}
53
56
} ;
@@ -82,6 +85,8 @@ angular.mock.$Browser = function() {
82
85
self . deferredNextId = 0 ;
83
86
84
87
self . defer = function ( fn , delay ) {
88
+ // Note that we do not use `$$incOutstandingRequestCount` or `$$completeOutstandingRequest`
89
+ // in this mock implementation.
85
90
delay = delay || 0 ;
86
91
self . deferredFns . push ( { time :( self . defer . now + delay ) , fn :fn , id : self . deferredNextId } ) ;
87
92
self . deferredFns . sort ( function ( a , b ) { return a . time - b . time ; } ) ;
Original file line number Diff line number Diff line change @@ -706,6 +706,11 @@ function $RouteProvider() {
706
706
$rootScope . $broadcast ( '$routeChangeError' , nextRoute , lastRoute , error ) ;
707
707
}
708
708
} ) . finally ( function ( ) {
709
+ // Because `commitRoute()` is called from a `$rootScope.$evalAsync` block (see
710
+ // `$locationWatch`), this `$$completeOutstandingRequest()` call will not cause
711
+ // `outstandingRequestCount` to hit zero. This is important in case we are redirecting
712
+ // to a new route which also requires some asynchronous work.
713
+
709
714
$browser . $$completeOutstandingRequest ( noop ) ;
710
715
} ) ;
711
716
}
Original file line number Diff line number Diff line change @@ -2199,13 +2199,10 @@ describe('$route', function() {
2199
2199
2200
2200
module ( function ( $provide , $routeProvider ) {
2201
2201
// While normally `$browser.defer()` modifies the `outstandingRequestCount`, the mocked
2202
- // version (provided by `ngMock`) does not. This doesn't matter in most tests, but it does
2203
- // here:
2204
- // `$browser.defer()` will be indirectly called as a result of `$locationWatch`'s call to
2205
- // `$rootScope.$evalAsync()`. In the async function, the `$locationChangeSuccess` event will
2206
- // be broadcasted, which will in turn trigger `commitRoute()` in `ngRoute`.
2207
- // `outstandingRequestCount` must not reach 0 during the time between calling
2208
- // `$rootScope.$evalAsync()` and executing the async function.
2202
+ // version (provided by `ngMock`) does not. This doesn't matter in most tests, but in this
2203
+ // case we need the `outstandingRequestCount` logic to ensure that we don't call the
2204
+ // `$$testability.whenStable()` callbacks part way through a `$rootScope.$evalAsync` block.
2205
+ // See ngRoute's commitRoute()'s finally() block for details.
2209
2206
$provide . decorator ( '$browser' , function ( $delegate ) {
2210
2207
var oldDefer = $delegate . defer ;
2211
2208
var newDefer = function ( fn , delay ) {
You can’t perform that action at this time.
0 commit comments