diff options
| author | Igor Minar | 2011-09-01 14:19:22 -0700 |
|---|---|---|
| committer | Igor Minar | 2011-09-01 15:00:22 -0700 |
| commit | 31b86241215bb37cc6bb81f98a47942738d710c2 (patch) | |
| tree | 5cefa3afacad07b28b07654cde2280aa0e51a0eb /test/ScopeSpec.js | |
| parent | a5607e3061f5b0aa7988446025dba8f89413fd9d (diff) | |
| download | angular.js-31b86241215bb37cc6bb81f98a47942738d710c2.tar.bz2 | |
feat(scope): add listener deregistration fn for $watch and $on
- both $watch and $on now return a function which when called
deregisters the listener
- $removeListener was removed and replaced with the above
- added more tests for $watch and $on
Closes #542
Diffstat (limited to 'test/ScopeSpec.js')
| -rw-r--r-- | test/ScopeSpec.js | 88 |
1 files changed, 73 insertions, 15 deletions
diff --git a/test/ScopeSpec.js b/test/ScopeSpec.js index 4e21537c..492396c5 100644 --- a/test/ScopeSpec.js +++ b/test/ScopeSpec.js @@ -207,6 +207,7 @@ describe('Scope', function() { expect(log).toEqual('abc'); }); + it('should repeat watch cycle from the root elemnt', function() { var log = ''; var child = root.$new(); @@ -269,6 +270,29 @@ describe('Scope', function() { root.$digest(); expect(callCount).toEqual(1); }); + + + it('should return a function that allows listeners to be unregistered', function() { + var root = angular.scope(), + listener = jasmine.createSpy('watch listener'), + listenerRemove; + + listenerRemove = root.$watch('foo', listener); + root.$digest(); //init + expect(listener).toHaveBeenCalled(); + expect(listenerRemove).toBeDefined(); + + listener.reset(); + root.foo = 'bar'; + root.$digest(); //triger + expect(listener).toHaveBeenCalledOnce(); + + listener.reset(); + root.foo = 'baz'; + listenerRemove(); + root.$digest(); //trigger + expect(listener).not.toHaveBeenCalled(); + }); }); @@ -434,6 +458,55 @@ describe('Scope', function() { describe('events', function() { + describe('$on', function() { + + it('should add listener for both $emit and $broadcast events', function() { + var log = '', + root = angular.scope(), + child = root.$new(); + + function eventFn(){ + log += 'X'; + } + + child.$on('abc', eventFn); + expect(log).toEqual(''); + + child.$emit('abc'); + expect(log).toEqual('X'); + + child.$broadcast('abc'); + expect(log).toEqual('XX'); + }); + + + it('should return a function that deregisters the listener', function() { + var log = '', + root = angular.scope(), + child = root.$new(), + listenerRemove; + + function eventFn(){ + log += 'X'; + } + + listenerRemove = child.$on('abc', eventFn); + expect(log).toEqual(''); + expect(listenerRemove).toBeDefined(); + + child.$emit('abc'); + child.$broadcast('abc'); + expect(log).toEqual('XX'); + + log = ''; + listenerRemove(); + child.$emit('abc'); + child.$broadcast('abc'); + expect(log).toEqual(''); + }); + }); + + describe('$emit', function() { var log, child, grandChild, greatGrandChild; @@ -480,21 +553,6 @@ describe('Scope', function() { }); - it('should remove event listener', function() { - function eventFn(){ - log += 'abc;'; - } - - child.$on('abc', eventFn); - child.$emit('abc'); - expect(log).toEqual('abc;'); - log = ''; - child.$removeListener('abc', eventFn); - child.$emit('abc'); - expect(log).toEqual(''); - }); - - it('should forward method arguments', function() { child.$on('abc', function(event, arg1, arg2){ expect(event.name).toBe('abc'); |
