@@ -14,6 +14,23 @@ function isShow(elm) {
14
14
return getComputedStyle ( elm ) . display !== 'none' ;
15
15
}
16
16
17
+ /**
18
+ * continues call the specified number of times for a function
19
+ * @param {Function } fn target function
20
+ * @param {Number } times calls
21
+ * @param {Function } cb [description]
22
+ */
23
+ function continuesCall ( fn , times , cb ) {
24
+ if ( times ) {
25
+ fn ( ) ;
26
+ setTimeout ( ( ) => {
27
+ continuesCall ( fn , times - 1 , cb ) ;
28
+ } , 1 ) ;
29
+ } else {
30
+ cb ( ) ;
31
+ }
32
+ }
33
+
17
34
describe ( 'vue-infinite-loading' , ( ) => {
18
35
let vm ; // save Vue model
19
36
const basicConfig = {
@@ -193,7 +210,7 @@ describe('vue-infinite-loading', () => {
193
210
vm . $mount ( '#app' ) ;
194
211
} ) ;
195
212
196
- it ( 'should always load data until fill up the contianer \n (use div as the container)' , ( done ) => {
213
+ it ( 'should always load data until fill up the container \n (use div as the container)' , ( done ) => {
197
214
let timer ;
198
215
199
216
vm = new Vue ( Object . assign ( { } , basicConfig , {
@@ -244,7 +261,7 @@ describe('vue-infinite-loading', () => {
244
261
vm . $mount ( wrapper ) ;
245
262
} ) ;
246
263
247
- it ( 'should not works when deactivated by the `keep-alive` feature\n (use top direction)' , ( done ) => {
264
+ it ( 'should not works when deactivated by the `keep-alive` feature\n (use top direction and use div as the container )' , ( done ) => {
248
265
let calledTimes = 0 ;
249
266
const InfiniteView = Object . assign ( { } , basicConfig , {
250
267
data ( ) {
@@ -361,4 +378,31 @@ describe('vue-infinite-loading', () => {
361
378
362
379
vm . $mount ( '#app' ) ;
363
380
} ) ;
381
+
382
+ it ( 'should debounce properly for the scroll event handler\n (use div as the container)' , ( done ) => {
383
+ vm = new Vue ( Object . assign ( { } , basicConfig , {
384
+ data : {
385
+ list : [ ...new Array ( 20 ) . join ( '1' ) . split ( '' ) ] ,
386
+ isDivScroll : true ,
387
+ direction : 'bottom' ,
388
+ } ,
389
+ mounted : function mounted ( ) {
390
+ const scrollParent = this . $refs . infiniteLoading . scrollParent ;
391
+ const spyFn = sinon . spy ( this . $refs . infiniteLoading , 'attemptLoad' ) ;
392
+ const alreadyCalledTimes = 1 ; // it will be called immediately after mount
393
+
394
+ continuesCall ( ( ) => {
395
+ scrollParent . scrollTop += 10 ;
396
+ } , 10 , ( ) => {
397
+ expect ( spyFn ) . to . have . been . callCount ( 0 + alreadyCalledTimes ) ;
398
+ setTimeout ( ( ) => {
399
+ expect ( spyFn ) . to . have . been . callCount ( 1 + alreadyCalledTimes ) ;
400
+ done ( ) ;
401
+ } , this . $refs . infiniteLoading . debounceDuration + 10 ) ;
402
+ } ) ;
403
+ } ,
404
+ } ) ) ;
405
+
406
+ vm . $mount ( '#app' ) ;
407
+ } ) ;
364
408
} ) ;
0 commit comments