@@ -319,7 +319,7 @@ describe('ngMock', function() {
319319 browser . defer ( logFn ( 'B' ) , 2 ) ;
320320 browser . defer ( logFn ( 'C' ) , 3 ) ;
321321
322- browser . defer . flush ( 0 ) ;
322+ expect ( function ( ) { browser . defer . flush ( 0 ) ; } ) . toThrow ( 'No deferred tasks with delay up to 0ms to be flushed!' ) ;
323323 expect ( browser . defer . now ) . toEqual ( 0 ) ;
324324 expect ( log ) . toEqual ( '' ) ;
325325
@@ -333,7 +333,15 @@ describe('ngMock', function() {
333333 } ) ;
334334
335335 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' ) ;
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;' ) ;
337345 } ) ;
338346 } ) ;
339347
@@ -364,52 +372,45 @@ describe('ngMock', function() {
364372
365373
366374 describe ( '$timeout' , function ( ) {
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 ) } } ;
375+ var log , $timeout ;
371376
372- $timeout ( logFn ( 't1' ) ) ;
373- $timeout ( logFn ( 't2' ) , 200 ) ;
374- $timeout ( logFn ( 't3' ) ) ;
375- expect ( logger ) . toEqual ( [ ] ) ;
377+ beforeEach ( module ( provideLog ) ) ;
376378
377- $timeout . flush ( ) ;
378- expect ( logger ) . toEqual ( [ 't1' , 't3' , 't2' ] ) ;
379+ beforeEach ( inject ( function ( _log_ , _$timeout_ ) {
380+ log = _log_ ;
381+ $timeout = _$timeout_ ;
379382 } ) ) ;
380383
381384
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- } ) ) ;
385+ it ( 'should expose flush method that will flush the pending queue of tasks' , function ( ) {
388386
389387
390- it ( 'should do nothing when all tasks have been flushed' , inject ( function ( $timeout ) {
391- $timeout ( noop ) ;
388+ $timeout ( log . fn ( 't1' ) ) ;
389+ $timeout ( log . fn ( 't2' ) , 200 ) ;
390+ $timeout ( log . fn ( 't3' ) ) ;
391+ expect ( log ) . toEqual ( [ ] ) ;
392392
393393 $timeout . flush ( ) ;
394- expect ( function ( ) { $timeout . verifyNoPendingTasks ( ) ; } ) . not . toThrow ( ) ;
395- } ) ) ;
394+ expect ( log ) . toEqual ( [ 't1' , 't3' , 't2' ] ) ;
395+ } ) ;
396396
397397
398- it ( 'should check against the delay if provided within timeout ' , inject ( function ( $timeout ) {
399- $timeout ( noop , 100 ) ;
398+ it ( 'should flush tasks only up to a delay if flush delay is provided ' , function ( ) {
399+ $timeout ( log . fn ( 't1' ) , 100 ) ;
400400 $timeout . flush ( 100 ) ;
401- expect ( function ( ) { $timeout . verifyNoPendingTasks ( ) ; } ) . not . toThrow ( ) ;
401+ expect ( log ) . toEqual ( [ 't1' ] ) ;
402402
403- $timeout ( noop , 1000 ) ;
404- $timeout . flush ( 100 ) ;
405- expect ( function ( ) { $timeout . verifyNoPendingTasks ( ) ; } ) . toThrow ( ) ;
403+ $timeout ( log . fn ( 't2' ) , 1000 ) ;
404+ expect ( function ( ) { $timeout . flush ( 100 ) ; } ) . toThrow ( ) ;
405+ expect ( log ) . toEqual ( [ 't1' ] ) ;
406406
407407 $timeout . flush ( 900 ) ;
408- expect ( function ( ) { $timeout . verifyNoPendingTasks ( ) ; } ) . not . toThrow ( ) ;
409- } ) ) ;
408+ expect ( log ) . toEqual ( [ 't1' , 't2' ] ) ;
409+ expect ( function ( ) { $timeout . flush ( ) ; } ) . toThrow ( ) ;
410+ } ) ;
410411
411412
412- it ( 'should assert against the delay value' , inject ( function ( $timeout ) {
413+ it ( 'should assert against the delay value' , function ( ) {
413414 var count = 0 ;
414415 var iterate = function ( ) {
415416 count ++ ;
@@ -421,7 +422,26 @@ describe('ngMock', function() {
421422 expect ( count ) . toBe ( 1 ) ;
422423 $timeout . flushNext ( 123 ) ;
423424 expect ( count ) . toBe ( 2 ) ;
424- } ) ) ;
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+ } ) ;
425445 } ) ;
426446
427447
0 commit comments