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

Commit 4114f9c

Browse files
committed
revert: feat(mocks): make $timeout#flush throw an exception when empty
This reverts commit cbf06a5. This turned out to be a bad idea because it allow us to fast-forward the wall clock time (see previous commit).
1 parent 70b44ad commit 4114f9c

File tree

3 files changed

+35
-64
lines changed

3 files changed

+35
-64
lines changed

src/ngMock/angular-mocks.js

+2-11
Original file line numberDiff line numberDiff line change
@@ -104,28 +104,19 @@ angular.mock.$Browser = function() {
104104
* @param {number=} number of milliseconds to flush. See {@link #defer.now}
105105
*/
106106
self.defer.flush = function(delay) {
107-
var flushedSomething = false;
108-
109107
if (angular.isDefined(delay)) {
110108
self.defer.now += delay;
111109
} else {
112110
if (self.deferredFns.length) {
113111
self.defer.now = self.deferredFns[self.deferredFns.length-1].time;
112+
} else {
113+
throw Error('No deferred tasks to be flushed');
114114
}
115115
}
116116

117117
while (self.deferredFns.length && self.deferredFns[0].time <= self.defer.now) {
118-
flushedSomething = true;
119118
self.deferredFns.shift().fn();
120119
}
121-
122-
if (!flushedSomething) {
123-
if (angular.isUndefined(delay)) {
124-
throw Error('No deferred tasks to be flushed!');
125-
} else {
126-
throw Error('No deferred tasks with delay up to ' + delay + 'ms to be flushed!')
127-
}
128-
}
129120
};
130121

131122
/**

test/ng/timeoutSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('$timeout', function() {
1414
$browser.defer.flush();
1515
expect(counter).toBe(1);
1616

17-
expect(function() {$browser.defer.flush();}).toThrow('No deferred tasks to be flushed!');
17+
expect(function() {$browser.defer.flush();}).toThrow('No deferred tasks to be flushed');
1818
expect(counter).toBe(1);
1919
}));
2020

test/ngMock/angular-mocksSpec.js

+32-52
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ describe('ngMock', function() {
319319
browser.defer(logFn('B'), 2);
320320
browser.defer(logFn('C'), 3);
321321

322-
expect(function() {browser.defer.flush(0);}).toThrow('No deferred tasks with delay up to 0ms to be flushed!');
322+
browser.defer.flush(0);
323323
expect(browser.defer.now).toEqual(0);
324324
expect(log).toEqual('');
325325

@@ -333,15 +333,7 @@ describe('ngMock', function() {
333333
});
334334

335335
it('should throw an exception if there is nothing to be flushed', function() {
336-
expect(function() {browser.defer.flush();}).toThrow('No deferred tasks to be flushed!');
337-
});
338-
339-
it('should throw an exception if there is nothing to be flushed within the delay provided', function() {
340-
browser.defer(logFn('A'), 1);
341-
expect(function() {browser.defer.flush(0);}).toThrow('No deferred tasks with delay up to 0ms to be flushed!');
342-
343-
browser.defer.flush(1);
344-
expect(log).toEqual('A;');
336+
expect(function() {browser.defer.flush();}).toThrow('No deferred tasks to be flushed');
345337
});
346338
});
347339

@@ -372,45 +364,52 @@ describe('ngMock', function() {
372364

373365

374366
describe('$timeout', function() {
375-
var log, $timeout;
367+
it('should expose flush method that will flush the pending queue of tasks', inject(
368+
function($timeout) {
369+
var logger = [],
370+
logFn = function(msg) { return function() { logger.push(msg) }};
376371

377-
beforeEach(module(provideLog));
372+
$timeout(logFn('t1'));
373+
$timeout(logFn('t2'), 200);
374+
$timeout(logFn('t3'));
375+
expect(logger).toEqual([]);
378376

379-
beforeEach(inject(function(_log_, _$timeout_) {
380-
log = _log_;
381-
$timeout = _$timeout_;
377+
$timeout.flush();
378+
expect(logger).toEqual(['t1', 't3', 't2']);
382379
}));
383380

384381

385-
it('should expose flush method that will flush the pending queue of tasks', function() {
382+
it('should throw an exception when not flushed', inject(function($timeout){
383+
$timeout(noop);
384+
385+
var expectedError = 'Deferred tasks to flush (1): {id: 0, time: 0}';
386+
expect(function() {$timeout.verifyNoPendingTasks();}).toThrow(expectedError);
387+
}));
386388

387389

388-
$timeout(log.fn('t1'));
389-
$timeout(log.fn('t2'), 200);
390-
$timeout(log.fn('t3'));
391-
expect(log).toEqual([]);
390+
it('should do nothing when all tasks have been flushed', inject(function($timeout) {
391+
$timeout(noop);
392392

393393
$timeout.flush();
394-
expect(log).toEqual(['t1', 't3', 't2']);
395-
});
394+
expect(function() {$timeout.verifyNoPendingTasks();}).not.toThrow();
395+
}));
396396

397397

398-
it('should flush tasks only up to a delay if flush delay is provided', function() {
399-
$timeout(log.fn('t1'), 100);
398+
it('should check against the delay if provided within timeout', inject(function($timeout) {
399+
$timeout(noop, 100);
400400
$timeout.flush(100);
401-
expect(log).toEqual(['t1']);
401+
expect(function() {$timeout.verifyNoPendingTasks();}).not.toThrow();
402402

403-
$timeout(log.fn('t2'), 1000);
404-
expect(function() {$timeout.flush(100);}).toThrow();
405-
expect(log).toEqual(['t1']);
403+
$timeout(noop, 1000);
404+
$timeout.flush(100);
405+
expect(function() {$timeout.verifyNoPendingTasks();}).toThrow();
406406

407407
$timeout.flush(900);
408-
expect(log).toEqual(['t1', 't2']);
409-
expect(function() {$timeout.flush();}).toThrow();
410-
});
408+
expect(function() {$timeout.verifyNoPendingTasks();}).not.toThrow();
409+
}));
411410

412411

413-
it('should assert against the delay value', function() {
412+
it('should assert against the delay value', inject(function($timeout) {
414413
var count = 0;
415414
var iterate = function() {
416415
count++;
@@ -422,26 +421,7 @@ describe('ngMock', function() {
422421
expect(count).toBe(1);
423422
$timeout.flushNext(123);
424423
expect(count).toBe(2);
425-
});
426-
427-
428-
describe('verifyNoPendingTasks', function() {
429-
430-
it('should throw an exception when not flushed', function() {
431-
$timeout(noop);
432-
433-
var expectedError = 'Deferred tasks to flush (1): {id: 0, time: 0}';
434-
expect(function() {$timeout.verifyNoPendingTasks();}).toThrow(expectedError);
435-
});
436-
437-
438-
it('should do nothing when all tasks have been flushed', function() {
439-
$timeout(noop);
440-
441-
$timeout.flush();
442-
expect(function() {$timeout.verifyNoPendingTasks();}).not.toThrow();
443-
});
444-
});
424+
}));
445425
});
446426

447427

0 commit comments

Comments
 (0)