test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. var debounce = require('./index.js');
  2. var test = require('tape');
  3. test('debauce', function (t) {
  4. t.plan(3);
  5. var fn = debounce(function (a, b) {
  6. t.deepEqual(this, { call: 3 }, 'context should be preserved');
  7. t.equal(a, 30, 'should preserve args');
  8. t.equal(b, 300, 'should preserve args');
  9. }, 10);
  10. fn.call({ call: 1 }, 10, 100);
  11. fn.call({ call: 2 }, 20, 200);
  12. setTimeout(function () {
  13. fn.call({ call: 3 }, 30, 300);
  14. }, 3);
  15. });
  16. test('multiple calls should extend delay', function (t) {
  17. t.plan(4);
  18. var wasDelayed = false;
  19. var fn = debounce(function (a, b) {
  20. t.deepEqual(this, { call: 3 }, 'context should be preserved');
  21. t.equal(a, 30, 'should preserve args');
  22. t.equal(b, 300, 'should preserve args');
  23. t.ok(wasDelayed, 'should have waited longer than debounce period');
  24. }, 6);
  25. setTimeout(function longer() {
  26. wasDelayed = true;
  27. }, 9);
  28. fn.call({ call: 1 }, 10, 100);
  29. setTimeout(function () {
  30. fn.call({ call: 2 }, 20, 200);
  31. setTimeout(function () {
  32. fn.call({ call: 3 }, 30, 300);
  33. }, 5);
  34. }, 3);
  35. });
  36. test('multiple calls should not extend delay when guarantee is true', function (t) {
  37. t.plan(8);
  38. var first = true;
  39. var wasDelayed = false;
  40. var fn = debounce(
  41. function (a, b) {
  42. if (first) {
  43. t.deepEqual(this, { call: 2 }, '1st context should be preserved');
  44. t.equal(a, 20, '1st should preserve 1st args');
  45. t.equal(b, 200, '1st should preserve 2nd args');
  46. t.notOk(wasDelayed, 'should not have waited longer than debounce period');
  47. first = false;
  48. } else {
  49. t.deepEqual(this, { call: 3 }, 'context should be preserved');
  50. t.equal(a, 30, 'should preserve args');
  51. t.equal(b, 300, 'should preserve args');
  52. t.ok(wasDelayed, 'should have waited longer than debounce period');
  53. }
  54. },
  55. 6,
  56. false,
  57. true
  58. );
  59. setTimeout(function longer() {
  60. wasDelayed = true;
  61. }, 7);
  62. fn.call({ call: 1 }, 10, 100);
  63. setTimeout(function () {
  64. fn.call({ call: 2 }, 20, 200);
  65. setTimeout(function () {
  66. fn.call({ call: 3 }, 30, 300);
  67. }, 5);
  68. }, 3);
  69. });
  70. test('at start', function (t) {
  71. t.plan(9);
  72. var callCount = 0;
  73. var fn = debounce(
  74. function (a, b) {
  75. if (callCount === 0) {
  76. t.deepEqual(this, { call: 1 }, '1st context should be preserved');
  77. t.equal(a, 10, '1st should preserve 1st args');
  78. t.equal(b, 100, '1st should preserve 2nd args');
  79. } else if (callCount === 1) {
  80. t.deepEqual(this, { call: 3 }, 'context should be preserved');
  81. t.equal(a, 30, 'should preserve args');
  82. t.equal(b, 300, 'should preserve args');
  83. } else {
  84. t.deepEqual(this, { call: 4 }, 'context should be preserved');
  85. t.equal(a, 40, 'should preserve 1st args');
  86. t.equal(b, 400, 'should preserve 2nd args');
  87. }
  88. callCount += 1;
  89. },
  90. 6,
  91. true
  92. );
  93. fn.call({ call: 1 }, 10, 100);
  94. fn.call({ call: 2 }, 20, 200);
  95. setTimeout(function () {
  96. fn.call({ call: 3 }, 30, 300);
  97. setTimeout(function () {
  98. fn.call({ call: 4 }, 40, 400);
  99. }, 10);
  100. setTimeout(function () {
  101. fn.call({ call: 5 }, 50, 500);
  102. }, 3);
  103. }, 10);
  104. });