Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit 459eace

Browse files
committed
Merge pull request #103 from gary-b/nullWatches
fix(scopes): handle watches on null expression
2 parents 706d1b7 + c05fd58 commit 459eace

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

src/modules/scopes.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ function scopeDescriptor (elt, scope) {
299299
}
300300

301301
function humanReadableWatchExpression (fn) {
302+
if (fn == null) {
303+
return null;
304+
}
302305
if (fn.exp) {
303306
fn = fn.exp;
304307
} else if (fn.name) {

test/scopes.spec.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,35 @@ describe('ngHintScopes', function() {
2020
});
2121

2222
it('should run perf timers for string expressions', function() {
23-
var calls = hint.emit.calls;
2423
scope.$watch('a.b', function() {});
25-
expect(calls.count()).toBe(0);
24+
expect(hint.emit.calls.count()).toBe(0);
2625

2726
scope.$apply();
28-
var evt = calls.mostRecent().args[1].events[0];
29-
expect(calls.count()).toBe(1);
30-
expect(evt.time).toEqual(jasmine.any(Number));
31-
evt.time = null
32-
expect(evt).toEqual({
27+
var expectedEvent = {
3328
eventType: 'scope:watch',
3429
id: scope.$id,
3530
watch: 'a.b',
3631
time: null
37-
});
32+
};
33+
checkMostRecentCall(1, expectedEvent);
34+
scope.$apply();
35+
checkMostRecentCall(2, expectedEvent);
36+
});
37+
38+
it('should handle null watchExpression', function() {
39+
scope.$watch(null, function() {});
40+
expect(hint.emit.calls.count()).toBe(0);
3841

3942
scope.$apply();
40-
expect(calls.count()).toBe(2);
41-
var evt = calls.mostRecent().args[1].events[0];
42-
expect(evt.time).toEqual(jasmine.any(Number));
43-
evt.time = null
44-
expect(evt).toEqual({
43+
var expectedEvent = {
4544
eventType: 'scope:watch',
4645
id: scope.$id,
47-
watch: 'a.b',
46+
watch: null,
4847
time: null
49-
});
50-
48+
};
49+
checkMostRecentCall(1, expectedEvent);
50+
scope.$apply();
51+
checkMostRecentCall(2, expectedEvent);
5152
});
5253

5354
if (angular.version.minor >= 3) {
@@ -70,6 +71,17 @@ describe('ngHintScopes', function() {
7071
expect(evt).toBeUndefined();
7172
});
7273
}
74+
75+
// the event's time property is set to null before comparison with expectedEvent, so callers
76+
// should set the time property on expectedEvent to null as well
77+
function checkMostRecentCall(expectedCount, expectedEvent){
78+
var calls = hint.emit.calls;
79+
var evt = calls.mostRecent().args[1].events[0];
80+
expect(calls.count()).toBe(expectedCount);
81+
expect(evt.time).toEqual(jasmine.any(Number));
82+
evt.time = null;
83+
expect(evt).toEqual(expectedEvent);
84+
}
7385
});
7486

7587
// TODO: revisit this when I figure out a good way to make this

0 commit comments

Comments
 (0)