gesture_service.spec.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import GestureService from 'src/services/gesture_service/gesture_service.js'
  2. const mockTouchEvent = (x, y) => ({
  3. touches: [
  4. {
  5. screenX: x,
  6. screenY: y
  7. }
  8. ]
  9. })
  10. describe('GestureService', () => {
  11. describe('swipeGesture', () => {
  12. it('calls the callback on a successful swipe', () => {
  13. let swiped = false
  14. const callback = () => { swiped = true }
  15. const gesture = GestureService.swipeGesture(
  16. GestureService.DIRECTION_RIGHT,
  17. callback
  18. )
  19. GestureService.beginSwipe(mockTouchEvent(100, 100), gesture)
  20. GestureService.updateSwipe(mockTouchEvent(200, 100), gesture)
  21. expect(swiped).to.eql(true)
  22. })
  23. it('calls the callback only once per begin', () => {
  24. let hits = 0
  25. const callback = () => { hits += 1 }
  26. const gesture = GestureService.swipeGesture(
  27. GestureService.DIRECTION_RIGHT,
  28. callback
  29. )
  30. GestureService.beginSwipe(mockTouchEvent(100, 100), gesture)
  31. GestureService.updateSwipe(mockTouchEvent(150, 100), gesture)
  32. GestureService.updateSwipe(mockTouchEvent(200, 100), gesture)
  33. expect(hits).to.eql(1)
  34. })
  35. it('doesn\'t call the callback on an opposite swipe', () => {
  36. let swiped = false
  37. const callback = () => { swiped = true }
  38. const gesture = GestureService.swipeGesture(
  39. GestureService.DIRECTION_RIGHT,
  40. callback
  41. )
  42. GestureService.beginSwipe(mockTouchEvent(100, 100), gesture)
  43. GestureService.updateSwipe(mockTouchEvent(0, 100), gesture)
  44. expect(swiped).to.eql(false)
  45. })
  46. it('doesn\'t call the callback on a swipe below threshold', () => {
  47. let swiped = false
  48. const callback = () => { swiped = true }
  49. const gesture = GestureService.swipeGesture(
  50. GestureService.DIRECTION_RIGHT,
  51. callback,
  52. 100
  53. )
  54. GestureService.beginSwipe(mockTouchEvent(100, 100), gesture)
  55. GestureService.updateSwipe(mockTouchEvent(150, 100), gesture)
  56. expect(swiped).to.eql(false)
  57. })
  58. it('doesn\'t call the callback on a perpendicular swipe', () => {
  59. let swiped = false
  60. const callback = () => { swiped = true }
  61. const gesture = GestureService.swipeGesture(
  62. GestureService.DIRECTION_RIGHT,
  63. callback,
  64. 30,
  65. 0.5
  66. )
  67. GestureService.beginSwipe(mockTouchEvent(100, 100), gesture)
  68. GestureService.updateSwipe(mockTouchEvent(150, 200), gesture)
  69. expect(swiped).to.eql(false)
  70. })
  71. it('calls the callback on perpendicular swipe if within tolerance', () => {
  72. let swiped = false
  73. const callback = () => { swiped = true }
  74. const gesture = GestureService.swipeGesture(
  75. GestureService.DIRECTION_RIGHT,
  76. callback,
  77. 30,
  78. 2.0
  79. )
  80. GestureService.beginSwipe(mockTouchEvent(100, 100), gesture)
  81. GestureService.updateSwipe(mockTouchEvent(150, 150), gesture)
  82. expect(swiped).to.eql(true)
  83. })
  84. it('works with any arbitrary 2d directions', () => {
  85. let swiped = false
  86. const callback = () => { swiped = true }
  87. const gesture = GestureService.swipeGesture(
  88. [-1, -1],
  89. callback,
  90. 30,
  91. 0.1
  92. )
  93. GestureService.beginSwipe(mockTouchEvent(100, 100), gesture)
  94. GestureService.updateSwipe(mockTouchEvent(60, 60), gesture)
  95. expect(swiped).to.eql(true)
  96. })
  97. })
  98. })