diff options
| author | Igor Minar | 2013-08-25 14:36:10 -0700 | 
|---|---|---|
| committer | Igor Minar | 2013-08-25 14:46:55 -0700 | 
| commit | 42af8eada2803a54a98b4f792e60feb480d68a0c (patch) | |
| tree | 9e0f5a3c42dfb270913cdc0e48185d8970fa31c9 /src/ngMock | |
| parent | cbf06a5d64aba537f0e2679a194d3998d8365493 (diff) | |
| download | angular.js-42af8eada2803a54a98b4f792e60feb480d68a0c.tar.bz2 | |
fix(mocks): $timeout#flush should not update time when empty
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
```
Diffstat (limited to 'src/ngMock')
| -rw-r--r-- | src/ngMock/angular-mocks.js | 9 | 
1 files changed, 6 insertions, 3 deletions
| diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index 05fdc4ed..d0c1b9b2 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -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();      } @@ -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;    };    /** | 
