@@ -873,6 +873,110 @@ describe("lolex", function () {
873873
874874 } ) ;
875875
876+ describe ( 'runToLast' , function ( ) {
877+
878+ it ( 'returns current time when there are no timers' , function ( ) {
879+ this . clock = lolex . createClock ( ) ;
880+
881+ var time = this . clock . runToLast ( ) ;
882+
883+ assert . equals ( time , 0 ) ;
884+ } ) ;
885+
886+ it ( 'runs all existing timers' , function ( ) {
887+ this . clock = lolex . createClock ( ) ;
888+ var spies = [ sinon . spy ( ) , sinon . spy ( ) ] ;
889+ this . clock . setTimeout ( spies [ 0 ] , 10 ) ;
890+ this . clock . setTimeout ( spies [ 1 ] , 50 ) ;
891+
892+ this . clock . runToLast ( ) ;
893+
894+ assert ( spies [ 0 ] . called ) ;
895+ assert ( spies [ 1 ] . called ) ;
896+ } ) ;
897+
898+ it ( 'returns time of the last timer' , function ( ) {
899+ this . clock = lolex . createClock ( ) ;
900+ var spies = [ sinon . spy ( ) , sinon . spy ( ) ] ;
901+ this . clock . setTimeout ( spies [ 0 ] , 10 ) ;
902+ this . clock . setTimeout ( spies [ 1 ] , 50 ) ;
903+
904+ var time = this . clock . runToLast ( ) ;
905+
906+ assert . equals ( time , 50 ) ;
907+ } ) ;
908+
909+ it ( 'runs all existing timers when two timers are matched for being last' , function ( ) {
910+ this . clock = lolex . createClock ( ) ;
911+ var spies = [ sinon . spy ( ) , sinon . spy ( ) ] ;
912+ this . clock . setTimeout ( spies [ 0 ] , 10 ) ;
913+ this . clock . setTimeout ( spies [ 1 ] , 10 ) ;
914+
915+ this . clock . runToLast ( ) ;
916+
917+ assert ( spies [ 0 ] . called ) ;
918+ assert ( spies [ 1 ] . called ) ;
919+ } ) ;
920+
921+ it ( 'new timers added with a call time later than the last existing timer are NOT run' , function ( ) {
922+ this . clock = lolex . createClock ( ) ;
923+ var test = this ;
924+ var spies = [
925+ sinon . spy ( function ( ) {
926+ test . clock . setTimeout ( spies [ 1 ] , 50 ) ;
927+ } ) ,
928+ sinon . spy ( )
929+ ] ;
930+
931+ // Spy calls another setTimeout
932+ this . clock . setTimeout ( spies [ 0 ] , 10 ) ;
933+
934+ this . clock . runToLast ( ) ;
935+
936+ assert . isTrue ( spies [ 0 ] . called ) ;
937+ assert . isFalse ( spies [ 1 ] . called ) ;
938+ } ) ;
939+
940+ it ( 'new timers added with a call time ealier than the last existing timer are run' , function ( ) {
941+ this . clock = lolex . createClock ( ) ;
942+ var test = this ;
943+ var spies = [
944+ sinon . spy ( ) ,
945+ sinon . spy ( function ( ) {
946+ test . clock . setTimeout ( spies [ 2 ] , 50 ) ;
947+ } ) ,
948+ sinon . spy ( )
949+ ] ;
950+
951+ this . clock . setTimeout ( spies [ 0 ] , 100 ) ;
952+ // Spy calls another setTimeout
953+ this . clock . setTimeout ( spies [ 1 ] , 10 ) ;
954+
955+ this . clock . runToLast ( ) ;
956+
957+ assert . isTrue ( spies [ 0 ] . called ) ;
958+ assert . isTrue ( spies [ 1 ] . called ) ;
959+ assert . isTrue ( spies [ 2 ] . called ) ;
960+ } ) ;
961+
962+ it ( 'new timers cannot cause an infinite loop' , function ( ) {
963+ this . clock = lolex . createClock ( ) ;
964+ var test = this ;
965+ var spy = sinon . spy ( ) ;
966+ var recursiveCallback = function ( ) {
967+ test . clock . setTimeout ( recursiveCallback , 0 ) ;
968+ } ;
969+
970+ this . clock . setTimeout ( recursiveCallback , 0 ) ;
971+ this . clock . setTimeout ( spy , 100 ) ;
972+
973+ this . clock . runToLast ( ) ;
974+
975+ assert . isTrue ( spy . called ) ;
976+ } ) ;
977+
978+ } ) ;
979+
876980 describe ( "clearTimeout" , function ( ) {
877981
878982 beforeEach ( function ( ) {
0 commit comments