@@ -601,7 +601,7 @@ describe('ngMock', function() {
601
601
} ) ;
602
602
603
603
604
- describe ( 'defer ' , function ( ) {
604
+ describe ( '$browser ' , function ( ) {
605
605
var browser , log ;
606
606
beforeEach ( inject ( function ( $browser ) {
607
607
browser = $browser ;
@@ -614,47 +614,199 @@ describe('ngMock', function() {
614
614
} ;
615
615
}
616
616
617
- it ( 'should flush' , function ( ) {
618
- browser . defer ( logFn ( 'A' ) ) ;
619
- expect ( log ) . toEqual ( '' ) ;
620
- browser . defer . flush ( ) ;
621
- expect ( log ) . toEqual ( 'A;' ) ;
617
+ describe ( 'defer.flush' , function ( ) {
618
+ it ( 'should flush' , function ( ) {
619
+ browser . defer ( logFn ( 'A' ) ) ;
620
+ browser . defer ( logFn ( 'B' ) , null , 'taskType' ) ;
621
+ expect ( log ) . toEqual ( '' ) ;
622
+
623
+ browser . defer . flush ( ) ;
624
+ expect ( log ) . toEqual ( 'A;B;' ) ;
625
+ } ) ;
626
+
627
+ it ( 'should flush delayed' , function ( ) {
628
+ browser . defer ( logFn ( 'A' ) ) ;
629
+ browser . defer ( logFn ( 'B' ) , 10 , 'taskType' ) ;
630
+ browser . defer ( logFn ( 'C' ) , 20 ) ;
631
+ expect ( log ) . toEqual ( '' ) ;
632
+ expect ( browser . defer . now ) . toEqual ( 0 ) ;
633
+
634
+ browser . defer . flush ( 0 ) ;
635
+ expect ( log ) . toEqual ( 'A;' ) ;
636
+
637
+ browser . defer . flush ( ) ;
638
+ expect ( log ) . toEqual ( 'A;B;C;' ) ;
639
+ } ) ;
640
+
641
+ it ( 'should defer and flush over time' , function ( ) {
642
+ browser . defer ( logFn ( 'A' ) , 1 ) ;
643
+ browser . defer ( logFn ( 'B' ) , 2 , 'taskType' ) ;
644
+ browser . defer ( logFn ( 'C' ) , 3 ) ;
645
+
646
+ browser . defer . flush ( 0 ) ;
647
+ expect ( browser . defer . now ) . toEqual ( 0 ) ;
648
+ expect ( log ) . toEqual ( '' ) ;
649
+
650
+ browser . defer . flush ( 1 ) ;
651
+ expect ( browser . defer . now ) . toEqual ( 1 ) ;
652
+ expect ( log ) . toEqual ( 'A;' ) ;
653
+
654
+ browser . defer . flush ( 2 ) ;
655
+ expect ( browser . defer . now ) . toEqual ( 3 ) ;
656
+ expect ( log ) . toEqual ( 'A;B;C;' ) ;
657
+ } ) ;
658
+
659
+ it ( 'should throw an exception if there is nothing to be flushed' , function ( ) {
660
+ expect ( function ( ) { browser . defer . flush ( ) ; } ) . toThrowError ( 'No deferred tasks to be flushed' ) ;
661
+ } ) ;
662
+
663
+ it ( 'should not throw an exception when passing a specific delay' , function ( ) {
664
+ expect ( function ( ) { browser . defer . flush ( 100 ) ; } ) . not . toThrow ( ) ;
665
+ } ) ;
622
666
} ) ;
623
667
624
- it ( 'should flush delayed' , function ( ) {
625
- browser . defer ( logFn ( 'A' ) ) ;
626
- browser . defer ( logFn ( 'B' ) , 10 ) ;
627
- browser . defer ( logFn ( 'C' ) , 20 ) ;
628
- expect ( log ) . toEqual ( '' ) ;
668
+ describe ( 'defer.cancel' , function ( ) {
669
+ it ( 'should cancel a pending task' , function ( ) {
670
+ var taskId1 = browser . defer ( logFn ( 'A' ) , 100 , 'fooType' ) ;
671
+ var taskId2 = browser . defer ( logFn ( 'B' ) , 200 ) ;
672
+
673
+ expect ( log ) . toBe ( '' ) ;
674
+ expect ( function ( ) { browser . defer . verifyNoPendingTasks ( 'fooType' ) ; } ) . toThrow ( ) ;
675
+ expect ( function ( ) { browser . defer . verifyNoPendingTasks ( ) ; } ) . toThrow ( ) ;
676
+
677
+ browser . defer . cancel ( taskId1 ) ;
678
+ expect ( function ( ) { browser . defer . verifyNoPendingTasks ( 'fooType' ) ; } ) . not . toThrow ( ) ;
679
+ expect ( function ( ) { browser . defer . verifyNoPendingTasks ( ) ; } ) . toThrow ( ) ;
629
680
630
- expect ( browser . defer . now ) . toEqual ( 0 ) ;
631
- browser . defer . flush ( 0 ) ;
632
- expect ( log ) . toEqual ( 'A;' ) ;
681
+ browser . defer . cancel ( taskId2 ) ;
682
+ expect ( function ( ) { browser . defer . verifyNoPendingTasks ( 'fooType' ) ; } ) . not . toThrow ( ) ;
683
+ expect ( function ( ) { browser . defer . verifyNoPendingTasks ( ) ; } ) . not . toThrow ( ) ;
633
684
634
- browser . defer . flush ( ) ;
635
- expect ( log ) . toEqual ( 'A;B;C;' ) ;
685
+ browser . defer . flush ( 1000 ) ;
686
+ expect ( log ) . toBe ( '' ) ;
687
+ } ) ;
636
688
} ) ;
637
689
638
- it ( 'should defer and flush over time' , function ( ) {
639
- browser . defer ( logFn ( 'A' ) , 1 ) ;
640
- browser . defer ( logFn ( 'B' ) , 2 ) ;
641
- browser . defer ( logFn ( 'C' ) , 3 ) ;
690
+ describe ( 'defer.verifyNoPendingTasks' , function ( ) {
691
+ it ( 'should throw if there are pending tasks' , function ( ) {
692
+ expect ( browser . defer . verifyNoPendingTasks ) . not . toThrow ( ) ;
693
+
694
+ browser . defer ( noop ) ;
695
+ expect ( browser . defer . verifyNoPendingTasks ) . toThrow ( ) ;
696
+ } ) ;
697
+
698
+ it ( 'should list the pending tasks (in order) in the error message' , function ( ) {
699
+ browser . defer ( noop , 100 ) ;
700
+ browser . defer ( noop , 300 , 'fooType' ) ;
701
+ browser . defer ( noop , 200 , 'barType' ) ;
642
702
643
- browser . defer . flush ( 0 ) ;
644
- expect ( browser . defer . now ) . toEqual ( 0 ) ;
645
- expect ( log ) . toEqual ( '' ) ;
703
+ var expectedError =
704
+ 'Deferred tasks to flush (3):\n' +
705
+ ' {id: 0, type: $$default$$, time: 100}\n' +
706
+ ' {id: 2, type: barType, time: 200}\n' +
707
+ ' {id: 1, type: fooType, time: 300}' ;
708
+ expect ( browser . defer . verifyNoPendingTasks ) . toThrowError ( expectedError ) ;
709
+ } ) ;
710
+
711
+ describe ( 'with specific task type' , function ( ) {
712
+ it ( 'should throw if there are pending tasks' , function ( ) {
713
+ browser . defer ( noop , 0 , 'fooType' ) ;
646
714
647
- browser . defer . flush ( 1 ) ;
648
- expect ( browser . defer . now ) . toEqual ( 1 ) ;
649
- expect ( log ) . toEqual ( 'A;' ) ;
715
+ expect ( function ( ) { browser . defer . verifyNoPendingTasks ( 'barType' ) ; } ) . not . toThrow ( ) ;
716
+ expect ( function ( ) { browser . defer . verifyNoPendingTasks ( 'fooType' ) ; } ) . toThrow ( ) ;
717
+ expect ( function ( ) { browser . defer . verifyNoPendingTasks ( ) ; } ) . toThrow ( ) ;
718
+ } ) ;
650
719
651
- browser . defer . flush ( 2 ) ;
652
- expect ( browser . defer . now ) . toEqual ( 3 ) ;
653
- expect ( log ) . toEqual ( 'A;B;C;' ) ;
720
+ it ( 'should list the pending tasks (in order) in the error message' , function ( ) {
721
+ browser . defer ( noop , 100 ) ;
722
+ browser . defer ( noop , 300 , 'fooType' ) ;
723
+ browser . defer ( noop , 200 , 'barType' ) ;
724
+ browser . defer ( noop , 400 , 'fooType' ) ;
725
+
726
+ var expectedError =
727
+ 'Deferred tasks to flush (2):\n' +
728
+ ' {id: 1, type: fooType, time: 300}\n' +
729
+ ' {id: 3, type: fooType, time: 400}' ;
730
+ expect ( function ( ) { browser . defer . verifyNoPendingTasks ( 'fooType' ) ; } ) .
731
+ toThrowError ( expectedError ) ;
732
+ } ) ;
733
+ } ) ;
654
734
} ) ;
655
735
656
- it ( 'should throw an exception if there is nothing to be flushed' , function ( ) {
657
- expect ( function ( ) { browser . defer . flush ( ) ; } ) . toThrowError ( 'No deferred tasks to be flushed' ) ;
736
+ describe ( 'notifyWhenNoOutstandingRequests' , function ( ) {
737
+ var callback ;
738
+ beforeEach ( function ( ) {
739
+ callback = jasmine . createSpy ( 'callback' ) ;
740
+ } ) ;
741
+
742
+ it ( 'should immediately run the callback if no pending tasks' , function ( ) {
743
+ browser . notifyWhenNoOutstandingRequests ( callback ) ;
744
+ expect ( callback ) . toHaveBeenCalled ( ) ;
745
+ } ) ;
746
+
747
+ it ( 'should run the callback as soon as there are no pending tasks' , function ( ) {
748
+ browser . defer ( noop , 100 ) ;
749
+ browser . defer ( noop , 200 ) ;
750
+
751
+ browser . notifyWhenNoOutstandingRequests ( callback ) ;
752
+ expect ( callback ) . not . toHaveBeenCalled ( ) ;
753
+
754
+ browser . defer . flush ( 100 ) ;
755
+ expect ( callback ) . not . toHaveBeenCalled ( ) ;
756
+
757
+ browser . defer . flush ( 100 ) ;
758
+ expect ( callback ) . toHaveBeenCalled ( ) ;
759
+ } ) ;
760
+
761
+ it ( 'should not run the callback more than once' , function ( ) {
762
+ browser . defer ( noop , 100 ) ;
763
+ browser . notifyWhenNoOutstandingRequests ( callback ) ;
764
+ expect ( callback ) . not . toHaveBeenCalled ( ) ;
765
+
766
+ browser . defer . flush ( 100 ) ;
767
+ expect ( callback ) . toHaveBeenCalledOnce ( ) ;
768
+
769
+ browser . defer ( noop , 200 ) ;
770
+ browser . defer . flush ( 100 ) ;
771
+ expect ( callback ) . toHaveBeenCalledOnce ( ) ;
772
+ } ) ;
773
+
774
+ describe ( 'with specific task type' , function ( ) {
775
+ it ( 'should immediately run the callback if no pending tasks' , function ( ) {
776
+ browser . notifyWhenNoOutstandingRequests ( callback , 'fooType' ) ;
777
+ expect ( callback ) . toHaveBeenCalled ( ) ;
778
+ } ) ;
779
+
780
+ it ( 'should run the callback as soon as there are no pending tasks' , function ( ) {
781
+ browser . defer ( noop , 100 , 'fooType' ) ;
782
+ browser . defer ( noop , 200 , 'barType' ) ;
783
+
784
+ browser . notifyWhenNoOutstandingRequests ( callback , 'fooType' ) ;
785
+ expect ( callback ) . not . toHaveBeenCalled ( ) ;
786
+
787
+ browser . defer . flush ( 100 ) ;
788
+ expect ( callback ) . toHaveBeenCalled ( ) ;
789
+ } ) ;
790
+
791
+ it ( 'should not run the callback more than once' , function ( ) {
792
+ browser . defer ( noop , 100 , 'fooType' ) ;
793
+ browser . defer ( noop , 200 ) ;
794
+
795
+ browser . notifyWhenNoOutstandingRequests ( callback , 'fooType' ) ;
796
+ expect ( callback ) . not . toHaveBeenCalled ( ) ;
797
+
798
+ browser . defer . flush ( 100 ) ;
799
+ expect ( callback ) . toHaveBeenCalledOnce ( ) ;
800
+
801
+ browser . defer . flush ( 100 ) ;
802
+ expect ( callback ) . toHaveBeenCalledOnce ( ) ;
803
+
804
+ browser . defer ( noop , 100 , 'fooType' ) ;
805
+ browser . defer ( noop , 200 ) ;
806
+ browser . defer . flush ( ) ;
807
+ expect ( callback ) . toHaveBeenCalledOnce ( ) ;
808
+ } ) ;
809
+ } ) ;
658
810
} ) ;
659
811
} ) ;
660
812
@@ -705,47 +857,53 @@ describe('ngMock', function() {
705
857
706
858
describe ( '$timeout' , function ( ) {
707
859
it ( 'should expose flush method that will flush the pending queue of tasks' , inject (
708
- function ( $timeout ) {
860
+ function ( $rootScope , $ timeout) {
709
861
var logger = [ ] ,
710
862
logFn = function ( msg ) { return function ( ) { logger . push ( msg ) ; } ; } ;
711
863
712
864
$timeout ( logFn ( 't1' ) ) ;
713
865
$timeout ( logFn ( 't2' ) , 200 ) ;
866
+ $rootScope . $evalAsync ( logFn ( 'rs' ) ) ; // Non-timeout tasks are flushed as well.
714
867
$timeout ( logFn ( 't3' ) ) ;
715
868
expect ( logger ) . toEqual ( [ ] ) ;
716
869
717
870
$timeout . flush ( ) ;
718
- expect ( logger ) . toEqual ( [ 't1' , 't3' , 't2' ] ) ;
871
+ expect ( logger ) . toEqual ( [ 't1' , 'rs' , ' t3', 't2' ] ) ;
719
872
} ) ) ;
720
873
721
874
722
- it ( 'should throw an exception when not flushed' , inject ( function ( $timeout ) {
723
- $timeout ( noop ) ;
875
+ it ( 'should throw an exception when not flushed' , inject ( function ( $rootScope , $timeout ) {
876
+ $timeout ( noop , 100 ) ;
877
+ $rootScope . $evalAsync ( noop ) ;
724
878
725
- var expectedError = 'Deferred tasks to flush (1): {id: 0, time: 0}' ;
726
- expect ( function ( ) { $timeout . verifyNoPendingTasks ( ) ; } ) . toThrowError ( expectedError ) ;
879
+ var expectedError =
880
+ 'Deferred tasks to flush (2):\n' +
881
+ ' {id: 1, type: $evalAsync, time: 0}\n' +
882
+ ' {id: 0, type: $timeout, time: 100}' ;
883
+ expect ( $timeout . verifyNoPendingTasks ) . toThrowError ( expectedError ) ;
727
884
} ) ) ;
728
885
729
886
730
- it ( 'should do nothing when all tasks have been flushed' , inject ( function ( $timeout ) {
731
- $timeout ( noop ) ;
887
+ it ( 'should do nothing when all tasks have been flushed' , inject ( function ( $rootScope , $timeout ) {
888
+ $timeout ( noop , 100 ) ;
889
+ $rootScope . $evalAsync ( noop ) ;
732
890
733
891
$timeout . flush ( ) ;
734
- expect ( function ( ) { $timeout . verifyNoPendingTasks ( ) ; } ) . not . toThrow ( ) ;
892
+ expect ( $timeout . verifyNoPendingTasks ) . not . toThrow ( ) ;
735
893
} ) ) ;
736
894
737
895
738
896
it ( 'should check against the delay if provided within timeout' , inject ( function ( $timeout ) {
739
897
$timeout ( noop , 100 ) ;
740
898
$timeout . flush ( 100 ) ;
741
- expect ( function ( ) { $timeout . verifyNoPendingTasks ( ) ; } ) . not . toThrow ( ) ;
899
+ expect ( $timeout . verifyNoPendingTasks ) . not . toThrow ( ) ;
742
900
743
901
$timeout ( noop , 1000 ) ;
744
902
$timeout . flush ( 100 ) ;
745
- expect ( function ( ) { $timeout . verifyNoPendingTasks ( ) ; } ) . toThrow ( ) ;
903
+ expect ( $timeout . verifyNoPendingTasks ) . toThrow ( ) ;
746
904
747
905
$timeout . flush ( 900 ) ;
748
- expect ( function ( ) { $timeout . verifyNoPendingTasks ( ) ; } ) . not . toThrow ( ) ;
906
+ expect ( $timeout . verifyNoPendingTasks ) . not . toThrow ( ) ;
749
907
} ) ) ;
750
908
751
909
@@ -763,6 +921,7 @@ describe('ngMock', function() {
763
921
expect ( count ) . toBe ( 2 ) ;
764
922
} ) ) ;
765
923
924
+
766
925
it ( 'should resolve timeout functions following the timeline' , inject ( function ( $timeout ) {
767
926
var count1 = 0 , count2 = 0 ;
768
927
var iterate1 = function ( ) {
@@ -1056,7 +1215,7 @@ describe('ngMock', function() {
1056
1215
1057
1216
1058
1217
describe ( '$httpBackend' , function ( ) {
1059
- var hb , callback , realBackendSpy ;
1218
+ var hb , callback ;
1060
1219
1061
1220
beforeEach ( inject ( function ( $httpBackend ) {
1062
1221
callback = jasmine . createSpy ( 'callback' ) ;
0 commit comments