unit.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // Not sure why this isn't set by default in qunit.js..
  2. QUnit.jsDump.HTML = false;
  3. $(function(){ // START CLOSURE
  4. $('#jq_version').html( $.fn.jquery );
  5. var pause = 500,
  6. delay = 100;
  7. function exec_many_times( each, complete ) {
  8. var i = 0,
  9. repeated,
  10. id;
  11. function start(){
  12. id = setInterval(function(){
  13. each();
  14. if ( ++i === 50 ) {
  15. clearInterval( id );
  16. complete( repeated ? null : function(){
  17. i = 0;
  18. repeated = true;
  19. setTimeout( start, pause );
  20. });
  21. }
  22. }, 20);
  23. }
  24. setTimeout( start, pause );
  25. };
  26. module( '$.throttle' );
  27. test( 'delay, callback', function() {
  28. expect( 7 );
  29. stop();
  30. var start_time,
  31. i = 0,
  32. arr = [],
  33. fn = function( now ){
  34. arr.push( now - this )
  35. },
  36. throttled = $.throttle( delay, fn );
  37. equals( throttled.guid, fn.guid, 'throttled-callback and callback should have the same .guid' );
  38. exec_many_times( function(){
  39. var now = +new Date();
  40. start_time = start_time || now;
  41. i++;
  42. throttled.call( start_time, now );
  43. }, function( callback ){
  44. var len = arr.length;
  45. setTimeout(function(){
  46. //console.log( arr, arr.length, len, i );
  47. ok( arr.length < i, 'callback should be executed less # of times than throttled-callback' );
  48. equals( arr[0], 0, 'callback should be executed immediately' );
  49. equals( arr.length - len, 1, 'callback should be executed one more time after finish' );
  50. start_time = null;
  51. arr = [];
  52. i = 0;
  53. callback ? callback() : start();
  54. }, delay * 2);
  55. })
  56. });
  57. test( 'delay, false, callback', function() {
  58. expect( 7 );
  59. stop();
  60. var start_time,
  61. i = 0,
  62. arr = [],
  63. fn = function( now ){
  64. arr.push( now - this )
  65. },
  66. throttled = $.throttle( delay, false, fn );
  67. equals( throttled.guid, fn.guid, 'throttled-callback and callback should have the same .guid' );
  68. exec_many_times( function(){
  69. var now = +new Date();
  70. start_time = start_time || now;
  71. i++;
  72. throttled.call( start_time, now );
  73. }, function( callback ){
  74. var len = arr.length;
  75. setTimeout(function(){
  76. //console.log( arr, arr.length, len, i );
  77. ok( arr.length < i, 'callback should be executed less # of times than throttled-callback' );
  78. equals( arr[0], 0, 'callback should be executed immediately' );
  79. equals( arr.length - len, 1, 'callback should be executed one more time after finish' );
  80. start_time = null;
  81. arr = [];
  82. i = 0;
  83. callback ? callback() : start();
  84. }, delay * 2);
  85. })
  86. });
  87. test( 'delay, true, callback', function() {
  88. expect( 7 );
  89. stop();
  90. var start_time,
  91. i = 0,
  92. arr = [],
  93. fn = function( now ){
  94. arr.push( now - this )
  95. },
  96. throttled = $.throttle( delay, true, fn );
  97. equals( throttled.guid, fn.guid, 'throttled-callback and callback should have the same .guid' );
  98. exec_many_times( function(){
  99. var now = +new Date();
  100. start_time = start_time || now;
  101. i++;
  102. throttled.call( start_time, now );
  103. }, function( callback ){
  104. var len = arr.length;
  105. setTimeout(function(){
  106. //console.log( arr, arr.length, len, i );
  107. ok( arr.length < i, 'callback should be executed less # of times than throttled-callback' );
  108. equals( arr[0], 0, 'callback should be executed immediately' );
  109. equals( arr.length - len, 0, 'callback should NOT be executed one more time after finish' );
  110. start_time = null;
  111. arr = [];
  112. i = 0;
  113. callback ? callback() : start();
  114. }, delay * 2);
  115. })
  116. });
  117. module( '$.debounce' );
  118. test( 'delay, callback', function() {
  119. expect( 5 );
  120. stop();
  121. var start_time,
  122. i = 0,
  123. arr = [],
  124. fn = function(){
  125. arr.push( +new Date() )
  126. },
  127. debounced = $.debounce( delay, fn );
  128. equals( debounced.guid, fn.guid, 'throttled-callback and callback should have the same .guid' );
  129. exec_many_times( function(){
  130. start_time = start_time || +new Date();
  131. i++;
  132. debounced.call();
  133. }, function( callback ){
  134. var len = arr.length,
  135. done_time = +new Date();
  136. setTimeout(function(){
  137. //console.log( arr[0] - done_time );
  138. equals( arr.length, 1, 'callback was executed once' );
  139. ok( arr[0] >= done_time, 'callback should be executed after the finish' );
  140. start_time = null;
  141. arr = [];
  142. i = 0;
  143. callback ? callback() : start();
  144. }, delay * 2);
  145. })
  146. });
  147. test( 'delay, false, callback', function() {
  148. expect( 5 );
  149. stop();
  150. var start_time,
  151. i = 0,
  152. arr = [],
  153. fn = function(){
  154. arr.push( +new Date() )
  155. },
  156. debounced = $.debounce( delay, false, fn );
  157. equals( debounced.guid, fn.guid, 'throttled-callback and callback should have the same .guid' );
  158. exec_many_times( function(){
  159. start_time = start_time || +new Date();
  160. i++;
  161. debounced.call();
  162. }, function( callback ){
  163. var len = arr.length,
  164. done_time = +new Date();
  165. setTimeout(function(){
  166. //console.log( arr[0] - done_time );
  167. equals( arr.length, 1, 'callback was executed once' );
  168. ok( arr[0] >= done_time, 'callback should be executed after the finish' );
  169. start_time = null;
  170. arr = [];
  171. i = 0;
  172. callback ? callback() : start();
  173. }, delay * 2);
  174. })
  175. });
  176. test( 'delay, true, callback', function() {
  177. expect( 5 );
  178. stop();
  179. var start_time,
  180. i = 0,
  181. arr = [],
  182. fn = function(){
  183. arr.push( +new Date() )
  184. },
  185. debounced = $.debounce( delay, true, fn );
  186. equals( debounced.guid, fn.guid, 'throttled-callback and callback should have the same .guid' );
  187. exec_many_times( function(){
  188. start_time = start_time || +new Date();
  189. i++;
  190. debounced.call();
  191. }, function( callback ){
  192. var len = arr.length;
  193. setTimeout(function(){
  194. //console.log( arr[0] - start_time );
  195. equals( arr.length, 1, 'callback was executed once' );
  196. ok( arr[0] - start_time <= 5, 'callback should be executed at the start' );
  197. start_time = null;
  198. arr = [];
  199. i = 0;
  200. callback ? callback() : start();
  201. }, delay * 2);
  202. })
  203. });
  204. }); // END CLOSURE