aboutsummaryrefslogtreecommitdiff
path: root/test/unit/specs/services/gesture_service/gesture_service.spec.js
blob: a91f95db6c84654d0f00dd18af614a532f67c1ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import GestureService from 'src/services/gesture_service/gesture_service.js'

const mockTouchEvent = (x, y) => ({
  touches: [
    {
      screenX: x,
      screenY: y
    }
  ]
})

describe('GestureService', () => {
  describe('swipeGesture', () => {
    it('calls the callback on a successful swipe', () => {
      let swiped = false
      const callback = () => { swiped = true }
      const gesture = GestureService.swipeGesture(
        GestureService.DIRECTION_RIGHT,
        callback
      )

      GestureService.beginSwipe(mockTouchEvent(100, 100), gesture)
      GestureService.updateSwipe(mockTouchEvent(200, 100), gesture)

      expect(swiped).to.eql(true)
    })

    it('calls the callback only once per begin', () => {
      let hits = 0
      const callback = () => { hits += 1 }
      const gesture = GestureService.swipeGesture(
        GestureService.DIRECTION_RIGHT,
        callback
      )

      GestureService.beginSwipe(mockTouchEvent(100, 100), gesture)
      GestureService.updateSwipe(mockTouchEvent(150, 100), gesture)
      GestureService.updateSwipe(mockTouchEvent(200, 100), gesture)

      expect(hits).to.eql(1)
    })

    it('doesn\'t call the callback on an opposite swipe', () => {
      let swiped = false
      const callback = () => { swiped = true }
      const gesture = GestureService.swipeGesture(
        GestureService.DIRECTION_RIGHT,
        callback
      )

      GestureService.beginSwipe(mockTouchEvent(100, 100), gesture)
      GestureService.updateSwipe(mockTouchEvent(0, 100), gesture)

      expect(swiped).to.eql(false)
    })

    it('doesn\'t call the callback on a swipe below threshold', () => {
      let swiped = false
      const callback = () => { swiped = true }
      const gesture = GestureService.swipeGesture(
        GestureService.DIRECTION_RIGHT,
        callback,
        100
      )

      GestureService.beginSwipe(mockTouchEvent(100, 100), gesture)
      GestureService.updateSwipe(mockTouchEvent(150, 100), gesture)

      expect(swiped).to.eql(false)
    })

    it('doesn\'t call the callback on a perpendicular swipe', () => {
      let swiped = false
      const callback = () => { swiped = true }
      const gesture = GestureService.swipeGesture(
        GestureService.DIRECTION_RIGHT,
        callback,
        30,
        0.5
      )

      GestureService.beginSwipe(mockTouchEvent(100, 100), gesture)
      GestureService.updateSwipe(mockTouchEvent(150, 200), gesture)

      expect(swiped).to.eql(false)
    })

    it('calls the callback on perpendicular swipe if within tolerance', () => {
      let swiped = false
      const callback = () => { swiped = true }
      const gesture = GestureService.swipeGesture(
        GestureService.DIRECTION_RIGHT,
        callback,
        30,
        2.0
      )

      GestureService.beginSwipe(mockTouchEvent(100, 100), gesture)
      GestureService.updateSwipe(mockTouchEvent(150, 150), gesture)

      expect(swiped).to.eql(true)
    })

    it('works with any arbitrary 2d directions', () => {
      let swiped = false
      const callback = () => { swiped = true }
      const gesture = GestureService.swipeGesture(
        [-1, -1],
        callback,
        30,
        0.1
      )

      GestureService.beginSwipe(mockTouchEvent(100, 100), gesture)
      GestureService.updateSwipe(mockTouchEvent(60, 60), gesture)

      expect(swiped).to.eql(true)
    })
  })
})