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

Commit 804b9b9

Browse files
committed
Revert "Revert "fix(ngMock/$interval): add support for zero-delay intervals in tests""
This reverts commit 510afbc.
1 parent 510afbc commit 804b9b9

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

src/ngMock/angular-mocks.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,8 @@ angular.mock.$IntervalProvider = function() {
504504
}
505505

506506
repeatFns.push({
507-
nextTime:(now + delay),
508-
delay: delay,
507+
nextTime: (now + (delay || 0)),
508+
delay: delay || 1,
509509
fn: tick,
510510
id: nextRepeatId,
511511
deferred: deferred
@@ -555,10 +555,16 @@ angular.mock.$IntervalProvider = function() {
555555
* @return {number} The amount of time moved forward.
556556
*/
557557
$interval.flush = function(millis) {
558+
var before = now;
558559
now += millis;
559560
while (repeatFns.length && repeatFns[0].nextTime <= now) {
560561
var task = repeatFns[0];
561562
task.fn();
563+
if (task.nextTime === before) {
564+
// this can only happen the first time
565+
// a zero-delay interval gets triggered
566+
task.nextTime++;
567+
}
562568
task.nextTime += task.delay;
563569
repeatFns.sort(function(a, b) { return a.nextTime - b.nextTime;});
564570
}

test/ngMock/angular-mocksSpec.js

+69
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,75 @@ describe('ngMock', function() {
351351
}));
352352

353353

354+
it('should allow you to NOT specify the delay time', inject(function($interval) {
355+
var counterA = 0;
356+
var counterB = 0;
357+
358+
$interval(function() { counterA++; });
359+
$interval(function() { counterB++; }, 0);
360+
361+
$interval.flush(1000);
362+
expect(counterA).toBe(1000);
363+
expect(counterB).toBe(1000);
364+
$interval.flush(1000);
365+
expect(counterA).toBe(2000);
366+
expect(counterB).toBe(2000);
367+
}));
368+
369+
370+
it('should run tasks in correct relative order', inject(function($interval) {
371+
var counterA = 0;
372+
var counterB = 0;
373+
$interval(function() { counterA++; }, 0);
374+
$interval(function() { counterB++; }, 1000);
375+
376+
$interval.flush(1000);
377+
expect(counterA).toBe(1000);
378+
expect(counterB).toBe(1);
379+
$interval.flush(999);
380+
expect(counterA).toBe(1999);
381+
expect(counterB).toBe(1);
382+
$interval.flush(1);
383+
expect(counterA).toBe(2000);
384+
expect(counterB).toBe(2);
385+
}));
386+
387+
388+
it('should NOT trigger zero-delay interval when flush has ran before', inject(function($interval) {
389+
var counterA = 0;
390+
var counterB = 0;
391+
392+
$interval.flush(100);
393+
394+
$interval(function() { counterA++; });
395+
$interval(function() { counterB++; }, 0);
396+
397+
expect(counterA).toBe(0);
398+
expect(counterB).toBe(0);
399+
400+
$interval.flush(100);
401+
402+
expect(counterA).toBe(100);
403+
expect(counterB).toBe(100);
404+
}));
405+
406+
407+
it('should trigger zero-delay interval only once on flush zero', inject(function($interval) {
408+
var counterA = 0;
409+
var counterB = 0;
410+
411+
$interval(function() { counterA++; });
412+
$interval(function() { counterB++; }, 0);
413+
414+
$interval.flush(0);
415+
expect(counterA).toBe(1);
416+
expect(counterB).toBe(1);
417+
$interval.flush(0);
418+
expect(counterA).toBe(1);
419+
expect(counterB).toBe(1);
420+
}));
421+
422+
354423
it('should allow you to specify a number of iterations', inject(function($interval) {
355424
var counter = 0;
356425
$interval(function() {counter++;}, 1000, 2);

0 commit comments

Comments
 (0)