@@ -319,7 +319,7 @@ describe('ngMock', function() {
319319 browser . defer ( logFn ( 'B' ) , 2 ) ;
320320 browser . defer ( logFn ( 'C' ) , 3 ) ;
321321
322- expect ( function ( ) { browser . defer . flush ( 0 ) ; } ) . toThrow ( 'No deferred tasks with delay up to 0ms to be flushed!' ) ;
322+ browser . defer . flush ( 0 ) ;
323323 expect ( browser . defer . now ) . toEqual ( 0 ) ;
324324 expect ( log ) . toEqual ( '' ) ;
325325
@@ -333,15 +333,7 @@ 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!' ) ;
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' ) ;
345337 } ) ;
346338 } ) ;
347339
@@ -372,45 +364,52 @@ describe('ngMock', function() {
372364
373365
374366 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 ) } } ;
376371
377- beforeEach ( module ( provideLog ) ) ;
372+ $timeout ( logFn ( 't1' ) ) ;
373+ $timeout ( logFn ( 't2' ) , 200 ) ;
374+ $timeout ( logFn ( 't3' ) ) ;
375+ expect ( logger ) . toEqual ( [ ] ) ;
378376
379- beforeEach ( inject ( function ( _log_ , _$timeout_ ) {
380- log = _log_ ;
381- $timeout = _$timeout_ ;
377+ $timeout . flush ( ) ;
378+ expect ( logger ) . toEqual ( [ 't1' , 't3' , 't2' ] ) ;
382379 } ) ) ;
383380
384381
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+ } ) ) ;
386388
387389
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 ) ;
392392
393393 $timeout . flush ( ) ;
394- expect ( log ) . toEqual ( [ 't1' , 't3' , 't2' ] ) ;
395- } ) ;
394+ expect ( function ( ) { $timeout . verifyNoPendingTasks ( ) ; } ) . not . toThrow ( ) ;
395+ } ) ) ;
396396
397397
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 ) ;
400400 $timeout . flush ( 100 ) ;
401- expect ( log ) . toEqual ( [ 't1' ] ) ;
401+ expect ( function ( ) { $timeout . verifyNoPendingTasks ( ) ; } ) . not . toThrow ( ) ;
402402
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 ( ) ;
406406
407407 $timeout . flush ( 900 ) ;
408- expect ( log ) . toEqual ( [ 't1' , 't2' ] ) ;
409- expect ( function ( ) { $timeout . flush ( ) ; } ) . toThrow ( ) ;
410- } ) ;
408+ expect ( function ( ) { $timeout . verifyNoPendingTasks ( ) ; } ) . not . toThrow ( ) ;
409+ } ) ) ;
411410
412411
413- it ( 'should assert against the delay value' , function ( ) {
412+ it ( 'should assert against the delay value' , inject ( function ( $timeout ) {
414413 var count = 0 ;
415414 var iterate = function ( ) {
416415 count ++ ;
@@ -422,26 +421,7 @@ describe('ngMock', function() {
422421 expect ( count ) . toBe ( 1 ) ;
423422 $timeout . flushNext ( 123 ) ;
424423 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+ } ) ) ;
445425 } ) ;
446426
447427
0 commit comments