@@ -734,74 +734,54 @@ describe('Scope', function() {
734
734
} ) ;
735
735
736
736
describe ( '$watchSet' , function ( ) {
737
- var scope ;
738
- beforeEach ( inject ( function ( $rootScope ) {
739
- scope = $rootScope . $new ( ) ;
740
- } ) ) ;
741
-
742
- it ( 'should skip empty sets' , function ( ) {
743
- expect ( scope . $watchSet ( [ ] , null ) ( ) ) . toBeUndefined ( ) ;
744
- } ) ;
745
737
746
- it ( 'should treat set of 1 as direct watch' , function ( ) {
747
- var lastValues = [ 'foo' ] ;
748
- var log = '' ;
749
- var clean = scope . $watchSet ( [ 'a' ] , function ( values , oldValues , s ) {
750
- log += values . join ( ',' ) + ';' ;
751
- expect ( s ) . toBe ( scope ) ;
752
- expect ( oldValues ) . toEqual ( lastValues ) ;
753
- lastValues = values . slice ( ) ;
738
+ it ( 'should detect a change to any expression in the array' , inject ( function ( $rootScope , log ) {
739
+ $rootScope . $watchSet ( [ 'a' , 'b' ] , function ( values , oldValues , scope ) {
740
+ expect ( scope ) . toBe ( $rootScope ) ;
741
+ log ( values + '-' + oldValues ) ;
754
742
} ) ;
755
743
756
- scope . a = 'foo' ;
757
- scope . $digest ( ) ;
758
- expect ( log ) . toEqual ( 'foo;' ) ;
759
-
760
- scope . $digest ( ) ;
761
- expect ( log ) . toEqual ( 'foo;' ) ;
762
-
763
- scope . a = 'bar' ;
764
- scope . $digest ( ) ;
765
- expect ( log ) . toEqual ( 'foo;bar;' ) ;
744
+ $rootScope . a = 'foo' ;
745
+ $rootScope . b = 'bar' ;
746
+ $rootScope . $digest ( ) ;
747
+ expect ( log ) . toEqual ( 'foo,bar-foo,bar' ) ;
766
748
767
- clean ( ) ;
768
- scope . a = 'xxx' ;
769
- scope . $digest ( ) ;
770
- expect ( log ) . toEqual ( 'foo;bar;' ) ;
771
- } ) ;
749
+ log . reset ( ) ;
750
+ $rootScope . $digest ( ) ;
751
+ expect ( log ) . toEqual ( '' ) ;
772
752
773
- it ( 'should detect a change to any one in a set' , function ( ) {
774
- var lastValues = [ 'foo' , 'bar' ] ;
775
- var log = '' ;
776
- var clean = scope . $watchSet ( [ 'a' , 'b' ] , function ( values , oldValues , s ) {
777
- log += values . join ( ',' ) + ';' ;
778
- expect ( s ) . toBe ( scope ) ;
779
- expect ( oldValues ) . toEqual ( lastValues ) ;
780
- lastValues = values . slice ( ) ;
781
- } ) ;
753
+ log . reset ( ) ;
754
+ $rootScope . a = 'a' ;
755
+ $rootScope . $digest ( ) ;
756
+ expect ( log ) . toEqual ( 'a,bar-foo,bar' ) ;
782
757
783
- scope . a = 'foo' ;
784
- scope . b = 'bar' ;
785
- scope . $digest ( ) ;
786
- expect ( log ) . toEqual ( 'foo,bar;' ) ;
758
+ log . reset ( ) ;
759
+ $rootScope . a = 'A' ;
760
+ $rootScope . b = 'B' ;
761
+ $rootScope . $digest ( ) ;
762
+ expect ( log ) . toEqual ( 'A,B-a,bar' ) ;
763
+ } ) ) ;
787
764
788
- scope . $digest ( ) ;
789
- expect ( log ) . toEqual ( 'foo,bar;' ) ;
765
+ it ( 'should return a function that allows listeners to be deregistered' , inject ( function ( $rootScope ) {
766
+ var listener = jasmine . createSpy ( 'watchSet listener' ) ,
767
+ listenerRemove ;
790
768
791
- scope . a = 'a' ;
792
- scope . $digest ( ) ;
793
- expect ( log ) . toEqual ( 'foo,bar;a,bar;' ) ;
769
+ listenerRemove = $rootScope . $watchSet ( [ 'a' ] , listener ) ;
770
+ $rootScope . $digest ( ) ; //init
771
+ expect ( listener ) . toHaveBeenCalled ( ) ;
772
+ expect ( listenerRemove ) . toBeDefined ( ) ;
794
773
795
- scope . a = 'A' ;
796
- scope . b = 'B ' ;
797
- scope . $digest ( ) ;
798
- expect ( log ) . toEqual ( 'foo,bar;a,bar;A,B;' ) ;
774
+ listener . reset ( ) ;
775
+ $rootScope . a = 'bar ' ;
776
+ $rootScope . $digest ( ) ; //trigger
777
+ expect ( listener ) . toHaveBeenCalledOnce ( ) ;
799
778
800
- clean ( ) ;
801
- scope . a = 'xxx' ;
802
- scope . $digest ( ) ;
803
- expect ( log ) . toEqual ( 'foo,bar;a,bar;A,B;' ) ;
804
- } ) ;
779
+ listener . reset ( ) ;
780
+ $rootScope . a = 'baz' ;
781
+ listenerRemove ( ) ;
782
+ $rootScope . $digest ( ) ; //trigger
783
+ expect ( listener ) . not . toHaveBeenCalled ( ) ;
784
+ } ) ) ;
805
785
} ) ;
806
786
807
787
describe ( '$destroy' , function ( ) {
0 commit comments