22
33describe ( 'Scope' , function ( ) {
44
5- beforeEach ( inject ( function ( $exceptionHandlerProvider ) {
6- $exceptionHandlerProvider . mode ( 'log' ) ;
7- } ) ) ;
8-
9-
105 describe ( '$root' , function ( ) {
116 it ( 'should point to itself' , inject ( function ( $rootScope ) {
127 expect ( $rootScope . $root ) . toEqual ( $rootScope ) ;
@@ -122,7 +117,9 @@ describe('Scope', function() {
122117 } ) ) ;
123118
124119
125- it ( 'should delegate exceptions' , inject ( function ( $rootScope , $exceptionHandler , $log ) {
120+ it ( 'should delegate exceptions' , inject ( function ( $exceptionHandlerProvider ) {
121+ $exceptionHandlerProvider . mode ( 'log' ) ;
122+ } , function ( $rootScope , $exceptionHandler , $log ) {
126123 $rootScope . $watch ( 'a' , function ( ) { throw new Error ( 'abc' ) ; } ) ;
127124 $rootScope . a = 1 ;
128125 $rootScope . $digest ( ) ;
@@ -227,7 +224,7 @@ describe('Scope', function() {
227224 } ) ) ;
228225
229226
230- it ( 'should prevent infinite recurcion and print print watcher function name or body' ,
227+ it ( 'should prevent infinite recursion and print print watcher function name or body' ,
231228 inject ( function ( $rootScope ) {
232229 $rootScope . $watch ( function watcherA ( ) { return $rootScope . a ; } , function ( self ) { self . b ++ ; } ) ;
233230 $rootScope . $watch ( function ( ) { return $rootScope . b ; } , function ( self ) { self . a ++ ; } ) ;
@@ -277,7 +274,7 @@ describe('Scope', function() {
277274 } ) ) ;
278275
279276
280- it ( 'should prevent recursion' , inject ( function ( $rootScope ) {
277+ it ( 'should prevent $digest recursion' , inject ( function ( $rootScope ) {
281278 var callCount = 0 ;
282279 $rootScope . $watch ( 'name' , function ( ) {
283280 expect ( function ( ) {
@@ -462,7 +459,9 @@ describe('Scope', function() {
462459 } ) ) ;
463460
464461
465- it ( 'should catch exceptions' , inject ( function ( $rootScope , $exceptionHandler , $log ) {
462+ it ( 'should catch exceptions' , inject ( function ( $exceptionHandlerProvider ) {
463+ $exceptionHandlerProvider . mode ( 'log' ) ;
464+ } , function ( $rootScope , $exceptionHandler , $log ) {
466465 var log = '' ;
467466 var child = $rootScope . $new ( ) ;
468467 $rootScope . $watch ( 'a' , function ( scope , a ) { log += '1' ; } ) ;
@@ -476,7 +475,9 @@ describe('Scope', function() {
476475
477476 describe ( 'exceptions' , function ( ) {
478477 var log ;
479- beforeEach ( inject ( function ( $rootScope ) {
478+ beforeEach ( inject ( function ( $exceptionHandlerProvider ) {
479+ $exceptionHandlerProvider . mode ( 'log' ) ;
480+ } , function ( $rootScope ) {
480481 log = '' ;
481482 $rootScope . $watch ( function ( ) { log += '$digest;' ; } ) ;
482483 $rootScope . $digest ( ) ;
@@ -502,6 +503,57 @@ describe('Scope', function() {
502503 expect ( $exceptionHandler . errors ) . toEqual ( [ error ] ) ;
503504 } ) ) ;
504505 } ) ;
506+
507+
508+ describe ( 'recursive $apply protection' , function ( ) {
509+ it ( 'should throw an exception if $apply is called while an $apply is in progress' , inject (
510+ function ( $rootScope ) {
511+ expect ( function ( ) {
512+ $rootScope . $apply ( function ( ) {
513+ $rootScope . $apply ( ) ;
514+ } ) ;
515+ } ) . toThrow ( '$apply already in progress' ) ;
516+ } ) ) ;
517+
518+
519+ it ( 'should throw an exception if $apply is called while flushing evalAsync queue' , inject (
520+ function ( $rootScope ) {
521+ expect ( function ( ) {
522+ $rootScope . $apply ( function ( ) {
523+ $rootScope . $evalAsync ( function ( ) {
524+ $rootScope . $apply ( ) ;
525+ } ) ;
526+ } ) ;
527+ } ) . toThrow ( '$digest already in progress' ) ;
528+ } ) ) ;
529+
530+
531+ it ( 'should throw an exception if $apply is called while a watch is being initialized' , inject (
532+ function ( $rootScope ) {
533+ var childScope1 = $rootScope . $new ( ) ;
534+ childScope1 . $watch ( 'x' , function ( ) {
535+ childScope1 . $apply ( ) ;
536+ } ) ;
537+ expect ( function ( ) { childScope1 . $apply ( ) ; } ) . toThrow ( '$digest already in progress' ) ;
538+ } ) ) ;
539+
540+
541+ it ( 'should thrown an exception if $apply in called from a watch fn (after init)' , inject (
542+ function ( $rootScope ) {
543+ var childScope2 = $rootScope . $new ( ) ;
544+ childScope2 . $apply ( function ( ) {
545+ childScope2 . $watch ( 'x' , function ( scope , newVal , oldVal ) {
546+ if ( newVal !== oldVal ) {
547+ childScope2 . $apply ( ) ;
548+ }
549+ } ) ;
550+ } ) ;
551+
552+ expect ( function ( ) { childScope2 . $apply ( function ( ) {
553+ childScope2 . x = 'something' ;
554+ } ) ; } ) . toThrow ( '$digest already in progress' ) ;
555+ } ) ) ;
556+ } ) ;
505557 } ) ;
506558
507559
@@ -561,7 +613,10 @@ describe('Scope', function() {
561613 log += event . currentScope . id + '>' ;
562614 }
563615
564- beforeEach ( inject ( function ( $rootScope ) {
616+ beforeEach ( inject (
617+ function ( $exceptionHandlerProvider ) {
618+ $exceptionHandlerProvider . mode ( 'log' ) ;
619+ } , function ( $rootScope ) {
565620 log = '' ;
566621 child = $rootScope . $new ( ) ;
567622 grandChild = child . $new ( ) ;
0 commit comments