feat(mocks): make $timeout#flush throw an exception when empty

When calling $timeout.flush with or without a delay an exception should
be thrown if there is nothing to be flushed.

This prevents tests from flushing stuff unnecessarily.

BREAKING CHANGE: calling $timeout.flush(delay) when there is no task to be flushed
within the delay throws an exception now.

Please adjust the delay or remove the flush call from your tests as the exception
is a signed of a programming error.
This commit is contained in:
Igor Minar
2013-08-24 14:18:47 -07:00
parent 92700509c8
commit cbf06a5d64
3 changed files with 70 additions and 41 deletions

View File

@@ -104,19 +104,28 @@ angular.mock.$Browser = function() {
* @param {number=} number of milliseconds to flush. See {@link #defer.now}
*/
self.defer.flush = function(delay) {
var flushedSomething = false;
if (angular.isDefined(delay)) {
self.defer.now += delay;
} else {
if (self.deferredFns.length) {
self.defer.now = self.deferredFns[self.deferredFns.length-1].time;
} else {
throw Error('No deferred tasks to be flushed');
}
}
while (self.deferredFns.length && self.deferredFns[0].time <= self.defer.now) {
flushedSomething = true;
self.deferredFns.shift().fn();
}
if (!flushedSomething) {
if (angular.isUndefined(delay)) {
throw Error('No deferred tasks to be flushed!');
} else {
throw Error('No deferred tasks with delay up to ' + delay + 'ms to be flushed!')
}
}
};
/**