From 24677d73e687247603a3eec2ccc0aa515ed82703 Mon Sep 17 00:00:00 2001 From: Michael Giambalvo Date: Fri, 19 Feb 2016 16:03:08 -0800 Subject: [PATCH 1/3] fix($$testability): Call notifiyWhenNoOutstandingRequests async Services such as $location and $http will initiate async calls. Thus, we need to wrap the call to $browser.notifyWhenNoOutstandingRequest in $timeout() in order to give any potentially outstanding async calls a chance to run. Fixes #13782 --- src/ng/testability.js | 10 +++++++--- test/ng/testabilitySpec.js | 31 ++++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/ng/testability.js b/src/ng/testability.js index c9d0afd2c38e..be7f241b8f48 100644 --- a/src/ng/testability.js +++ b/src/ng/testability.js @@ -2,8 +2,8 @@ function $$TestabilityProvider() { - this.$get = ['$rootScope', '$browser', '$location', - function($rootScope, $browser, $location) { + this.$get = ['$rootScope', '$browser', '$location', '$window', + function($rootScope, $browser, $location, $window) { /** * @name $testability @@ -109,7 +109,11 @@ function $$TestabilityProvider() { * @param {function} callback */ testability.whenStable = function(callback) { - $browser.notifyWhenNoOutstandingRequests(callback); + // Register the callback asynchronously, so that services that trigger + // async callbacks have a chance to run. + $rootScope.$evalAsync(function() { + $browser.notifyWhenNoOutstandingRequests(callback); + }); }; return testability; diff --git a/test/ng/testabilitySpec.js b/test/ng/testabilitySpec.js index ce40de5f4966..5b336fa2878c 100644 --- a/test/ng/testabilitySpec.js +++ b/test/ng/testabilitySpec.js @@ -189,10 +189,35 @@ describe('$$testability', function() { describe('waiting for stability', function() { it('should process callbacks immediately with no outstanding requests', - inject(function($$testability) { + inject(function($$testability, $timeout) { var callback = jasmine.createSpy('callback'); - $$testability.whenStable(callback); - expect(callback).toHaveBeenCalled(); + runs(function() { + $$testability.whenStable(callback); + $timeout.flush(); + }); + + waitsFor(function() { + return callback.calls.length > 0; + }, "The callback should be called.", 500); + })); + + it('should wait for new $http calls asynchronously', + inject(function($$testability, $httpBackend, $http, $timeout) { + var callback = jasmine.createSpy('callback'); + runs(function() { + $httpBackend.when('GET').respond(200); + + $http.get(''); + $$testability.whenStable(callback); + expect(callback).not.toHaveBeenCalled(); + + $httpBackend.flush(); + $timeout.flush(); + }); + + waitsFor(function() { + return callback.calls.length > 0; + }, "The callback should be called.", 500); })); }); }); From dca42a4448707b283052081a72f1c9e4681e88f5 Mon Sep 17 00:00:00 2001 From: Michael Giambalvo Date: Tue, 23 Feb 2016 10:10:30 -0800 Subject: [PATCH 2/3] Remove unneeded import. --- src/ng/testability.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ng/testability.js b/src/ng/testability.js index be7f241b8f48..202159de389b 100644 --- a/src/ng/testability.js +++ b/src/ng/testability.js @@ -2,8 +2,8 @@ function $$TestabilityProvider() { - this.$get = ['$rootScope', '$browser', '$location', '$window', - function($rootScope, $browser, $location, $window) { + this.$get = ['$rootScope', '$browser', '$location', + function($rootScope, $browser, $location) { /** * @name $testability From bfe8af0640fd97ad200b5f718eea8cf64f2fbbbe Mon Sep 17 00:00:00 2001 From: Michael Giambalvo Date: Mon, 14 Mar 2016 15:30:17 -0700 Subject: [PATCH 3/3] Make test description clearer. --- test/ng/testabilitySpec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ng/testabilitySpec.js b/test/ng/testabilitySpec.js index 5b336fa2878c..7e27fb52156a 100644 --- a/test/ng/testabilitySpec.js +++ b/test/ng/testabilitySpec.js @@ -188,7 +188,7 @@ describe('$$testability', function() { }); describe('waiting for stability', function() { - it('should process callbacks immediately with no outstanding requests', + it('should process callbacks without waiting if there are no outstanding requests', inject(function($$testability, $timeout) { var callback = jasmine.createSpy('callback'); runs(function() {