@@ -14,6 +14,23 @@ function isShow(elm) {
1414 return getComputedStyle ( elm ) . display !== 'none' ;
1515}
1616
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+
1734describe ( 'vue-infinite-loading' , ( ) => {
1835 let vm ; // save Vue model
1936 const basicConfig = {
@@ -193,7 +210,7 @@ describe('vue-infinite-loading', () => {
193210 vm . $mount ( '#app' ) ;
194211 } ) ;
195212
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 ) => {
197214 let timer ;
198215
199216 vm = new Vue ( Object . assign ( { } , basicConfig , {
@@ -244,7 +261,7 @@ describe('vue-infinite-loading', () => {
244261 vm . $mount ( wrapper ) ;
245262 } ) ;
246263
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 ) => {
248265 let calledTimes = 0 ;
249266 const InfiniteView = Object . assign ( { } , basicConfig , {
250267 data ( ) {
@@ -361,4 +378,31 @@ describe('vue-infinite-loading', () => {
361378
362379 vm . $mount ( '#app' ) ;
363380 } ) ;
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+ } ) ;
364408} ) ;
0 commit comments