-
Notifications
You must be signed in to change notification settings - Fork 27.5k
feat($rootScope): adds $onRootScope method #5507
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1116,6 +1116,72 @@ describe('Scope', function() { | |
})); | ||
}); | ||
|
||
describe('$onRootScope', function() { | ||
|
||
it('should add listener for both $emit and $broadcast events that are triggered on $rootScope', inject(function($rootScope) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there isn't really a distinction between $emit and $broadcast events, so this confuses me... I don't think it's terrible to test both, but the name just seems a bit more verbose than it needs to be (it is just my opinion, though) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, you are right. I basically just copied over the tests for |
||
var log = '', | ||
child = $rootScope.$new(); | ||
|
||
function eventFn() { | ||
log += 'X'; | ||
} | ||
|
||
child.$onRootScope('abc', eventFn); | ||
expect(log).toEqual(''); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you will probably be told to change these to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. I basically just copied over the tests for |
||
|
||
$rootScope.$emit('abc'); | ||
expect(log).toEqual('X'); | ||
|
||
$rootScope.$broadcast('abc'); | ||
expect(log).toEqual('XX'); | ||
})); | ||
|
||
|
||
it('should return a function that deregisters the listener', inject(function($rootScope) { | ||
var log = '', | ||
child = $rootScope.$new(), | ||
listenerRemove; | ||
|
||
function eventFn() { | ||
log += 'X'; | ||
} | ||
|
||
listenerRemove = child.$onRootScope('abc', eventFn); | ||
expect(log).toEqual(''); | ||
expect(listenerRemove).toBeDefined(); | ||
|
||
$rootScope.$emit('abc'); | ||
$rootScope.$broadcast('abc'); | ||
expect(log).toEqual('XX'); | ||
|
||
log = ''; | ||
listenerRemove(); | ||
$rootScope.$emit('abc'); | ||
$rootScope.$broadcast('abc'); | ||
expect(log).toEqual(''); | ||
})); | ||
|
||
it('should remove listener when local scope gets destroyed', inject(function($rootScope) { | ||
var log = '', | ||
child = $rootScope.$new(); | ||
|
||
function eventFn() { | ||
log += 'X'; | ||
} | ||
|
||
child.$onRootScope('abc', eventFn); | ||
expect(log).toEqual(''); | ||
|
||
$rootScope.$emit('abc'); | ||
expect(log).toEqual('X'); | ||
|
||
child.$destroy(); | ||
|
||
$rootScope.$emit('abc'); | ||
expect(log).toEqual('X'); | ||
})); | ||
|
||
}); | ||
|
||
describe('$emit', function() { | ||
var log, child, grandChild, greatGrandChild; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't we unsubscribe both listeners?
root.$on
&this.$on