mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-22 05:11:51 +00:00
feat($timeout-mock): add verifyNoPendingTasks method
added verifyNoPendingTasks method, which throws error if not all deferred tasks have been flushed Closes #1245
This commit is contained in:
parent
59d9b89852
commit
f0c6ebc076
2 changed files with 59 additions and 17 deletions
60
src/ngMock/angular-mocks.js
vendored
60
src/ngMock/angular-mocks.js
vendored
|
|
@ -1328,17 +1328,49 @@ function MockXhr() {
|
||||||
* @description
|
* @description
|
||||||
*
|
*
|
||||||
* This service is just a simple decorator for {@link ng.$timeout $timeout} service
|
* 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.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
angular.mock.$TimeoutDecorator = function($delegate, $browser) {
|
||||||
* @ngdoc method
|
|
||||||
* @name ngMock.$timeout#flush
|
/**
|
||||||
* @methodOf ngMock.$timeout
|
* @ngdoc method
|
||||||
* @description
|
* @name ngMock.$timeout#flush
|
||||||
*
|
* @methodOf ngMock.$timeout
|
||||||
* Flushes the queue of pending tasks.
|
* @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;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
|
@ -1364,15 +1396,9 @@ angular.module('ngMock', ['ng']).provider({
|
||||||
$httpBackend: angular.mock.$HttpBackendProvider,
|
$httpBackend: angular.mock.$HttpBackendProvider,
|
||||||
$rootElement: angular.mock.$RootElementProvider
|
$rootElement: angular.mock.$RootElementProvider
|
||||||
}).config(function($provide) {
|
}).config(function($provide) {
|
||||||
$provide.decorator('$timeout', function($delegate, $browser) {
|
$provide.decorator('$timeout', angular.mock.$TimeoutDecorator);
|
||||||
$delegate.flush = function() {
|
|
||||||
$browser.defer.flush();
|
|
||||||
};
|
|
||||||
return $delegate;
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ngdoc overview
|
* @ngdoc overview
|
||||||
* @name ngMockE2E
|
* @name ngMockE2E
|
||||||
|
|
|
||||||
16
test/ngMock/angular-mocksSpec.js
vendored
16
test/ngMock/angular-mocksSpec.js
vendored
|
|
@ -327,6 +327,22 @@ describe('ngMock', function() {
|
||||||
$timeout.flush();
|
$timeout.flush();
|
||||||
expect(logger).toEqual(['t1', 't3', 't2']);
|
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();
|
||||||
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue