|
2 | 2 | 'use strict'; |
3 | 3 |
|
4 | 4 | describe('uiGridScroller', function() { |
5 | | - var element, scrollHandler, gridUtil, uiGridScroller, uiGridScrollerConstants; |
| 5 | + var element, scrollHandler, gridUtil, uiGridScroller, uiGridScrollerConstants, |
| 6 | + callbacks = {}; |
6 | 7 |
|
7 | 8 | beforeEach(function() { |
8 | 9 | element = { |
9 | 10 | 0: { |
| 11 | + scrollTop: 0, |
| 12 | + scrollLeft: 0, |
| 13 | + clientWidth: 300, |
| 14 | + clientHeight: 300, |
10 | 15 | children: { |
11 | | - 0: {} |
| 16 | + 0: { |
| 17 | + offsetWidth: 300, |
| 18 | + offsetHeight: 300 |
| 19 | + } |
12 | 20 | } |
13 | 21 | }, |
14 | | - on: jasmine.createSpy('on'), |
15 | | - off: jasmine.createSpy('off') |
| 22 | + on: jasmine.createSpy('on').and.callFake(function(eventName, callback) { |
| 23 | + callbacks[eventName] = callback; |
| 24 | + }), |
| 25 | + off: jasmine.createSpy('off'), |
| 26 | + trigger: function(eventName, args) { |
| 27 | + args.type = eventName; |
| 28 | + callbacks[eventName](args); |
| 29 | + } |
16 | 30 | }; |
17 | 31 | scrollHandler = jasmine.createSpy('scrollHandler'); |
18 | 32 |
|
|
23 | 37 | uiGridScrollerConstants = _uiGridScrollerConstants_; |
24 | 38 | gridUtil = _gridUtil_; |
25 | 39 | }); |
| 40 | + |
| 41 | + spyOn(window, 'requestAnimationFrame').and.callFake(jasmine.createSpy('requestAnimationFrame')); |
26 | 42 | }); |
27 | 43 |
|
28 | 44 | describe('when gridUtils.isTouchEnabled returns true', function() { |
|
45 | 61 | it('should initialize touchcancel', function() { |
46 | 62 | expect(element.on).toHaveBeenCalledWith('touchcancel', jasmine.any(Function)); |
47 | 63 | }); |
48 | | - xdescribe('events', function() { |
| 64 | + describe('events', function() { |
49 | 65 | describe('on touchstart', function() { |
50 | 66 | beforeEach(function() { |
51 | | - element.on.and.callFake(function(eventName, callback) { |
52 | | - if (eventName === 'touchstart') { |
53 | | - callback({ |
54 | | - type: eventName, |
55 | | - touches: [true] |
56 | | - }); |
57 | | - } |
| 67 | + element.trigger('touchstart', { |
| 68 | + touches: [{ |
| 69 | + pageX: 0, |
| 70 | + pageY: 0 |
| 71 | + }] |
58 | 72 | }); |
59 | | - uiGridScroller(element, scrollHandler); |
60 | 73 | }); |
61 | 74 | it('should remove the scroll event from the element', function() { |
62 | 75 | expect(element.off).toHaveBeenCalledWith('scroll', scrollHandler); |
63 | 76 | }); |
64 | 77 | it('should update the uiGridScroller.initiated value to TOUCHABLE', function() { |
65 | 78 | expect(uiGridScroller.initiated).toEqual(uiGridScrollerConstants.scrollType.TOUCHABLE); |
66 | 79 | }); |
67 | | - afterEach(function() { |
68 | | - element.on.and.callFake(angular.noop); |
| 80 | + it('should update the uiGridScroller.isAnimating value to false', function() { |
| 81 | + expect(uiGridScroller.isAnimating).toBe(false); |
69 | 82 | }); |
70 | 83 | }); |
71 | 84 | describe('on touchmove', function() { |
72 | 85 | var preventDefaultSpy; |
73 | 86 |
|
74 | 87 | beforeEach(function() { |
75 | 88 | preventDefaultSpy = jasmine.createSpy('preventDefault'); |
76 | | - element.on.and.callFake(function(eventName, callback) { |
77 | | - if (eventName === 'touchmove') { |
78 | | - callback({ |
79 | | - type: eventName, |
80 | | - touches: [true], |
81 | | - preventDefault: preventDefaultSpy |
82 | | - }); |
83 | | - } |
84 | | - }); |
85 | 89 | }); |
86 | 90 | describe('when the uiGridScroller has been initiated with a touch event', function() { |
87 | 91 | beforeEach(function() { |
88 | 92 | uiGridScroller.initiated = uiGridScrollerConstants.scrollType.TOUCHABLE; |
89 | | - uiGridScroller(element, scrollHandler); |
| 93 | + element.trigger('touchmove', { |
| 94 | + touches: [{ |
| 95 | + pageX: 0, |
| 96 | + pageY: 0 |
| 97 | + }], |
| 98 | + preventDefault: preventDefaultSpy |
| 99 | + }); |
90 | 100 | }); |
91 | 101 | it('should prevent the default behavior', function() { |
92 | 102 | expect(preventDefaultSpy).toHaveBeenCalled(); |
|
98 | 108 | describe('when the uiGridScroller has not been initiated with a touch event', function() { |
99 | 109 | beforeEach(function() { |
100 | 110 | uiGridScroller.initiated = uiGridScrollerConstants.scrollType.NONE; |
101 | | - uiGridScroller(element, scrollHandler); |
| 111 | + element.trigger('touchmove', { |
| 112 | + touches: [{ |
| 113 | + pageX: 0, |
| 114 | + pageY: 0 |
| 115 | + }], |
| 116 | + preventDefault: preventDefaultSpy |
| 117 | + }); |
102 | 118 | }); |
103 | 119 | it('should prevent the default behavior', function() { |
104 | 120 | expect(preventDefaultSpy).toHaveBeenCalled(); |
|
107 | 123 | expect(scrollHandler).not.toHaveBeenCalled(); |
108 | 124 | }); |
109 | 125 | }); |
110 | | - afterEach(function() { |
111 | | - element.on.and.callFake(angular.noop); |
112 | | - }); |
113 | 126 | }); |
114 | | - function testEndFunction() { |
| 127 | + function testEndFunction(eventName) { |
115 | 128 | describe('when the uiGridScroller has been initiated with a touch event', function() { |
116 | 129 | beforeEach(function() { |
| 130 | + uiGridScroller.isAnimating = false; |
117 | 131 | uiGridScroller.initiated = uiGridScrollerConstants.scrollType.TOUCHABLE; |
118 | | - uiGridScroller(element, scrollHandler); |
| 132 | + element.trigger(eventName, { |
| 133 | + touches: [{ |
| 134 | + pageX: 0, |
| 135 | + pageY: 0 |
| 136 | + }] |
| 137 | + }); |
119 | 138 | }); |
120 | 139 | it('should update the uiGridScroller.initiated value to NONE', function() { |
121 | 140 | expect(uiGridScroller.initiated).toEqual(uiGridScrollerConstants.scrollType.NONE); |
122 | 141 | }); |
| 142 | + it('should update the uiGridScroller.isAnimating value to true', function() { |
| 143 | + expect(uiGridScroller.isAnimating).toBe(true); |
| 144 | + }); |
| 145 | + it('should call requestAnimationFrame in the window', function() { |
| 146 | + expect(window.requestAnimationFrame).toHaveBeenCalled(); |
| 147 | + }); |
123 | 148 | }); |
124 | 149 | describe('when the uiGridScroller has not been initiated with a touch event', function() { |
125 | 150 | beforeEach(function() { |
| 151 | + uiGridScroller.isAnimating = false; |
126 | 152 | uiGridScroller.initiated = uiGridScrollerConstants.scrollType.MOUSE; |
127 | | - uiGridScroller(element, scrollHandler); |
| 153 | + element.trigger(eventName, { |
| 154 | + touches: [{ |
| 155 | + pageX: 0, |
| 156 | + pageY: 0 |
| 157 | + }] |
| 158 | + }); |
128 | 159 | }); |
129 | 160 | it('should not update the uiGridScroller.initiated value', function() { |
130 | 161 | expect(uiGridScroller.initiated).toEqual(uiGridScrollerConstants.scrollType.MOUSE); |
131 | 162 | }); |
132 | | - }); |
133 | | - afterEach(function() { |
134 | | - element.on.and.callFake(angular.noop); |
| 163 | + it('should not update the uiGridScroller.isAnimating value', function() { |
| 164 | + expect(uiGridScroller.isAnimating).toBe(false); |
| 165 | + }); |
| 166 | + it('should not call requestAnimationFrame in the window', function() { |
| 167 | + expect(window.requestAnimationFrame).not.toHaveBeenCalled(); |
| 168 | + }); |
135 | 169 | }); |
136 | 170 | } |
137 | 171 | describe('on touchend', function() { |
138 | | - beforeEach(function() { |
139 | | - element.on.and.callFake(function(eventName, callback) { |
140 | | - if (eventName === 'touchend') { |
141 | | - callback({ |
142 | | - type: eventName, |
143 | | - touches: [true] |
144 | | - }); |
145 | | - } |
146 | | - }); |
147 | | - }); |
148 | | - testEndFunction(); |
| 172 | + testEndFunction('touchend'); |
149 | 173 | }); |
150 | 174 | describe('on touchcancel', function() { |
151 | | - beforeEach(function() { |
152 | | - element.on.and.callFake(function(eventName, callback) { |
153 | | - if (eventName === 'touchcancel') { |
154 | | - callback({ |
155 | | - type: eventName, |
156 | | - touches: [true] |
157 | | - }); |
158 | | - } |
159 | | - }); |
160 | | - }); |
161 | | - it('should be initialized', function() { |
162 | | - expect(element.on).toHaveBeenCalledWith('touchcancel', jasmine.any(Function)); |
163 | | - }); |
164 | | - testEndFunction(); |
| 175 | + testEndFunction('touchcancel'); |
165 | 176 | }); |
166 | 177 | }); |
167 | 178 | afterEach(function() { |
|
0 commit comments