mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-18 23:50:23 +00:00
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:
parent
92700509c8
commit
cbf06a5d64
3 changed files with 70 additions and 41 deletions
13
src/ngMock/angular-mocks.js
vendored
13
src/ngMock/angular-mocks.js
vendored
|
|
@ -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!')
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ describe('$timeout', function() {
|
|||
$browser.defer.flush();
|
||||
expect(counter).toBe(1);
|
||||
|
||||
expect(function() {$browser.defer.flush();}).toThrow('No deferred tasks to be flushed');
|
||||
expect(function() {$browser.defer.flush();}).toThrow('No deferred tasks to be flushed!');
|
||||
expect(counter).toBe(1);
|
||||
}));
|
||||
|
||||
|
|
|
|||
96
test/ngMock/angular-mocksSpec.js
vendored
96
test/ngMock/angular-mocksSpec.js
vendored
|
|
@ -319,7 +319,7 @@ describe('ngMock', function() {
|
|||
browser.defer(logFn('B'), 2);
|
||||
browser.defer(logFn('C'), 3);
|
||||
|
||||
browser.defer.flush(0);
|
||||
expect(function() {browser.defer.flush(0);}).toThrow('No deferred tasks with delay up to 0ms to be flushed!');
|
||||
expect(browser.defer.now).toEqual(0);
|
||||
expect(log).toEqual('');
|
||||
|
||||
|
|
@ -333,7 +333,15 @@ describe('ngMock', function() {
|
|||
});
|
||||
|
||||
it('should throw an exception if there is nothing to be flushed', function() {
|
||||
expect(function() {browser.defer.flush();}).toThrow('No deferred tasks to be flushed');
|
||||
expect(function() {browser.defer.flush();}).toThrow('No deferred tasks to be flushed!');
|
||||
});
|
||||
|
||||
it('should throw an exception if there is nothing to be flushed within the delay provided', function() {
|
||||
browser.defer(logFn('A'), 1);
|
||||
expect(function() {browser.defer.flush(0);}).toThrow('No deferred tasks with delay up to 0ms to be flushed!');
|
||||
|
||||
browser.defer.flush(1);
|
||||
expect(log).toEqual('A;');
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -364,52 +372,45 @@ describe('ngMock', function() {
|
|||
|
||||
|
||||
describe('$timeout', function() {
|
||||
it('should expose flush method that will flush the pending queue of tasks', inject(
|
||||
function($timeout) {
|
||||
var logger = [],
|
||||
logFn = function(msg) { return function() { logger.push(msg) }};
|
||||
var log, $timeout;
|
||||
|
||||
$timeout(logFn('t1'));
|
||||
$timeout(logFn('t2'), 200);
|
||||
$timeout(logFn('t3'));
|
||||
expect(logger).toEqual([]);
|
||||
beforeEach(module(provideLog));
|
||||
|
||||
beforeEach(inject(function(_log_, _$timeout_) {
|
||||
log = _log_;
|
||||
$timeout = _$timeout_;
|
||||
}));
|
||||
|
||||
|
||||
it('should expose flush method that will flush the pending queue of tasks', function() {
|
||||
|
||||
|
||||
$timeout(log.fn('t1'));
|
||||
$timeout(log.fn('t2'), 200);
|
||||
$timeout(log.fn('t3'));
|
||||
expect(log).toEqual([]);
|
||||
|
||||
$timeout.flush();
|
||||
expect(logger).toEqual(['t1', 't3', 't2']);
|
||||
}));
|
||||
expect(log).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();
|
||||
}));
|
||||
|
||||
|
||||
it('should check against the delay if provided within timeout', inject(function($timeout) {
|
||||
$timeout(noop, 100);
|
||||
it('should flush tasks only up to a delay if flush delay is provided', function() {
|
||||
$timeout(log.fn('t1'), 100);
|
||||
$timeout.flush(100);
|
||||
expect(function() {$timeout.verifyNoPendingTasks();}).not.toThrow();
|
||||
expect(log).toEqual(['t1']);
|
||||
|
||||
$timeout(noop, 1000);
|
||||
$timeout.flush(100);
|
||||
expect(function() {$timeout.verifyNoPendingTasks();}).toThrow();
|
||||
$timeout(log.fn('t2'), 1000);
|
||||
expect(function() {$timeout.flush(100);}).toThrow();
|
||||
expect(log).toEqual(['t1']);
|
||||
|
||||
$timeout.flush(900);
|
||||
expect(function() {$timeout.verifyNoPendingTasks();}).not.toThrow();
|
||||
}));
|
||||
expect(log).toEqual(['t1', 't2']);
|
||||
expect(function() {$timeout.flush();}).toThrow();
|
||||
});
|
||||
|
||||
|
||||
it('should assert against the delay value', inject(function($timeout) {
|
||||
it('should assert against the delay value', function() {
|
||||
var count = 0;
|
||||
var iterate = function() {
|
||||
count++;
|
||||
|
|
@ -421,7 +422,26 @@ describe('ngMock', function() {
|
|||
expect(count).toBe(1);
|
||||
$timeout.flushNext(123);
|
||||
expect(count).toBe(2);
|
||||
}));
|
||||
});
|
||||
|
||||
|
||||
describe('verifyNoPendingTasks', function() {
|
||||
|
||||
it('should throw an exception when not flushed', function() {
|
||||
$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', function() {
|
||||
$timeout(noop);
|
||||
|
||||
$timeout.flush();
|
||||
expect(function() {$timeout.verifyNoPendingTasks();}).not.toThrow();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue