diff options
| author | petrovalex | 2012-09-15 19:02:10 +0300 | 
|---|---|---|
| committer | Pawel Kozlowski | 2012-12-20 20:39:40 +0100 | 
| commit | f0c6ebc07653f6267acec898ccef5677884e3081 (patch) | |
| tree | f238d7ef4c30fa3e294be8955108da6bc445b692 /src/ngMock | |
| parent | 59d9b89852805ad08e8e3bc2f80625b9697dc768 (diff) | |
| download | angular.js-f0c6ebc07653f6267acec898ccef5677884e3081.tar.bz2 | |
feat($timeout-mock): add verifyNoPendingTasks method
added verifyNoPendingTasks method, which throws error if not all
deferred tasks have been flushed
Closes #1245
Diffstat (limited to 'src/ngMock')
| -rw-r--r-- | src/ngMock/angular-mocks.js | 60 | 
1 files changed, 43 insertions, 17 deletions
| diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index 647a01fe..7f31a582 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -1328,17 +1328,49 @@ function MockXhr() {   * @description   *   * This service is just a simple decorator for {@link ng.$timeout $timeout} service - * that adds a "flush" method. - */ + * that adds a "flush" and "verifyNoPendingTasks" methods. + */  -/** - * @ngdoc method - * @name ngMock.$timeout#flush - * @methodOf ngMock.$timeout - * @description - * - * Flushes the queue of pending tasks. - */ +angular.mock.$TimeoutDecorator = function($delegate, $browser) { + +  /** +   * @ngdoc method +   * @name ngMock.$timeout#flush +   * @methodOf ngMock.$timeout +   * @description +   * +   * Flushes the queue of pending tasks. +   */ +  $delegate.flush = function() { +    $browser.defer.flush(); +  }; + +  /** +   * @ngdoc method +   * @name ngMock.$timeout#verifyNoPendingTasks +   * @methodOf ngMock.$timeout +   * @description +   * +   * Verifies that there are no pending tasks that need to be flushed. +   */ +  $delegate.verifyNoPendingTasks = function() { +    if ($browser.deferredFns.length) { +      throw Error('Deferred tasks to flush (' + $browser.deferredFns.length + '): ' + +          formatPendingTasksAsString($browser.deferredFns)); +    } +  }; + +  function formatPendingTasksAsString(tasks) { +    var result = []; +    angular.forEach(tasks, function(task) { +      result.push('{id: ' + task.id + ', ' + 'time: ' + task.time + '}'); +    }); + +    return result.join(', '); +  } + +  return $delegate; +};  /**   * @@ -1364,15 +1396,9 @@ angular.module('ngMock', ['ng']).provider({    $httpBackend: angular.mock.$HttpBackendProvider,    $rootElement: angular.mock.$RootElementProvider  }).config(function($provide) { -  $provide.decorator('$timeout', function($delegate, $browser) { -    $delegate.flush = function() { -      $browser.defer.flush(); -    }; -    return $delegate; -  }); +  $provide.decorator('$timeout', angular.mock.$TimeoutDecorator);  }); -  /**   * @ngdoc overview   * @name ngMockE2E | 
