@@ -730,6 +730,28 @@ describe('Scope', function() {
730
730
first . $apply ( ) ;
731
731
expect ( log ) . toBe ( '1232323' ) ;
732
732
} ) ) ;
733
+
734
+
735
+ it ( 'should decrement anscestor $$listenerCount entries' , inject ( function ( $rootScope ) {
736
+ var EVENT = 'fooEvent' ,
737
+ spy = jasmine . createSpy ( 'listener' ) ,
738
+ firstSecond = first . $new ( ) ;
739
+
740
+ firstSecond . $on ( EVENT , spy ) ;
741
+ firstSecond . $on ( EVENT , spy ) ;
742
+ middle . $on ( EVENT , spy ) ;
743
+
744
+ expect ( $rootScope . $$listenerCount [ EVENT ] ) . toBe ( 3 ) ;
745
+ expect ( first . $$listenerCount [ EVENT ] ) . toBe ( 2 ) ;
746
+
747
+ firstSecond . $destroy ( ) ;
748
+
749
+ expect ( $rootScope . $$listenerCount [ EVENT ] ) . toBe ( 1 ) ;
750
+ expect ( first . $$listenerCount [ EVENT ] ) . toBeUndefined ( ) ;
751
+
752
+ $rootScope . $broadcast ( EVENT ) ;
753
+ expect ( spy . callCount ) . toBe ( 1 ) ;
754
+ } ) ) ;
733
755
} ) ;
734
756
735
757
@@ -1091,29 +1113,78 @@ describe('Scope', function() {
1091
1113
} ) ) ;
1092
1114
1093
1115
1094
- it ( 'should return a function that deregisters the listener' , inject ( function ( $rootScope ) {
1095
- var log = '' ,
1096
- child = $rootScope . $new ( ) ,
1097
- listenerRemove ;
1098
-
1099
- function eventFn ( ) {
1100
- log += 'X' ;
1101
- }
1116
+ it ( 'should increment ancestor $$listenerCount entries' , inject ( function ( $rootScope ) {
1117
+ var child1 = $rootScope . $new ( ) ,
1118
+ child2 = child1 . $new ( ) ,
1119
+ spy = jasmine . createSpy ( ) ;
1102
1120
1103
- listenerRemove = child . $on ( 'abc' , eventFn ) ;
1104
- expect ( log ) . toEqual ( '' ) ;
1105
- expect ( listenerRemove ) . toBeDefined ( ) ;
1121
+ $rootScope . $on ( 'event1' , spy ) ;
1122
+ expect ( $rootScope . $$listenerCount ) . toEqual ( { event1 : 1 } ) ;
1106
1123
1107
- child . $emit ( 'abc' ) ;
1108
- child . $broadcast ( 'abc' ) ;
1109
- expect ( log ) . toEqual ( 'XX' ) ;
1124
+ child1 . $on ( 'event1' , spy ) ;
1125
+ expect ( $rootScope . $$listenerCount ) . toEqual ( { event1 : 2 } ) ;
1126
+ expect ( child1 . $$listenerCount ) . toEqual ( { event1 : 1 } ) ;
1110
1127
1111
- log = '' ;
1112
- listenerRemove ( ) ;
1113
- child . $emit ( 'abc' ) ;
1114
- child . $broadcast ( 'abc' ) ;
1115
- expect ( log ) . toEqual ( '' ) ;
1128
+ child2 . $on ( 'event2' , spy ) ;
1129
+ expect ( $rootScope . $$listenerCount ) . toEqual ( { event1 : 2 , event2 : 1 } ) ;
1130
+ expect ( child1 . $$listenerCount ) . toEqual ( { event1 : 1 , event2 : 1 } ) ;
1131
+ expect ( child2 . $$listenerCount ) . toEqual ( { event2 : 1 } ) ;
1116
1132
} ) ) ;
1133
+
1134
+
1135
+ describe ( 'deregistration' , function ( ) {
1136
+
1137
+ it ( 'should return a function that deregisters the listener' , inject ( function ( $rootScope ) {
1138
+ var log = '' ,
1139
+ child = $rootScope . $new ( ) ,
1140
+ listenerRemove ;
1141
+
1142
+ function eventFn ( ) {
1143
+ log += 'X' ;
1144
+ }
1145
+
1146
+ listenerRemove = child . $on ( 'abc' , eventFn ) ;
1147
+ expect ( log ) . toEqual ( '' ) ;
1148
+ expect ( listenerRemove ) . toBeDefined ( ) ;
1149
+
1150
+ child . $emit ( 'abc' ) ;
1151
+ child . $broadcast ( 'abc' ) ;
1152
+ expect ( log ) . toEqual ( 'XX' ) ;
1153
+ expect ( $rootScope . $$listenerCount [ 'abc' ] ) . toBe ( 1 ) ;
1154
+
1155
+ log = '' ;
1156
+ listenerRemove ( ) ;
1157
+ child . $emit ( 'abc' ) ;
1158
+ child . $broadcast ( 'abc' ) ;
1159
+ expect ( log ) . toEqual ( '' ) ;
1160
+ expect ( $rootScope . $$listenerCount [ 'abc' ] ) . toBeUndefined ( ) ;
1161
+ } ) ) ;
1162
+
1163
+
1164
+ it ( 'should decrement ancestor $$listenerCount entries' , inject ( function ( $rootScope ) {
1165
+ var child1 = $rootScope . $new ( ) ,
1166
+ child2 = child1 . $new ( ) ,
1167
+ spy = jasmine . createSpy ( ) ;
1168
+
1169
+ $rootScope . $on ( 'event1' , spy ) ;
1170
+ expect ( $rootScope . $$listenerCount ) . toEqual ( { event1 : 1 } ) ;
1171
+
1172
+ child1 . $on ( 'event1' , spy ) ;
1173
+ expect ( $rootScope . $$listenerCount ) . toEqual ( { event1 : 2 } ) ;
1174
+ expect ( child1 . $$listenerCount ) . toEqual ( { event1 : 1 } ) ;
1175
+
1176
+ var deregisterEvent2Listener = child2 . $on ( 'event2' , spy ) ;
1177
+ expect ( $rootScope . $$listenerCount ) . toEqual ( { event1 : 2 , event2 : 1 } ) ;
1178
+ expect ( child1 . $$listenerCount ) . toEqual ( { event1 : 1 , event2 : 1 } ) ;
1179
+ expect ( child2 . $$listenerCount ) . toEqual ( { event2 : 1 } ) ;
1180
+
1181
+ deregisterEvent2Listener ( ) ;
1182
+
1183
+ expect ( $rootScope . $$listenerCount ) . toEqual ( { event1 : 2 } ) ;
1184
+ expect ( child1 . $$listenerCount ) . toEqual ( { event1 : 1 } ) ;
1185
+ expect ( child2 . $$listenerCount ) . toEqual ( { } ) ;
1186
+ } ) )
1187
+ } ) ;
1117
1188
} ) ;
1118
1189
1119
1190
@@ -1360,6 +1431,23 @@ describe('Scope', function() {
1360
1431
} ) ) ;
1361
1432
1362
1433
1434
+ it ( 'should not descend past scopes with a $$listerCount of 0 or undefined' ,
1435
+ inject ( function ( $rootScope ) {
1436
+ var EVENT = 'fooEvent' ,
1437
+ spy = jasmine . createSpy ( 'listener' ) ;
1438
+
1439
+ // Precondition: There should be no listeners for fooEvent.
1440
+ expect ( $rootScope . $$listenerCount [ EVENT ] ) . toBeUndefined ( ) ;
1441
+
1442
+ // Add a spy listener to a child scope.
1443
+ $rootScope . $$childHead . $$listeners [ EVENT ] = [ spy ] ;
1444
+
1445
+ // $rootScope's count for 'fooEvent' is undefined, so spy should not be called.
1446
+ $rootScope . $broadcast ( EVENT ) ;
1447
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
1448
+ } ) ) ;
1449
+
1450
+
1363
1451
it ( 'should return event object' , function ( ) {
1364
1452
var result = child1 . $broadcast ( 'some' ) ;
1365
1453
0 commit comments