@@ -121,6 +121,8 @@ angular.mock.$Browser = function($log, $$taskTrackerFactory) {
121121 * @description
122122 * Flushes all pending requests and executes the defer callbacks.
123123 *
124+ * See {@link ngMock.$flushPendingsTasks} for more info.
125+ *
124126 * @param {number= } number of milliseconds to flush. See {@link #defer.now}
125127 */
126128 self . defer . flush = function ( delay ) {
@@ -155,7 +157,9 @@ angular.mock.$Browser = function($log, $$taskTrackerFactory) {
155157 * Verifies that there are no pending tasks that need to be flushed.
156158 * You can check for a specific type of tasks only, by specifying a `taskType`.
157159 *
158- * @param {string= } taskType - The type task to check for.
160+ * See {@link $verifyNoPendingTasks} for more info.
161+ *
162+ * @param {string= } taskType - The type tasks to check for.
159163 */
160164 self . defer . verifyNoPendingTasks = function ( taskType ) {
161165 var pendingTasks = ! taskType
@@ -212,6 +216,82 @@ angular.mock.$Browser.prototype = {
212216 }
213217} ;
214218
219+ /**
220+ * @ngdoc function
221+ * @name $flushPendingTasks
222+ *
223+ * @description
224+ * Flushes all currently pending tasks and executes the corresponding callbacks.
225+ *
226+ * Optionally, you can also pass a `delay` argument to only flush tasks that are scheduled to be
227+ * executed within `delay` milliseconds. Currently, `delay` only applies to timeouts, since all
228+ * other tasks have a delay of 0 (i.e. they are scheduled to be executed as soon as possible, but
229+ * still asynchronously).
230+ *
231+ * If no delay is specified, it uses a delay such that all currently pending tasks are flushed.
232+ *
233+ * The types of tasks that are flushed include:
234+ *
235+ * - Pending timeouts (via {@link $timeout}).
236+ * - Pending tasks scheduled via {@link ng.$rootScope.Scope#$applyAsync $applyAsync}.
237+ * - Pending tasks scheduled via {@link ng.$rootScope.Scope#$evalAsync $evalAsync}.
238+ * These include tasks scheduled via `$evalAsync()` indirectly (such as {@link $q} promises).
239+ *
240+ * <div class="alert alert-info">
241+ * Periodic tasks scheduled via {@link $interval} use a different queue and are not flushed by
242+ * `$flushPendingTasks()`. Use {@link ngMock.$interval#flush $interval.flush([millis])} instead.
243+ * </div>
244+ *
245+ * @param {number= } delay - The number of milliseconds to flush.
246+ */
247+ angular . mock . $FlushPendingTasksProvider = function ( ) {
248+ this . $get = [
249+ '$browser' ,
250+ function ( $browser ) {
251+ return function $flushPendingTasks ( delay ) {
252+ return $browser . defer . flush ( delay ) ;
253+ } ;
254+ }
255+ ] ;
256+ } ;
257+
258+ /**
259+ * @ngdoc function
260+ * @name $verifyNoPendingTasks
261+ *
262+ * @description
263+ * Verifies that there are no pending tasks that need to be flushed. It throws an error if there are
264+ * still pending tasks.
265+ *
266+ * You can check for a specific type of tasks only, by specifying a `taskType`.
267+ *
268+ * Available task types:
269+ *
270+ * - `$timeout`: Pending timeouts (via {@link $timeout}).
271+ * - `$http`: Pending HTTP requests (via {@link $http}).
272+ * - `$route`: In-progress route transitions (via {@link $route}).
273+ * - `$applyAsync`: Pending tasks scheduled via {@link ng.$rootScope.Scope#$applyAsync $applyAsync}.
274+ * - `$evalAsync`: Pending tasks scheduled via {@link ng.$rootScope.Scope#$evalAsync $evalAsync}.
275+ * These include tasks scheduled via `$evalAsync()` indirectly (such as {@link $q} promises).
276+ *
277+ * <div class="alert alert-info">
278+ * Periodic tasks scheduled via {@link $interval} use a different queue and are not taken into
279+ * account by `$verifyNoPendingTasks()`. There is currently no way to verify that there are no
280+ * pending {@link $interval} tasks.
281+ * </div>
282+ *
283+ * @param {string= } taskType - The type of tasks to check for.
284+ */
285+ angular . mock . $VerifyNoPendingTasksProvider = function ( ) {
286+ this . $get = [
287+ '$browser' ,
288+ function ( $browser ) {
289+ return function $verifyNoPendingTasks ( taskType ) {
290+ return $browser . defer . verifyNoPendingTasks ( taskType ) ;
291+ } ;
292+ }
293+ ] ;
294+ } ;
215295
216296/**
217297 * @ngdoc provider
@@ -2179,6 +2259,15 @@ angular.mock.$TimeoutDecorator = ['$delegate', '$browser', function($delegate, $
21792259 *
21802260 * Flushes the queue of pending tasks.
21812261 *
2262+ * _This method is essentially an alias of {@link ngMock.$flushPendingTasks}._
2263+ *
2264+ * <div class="alert alert-warning">
2265+ * For historical reasons, this method will also flush non-`$timeout` pending tasks, such as
2266+ * {@link $q} promises and tasks scheduled via
2267+ * {@link ng.$rootScope.Scope#$applyAsync $applyAsync} and
2268+ * {@link ng.$rootScope.Scope#$evalAsync $evalAsync}.
2269+ * </div>
2270+ *
21822271 * @param {number= } delay maximum timeout amount to flush up until
21832272 */
21842273 $delegate . flush = function ( delay ) {
@@ -2193,7 +2282,26 @@ angular.mock.$TimeoutDecorator = ['$delegate', '$browser', function($delegate, $
21932282 * @name $timeout#verifyNoPendingTasks
21942283 * @description
21952284 *
2196- * Verifies that there are no pending tasks that need to be flushed.
2285+ * Verifies that there are no pending tasks that need to be flushed. It throws an error if there
2286+ * are still pending tasks.
2287+ *
2288+ * _This method is essentially an alias of {@link ngMock.$verifyNoPendingTasks} (called with no
2289+ * arguments)._
2290+ *
2291+ * <div class="alert alert-warning">
2292+ * <p>
2293+ * For historical reasons, this method will also verify non-`$timeout` pending tasks, such as
2294+ * pending {@link $http} requests, in-progress {@link $route} transitions, unresolved
2295+ * {@link $q} promises and tasks scheduled via
2296+ * {@link ng.$rootScope.Scope#$applyAsync $applyAsync} and
2297+ * {@link ng.$rootScope.Scope#$evalAsync $evalAsync}.
2298+ * </p>
2299+ * <p>
2300+ * It is recommended to use {@link ngMock.$verifyNoPendingTasks} instead, which additionally
2301+ * supports verifying a specific type of tasks. For example, you can verify there are no
2302+ * pending timeouts with `$verifyNoPendingTasks('$timeout')`.
2303+ * </p>
2304+ * </div>
21972305 */
21982306 $delegate . verifyNoPendingTasks = function ( ) {
21992307 // For historical reasons, `$timeout.verifyNoPendingTasks()` takes all types of pending tasks
@@ -2422,7 +2530,9 @@ angular.module('ngMock', ['ng']).provider({
24222530 $log : angular . mock . $LogProvider ,
24232531 $interval : angular . mock . $IntervalProvider ,
24242532 $rootElement : angular . mock . $RootElementProvider ,
2425- $componentController : angular . mock . $ComponentControllerProvider
2533+ $componentController : angular . mock . $ComponentControllerProvider ,
2534+ $flushPendingTasks : angular . mock . $FlushPendingTasksProvider ,
2535+ $verifyNoPendingTasks : angular . mock . $VerifyNoPendingTasksProvider
24262536} ) . config ( [ '$provide' , '$compileProvider' , function ( $provide , $compileProvider ) {
24272537 $provide . decorator ( '$timeout' , angular . mock . $TimeoutDecorator ) ;
24282538 $provide . decorator ( '$$rAF' , angular . mock . $RAFDecorator ) ;
0 commit comments