-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat(ngMock): decorator that adds Scope#$countChildScopes and Scope#$countWatchers #9871
Conversation
…countWatchers When writing tests it's often useful to check the number of child scopes or watchers within the current current scope subtree. Common use-case for advanced directives is to test that the directive is properly cleaning up after itself. These new methods simplify writing assertions that verify that child scopes were properly destroyed or that watchers were deregistered.
$rootScopePrototype.$countChildScopes = countChildScopes; | ||
$rootScopePrototype.$countWatchers = countWatchers; | ||
|
||
// TODO: remove if Object.getPrototypeOF works on IE9 |
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.
according to http://kangax.github.io/compat-table/es5/ it does
I think people might want to use this in production, for things like live debuggers. But it's a fair bit of code, so maybe we don't want that. Need to fix the doc annotations so that travis will run. |
currentScope = pendingChildHeads.shift(); | ||
|
||
while (currentScope) { | ||
count += currentScope.$$watchers ? currentScope.$$watchers.length : 0; |
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.
Looking at this, you have two methods implementing the same BFS, with only a tiny variation (accumulate $$watchers.length vs accumulate number of visits). Not sure if it's worth it, but you could consider pulling that out. Might be roughly the same amount of code, but you wouldn't have to write the same set of tests twice.
Thanks Igor, this will fit my need. I think this should also be interesting for anybody else writing large / complex widget sets, like the Material Design folks (@naomiblack ?), to make sure you're not leaking scopes or watches. |
})); | ||
|
||
|
||
it('should correctly navigate complex scope tree', inject(function($rootScope) { |
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.
Might be nice to mention in the description that this tests the sibling navigation.
@petebacondarwin lets get this in so we can land lucas' /others prs mm? |
@IgorMinar - we actually need to use such functions in $compile tests but the way this is written makes it hard to access them - or am I missing something like being able to load the |
Oh, I take that back. It is available. |
I think that @mprobst is right when he notes that the two methods have a lot of overlap. |
I honestly don't think it's worth worrying about |
Probably - bigger fish and all that. |
When writing tests it's often useful to check the number of child scopes
or watchers within the current current scope subtree. Common use-case for advanced
directives is to test that the directive is properly cleaning up after itself. These
new methods simplify writing assertions that verify that child scopes were properly
destroyed or that watchers were deregistered.
NOTE for reviewers
src/
. What do others think?