@@ -604,6 +604,138 @@ describe('Scope', function() {
604
604
} ) ;
605
605
606
606
607
+ describe ( 'watch reregistration' , function ( ) {
608
+ it ( 'should be possible to reregister a watcher' , inject ( function ( $rootScope , log ) {
609
+ var w1 = $rootScope . $watch ( log . fn ( 'watch1' ) , noop ) ;
610
+ var w2 = $rootScope . $watch ( log . fn ( 'watch2' ) , noop ) ;
611
+ var w3 = $rootScope . $watch ( log . fn ( 'watch3' ) , noop ) ;
612
+
613
+ $rootScope . $digest ( ) ;
614
+ expect ( log ) . toEqual ( [ 'watch1' , 'watch2' , 'watch3' , 'watch1' , 'watch2' , 'watch3' ] ) ;
615
+ log . reset ( ) ;
616
+
617
+ $rootScope . $digest ( ) ;
618
+ expect ( log ) . toEqual ( [ 'watch1' , 'watch2' , 'watch3' ] ) ;
619
+ log . reset ( ) ;
620
+
621
+ w1 ( ) ;
622
+ $rootScope . $digest ( ) ;
623
+ expect ( log ) . toEqual ( [ 'watch2' , 'watch3' ] ) ;
624
+ log . reset ( ) ;
625
+
626
+ w1 . restore ( ) ;
627
+ $rootScope . $digest ( ) ;
628
+ expect ( log ) . toEqual ( [ 'watch1' , 'watch2' , 'watch3' ] ) ;
629
+ log . reset ( ) ;
630
+
631
+ w2 ( ) ;
632
+ $rootScope . $digest ( ) ;
633
+ expect ( log ) . toEqual ( [ 'watch1' , 'watch3' ] ) ;
634
+ log . reset ( ) ;
635
+
636
+ w2 . restore ( ) ;
637
+ $rootScope . $digest ( ) ;
638
+ expect ( log ) . toEqual ( [ 'watch1' , 'watch2' , 'watch3' ] ) ;
639
+ log . reset ( ) ;
640
+
641
+ w3 ( ) ;
642
+ $rootScope . $digest ( ) ;
643
+ expect ( log ) . toEqual ( [ 'watch1' , 'watch2' ] ) ;
644
+ log . reset ( ) ;
645
+
646
+ w3 . restore ( ) ;
647
+ $rootScope . $digest ( ) ;
648
+ expect ( log ) . toEqual ( [ 'watch1' , 'watch2' , 'watch3' ] ) ;
649
+ log . reset ( ) ;
650
+ } ) ) ;
651
+
652
+
653
+ it ( 'should not add multiple times the same watcher when calling multiple times `restore`' , inject ( function ( $rootScope , log ) {
654
+ var w1 = $rootScope . $watch ( log . fn ( 'watch1' ) , noop ) ;
655
+ var w2 = $rootScope . $watch ( log . fn ( 'watch2' ) , noop ) ;
656
+ var w3 = $rootScope . $watch ( log . fn ( 'watch3' ) , noop ) ;
657
+
658
+ $rootScope . $digest ( ) ;
659
+ log . reset ( ) ;
660
+
661
+ w1 ( ) ;
662
+ $rootScope . $digest ( ) ;
663
+ log . reset ( ) ;
664
+ w1 . restore ( ) ;
665
+ w1 . restore ( ) ;
666
+ $rootScope . $digest ( ) ;
667
+ expect ( log ) . toEqual ( [ 'watch1' , 'watch2' , 'watch3' ] ) ;
668
+ log . reset ( ) ;
669
+
670
+ w1 ( ) ;
671
+ $rootScope . $digest ( ) ;
672
+ log . reset ( ) ;
673
+ w1 . restore ( ) ;
674
+ w1 ( ) ;
675
+ w1 . restore ( ) ;
676
+ $rootScope . $digest ( ) ;
677
+ expect ( log ) . toEqual ( [ 'watch1' , 'watch2' , 'watch3' ] ) ;
678
+ log . reset ( ) ;
679
+ } ) ) ;
680
+
681
+ it ( 'should be possible to reregister a watcher from watchGroup' , inject ( function ( $rootScope , log ) {
682
+ var w = $rootScope . $watchGroup ( [ log . fn ( 'watch1' ) , log . fn ( 'watch2' ) , log . fn ( 'watch3' ) ] , noop ) ;
683
+
684
+ $rootScope . $digest ( ) ;
685
+ expect ( log ) . toEqual ( [ 'watch1' , 'watch2' , 'watch3' , 'watch1' , 'watch2' , 'watch3' ] ) ;
686
+ log . reset ( ) ;
687
+
688
+ $rootScope . $digest ( ) ;
689
+ expect ( log ) . toEqual ( [ 'watch1' , 'watch2' , 'watch3' ] ) ;
690
+ log . reset ( ) ;
691
+
692
+ w ( ) ;
693
+ $rootScope . $digest ( ) ;
694
+ expect ( log ) . toEqual ( [ ] ) ;
695
+ log . reset ( ) ;
696
+
697
+ w . restore ( ) ;
698
+ $rootScope . $digest ( ) ;
699
+ expect ( log ) . toEqual ( [ 'watch1' , 'watch2' , 'watch3' ] ) ;
700
+ log . reset ( ) ;
701
+
702
+ w . restore ( ) ;
703
+ $rootScope . $digest ( ) ;
704
+ expect ( log ) . toEqual ( [ 'watch1' , 'watch2' , 'watch3' ] ) ;
705
+ log . reset ( ) ;
706
+ } ) ) ;
707
+
708
+ it ( 'should be possible to reregister a watcher from watchCollection' , inject ( function ( $rootScope , log ) {
709
+ $rootScope . obj = [ 0 , 1 , 2 ] ;
710
+ var w = $rootScope . $watchCollection ( 'obj' , log . fn ( 'watch!' ) ) ;
711
+
712
+ $rootScope . $digest ( ) ;
713
+ expect ( log ) . toEqual ( [ 'watch!' ] ) ;
714
+ log . reset ( ) ;
715
+
716
+ $rootScope . $digest ( ) ;
717
+ expect ( log ) . toEqual ( [ ] ) ;
718
+ log . reset ( ) ;
719
+
720
+ $rootScope . obj . push ( 3 ) ;
721
+ $rootScope . $digest ( ) ;
722
+ expect ( log ) . toEqual ( [ 'watch!' ] ) ;
723
+ log . reset ( ) ;
724
+
725
+ w ( ) ;
726
+ $rootScope . obj . push ( 4 ) ;
727
+ $rootScope . $digest ( ) ;
728
+ expect ( log ) . toEqual ( [ ] ) ;
729
+ log . reset ( ) ;
730
+
731
+ w . restore ( ) ;
732
+ $rootScope . $digest ( ) ;
733
+ expect ( log ) . toEqual ( [ 'watch!' ] ) ;
734
+ log . reset ( ) ;
735
+ } ) ) ;
736
+ } ) ;
737
+
738
+
607
739
describe ( '$watchCollection' , function ( ) {
608
740
var log , $rootScope , deregister ;
609
741
0 commit comments