@@ -327,6 +327,119 @@ describe("PostProcesses", function() {
327327 });*/
328328} )
329329
330+ describe ( "setTimeout" , function ( ) {
331+ this . timeout ( 1000 ) ;
332+ it ( "should return an id greater than zero" , function ( ) {
333+ const id = setTimeout ( ( ) => { } , 0 ) ;
334+ expect ( id ) . to . be . greaterThan ( 0 ) ;
335+ } ) ;
336+ it ( "should return an id greater than zero when given an undefined function" , function ( ) {
337+ const id = setTimeout ( undefined , 0 ) ;
338+ expect ( id ) . to . be . greaterThan ( 0 ) ;
339+ } ) ;
340+ it ( "should call the given function after the given delay" , function ( done ) {
341+ const startTime = new Date ( ) . getTime ( ) ;
342+ setTimeout ( ( ) => {
343+ try {
344+ expect ( new Date ( ) . getTime ( ) - startTime ) . to . be . at . least ( 10 ) ;
345+ done ( ) ;
346+ }
347+ catch ( e ) {
348+ done ( e ) ;
349+ }
350+ } , 10 ) ;
351+ } ) ;
352+ it ( "should call the given nested function after the given delay" , function ( done ) {
353+ const startTime = new Date ( ) . getTime ( ) ;
354+ setTimeout ( ( ) => {
355+ setTimeout ( ( ) => {
356+ try {
357+ expect ( new Date ( ) . getTime ( ) - startTime ) . to . be . at . least ( 20 ) ;
358+ done ( ) ;
359+ }
360+ catch ( e ) {
361+ done ( e ) ;
362+ }
363+ } , 10 ) ;
364+ } , 10 ) ;
365+ } ) ;
366+ it ( "should call the given function after the given delay when the delay is a string representing a valid number" , function ( done ) {
367+ const startTime = new Date ( ) . getTime ( ) ;
368+ setTimeout ( ( ) => {
369+ try {
370+ expect ( new Date ( ) . getTime ( ) - startTime ) . to . be . at . least ( 10 ) ;
371+ done ( ) ;
372+ }
373+ catch ( e ) {
374+ done ( e ) ;
375+ }
376+ } , "10" ) ;
377+ } ) ;
378+ it ( "should call the given function after zero milliseconds when the delay is a string representing an invalid number" , function ( done ) {
379+ setTimeout ( ( ) => {
380+ done ( ) ;
381+ } , "a" ) ;
382+ } ) ;
383+ it ( "should call the given function after other tasks execute when the given delay is zero" , function ( done ) {
384+ let trailingCodeExecuted = false ;
385+ setTimeout ( ( ) => {
386+ try {
387+ expect ( trailingCodeExecuted ) . to . be . true ;
388+ done ( ) ;
389+ }
390+ catch ( e ) {
391+ done ( e ) ;
392+ }
393+ } , 0 ) ;
394+ trailingCodeExecuted = true ;
395+ } ) ;
396+ it ( "should call the given function after other tasks execute when the given delay is undefined" , function ( done ) {
397+ let trailingCodeExecuted = false ;
398+ setTimeout ( ( ) => {
399+ try {
400+ expect ( trailingCodeExecuted ) . to . be . true ;
401+ done ( ) ;
402+ }
403+ catch ( e ) {
404+ done ( e ) ;
405+ }
406+ } , undefined ) ;
407+ trailingCodeExecuted = true ;
408+ } ) ;
409+ it ( "should call the given functions in the correct order" , function ( done ) {
410+ const called = [ ] ;
411+ for ( let i = 9 ; i >= 0 ; i -- ) {
412+ setTimeout ( ( ) => {
413+ called . push ( i * 2 ) ;
414+ if ( called . length === 10 ) {
415+ try {
416+ expect ( called ) . to . deep . equal ( [ 0 , 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 , 18 ] ) ;
417+ done ( ) ;
418+ }
419+ catch ( e ) {
420+ done ( e ) ;
421+ }
422+ }
423+ } , i * 2 ) ;
424+ }
425+ } ) ;
426+ } )
427+
428+ describe ( "clearTimeout" , function ( ) {
429+ this . timeout ( 0 ) ;
430+ it ( "should stop the timeout matching the given timeout id" , function ( done ) {
431+ const id = setTimeout ( ( ) => {
432+ done ( new Error ( "Timeout was not cleared" ) ) ;
433+ } , 0 ) ;
434+ clearTimeout ( id ) ;
435+ setTimeout ( done , 100 ) ;
436+ } ) ;
437+ it ( "should do nothing if the given timeout id is undefined" , function ( done ) {
438+ setTimeout ( ( ) => { done ( ) ; } , 0 ) ;
439+ clearTimeout ( undefined ) ;
440+ } ) ;
441+ } )
442+
330443mocha . run ( failures => {
331444 // Test program will wait for code to be set before exiting
332445 if ( failures > 0 ) {
0 commit comments