@@ -272,7 +272,7 @@ describe('Function.bind', function(){
272
272
} ) ;
273
273
274
274
dit ( 'should still be possible to use it as constructor' , function ( ) {
275
- function Alien ( type ) {
275
+ function Alien ( type ) {
276
276
this . type = type ;
277
277
}
278
278
@@ -459,3 +459,232 @@ describe('Function.periodical', function(){
459
459
} ) ;
460
460
461
461
} ) ;
462
+
463
+ describe ( 'Debounce' , function ( ) {
464
+ var fn , debounceFn , caller , counter = 0 ,
465
+ debounceCalls = 0 ;
466
+ var sum = 0 ;
467
+
468
+ function startCaller ( ) {
469
+ caller = setInterval ( function ( ) {
470
+ if ( debounceFn ) {
471
+ counter ++ ;
472
+ debounceFn ( ) ;
473
+ }
474
+ } , 10 ) ;
475
+ }
476
+ beforeEach ( function ( ) {
477
+ fn = function ( ) {
478
+ debounceCalls ++ ;
479
+ } ;
480
+ startCaller ( ) ;
481
+ } ) ;
482
+
483
+ afterEach ( function ( ) {
484
+ expect ( counter > 40 ) . toBeTruthy ( ) ; // control spec
485
+ fn = debounceFn = caller = null ;
486
+ counter = debounceCalls = 0 ;
487
+ clearInterval ( caller ) ;
488
+ } ) ;
489
+
490
+ it ( 'should debounce as default' , function ( ) {
491
+ expect ( counter == debounceCalls && debounceCalls == 0 ) . toBeTruthy ( ) ; // control spec
492
+ debounceFn = fn . debounce ( ) ;
493
+ waitsFor ( function ( ) {
494
+ if ( counter > 40 ) {
495
+ clearInterval ( caller ) ;
496
+ expect ( debounceCalls == 0 ) . toBeTruthy ( ) ;
497
+ return true ;
498
+ }
499
+ } , 'enough calls to happen' , 1000 ) ;
500
+ waits ( 300 ) ;
501
+ runs ( function ( ) {
502
+ expect ( debounceCalls == 1 ) . toBeTruthy ( ) ;
503
+ } ) ;
504
+ } ) ;
505
+
506
+ it ( 'should debounce early' , function ( ) {
507
+ expect ( counter == debounceCalls && debounceCalls == 0 ) . toBeTruthy ( ) ; // control spec
508
+ debounceFn = fn . debounce ( {
509
+ when : 'early' ,
510
+ delay : 150
511
+ } ) ;
512
+ waitsFor ( function ( ) {
513
+ if ( counter > 19 ) {
514
+ clearInterval ( caller ) ;
515
+ caller = null ;
516
+ expect ( debounceCalls == 1 ) . toBeTruthy ( ) ;
517
+ return true ;
518
+ }
519
+ } , 'some calls to happen' , 1000 ) ;
520
+
521
+ waits ( 200 ) ;
522
+ waitsFor ( function ( ) {
523
+ if ( ! caller ) startCaller ( ) ;
524
+ if ( counter > 40 ) {
525
+ clearInterval ( caller ) ;
526
+ return true ;
527
+ }
528
+ } , 'even more calls to happen' , 1000 ) ;
529
+ waits ( 200 ) ;
530
+ runs ( function ( ) {
531
+ expect ( debounceCalls == 2 ) . toBeTruthy ( ) ;
532
+ } ) ;
533
+ } ) ;
534
+
535
+ it ( 'should debounce once early' , function ( ) {
536
+ expect ( counter == debounceCalls && debounceCalls == 0 ) . toBeTruthy ( ) ; // control spec
537
+ debounceFn = fn . debounce ( {
538
+ when : 'early' ,
539
+ once : true ,
540
+ delay : 150
541
+ } ) ;
542
+ waitsFor ( function ( ) {
543
+ if ( counter > 19 ) {
544
+ clearInterval ( caller ) ;
545
+ caller = null ;
546
+ expect ( debounceCalls == 1 ) . toBeTruthy ( ) ;
547
+ return true ;
548
+ }
549
+ } , 'some calls to happen' , 1000 ) ;
550
+
551
+ waits ( 200 ) ;
552
+ waitsFor ( function ( ) {
553
+ if ( ! caller ) startCaller ( ) ;
554
+ if ( counter > 40 ) {
555
+ clearInterval ( caller ) ;
556
+ return true ;
557
+ }
558
+ } , 'even more calls to happen' , 1000 ) ;
559
+ waits ( 200 ) ;
560
+ runs ( function ( ) {
561
+ expect ( debounceCalls == 1 ) . toBeTruthy ( ) ;
562
+ } ) ;
563
+ } ) ;
564
+
565
+ it ( 'should debounce late' , function ( ) {
566
+ expect ( counter == debounceCalls && debounceCalls == 0 ) . toBeTruthy ( ) ; // control spec
567
+ debounceFn = fn . debounce ( 150 ) ;
568
+ waitsFor ( function ( ) {
569
+ if ( counter > 19 ) {
570
+ clearInterval ( caller ) ;
571
+ caller = null ;
572
+ expect ( debounceCalls == 0 ) . toBeTruthy ( ) ;
573
+ return true ;
574
+ }
575
+ } , 'some calls to happen' , 1000 ) ;
576
+
577
+ waits ( 200 ) ;
578
+ waitsFor ( function ( ) {
579
+ if ( ! caller ) {
580
+ expect ( debounceCalls == 1 ) . toBeTruthy ( ) ;
581
+ startCaller ( ) ;
582
+ }
583
+ if ( counter > 40 ) {
584
+ clearInterval ( caller ) ;
585
+ return true ;
586
+ }
587
+ } , 'even more calls to happen' , 1000 ) ;
588
+ waits ( 200 ) ;
589
+ runs ( function ( ) {
590
+ expect ( debounceCalls == 2 ) . toBeTruthy ( ) ;
591
+ } ) ;
592
+ } ) ;
593
+
594
+ it ( 'should debounce once late' , function ( ) {
595
+ expect ( counter == debounceCalls && debounceCalls == 0 ) . toBeTruthy ( ) ; // control spec
596
+ debounceFn = fn . debounce ( {
597
+ when : 'late' ,
598
+ once : true
599
+ } ) ;
600
+ waitsFor ( function ( ) {
601
+ if ( counter > 19 ) {
602
+ clearInterval ( caller ) ;
603
+ caller = null ;
604
+ expect ( debounceCalls == 0 ) . toBeTruthy ( ) ;
605
+ return true ;
606
+ }
607
+ } , 'some calls to happen' , 1000 ) ;
608
+
609
+ waits ( 300 ) ;
610
+ waitsFor ( function ( ) {
611
+ if ( ! caller ) {
612
+ expect ( debounceCalls == 1 ) . toBeTruthy ( ) ;
613
+ startCaller ( ) ;
614
+ }
615
+ if ( counter > 40 ) {
616
+ clearInterval ( caller ) ;
617
+ return true ;
618
+ }
619
+ } , 'even more calls to happen' , 1000 ) ;
620
+ waits ( 300 ) ;
621
+ runs ( function ( ) {
622
+ expect ( debounceCalls == 1 ) . toBeTruthy ( ) ;
623
+ } ) ;
624
+ } ) ;
625
+
626
+ it ( 'should debounce both early and late' , function ( ) {
627
+ expect ( counter == debounceCalls && debounceCalls == 0 ) . toBeTruthy ( ) ; // control spec
628
+ debounceFn = fn . debounce ( {
629
+ when : 'both' ,
630
+ delay : 150
631
+ } ) ;
632
+ waitsFor ( function ( ) {
633
+ if ( counter > 19 ) {
634
+ clearInterval ( caller ) ;
635
+ caller = null ;
636
+ expect ( debounceCalls == 1 ) . toBeTruthy ( ) ;
637
+ return true ;
638
+ }
639
+ } , 'some calls to happen' , 1000 ) ;
640
+
641
+ waits ( 200 ) ;
642
+ waitsFor ( function ( ) {
643
+ if ( ! caller ) {
644
+ expect ( debounceCalls == 2 ) . toBeTruthy ( ) ;
645
+ startCaller ( ) ;
646
+ }
647
+ if ( counter > 40 ) {
648
+ clearInterval ( caller ) ;
649
+ return true ;
650
+ }
651
+ } , 'even more calls to happen' , 1000 ) ;
652
+ waits ( 200 ) ;
653
+ runs ( function ( ) {
654
+ expect ( debounceCalls == 4 ) . toBeTruthy ( ) ;
655
+ } ) ;
656
+ } ) ;
657
+
658
+ it ( 'should debounce both early and late, once' , function ( ) {
659
+ expect ( counter == debounceCalls && debounceCalls == 0 ) . toBeTruthy ( ) ; // control spec
660
+ debounceFn = fn . debounce ( {
661
+ when : 'both' ,
662
+ once : true
663
+ } ) ;
664
+ waitsFor ( function ( ) {
665
+ if ( counter > 19 ) {
666
+ clearInterval ( caller ) ;
667
+ caller = null ;
668
+ expect ( debounceCalls == 1 ) . toBeTruthy ( ) ;
669
+ return true ;
670
+ }
671
+ } , 'some calls to happen' , 1000 ) ;
672
+
673
+ waits ( 300 ) ;
674
+ waitsFor ( function ( ) {
675
+ if ( ! caller ) {
676
+ expect ( debounceCalls == 2 ) . toBeTruthy ( ) ;
677
+ startCaller ( ) ;
678
+ }
679
+ if ( counter > 40 ) {
680
+ clearInterval ( caller ) ;
681
+ return true ;
682
+ }
683
+ } , 'even more calls to happen' , 1000 ) ;
684
+ waits ( 300 ) ;
685
+ runs ( function ( ) {
686
+ expect ( debounceCalls == 2 ) . toBeTruthy ( ) ;
687
+ } ) ;
688
+ } ) ;
689
+
690
+ } ) ;
0 commit comments