Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Wait for browser async 1.5 #14116

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/ng/testability.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 29 additions & 4 deletions test/ng/testabilitySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,36 @@ describe('$$testability', function() {
});

describe('waiting for stability', function() {
it('should process callbacks immediately with no outstanding requests',
inject(function($$testability) {
it('should process callbacks without waiting if there are no outstanding requests',
inject(function($$testability, $timeout) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description of this test looks incorrect now (as nothing gets invoked immediately).

var callback = jasmine.createSpy('callback');
$$testability.whenStable(callback);
expect(callback).toHaveBeenCalled();
runs(function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since unit tests run synchronously, you shouldn't need async tests (using runs/waitsFor). Have you tried without ?
(BTW, note that we have updated to Jasmine v2 now, so these tests won't run anymore (since runs/waitsFor is a Jasmine 1.x thing). But you shouldn't need them anyway (hopefully)...)

$$testability.whenStable(callback);
$timeout.flush();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed ? Wouldn't $rootScope.$digest() suffice ?

});

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);
}));
});
});