test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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(function (a, b) {
  41. if (first) {
  42. t.deepEqual(this, {call: 2}, '1st context should be preserved')
  43. t.equal(a, 20, '1st should preserve 1st args')
  44. t.equal(b, 200, '1st should preserve 2nd args')
  45. t.notOk(wasDelayed, 'should not have waited longer than debounce period')
  46. first = false
  47. } else {
  48. t.deepEqual(this, {call: 3}, 'context should be preserved')
  49. t.equal(a, 30, 'should preserve args')
  50. t.equal(b, 300, 'should preserve args')
  51. t.ok(wasDelayed, 'should have waited longer than debounce period')
  52. }
  53. }, 6, false, true)
  54. setTimeout(function longer () {
  55. wasDelayed = true
  56. }, 7)
  57. fn.call({call: 1}, 10, 100)
  58. setTimeout(function () {
  59. fn.call({call: 2}, 20, 200)
  60. setTimeout(function () {
  61. fn.call({call: 3}, 30, 300)
  62. }, 5)
  63. }, 3)
  64. })
  65. test('at start', function (t) {
  66. t.plan(9)
  67. var callCount = 0
  68. var fn = debounce(function (a, b) {
  69. if (callCount === 0) {
  70. t.deepEqual(this, {call: 1}, '1st context should be preserved')
  71. t.equal(a, 10, '1st should preserve 1st args')
  72. t.equal(b, 100, '1st should preserve 2nd args')
  73. } else if (callCount === 1) {
  74. t.deepEqual(this, {call: 3}, 'context should be preserved')
  75. t.equal(a, 30, 'should preserve args')
  76. t.equal(b, 300, 'should preserve args')
  77. } else {
  78. t.deepEqual(this, {call: 4}, 'context should be preserved')
  79. t.equal(a, 40, 'should preserve 1st args')
  80. t.equal(b, 400, 'should preserve 2nd args')
  81. }
  82. callCount += 1
  83. }, 6, true)
  84. fn.call({call: 1}, 10, 100)
  85. fn.call({call: 2}, 20, 200)
  86. setTimeout(function () {
  87. fn.call({call: 3}, 30, 300)
  88. setTimeout(function () {
  89. fn.call({call: 4}, 40, 400)
  90. }, 10)
  91. setTimeout(function () {
  92. fn.call({call: 5}, 50, 500)
  93. }, 3)
  94. }, 10)
  95. })