This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
/
Copy pathngClickSpec.js
361 lines (248 loc) · 10.6 KB
/
ngClickSpec.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
'use strict';
describe('ngClick (mobile)', function() {
var element, time, orig_now;
// TODO(braden): Once we have other touch-friendly browsers on CI, allow them here.
// Currently Firefox and IE refuse to fire touch events.
var chrome = /chrome/.test(navigator.userAgent.toLowerCase());
if (!chrome) {
return;
}
function mockTime() {
return time;
}
beforeEach(function() {
module('ngMobile');
orig_now = Date.now;
time = 0;
Date.now = mockTime;
});
afterEach(function() {
dealoc(element);
Date.now = orig_now;
});
it('should get called on a tap', inject(function($rootScope, $compile) {
element = $compile('<div ng-click="tapped = true"></div>')($rootScope);
$rootScope.$digest();
expect($rootScope.tapped).toBeUndefined();
browserTrigger(element, 'touchstart');
browserTrigger(element, 'touchend');
expect($rootScope.tapped).toEqual(true);
}));
it('should pass event object', inject(function($rootScope, $compile) {
element = $compile('<div ng-click="event = $event"></div>')($rootScope);
$rootScope.$digest();
browserTrigger(element, 'touchstart');
browserTrigger(element, 'touchend');
expect($rootScope.event).toBeDefined();
}));
it('should not click if the touch is held too long', inject(function($rootScope, $compile, $rootElement) {
element = $compile('<div ng-click="count = count + 1"></div>')($rootScope);
$rootElement.append(element);
$rootScope.count = 0;
$rootScope.$digest();
expect($rootScope.count).toBe(0);
time = 10;
browserTrigger(element, 'touchstart', [], 10, 10);
time = 900;
browserTrigger(element, 'touchend', [], 10, 10);
expect($rootScope.count).toBe(0);
}));
it('should not click if the touchend is too far away', inject(function($rootScope, $compile, $rootElement) {
element = $compile('<div ng-click="tapped = true"></div>')($rootScope);
$rootElement.append(element);
$rootScope.$digest();
expect($rootScope.tapped).toBeUndefined();
browserTrigger(element, 'touchstart', [], 10, 10);
browserTrigger(element, 'touchend', [], 400, 400);
expect($rootScope.tapped).toBeUndefined();
}));
it('should not click if a touchmove comes before touchend', inject(function($rootScope, $compile, $rootElement) {
element = $compile('<div ng-click="tapped = true"></div>')($rootScope);
$rootElement.append(element);
$rootScope.$digest();
expect($rootScope.tapped).toBeUndefined();
browserTrigger(element, 'touchstart', [], 10, 10);
browserTrigger(element, 'touchmove');
browserTrigger(element, 'touchend', [], 400, 400);
expect($rootScope.tapped).toBeUndefined();
}));
it('should add the CSS class while the element is held down, and then remove it', inject(function($rootScope, $compile, $rootElement) {
element = $compile('<div ng-click="tapped = true"></div>')($rootScope);
$rootElement.append(element);
$rootScope.$digest();
expect($rootScope.tapped).toBeUndefined();
var CSS_CLASS = 'ng-click-active';
expect(element.hasClass(CSS_CLASS)).toBe(false);
browserTrigger(element, 'touchstart', 10, 10);
expect(element.hasClass(CSS_CLASS)).toBe(true);
browserTrigger(element, 'touchend', 10, 10);
expect(element.hasClass(CSS_CLASS)).toBe(false);
expect($rootScope.tapped).toBe(true);
}));
describe('the clickbuster', function() {
var element1, element2;
beforeEach(inject(function($rootElement, $document) {
$document.find('body').append($rootElement);
}));
afterEach(inject(function($document) {
$document.find('body').html('');
}));
it('should cancel the following click event', inject(function($rootScope, $compile, $rootElement, $document) {
element = $compile('<div ng-click="count = count + 1"></div>')($rootScope);
$rootElement.append(element);
$rootScope.count = 0;
$rootScope.$digest();
expect($rootScope.count).toBe(0);
// Fire touchstart at 10ms, touchend at 50ms, the click at 300ms.
time = 10;
browserTrigger(element, 'touchstart', [], 10, 10);
time = 50;
browserTrigger(element, 'touchend', [], 10, 10);
expect($rootScope.count).toBe(1);
time = 100;
browserTrigger(element, 'click', [], 10, 10);
expect($rootScope.count).toBe(1);
}));
it('should cancel the following click event even when the element has changed', inject(
function($rootScope, $compile, $rootElement) {
$rootElement.append(
'<div ng-show="!tapped" ng-click="count1 = count1 + 1; tapped = true">x</div>' +
'<div ng-show="tapped" ng-click="count2 = count2 + 1">y</div>'
);
$compile($rootElement)($rootScope);
element1 = $rootElement.find('div').eq(0);
element2 = $rootElement.find('div').eq(1);
$rootScope.count1 = 0;
$rootScope.count2 = 0;
$rootScope.$digest();
expect($rootScope.count1).toBe(0);
expect($rootScope.count2).toBe(0);
time = 10;
browserTrigger(element1, 'touchstart', [], 10, 10);
time = 50;
browserTrigger(element1, 'touchend', [], 10, 10);
expect($rootScope.count1).toBe(1);
time = 100;
browserTrigger(element2, 'click', [], 10, 10);
expect($rootScope.count1).toBe(1);
expect($rootScope.count2).toBe(0);
}));
it('should not cancel clicks on distant elements', inject(function($rootScope, $compile, $rootElement) {
$rootElement.append(
'<div ng-click="count1 = count1 + 1">x</div>' +
'<div ng-click="count2 = count2 + 1">y</div>'
);
$compile($rootElement)($rootScope);
element1 = $rootElement.find('div').eq(0);
element2 = $rootElement.find('div').eq(1);
$rootScope.count1 = 0;
$rootScope.count2 = 0;
$rootScope.$digest();
expect($rootScope.count1).toBe(0);
expect($rootScope.count2).toBe(0);
time = 10;
browserTrigger(element1, 'touchstart', [], 10, 10);
time = 50;
browserTrigger(element1, 'touchend', [], 10, 10);
expect($rootScope.count1).toBe(1);
time = 90;
browserTrigger(element1, 'click', [], 10, 10);
expect($rootScope.count1).toBe(1);
time = 100;
browserTrigger(element1, 'touchstart', [], 10, 10);
time = 130;
browserTrigger(element1, 'touchend', [], 10, 10);
expect($rootScope.count1).toBe(2);
// Click on other element that should go through.
time = 150;
browserTrigger(element2, 'touchstart', [], 100, 120);
browserTrigger(element2, 'touchend', [], 100, 120);
browserTrigger(element2, 'click', [], 100, 120);
expect($rootScope.count2).toBe(1);
// Click event for the element that should be busted.
time = 200;
browserTrigger(element1, 'click', [], 10, 10);
expect($rootScope.count1).toBe(2);
expect($rootScope.count2).toBe(1);
}));
it('should not cancel clicks that come long after', inject(function($rootScope, $compile) {
element1 = $compile('<div ng-click="count = count + 1"></div>')($rootScope);
$rootScope.count = 0;
$rootScope.$digest();
expect($rootScope.count).toBe(0);
time = 10;
browserTrigger(element1, 'touchstart', [], 10, 10);
time = 50;
browserTrigger(element1, 'touchend', [], 10, 10);
expect($rootScope.count).toBe(1);
time = 2700;
browserTrigger(element1, 'click', [], 10, 10);
expect($rootScope.count).toBe(2);
}));
it('should not cancel clicks that come long after', inject(function($rootScope, $compile) {
element1 = $compile('<div ng-click="count = count + 1"></div>')($rootScope);
$rootScope.count = 0;
$rootScope.$digest();
expect($rootScope.count).toBe(0);
time = 10;
browserTrigger(element1, 'touchstart', [], 10, 10);
time = 50;
browserTrigger(element1, 'touchend', [], 10, 10);
expect($rootScope.count).toBe(1);
time = 2700;
browserTrigger(element1, 'click', [], 10, 10);
expect($rootScope.count).toBe(2);
}));
});
describe('click fallback', function() {
it('should treat a click as a tap on desktop', inject(function($rootScope, $compile) {
element = $compile('<div ng-click="tapped = true"></div>')($rootScope);
$rootScope.$digest();
expect($rootScope.tapped).toBeFalsy();
browserTrigger(element, 'click');
expect($rootScope.tapped).toEqual(true);
}));
it('should pass event object', inject(function($rootScope, $compile) {
element = $compile('<div ng-click="event = $event"></div>')($rootScope);
$rootScope.$digest();
browserTrigger(element, 'click');
expect($rootScope.event).toBeDefined();
}));
});
describe('disabled state', function() {
it('should not trigger click if ngDisabled is true', inject(function($rootScope, $compile) {
element = $compile('<div ng-click="event = $event" ng-disabled="disabled"></div>')($rootScope);
$rootScope.disabled = true;
$rootScope.$digest();
browserTrigger(element, 'touchstart', [], 10, 10);
browserTrigger(element, 'touchend', [], 10, 10);
expect($rootScope.event).toBeUndefined();
}));
it('should trigger click if ngDisabled is false', inject(function($rootScope, $compile) {
element = $compile('<div ng-click="event = $event" ng-disabled="disabled"></div>')($rootScope);
$rootScope.disabled = false;
$rootScope.$digest();
browserTrigger(element, 'touchstart', [], 10, 10);
browserTrigger(element, 'touchend', [], 10, 10);
expect($rootScope.event).toBeDefined();
}));
it('should not trigger click if regular disabled is true', inject(function($rootScope, $compile) {
element = $compile('<div ng-click="event = $event" disabled="true"></div>')($rootScope);
browserTrigger(element, 'touchstart', [], 10, 10);
browserTrigger(element, 'touchend', [], 10, 10);
expect($rootScope.event).toBeUndefined();
}));
it('should not trigger click if regular disabled is present', inject(function($rootScope, $compile) {
element = $compile('<div ng-click="event = $event" disabled ></div>')($rootScope);
browserTrigger(element, 'touchstart', [], 10, 10);
browserTrigger(element, 'touchend', [], 10, 10);
expect($rootScope.event).toBeUndefined();
}));
it('should trigger click if regular disabled is not present', inject(function($rootScope, $compile) {
element = $compile('<div ng-click="event = $event" ></div>')($rootScope);
browserTrigger(element, 'touchstart', [], 10, 10);
browserTrigger(element, 'touchend', [], 10, 10);
expect($rootScope.event).toBeDefined();
}));
});
});