/
ionicScrollDelegate.unit.js
217 lines (179 loc) · 6.73 KB
/
ionicScrollDelegate.unit.js
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
describe('Ionic ScrollDelegate Service', function() {
var del, rootScope, compile, timeout, document;
beforeEach(module('ionic'));
beforeEach(inject(function($ionicScrollDelegate, $rootScope, $timeout, $compile, $document) {
del = $ionicScrollDelegate;
document = $document;
rootScope = $rootScope;
timeout = $timeout;
compile = $compile;
}));
it('Should get scroll view', function() {
var scope = rootScope.$new();
var el = compile('<ion-content></ion-content>')(scope);
var sv = del.getScrollView(scope);
expect(sv).not.toBe(undefined);
});
it('should resize', function() {
var scope = rootScope.$new();
var el = compile('<ion-content></ion-content>')(scope);
var sv = del.getScrollView(scope);
spyOn(sv, 'resize');
spyOn(sv, 'scrollTo');
del.resize();
timeout.flush();
expect(sv.resize).toHaveBeenCalled();
});
it('scroll event', function() {
var scope = rootScope.$new();
var el = compile('<ion-content></ion-content>')(scope);
scope = el.scope();
scope.$apply();
var top, left;
scope.onScroll = jasmine.createSpy('scroll').andCallFake(function(data) {
top = data.scrollTop;
left = data.scrollLeft;
});
ionic.trigger('scroll', {target: el[0]});
expect(scope.onScroll).toHaveBeenCalled();
expect(top).toBe(0);
expect(left).toBe(0);
ionic.trigger('scroll', {target: el[0], scrollTop: 3, scrollLeft: 4});
expect(top).toBe(3);
expect(left).toBe(4);
});
testWithAnimate(true);
testWithAnimate(false);
function testWithAnimate(animate) {
describe('with animate='+animate, function() {
it('should tapScrollToTop', function() {
var scope = rootScope.$new();
var el = angular.element('<div>' +
'<div class="not-button"></div>' +
'<div class="button"></div>' +
'</div>');
//ionic.trigger() REALLY doesnt want to work with tap,
//so we just mock on to catch the callback and use that...
var callback;
spyOn(ionic, 'on').andCallFake(function(eventName, cb) {
callback = cb;
});
del.tapScrollToTop(el, animate);
spyOn(del, 'scrollTop');
//Don't test the rectContains part, too much to mock
spyOn(ionic.DomUtil, 'rectContains').andCallFake(function() {
return true;
});
callback({
target: el[0].querySelector('.not-button'),
gesture:{ touches:[{}] }
});
expect(del.scrollTop).toHaveBeenCalledWith(animate);
del.scrollTop.reset();
callback({
target: el[0].querySelector('.button')
});
expect(del.scrollTop).not.toHaveBeenCalled();
});
it('should resize & scroll top', function() {
var scope = rootScope.$new();
var el = compile('<ion-content start-y="100"></ion-content>')(scope);
var sv = del.getScrollView(scope);
spyOn(sv, 'resize');
spyOn(sv, 'scrollTo');
del.scrollTop(animate);
timeout.flush();
expect(sv.resize).toHaveBeenCalled();
expect(sv.scrollTo.mostRecentCall.args).toEqual([0, 0, animate]);
});
it('should resize & scroll bottom', function() {
var scope = rootScope.$new();
var el = compile('<ion-content start-y="100"><br/><br/></ion-content>')(scope);
var sv = del.getScrollView(scope);
spyOn(sv, 'getScrollMax').andCallFake(function() {
return { left: 10, top: 11 };
});
spyOn(sv, 'resize');
spyOn(sv, 'scrollTo');
var max = sv.getScrollMax();
del.scrollBottom(animate);
timeout.flush();
expect(sv.resize).toHaveBeenCalled();
expect(sv.scrollTo.mostRecentCall.args).toEqual([max.left, max.top, animate]);
});
it('should resize & scrollTo', function() {
var scope = rootScope.$new();
var el = compile('<ion-content start-y="100"><br/><br/></ion-content>')(scope);
var sv = del.getScrollView(scope);
spyOn(sv, 'scrollTo');
spyOn(sv, 'resize');
del.scrollTo(2, 3, animate);
timeout.flush();
expect(sv.resize).toHaveBeenCalled();
expect(sv.scrollTo.mostRecentCall.args).toEqual([2, 3, animate]);
});
});
}
it('should finish refreshing', function() {
var scope = rootScope.$new();
var el = compile('<ion-content start-y="100"></ion-content>')(scope);
var sv = del.getScrollView(scope);
spyOn(sv, 'finishPullToRefresh');
del.finishRefreshing(scope);
expect(sv.finishPullToRefresh).toHaveBeenCalled();
});
});
describe('anchorScroll', function() {
function setLocationHash(hash) {
inject(function($location) {
$location.hash = function() { return hash; };
});
}
beforeEach(module('ionic'));
testWithAnimate(true);
testWithAnimate(false);
function testWithAnimate(animate) {
describe('with animate=' + animate, function() {
var contentEl, scope, del, timeout;
beforeEach(inject(function($rootScope, $compile, $timeout, $document, $ionicScrollDelegate) {
scope = $rootScope.$new();
contentEl = $compile('<ion-content></ion-content>')(scope);
document.body.appendChild(contentEl[0]);
del = $ionicScrollDelegate;
timeout = $timeout;
}));
it('should anchorScroll to an element with id', function() {
var anchorMe = angular.element('<div id="anchorMe">');
var sv = del.getScrollView(scope);
spyOn(sv, 'scrollTo');
spyOn(ionic.DomUtil, 'getPositionInParent').andCallFake(function() {
return { left: 2, top: 1 };
});
setLocationHash('anchorMe');
contentEl.append(anchorMe);
del.anchorScroll(animate);
timeout.flush();
expect(sv.scrollTo).toHaveBeenCalledWith(2, 1, animate);
});
it('should anchorScroll to top if !$location.hash()', function() {
var sv = del.getScrollView(scope);
spyOn(sv, 'scrollTo');
spyOn(ionic.DomUtil, 'getPositionInParent');
del.anchorScroll(animate);
timeout.flush();
expect(sv.scrollTo).toHaveBeenCalledWith(0, 0, animate);
expect(ionic.DomUtil.getPositionInParent).not.toHaveBeenCalled();
});
it('should anchorScroll to top if element with hash id doesnt exist', function() {
var sv = del.getScrollView(scope);
spyOn(sv, 'scrollTo');
spyOn(ionic.DomUtil, 'getPositionInParent');
setLocationHash('doesnotexist');
del.anchorScroll(animate);
timeout.flush();
expect(sv.scrollTo).toHaveBeenCalledWith(0, 0, animate);
expect(ionic.DomUtil.getPositionInParent).not.toHaveBeenCalled();
});
});
}
});