index.js 5.9 KB

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