Skip to content

Commit ee8a3cb

Browse files
authored
test(scroller): Adding unit tests for the custom scroller factory. (#5861)
1 parent 921d109 commit ee8a3cb

File tree

1 file changed

+213
-0
lines changed

1 file changed

+213
-0
lines changed
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
describe('ui.grid.customScrolling', function() {
2+
describe('uiGridScroller', function() {
3+
var element, scrollHandler, gridUtil, uiGridScroller, uiGridScrollerConstants;
4+
5+
beforeEach(function() {
6+
element = {
7+
0: {
8+
children: {
9+
0: {}
10+
}
11+
},
12+
on: jasmine.createSpy('on'),
13+
off: jasmine.createSpy('off')
14+
};
15+
scrollHandler = jasmine.createSpy('scrollHandler');
16+
gridUtil = jasmine.createSpyObj('gridUtil', ['isTouchEnabled']);
17+
18+
module('ui.grid.customScrolling', function($provide) {
19+
$provide.value('gridUtil', gridUtil);
20+
});
21+
22+
inject(function(_uiGridScroller_, _uiGridScrollerConstants_) {
23+
uiGridScroller = _uiGridScroller_;
24+
uiGridScrollerConstants = _uiGridScrollerConstants_;
25+
});
26+
});
27+
28+
describe('when gridUtils.isTouchEnabled returns true', function() {
29+
beforeEach(function() {
30+
gridUtil.isTouchEnabled.and.returnValue(true);
31+
uiGridScroller(element, scrollHandler);
32+
});
33+
it('should initialize uiGridScroller.initiated to NONE', function() {
34+
expect(uiGridScroller.initiated).toEqual(uiGridScrollerConstants.scrollType.NONE);
35+
});
36+
describe('events', function() {
37+
describe('on touchstart', function() {
38+
beforeEach(function() {
39+
element.on.and.callFake(function(eventName, callback) {
40+
if (eventName === 'touchstart') {
41+
callback({
42+
type: eventName,
43+
touches: [true]
44+
});
45+
}
46+
});
47+
uiGridScroller(element, scrollHandler);
48+
});
49+
it('should be initialized', function() {
50+
expect(element.on).toHaveBeenCalledWith('touchstart', jasmine.any(Function));
51+
});
52+
it('should remove the scroll event from the element', function() {
53+
expect(element.off).toHaveBeenCalledWith('scroll', scrollHandler);
54+
});
55+
it('should update the uiGridScroller.initiated value to TOUCHABLE', function() {
56+
expect(uiGridScroller.initiated).toEqual(uiGridScrollerConstants.scrollType.TOUCHABLE);
57+
});
58+
afterEach(function() {
59+
element.on.and.callFake(angular.noop);
60+
});
61+
});
62+
describe('on touchmove', function() {
63+
var preventDefaultSpy;
64+
65+
beforeEach(function() {
66+
preventDefaultSpy = jasmine.createSpy('preventDefault');
67+
element.on.and.callFake(function(eventName, callback) {
68+
if (eventName === 'touchmove') {
69+
callback({
70+
type: eventName,
71+
touches: [true],
72+
preventDefault: preventDefaultSpy
73+
});
74+
}
75+
});
76+
});
77+
it('should be initialized', function() {
78+
expect(element.on).toHaveBeenCalledWith('touchmove', jasmine.any(Function));
79+
});
80+
describe('when the uiGridScroller has been initiated with a touch event', function() {
81+
beforeEach(function() {
82+
uiGridScroller.initiated = uiGridScrollerConstants.scrollType.TOUCHABLE;
83+
uiGridScroller(element, scrollHandler);
84+
});
85+
it('should prevent the default behavior', function() {
86+
expect(preventDefaultSpy).toHaveBeenCalled();
87+
});
88+
it('should call the scrollHandler', function() {
89+
expect(scrollHandler).toHaveBeenCalled();
90+
});
91+
});
92+
describe('when the uiGridScroller has not been initiated with a touch event', function() {
93+
beforeEach(function() {
94+
uiGridScroller.initiated = uiGridScrollerConstants.scrollType.NONE;
95+
uiGridScroller(element, scrollHandler);
96+
});
97+
it('should prevent the default behavior', function() {
98+
expect(preventDefaultSpy).toHaveBeenCalled();
99+
});
100+
it('should not call the scrollHandler', function() {
101+
expect(scrollHandler).not.toHaveBeenCalled();
102+
});
103+
});
104+
afterEach(function() {
105+
element.on.and.callFake(angular.noop);
106+
});
107+
});
108+
function testEndFunction() {
109+
describe('when the uiGridScroller has been initiated with a touch event', function() {
110+
beforeEach(function() {
111+
uiGridScroller.initiated = uiGridScrollerConstants.scrollType.TOUCHABLE;
112+
uiGridScroller(element, scrollHandler);
113+
});
114+
it('should update the uiGridScroller.initiated value to NONE', function() {
115+
expect(uiGridScroller.initiated).toEqual(uiGridScrollerConstants.scrollType.NONE);
116+
});
117+
});
118+
describe('when the uiGridScroller has not been initiated with a touch event', function() {
119+
beforeEach(function() {
120+
uiGridScroller.initiated = uiGridScrollerConstants.scrollType.MOUSE;
121+
uiGridScroller(element, scrollHandler);
122+
});
123+
it('should not update the uiGridScroller.initiated value', function() {
124+
expect(uiGridScroller.initiated).toEqual(uiGridScrollerConstants.scrollType.MOUSE);
125+
});
126+
});
127+
afterEach(function() {
128+
element.on.and.callFake(angular.noop);
129+
});
130+
}
131+
describe('on touchend', function() {
132+
beforeEach(function() {
133+
element.on.and.callFake(function(eventName, callback) {
134+
if (eventName === 'touchend') {
135+
callback({
136+
type: eventName,
137+
touches: [true]
138+
});
139+
}
140+
});
141+
});
142+
it('should be initialized', function() {
143+
expect(element.on).toHaveBeenCalledWith('touchend', jasmine.any(Function));
144+
});
145+
testEndFunction();
146+
});
147+
describe('on touchcancel', function() {
148+
beforeEach(function() {
149+
element.on.and.callFake(function(eventName, callback) {
150+
if (eventName === 'touchcancel') {
151+
callback({
152+
type: eventName,
153+
touches: [true]
154+
});
155+
}
156+
});
157+
});
158+
it('should be initialized', function() {
159+
expect(element.on).toHaveBeenCalledWith('touchcancel', jasmine.any(Function));
160+
});
161+
testEndFunction();
162+
});
163+
});
164+
afterEach(function() {
165+
element.on.calls.reset();
166+
element.off.calls.reset();
167+
gridUtil.isTouchEnabled.calls.reset();
168+
});
169+
});
170+
171+
describe('when gridUtils.isTouchEnabled returns false', function() {
172+
beforeEach(function() {
173+
gridUtil.isTouchEnabled.and.returnValue(false);
174+
uiGridScroller(element, scrollHandler);
175+
});
176+
it('should initialize uiGridScroller.initiated to NONE', function() {
177+
expect(uiGridScroller.initiated).toEqual(uiGridScrollerConstants.scrollType.NONE);
178+
});
179+
describe('events', function() {
180+
describe('on scroll', function() {
181+
it('should be initialized', function() {
182+
expect(element.on).toHaveBeenCalledWith('scroll', scrollHandler);
183+
});
184+
});
185+
describe('on touchstart', function() {
186+
it('should not be initialized', function() {
187+
expect(element.on).not.toHaveBeenCalledWith('touchstart', jasmine.any(Function));
188+
});
189+
});
190+
describe('on touchmove', function() {
191+
it('should not be initialized', function() {
192+
expect(element.on).not.toHaveBeenCalledWith('touchmove', jasmine.any(Function));
193+
});
194+
});
195+
describe('on touchend', function() {
196+
it('should not be initialized', function() {
197+
expect(element.on).not.toHaveBeenCalledWith('touchend', jasmine.any(Function));
198+
});
199+
});
200+
describe('on touchcancel', function() {
201+
it('should not be initialized', function() {
202+
expect(element.on).not.toHaveBeenCalledWith('touchcancel', jasmine.any(Function));
203+
});
204+
});
205+
});
206+
afterEach(function() {
207+
element.on.calls.reset();
208+
element.off.calls.reset();
209+
gridUtil.isTouchEnabled.calls.reset();
210+
});
211+
});
212+
});
213+
});

0 commit comments

Comments
 (0)