Skip to content

Commit

Permalink
fix(mocks): $timeout#flush should not update time when empty
Browse files Browse the repository at this point in the history
When $timeout#flush is called with a delay and no task can be flushed within that
delay, the current time should not be updated as that gets the mock into an inconsistent
state.

BREAKING CHANGE: if a tests was written around the buggy behavior the delays might be off now

This would typically not be a problem, but because of the previous breaking change in
$timeout.flush, the combination of two might be confusing and that's why we are documenting
it.

Old behavior:

```
doSomething(); //schedules task to execute in 500ms from now
doOtherStuff(); //schedules task to execute in 600ms from now

try {
  $timeout.flush(300); // throws "no task to be flushed" exception
} catch(e) {};
$time.flush(200); //flushes only doSomething() task
```

New behavior:

```
doSomething(); //schedules task to execute in 500ms from now
doOtherStuff(); //schedules task to execute in 600ms from now

try {
  $timeout.flush(300); // throws "no task to be flushed" exception
} catch(e) {};
$time.flush(200); // throws "no task to be flushed" exception again
                  // because previous exception didn't move the time forward
```

Fixed test:

```
doSomething(); //schedules task to execute in 500ms from now
doOtherStuff(); //schedules task to execute in 600ms from now

try {
  $timeout.flush(300); // throws "no task to be flushed" exception
} catch(e) {};
$time.flush(500); // flushes only doSomething() task
```
  • Loading branch information
IgorMinar committed Aug 25, 2013
1 parent cbf06a5 commit 42af8ea
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,17 @@ angular.mock.$Browser = function() {
*/
self.defer.flush = function(delay) {
var flushedSomething = false;
now = self.defer.now;

if (angular.isDefined(delay)) {
self.defer.now += delay;
now += delay;
} else {
if (self.deferredFns.length) {
self.defer.now = self.deferredFns[self.deferredFns.length-1].time;
now = self.deferredFns[self.deferredFns.length-1].time;
}
}

while (self.deferredFns.length && self.deferredFns[0].time <= self.defer.now) {
while (self.deferredFns.length && self.deferredFns[0].time <= now) {
flushedSomething = true;
self.deferredFns.shift().fn();
}
Expand All @@ -126,6 +127,8 @@ angular.mock.$Browser = function() {
throw Error('No deferred tasks with delay up to ' + delay + 'ms to be flushed!')
}
}

self.defer.now = now;
};

/**
Expand Down
17 changes: 16 additions & 1 deletion test/ngMock/angular-mocksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ describe('ngMock', function() {
expect(function() {$timeout.flush(100);}).toThrow();
expect(log).toEqual(['t1']);

$timeout.flush(900);
$timeout.flush(1000);
expect(log).toEqual(['t1', 't2']);
expect(function() {$timeout.flush();}).toThrow();
});
Expand All @@ -425,6 +425,21 @@ describe('ngMock', function() {
});


it('should not update the current time if an exception is thrown during a flush', function() {
$timeout(log.fn('t1'), 100);
$timeout(log.fn('t2'), 101);

expect(function() { $timeout.flush(90); }).toThrow();
expect(function() { $timeout.flush(90); }).toThrow();

$timeout.flush(100);
expect(log).toEqual(['t1']);

$timeout.flush(1);
expect(log).toEqual(['t1', 't2']);
});


describe('verifyNoPendingTasks', function() {

it('should throw an exception when not flushed', function() {
Expand Down

0 comments on commit 42af8ea

Please sign in to comment.