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

Commit f0c6ebc

Browse files
petrovalexpkozlowski-opensource
authored andcommitted
feat($timeout-mock): add verifyNoPendingTasks method
added verifyNoPendingTasks method, which throws error if not all deferred tasks have been flushed Closes #1245
1 parent 59d9b89 commit f0c6ebc

File tree

2 files changed

+59
-17
lines changed

2 files changed

+59
-17
lines changed

src/ngMock/angular-mocks.js

+43-17
Original file line numberDiff line numberDiff line change
@@ -1328,17 +1328,49 @@ function MockXhr() {
13281328
* @description
13291329
*
13301330
* This service is just a simple decorator for {@link ng.$timeout $timeout} service
1331-
* that adds a "flush" method.
1332-
*/
1331+
* that adds a "flush" and "verifyNoPendingTasks" methods.
1332+
*/
13331333

1334-
/**
1335-
* @ngdoc method
1336-
* @name ngMock.$timeout#flush
1337-
* @methodOf ngMock.$timeout
1338-
* @description
1339-
*
1340-
* Flushes the queue of pending tasks.
1341-
*/
1334+
angular.mock.$TimeoutDecorator = function($delegate, $browser) {
1335+
1336+
/**
1337+
* @ngdoc method
1338+
* @name ngMock.$timeout#flush
1339+
* @methodOf ngMock.$timeout
1340+
* @description
1341+
*
1342+
* Flushes the queue of pending tasks.
1343+
*/
1344+
$delegate.flush = function() {
1345+
$browser.defer.flush();
1346+
};
1347+
1348+
/**
1349+
* @ngdoc method
1350+
* @name ngMock.$timeout#verifyNoPendingTasks
1351+
* @methodOf ngMock.$timeout
1352+
* @description
1353+
*
1354+
* Verifies that there are no pending tasks that need to be flushed.
1355+
*/
1356+
$delegate.verifyNoPendingTasks = function() {
1357+
if ($browser.deferredFns.length) {
1358+
throw Error('Deferred tasks to flush (' + $browser.deferredFns.length + '): ' +
1359+
formatPendingTasksAsString($browser.deferredFns));
1360+
}
1361+
};
1362+
1363+
function formatPendingTasksAsString(tasks) {
1364+
var result = [];
1365+
angular.forEach(tasks, function(task) {
1366+
result.push('{id: ' + task.id + ', ' + 'time: ' + task.time + '}');
1367+
});
1368+
1369+
return result.join(', ');
1370+
}
1371+
1372+
return $delegate;
1373+
};
13421374

13431375
/**
13441376
*
@@ -1364,15 +1396,9 @@ angular.module('ngMock', ['ng']).provider({
13641396
$httpBackend: angular.mock.$HttpBackendProvider,
13651397
$rootElement: angular.mock.$RootElementProvider
13661398
}).config(function($provide) {
1367-
$provide.decorator('$timeout', function($delegate, $browser) {
1368-
$delegate.flush = function() {
1369-
$browser.defer.flush();
1370-
};
1371-
return $delegate;
1372-
});
1399+
$provide.decorator('$timeout', angular.mock.$TimeoutDecorator);
13731400
});
13741401

1375-
13761402
/**
13771403
* @ngdoc overview
13781404
* @name ngMockE2E

test/ngMock/angular-mocksSpec.js

+16
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,22 @@ describe('ngMock', function() {
327327
$timeout.flush();
328328
expect(logger).toEqual(['t1', 't3', 't2']);
329329
}));
330+
331+
332+
it('should throw an exception when not flushed', inject(function($timeout){
333+
$timeout(noop);
334+
335+
var expectedError = 'Deferred tasks to flush (1): {id: 0, time: 0}';
336+
expect(function() {$timeout.verifyNoPendingTasks();}).toThrow(expectedError);
337+
}));
338+
339+
340+
it('should do nothing when all tasks have been flushed', inject(function($timeout) {
341+
$timeout(noop);
342+
343+
$timeout.flush();
344+
expect(function() {$timeout.verifyNoPendingTasks();}).not.toThrow();
345+
}));
330346
});
331347

332348

0 commit comments

Comments
 (0)