forked from DevExpress/DevExtreme
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrollable.actions.tests.js
More file actions
502 lines (413 loc) · 13.4 KB
/
scrollable.actions.tests.js
File metadata and controls
502 lines (413 loc) · 13.4 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
import $ from 'jquery';
import { noop } from 'core/utils/common';
import { getTranslateValues } from '__internal/ui/scroll_view/utils/get_translate_values';
import animationFrame from '__internal/common/core/animation/frameModule';
import pointerMock from '../../../helpers/pointerMock.js';
import {
SCROLLABLE_CONTAINER_CLASS,
SCROLLABLE_WRAPPER_CLASS,
SCROLLABLE_CONTENT_CLASS,
calculateInertiaDistance,
} from './scrollable.constants.js';
const moduleConfig = {
beforeEach: function() {
const markup = `
<style nonce="qunit-test">
#scrollable {
height: 50px;
width: 50px;
}
#scrollable .content1 {
height: 100px;
width: 100px;
}
</style>
<div id="scrollable">
<div class="content1"></div>
<div class="content2"></div>
</div>
<div id="scrollableNeighbour"></div>`;
$('#qunit-fixture').html(markup);
this.clock = sinon.useFakeTimers();
this.requestAnimationFrameStub = sinon.stub(animationFrame, 'requestAnimationFrame').callsFake((callback) => {
callback();
});
},
afterEach: function() {
this.clock.restore();
this.requestAnimationFrameStub.restore();
}
};
const getScrollOffset = function($scrollable) {
const $content = $scrollable.find(`.${SCROLLABLE_CONTENT_CLASS}`);
const $container = $scrollable.find('.' + SCROLLABLE_CONTAINER_CLASS);
const location = getTranslateValues($content.get(0));
return {
top: location.top - $container.scrollTop(),
left: location.left - $container.scrollLeft()
};
};
QUnit.module('actions', moduleConfig);
QUnit.test('start action not fired after creation', function(assert) {
let started = 0;
$('#scrollable').dxScrollable({
useNative: false,
onStart: function() {
started++;
}
});
assert.equal(started, 0, 'scroll was not started');
});
QUnit.test('start action fired once after several moves', function(assert) {
let started = 0;
const $scrollable = $('#scrollable').dxScrollable({
useNative: false,
onStart: function() {
started++;
}
});
pointerMock($scrollable.find('.' + SCROLLABLE_CONTENT_CLASS))
.start()
.down()
.move(0, 1)
.move(0, 1)
.up();
assert.equal(started, 1, 'scroll started once');
});
QUnit.test('scroll action fired on every move', function(assert) {
let scrolled = 0;
const $scrollable = $('#scrollable').dxScrollable({
useNative: false,
onScroll: function() {
scrolled++;
},
inertiaEnabled: false
});
pointerMock($scrollable.find('.' + SCROLLABLE_CONTENT_CLASS))
.start()
.down()
.move(0, -1)
.move(0, -1)
.move(0, -1)
.up();
assert.equal(scrolled, 3, 'scroll action fired three times');
});
QUnit.test('scroll action fired on every content move during inertia', function(assert) {
let scrolled = 0;
const $scrollable = $('#scrollable').dxScrollable({
useNative: false,
onScroll: function() {
scrolled++;
}
});
pointerMock($scrollable.find('.' + SCROLLABLE_CONTENT_CLASS))
.start()
.down()
.wait(10)
.move(0, -1)
.up();
assert.ok(scrolled > 1, 'scroll action fired during inertia');
});
QUnit.test('scroll action does not fire when location was not changed', function(assert) {
let scrolled = 0;
const $scrollable = $('#scrollable').dxScrollable({
useNative: false,
bounceEnabled: false,
onScroll: function() {
scrolled++;
},
inertiaEnabled: false
});
pointerMock($scrollable.find('.' + SCROLLABLE_CONTENT_CLASS))
.start()
.down()
.move(0, 1)
.move(0, 0)
.up();
assert.equal(scrolled, 0, 'scroll was not fired');
});
QUnit.test('end action fired on scroll end', function(assert) {
let end = 0;
const $scrollable = $('#scrollable').dxScrollable({
useNative: false,
inertiaEnabled: false,
onEnd: function() {
end++;
}
});
pointerMock($scrollable.find('.' + SCROLLABLE_CONTENT_CLASS))
.start()
.down()
.move(0, -1)
.up();
assert.equal(end, 1, 'end action fired once');
});
QUnit.test('end action isn\'t fired without move', function(assert) {
let end = 0;
const $scrollable = $('#scrollable').dxScrollable({
useNative: false,
onEnd: function() {
end++;
}
});
pointerMock($scrollable.find('.' + SCROLLABLE_CONTENT_CLASS))
.start()
.down()
.up();
assert.equal(end, 0, 'end action wasn\'t fired');
});
['vertical', 'horizontal', 'both'].forEach((direction) => {
[{ left: 50 }, { top: 50 }, { top: 50, left: 50 }, 50].forEach((scrollToValue) => {
QUnit.test(`fire onEnd action after scrollTo: ${JSON.stringify(scrollToValue)}, direction: ${direction}`, function(assert) {
const onEndHandler = sinon.spy();
const scrollable = $('#scrollable').dxScrollable({
direction,
useNative: false,
onEnd: onEndHandler
}).dxScrollable('instance');
scrollable.scrollTo(scrollToValue);
assert.strictEqual(onEndHandler.callCount, 1, 'end action fired');
});
});
});
QUnit.test('set actions by option', function(assert) {
let start = 0;
let scroll = 0;
let end = 0;
const $scrollable = $('#scrollable').dxScrollable({
useNative: false,
inertiaEnabled: false
});
const instance = $scrollable.dxScrollable('instance');
instance.option('onStart', function(assert) { start++; });
instance.option('onScroll', function(assert) { scroll++; });
instance.option('onEnd', function(assert) { end++; });
pointerMock($scrollable.find('.' + SCROLLABLE_CONTENT_CLASS))
.start()
.down()
.move(0, -1)
.up();
assert.equal(start, 1, 'start action fired');
assert.equal(scroll, 1, 'scroll action fired');
assert.equal(end, 1, 'end action fired');
});
QUnit.test('start not fired if event outside the scrollable', function(assert) {
let started = 0;
let scrolled = 0;
let ended = 0;
$('#scrollable').dxScrollable({
useNative: false,
onStart: function() {
started++;
},
onScroll: function() {
scrolled++;
},
onEnd: function() {
ended++;
}
});
pointerMock($('#scrollableNeighbour'))
.start()
.down()
.move(0, -1)
.up();
assert.equal(started, 0, 'start action not fired');
assert.equal(scrolled, 0, 'scroll action not fired');
assert.equal(ended, 0, 'end action not fired');
});
QUnit.test('scroll not fired without start', function(assert) {
let scrolled = 0;
const $scrollable = $('#scrollable').dxScrollable({
useNative: false,
onScroll: function() {
scrolled++;
},
});
pointerMock($scrollable.find('.' + SCROLLABLE_CONTENT_CLASS))
.start()
.move(0, -1);
assert.equal(scrolled, 0, 'scroll action not fired');
});
QUnit.test('scroll not fired when start on another element', function(assert) {
let scrolled = 0;
const $scrollable = $('#scrollable').dxScrollable({
useNative: false,
onScroll: function() {
scrolled++;
},
});
pointerMock($('#scrollableNeighbour'))
.start()
.down();
pointerMock($scrollable.find('.' + SCROLLABLE_CONTENT_CLASS))
.start()
.move(0, -1);
assert.equal(scrolled, 0, 'scroll action not fired');
});
QUnit.test('scroll isn\'t fired when moving after finish of previous scrolling', function(assert) {
let scrolled = 0;
const $scrollable = $('#scrollable').dxScrollable({
useNative: false,
inertiaEnabled: false,
onScroll: function() {
scrolled++;
},
});
pointerMock($scrollable.find('.' + SCROLLABLE_CONTENT_CLASS))
.start()
.down()
.move(0, -1)
.up()
.start()
.move(0, -1);
assert.equal(scrolled, 1, 'scroll action not fired');
});
QUnit.test('scroll action fired during native scroll', function(assert) {
let scrolled = 0;
const $scrollable = $('#scrollable').dxScrollable({
inertiaEnabled: false,
useNative: true,
onScroll: function() {
scrolled++;
},
});
$scrollable.find('.' + SCROLLABLE_CONTAINER_CLASS).trigger('scroll');
assert.equal(scrolled, 1, 'scroll action fired');
});
QUnit.test('changing action option does not cause render', function(assert) {
const $scrollable = $('#scrollable').dxScrollable({
useNative: false
});
const $content = $scrollable.find('.' + SCROLLABLE_WRAPPER_CLASS);
const mouse = pointerMock($content).start();
mouse
.down()
.move(0, -10);
const testAction = function(actionName) {
$scrollable.dxScrollable('option', actionName, noop);
const location = getScrollOffset($scrollable);
assert.equal(location.top, -10, actionName + ' case scrollable rerendered');
};
testAction('onStart');
testAction('onEnd');
testAction('onScroll');
testAction('onBounce');
testAction('onUpdated');
});
QUnit.test('update', function(assert) {
this.clock.restore();
const done = assert.async();
const moveDistance = -10;
const moveDuration = 10;
const onUpdatedHandler = sinon.spy();
const inertiaDistance = calculateInertiaDistance(moveDistance, moveDuration);
const distance = moveDistance + inertiaDistance;
const $scrollable = $('#scrollable');
const $scrollableChild = $scrollable.find('div');
$scrollableChild.height(0);
$scrollable.dxScrollable({
useNative: false,
onUpdated: onUpdatedHandler,
onEnd: function() {
const location = getScrollOffset($scrollable);
assert.roughEqual(location.top, distance, 1, 'distance was calculated correctly');
done();
}
});
const mouse = pointerMock($scrollable.find('.' + SCROLLABLE_CONTENT_CLASS)).start();
$scrollableChild.height(-1 * distance + 1);
onUpdatedHandler.resetHistory();
$scrollable.dxScrollable('instance').update();
assert.strictEqual(onUpdatedHandler.callCount, 1, 'onUpdatedHandler.callCount');
mouse
.down()
.wait(moveDuration)
.move(0, moveDistance)
.up();
});
QUnit.module('actions stay functional after callback errors', moduleConfig);
[
{
callbackName: 'onScroll',
message: 'onScroll error does not break subsequent actions',
configure: function(callback) {
const $scrollable = $('#scrollable').dxScrollable({
useNative: false,
inertiaEnabled: false,
bounceEnabled: false,
onScroll: callback,
});
return {
trigger: function() {
pointerMock($scrollable.find('.' + SCROLLABLE_CONTENT_CLASS))
.start()
.down()
.move(0, -10)
.up();
},
};
},
},
{
callbackName: 'onUpdated',
message: 'onUpdated error does not break subsequent actions',
configure: function(callback) {
const scrollable = $('#scrollable').dxScrollable({
useNative: false,
onUpdated: callback,
}).dxScrollable('instance');
return {
trigger: function() {
scrollable.update();
},
};
},
},
{
callbackName: 'onStart',
message: 'onStart error does not break subsequent actions',
configure: function(callback) {
const scrollable = $('#scrollable').dxScrollable({
useNative: false,
onStart: callback,
}).dxScrollable('instance');
return {
trigger: function() {
scrollable.scrollBy({ top: 10 });
},
};
},
},
{
callbackName: 'onEnd',
message: 'onEnd error does not break subsequent actions',
configure: function(callback) {
const scrollable = $('#scrollable').dxScrollable({
useNative: false,
onEnd: callback,
}).dxScrollable('instance');
return {
trigger: function() {
scrollable.scrollBy({ top: 10 });
},
};
},
},
].forEach((testCase) => {
QUnit.test(testCase.message, function(assert) {
let callbackCallCount = 0;
const { trigger } = testCase.configure(function() {
callbackCallCount++;
throw new Error(testCase.callbackName + ' error');
});
callbackCallCount = 0;
trigger();
trigger();
assert.strictEqual(
callbackCallCount,
2,
`${testCase.callbackName}: component remained functional and handled both actions after callback errors`,
);
});
});