@@ -121,6 +121,8 @@ angular.mock.$Browser = function($log, $$taskTrackerFactory) {
121
121
* @description
122
122
* Flushes all pending requests and executes the defer callbacks.
123
123
*
124
+ * See {@link ngMock.$flushPendingsTasks} for more info.
125
+ *
124
126
* @param {number= } number of milliseconds to flush. See {@link #defer.now}
125
127
*/
126
128
self . defer . flush = function ( delay ) {
@@ -155,7 +157,9 @@ angular.mock.$Browser = function($log, $$taskTrackerFactory) {
155
157
* Verifies that there are no pending tasks that need to be flushed.
156
158
* You can check for a specific type of tasks only, by specifying a `taskType`.
157
159
*
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.
159
163
*/
160
164
self . defer . verifyNoPendingTasks = function ( taskType ) {
161
165
var pendingTasks = ! taskType
@@ -212,6 +216,82 @@ angular.mock.$Browser.prototype = {
212
216
}
213
217
} ;
214
218
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
+ } ;
215
295
216
296
/**
217
297
* @ngdoc provider
@@ -2179,6 +2259,15 @@ angular.mock.$TimeoutDecorator = ['$delegate', '$browser', function($delegate, $
2179
2259
*
2180
2260
* Flushes the queue of pending tasks.
2181
2261
*
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
+ *
2182
2271
* @param {number= } delay maximum timeout amount to flush up until
2183
2272
*/
2184
2273
$delegate . flush = function ( delay ) {
@@ -2193,7 +2282,26 @@ angular.mock.$TimeoutDecorator = ['$delegate', '$browser', function($delegate, $
2193
2282
* @name $timeout#verifyNoPendingTasks
2194
2283
* @description
2195
2284
*
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>
2197
2305
*/
2198
2306
$delegate . verifyNoPendingTasks = function ( ) {
2199
2307
// For historical reasons, `$timeout.verifyNoPendingTasks()` takes all types of pending tasks
@@ -2422,7 +2530,9 @@ angular.module('ngMock', ['ng']).provider({
2422
2530
$log : angular . mock . $LogProvider ,
2423
2531
$interval : angular . mock . $IntervalProvider ,
2424
2532
$rootElement : angular . mock . $RootElementProvider ,
2425
- $componentController : angular . mock . $ComponentControllerProvider
2533
+ $componentController : angular . mock . $ComponentControllerProvider ,
2534
+ $flushPendingTasks : angular . mock . $FlushPendingTasksProvider ,
2535
+ $verifyNoPendingTasks : angular . mock . $VerifyNoPendingTasksProvider
2426
2536
} ) . config ( [ '$provide' , '$compileProvider' , function ( $provide , $compileProvider ) {
2427
2537
$provide . decorator ( '$timeout' , angular . mock . $TimeoutDecorator ) ;
2428
2538
$provide . decorator ( '$$rAF' , angular . mock . $RAFDecorator ) ;
0 commit comments