@@ -319,7 +319,7 @@ describe('ngMock', function() {
319
319
browser . defer ( logFn ( 'B' ) , 2 ) ;
320
320
browser . defer ( logFn ( 'C' ) , 3 ) ;
321
321
322
- expect ( function ( ) { browser . defer . flush ( 0 ) ; } ) . toThrow ( 'No deferred tasks with delay up to 0ms to be flushed!' ) ;
322
+ browser . defer . flush ( 0 ) ;
323
323
expect ( browser . defer . now ) . toEqual ( 0 ) ;
324
324
expect ( log ) . toEqual ( '' ) ;
325
325
@@ -333,15 +333,7 @@ describe('ngMock', function() {
333
333
} ) ;
334
334
335
335
it ( 'should throw an exception if there is nothing to be flushed' , function ( ) {
336
- expect ( function ( ) { browser . defer . flush ( ) ; } ) . toThrow ( 'No deferred tasks to be flushed!' ) ;
337
- } ) ;
338
-
339
- it ( 'should throw an exception if there is nothing to be flushed within the delay provided' , function ( ) {
340
- browser . defer ( logFn ( 'A' ) , 1 ) ;
341
- expect ( function ( ) { browser . defer . flush ( 0 ) ; } ) . toThrow ( 'No deferred tasks with delay up to 0ms to be flushed!' ) ;
342
-
343
- browser . defer . flush ( 1 ) ;
344
- expect ( log ) . toEqual ( 'A;' ) ;
336
+ expect ( function ( ) { browser . defer . flush ( ) ; } ) . toThrow ( 'No deferred tasks to be flushed' ) ;
345
337
} ) ;
346
338
} ) ;
347
339
@@ -372,45 +364,52 @@ describe('ngMock', function() {
372
364
373
365
374
366
describe ( '$timeout' , function ( ) {
375
- var log , $timeout ;
367
+ it ( 'should expose flush method that will flush the pending queue of tasks' , inject (
368
+ function ( $timeout ) {
369
+ var logger = [ ] ,
370
+ logFn = function ( msg ) { return function ( ) { logger . push ( msg ) } } ;
376
371
377
- beforeEach ( module ( provideLog ) ) ;
372
+ $timeout ( logFn ( 't1' ) ) ;
373
+ $timeout ( logFn ( 't2' ) , 200 ) ;
374
+ $timeout ( logFn ( 't3' ) ) ;
375
+ expect ( logger ) . toEqual ( [ ] ) ;
378
376
379
- beforeEach ( inject ( function ( _log_ , _$timeout_ ) {
380
- log = _log_ ;
381
- $timeout = _$timeout_ ;
377
+ $timeout . flush ( ) ;
378
+ expect ( logger ) . toEqual ( [ 't1' , 't3' , 't2' ] ) ;
382
379
} ) ) ;
383
380
384
381
385
- it ( 'should expose flush method that will flush the pending queue of tasks' , function ( ) {
382
+ it ( 'should throw an exception when not flushed' , inject ( function ( $timeout ) {
383
+ $timeout ( noop ) ;
384
+
385
+ var expectedError = 'Deferred tasks to flush (1): {id: 0, time: 0}' ;
386
+ expect ( function ( ) { $timeout . verifyNoPendingTasks ( ) ; } ) . toThrow ( expectedError ) ;
387
+ } ) ) ;
386
388
387
389
388
- $timeout ( log . fn ( 't1' ) ) ;
389
- $timeout ( log . fn ( 't2' ) , 200 ) ;
390
- $timeout ( log . fn ( 't3' ) ) ;
391
- expect ( log ) . toEqual ( [ ] ) ;
390
+ it ( 'should do nothing when all tasks have been flushed' , inject ( function ( $timeout ) {
391
+ $timeout ( noop ) ;
392
392
393
393
$timeout . flush ( ) ;
394
- expect ( log ) . toEqual ( [ 't1' , 't3' , 't2' ] ) ;
395
- } ) ;
394
+ expect ( function ( ) { $timeout . verifyNoPendingTasks ( ) ; } ) . not . toThrow ( ) ;
395
+ } ) ) ;
396
396
397
397
398
- it ( 'should flush tasks only up to a delay if flush delay is provided ' , function ( ) {
399
- $timeout ( log . fn ( 't1' ) , 100 ) ;
398
+ it ( 'should check against the delay if provided within timeout ' , inject ( function ( $timeout ) {
399
+ $timeout ( noop , 100 ) ;
400
400
$timeout . flush ( 100 ) ;
401
- expect ( log ) . toEqual ( [ 't1' ] ) ;
401
+ expect ( function ( ) { $timeout . verifyNoPendingTasks ( ) ; } ) . not . toThrow ( ) ;
402
402
403
- $timeout ( log . fn ( 't2' ) , 1000 ) ;
404
- expect ( function ( ) { $timeout . flush ( 100 ) ; } ) . toThrow ( ) ;
405
- expect ( log ) . toEqual ( [ 't1' ] ) ;
403
+ $timeout ( noop , 1000 ) ;
404
+ $timeout . flush ( 100 ) ;
405
+ expect ( function ( ) { $timeout . verifyNoPendingTasks ( ) ; } ) . toThrow ( ) ;
406
406
407
407
$timeout . flush ( 900 ) ;
408
- expect ( log ) . toEqual ( [ 't1' , 't2' ] ) ;
409
- expect ( function ( ) { $timeout . flush ( ) ; } ) . toThrow ( ) ;
410
- } ) ;
408
+ expect ( function ( ) { $timeout . verifyNoPendingTasks ( ) ; } ) . not . toThrow ( ) ;
409
+ } ) ) ;
411
410
412
411
413
- it ( 'should assert against the delay value' , function ( ) {
412
+ it ( 'should assert against the delay value' , inject ( function ( $timeout ) {
414
413
var count = 0 ;
415
414
var iterate = function ( ) {
416
415
count ++ ;
@@ -422,26 +421,7 @@ describe('ngMock', function() {
422
421
expect ( count ) . toBe ( 1 ) ;
423
422
$timeout . flushNext ( 123 ) ;
424
423
expect ( count ) . toBe ( 2 ) ;
425
- } ) ;
426
-
427
-
428
- describe ( 'verifyNoPendingTasks' , function ( ) {
429
-
430
- it ( 'should throw an exception when not flushed' , function ( ) {
431
- $timeout ( noop ) ;
432
-
433
- var expectedError = 'Deferred tasks to flush (1): {id: 0, time: 0}' ;
434
- expect ( function ( ) { $timeout . verifyNoPendingTasks ( ) ; } ) . toThrow ( expectedError ) ;
435
- } ) ;
436
-
437
-
438
- it ( 'should do nothing when all tasks have been flushed' , function ( ) {
439
- $timeout ( noop ) ;
440
-
441
- $timeout . flush ( ) ;
442
- expect ( function ( ) { $timeout . verifyNoPendingTasks ( ) ; } ) . not . toThrow ( ) ;
443
- } ) ;
444
- } ) ;
424
+ } ) ) ;
445
425
} ) ;
446
426
447
427
0 commit comments