From 8bd6619b7efa485b020fec96c76047e480469871 Mon Sep 17 00:00:00 2001 From: Paulo Scardine Date: Sun, 9 Jun 2013 23:16:26 -0300 Subject: fix(scope): watches can be safely unregistered inside watch handlers Closes #2915 --- test/ng/rootScopeSpec.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'test') diff --git a/test/ng/rootScopeSpec.js b/test/ng/rootScopeSpec.js index 9d86c098..15990c96 100644 --- a/test/ng/rootScopeSpec.js +++ b/test/ng/rootScopeSpec.js @@ -330,6 +330,30 @@ describe('Scope', function() { expect(listener).not.toHaveBeenCalled(); })); + it('should allow a watch to be unregistered while in a digest', inject(function($rootScope) { + var remove1, remove2; + $rootScope.$watch('remove', function() { + remove1(); + remove2(); + }); + remove1 = $rootScope.$watch('thing', function() {}); + remove2 = $rootScope.$watch('thing', function() {}); + expect(function() { + $rootScope.$apply('remove = true'); + }).not.toThrow(); + })); + + it('should allow a watch to be added while in a digest', inject(function($rootScope) { + var watch1 = jasmine.createSpy('watch1'), + watch2 = jasmine.createSpy('watch2'); + $rootScope.$watch('foo', function() { + $rootScope.$watch('foo', watch1); + $rootScope.$watch('foo', watch2); + }); + $rootScope.$apply('foo = true'); + expect(watch1).toHaveBeenCalled(); + expect(watch2).toHaveBeenCalled(); + })); it('should not infinitely digest when current value is NaN', inject(function($rootScope) { $rootScope.$watch(function() { return NaN;}); -- cgit v1.2.3