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

feat($timeout-mock): verify no pending tasks #1368

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
60 changes: 43 additions & 17 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1329,17 +1329,49 @@ function MockXhr() {
* @description
*
* This service is just a simple decorator for {@link ng.$timeout $timeout} service
* that adds a "flush" method.
*/
* that adds a "flush" and "verifyNoPendingTasks" methods.
*/

/**
* @ngdoc method
* @name ngMock.$timeout#flush
* @methodOf ngMock.$timeout
* @description
*
* Flushes the queue of pending tasks.
*/
angular.mock.$TimeoutDecorator = function($delegate, $browser) {

/**
* @ngdoc method
* @name ngMock.$timeout#flush
* @methodOf ngMock.$timeout
* @description
*
* Flushes the queue of pending tasks.
*/
$delegate.flush = function() {
$browser.defer.flush();
};

/**
* @ngdoc method
* @name ngMock.$timeout#verifyNoPendingTasks
* @methodOf ngMock.$timeout
* @description
*
* Verifies that there are no pending tasks that need to be flushed.
*/
$delegate.verifyNoPendingTasks = function() {
if ($browser.deferredFns.length) {
throw Error('Deferred tasks to flush (' + $browser.deferredFns.length + '): ' +
formatPendingTasksAsString($browser.deferredFns));
}
};

function formatPendingTasksAsString(tasks) {
var result = [];
angular.forEach(tasks, function(task) {
result.push('{id: ' + task.id + ', ' + 'time: ' + task.time + '}');
});

return result.join(', ');
}

return $delegate;
};

/**
*
Expand All @@ -1365,15 +1397,9 @@ angular.module('ngMock', ['ng']).provider({
$httpBackend: angular.mock.$HttpBackendProvider,
$rootElement: angular.mock.$RootElementProvider
}).config(function($provide) {
$provide.decorator('$timeout', function($delegate, $browser) {
$delegate.flush = function() {
$browser.defer.flush();
};
return $delegate;
});
$provide.decorator('$timeout', angular.mock.$TimeoutDecorator);
});


/**
* @ngdoc overview
* @name ngMockE2E
Expand Down
16 changes: 16 additions & 0 deletions test/ngMock/angular-mocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,22 @@ describe('ngMock', function() {
$timeout.flush();
expect(logger).toEqual(['t1', 't3', 't2']);
}));


it('should throw an exception when not flushed', inject(function($timeout){
$timeout(noop);

var expectedError = 'Deferred tasks to flush (1): {id: 0, time: 0}';
expect(function() {$timeout.verifyNoPendingTasks();}).toThrow(expectedError);
}));


it('should do nothing when all tasks have been flushed', inject(function($timeout) {
$timeout(noop);

$timeout.flush();
expect(function() {$timeout.verifyNoPendingTasks();}).not.toThrow();
}));
});


Expand Down