test.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. var test = require('tape')
  2. , raf = require('./index.js')
  3. test('continues to emit events', function(t) {
  4. t.plan(11)
  5. var start = new Date().getTime()
  6. , times = 0
  7. raf(function tick(dt) {
  8. t.ok(dt >= 0, 'time has passed: ' + dt)
  9. if(++times == 10) {
  10. var elapsed = (new Date().getTime() - start)
  11. t.ok(elapsed >= 150, 'should take at least 9 frames worth of wall time: ' + elapsed)
  12. t.end()
  13. } else {
  14. raf(tick)
  15. }
  16. })
  17. })
  18. test('cancel removes callbacks from queue', function(t) {
  19. t.plan(6)
  20. function cb1() { cb1.called = true }
  21. function cb2() { cb2.called = true }
  22. function cb3() { cb3.called = true }
  23. var handle1 = raf(cb1)
  24. t.ok(handle1, 'returns a handle')
  25. var handle2 = raf(cb2)
  26. t.ok(handle2, 'returns a handle')
  27. var handle3 = raf(cb3)
  28. t.ok(handle3, 'returns a handle')
  29. raf.cancel(handle2)
  30. raf(function() {
  31. t.ok(cb1.called, 'callback was invoked')
  32. t.notOk(cb2.called, 'callback was cancelled')
  33. t.ok(cb3.called, 'callback was invoked')
  34. t.end()
  35. })
  36. })
  37. test('raf should throw on errors', function(t) {
  38. t.plan(1)
  39. var onError = function() {
  40. t.pass('error bubbled up to event loop')
  41. }
  42. if(typeof window !== 'undefined') {
  43. window.onerror = onError
  44. } else if(typeof process !== 'undefined') {
  45. process.on('uncaughtException', onError)
  46. }
  47. raf(function() {
  48. throw new Error('foo')
  49. })
  50. })