test.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. var run = require('tape')
  2. var ease = require('ease-component')
  3. var scroll = require('./')
  4. var container = document.createElement('div')
  5. var box = document.createElement('div')
  6. container.style.cssText = [
  7. 'height: 100px',
  8. 'outline: 1px solid #000',
  9. 'overflow: scroll',
  10. 'width: 100px'
  11. ].join(';')
  12. box.style.cssText = [
  13. 'outline: 1px solid #888',
  14. 'height: 50px',
  15. 'width: 300px'
  16. ].join(';')
  17. var n = 50
  18. while (--n) {
  19. container.appendChild(box.cloneNode(true))
  20. }
  21. document.body.appendChild(container)
  22. run('it scrolls', function (test) {
  23. container.scrollTop = 0
  24. container.scrollLeft = 200
  25. test.plan(3)
  26. scroll.top(container, 200, function (error, position) {
  27. test.ok(position === 200, 'it scrolled down 200 pixels')
  28. scroll.top(container, 200, function (error, position) {
  29. test.equal(error.message, 'Element already at target scroll position')
  30. })
  31. })
  32. var leftOptions = { duration: 1000, ease: ease.inBounce }
  33. scroll.left(container, -200, leftOptions, function (error, position) {
  34. test.ok(position === 0, 'it scrolled across 200 pixels')
  35. })
  36. })
  37. run('it can be cancelled', function (test) {
  38. container.scrollTop = 0
  39. container.scrollLeft = 200
  40. test.plan(2)
  41. var options = { duration: 1000, ease: ease.inBounce }
  42. var cancel = scroll.left(container, -200, options,
  43. function (error, position) {
  44. test.ok(error, 'it produced an error')
  45. test.equal(error.message, 'Scroll cancelled', 'it cancelled the animation')
  46. })
  47. setTimeout(cancel, 500)
  48. })
  49. run('callback fires after scroll events', function (test) {
  50. container.scrollTop = 0
  51. test.plan(1)
  52. var okay = true
  53. var done = false
  54. container.addEventListener('scroll', function () {
  55. if (done) okay = false
  56. }, false)
  57. scroll.top(container, 200, function () {
  58. done = true
  59. setTimeout(function () {
  60. test.ok(okay, 'callback fired after scroll events')
  61. }, 100)
  62. })
  63. })