-
Notifications
You must be signed in to change notification settings - Fork 27.5k
watchCollection: Return correct oldCollection argument to listener #5661
Changes from 5 commits
950c6f3
ca6776a
42dd6a1
69ea4bc
34333fb
b809444
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 |
---|---|---|
|
@@ -456,12 +456,36 @@ describe('Scope', function() { | |
|
||
|
||
describe('$watchCollection', function() { | ||
var log, $rootScope, deregister; | ||
var log, logb, $rootScope, deregister; | ||
|
||
it('should return oldCollection === newCollection on first call (only) to listener', inject(function($rootScope) { | ||
var paramsEqual; | ||
$rootScope.obj = {'a': 'b'}; | ||
deregister = $rootScope.$watchCollection('obj', function listener(obj, objb) { | ||
paramsEqual = (obj === objb); | ||
}); | ||
|
||
// equal first time | ||
$rootScope.$digest(); | ||
expect(paramsEqual).toBeTruthy(); | ||
|
||
$rootScope.obj.a = 'c'; | ||
$rootScope.$digest(); | ||
expect(paramsEqual).toBeFalsy(); | ||
|
||
$rootScope.obj = undefined; | ||
$rootScope.$digest(); | ||
expect(paramsEqual).toBeFalsy(); | ||
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. please don't use |
||
|
||
})); | ||
|
||
|
||
beforeEach(inject(function(_$rootScope_) { | ||
log = []; | ||
logb = []; // list of before values | ||
log = []; // list of after values | ||
$rootScope = _$rootScope_; | ||
deregister = $rootScope.$watchCollection('obj', function logger(obj) { | ||
deregister = $rootScope.$watchCollection('obj', function logger(obj, objb) { | ||
logb.push(toJson(objb)); | ||
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. why reverse the order? (logb before log)? it's a bit surprising.. |
||
log.push(toJson(obj)); | ||
}); | ||
})); | ||
|
@@ -494,22 +518,27 @@ describe('Scope', function() { | |
it('should trigger when property changes into array', function() { | ||
$rootScope.obj = 'test'; | ||
$rootScope.$digest(); | ||
expect(logb).toEqual(['"test"']); | ||
expect(log).toEqual(['"test"']); | ||
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. all these changes made the tests harder to understand. maybe having just a single logger that would log 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. in any case we are missing this explicit test here. it's a must. just like you had the test for initial state, we need one for non initial state. |
||
|
||
$rootScope.obj = []; | ||
$rootScope.$digest(); | ||
expect(logb).toEqual(['"test"', '"test"']); | ||
expect(log).toEqual(['"test"', '[]']); | ||
|
||
$rootScope.obj = {}; | ||
$rootScope.$digest(); | ||
expect(logb).toEqual(['"test"', '"test"', '[]']); | ||
expect(log).toEqual(['"test"', '[]', '{}']); | ||
|
||
$rootScope.obj = []; | ||
$rootScope.$digest(); | ||
expect(logb).toEqual(['"test"', '"test"', '[]', '{}']); | ||
expect(log).toEqual(['"test"', '[]', '{}', '[]']); | ||
|
||
$rootScope.obj = undefined; | ||
$rootScope.$digest(); | ||
expect(logb).toEqual(['"test"', '"test"', '[]', '{}', '[]']); | ||
expect(log).toEqual(['"test"', '[]', '{}', '[]', undefined]); | ||
}); | ||
|
||
|
@@ -532,22 +561,27 @@ describe('Scope', function() { | |
|
||
$rootScope.obj.push('a'); | ||
$rootScope.$digest(); | ||
expect(logb).toEqual(['[]', '[]']); | ||
expect(log).toEqual(['[]', '["a"]']); | ||
|
||
$rootScope.obj[0] = 'b'; | ||
$rootScope.$digest(); | ||
expect(logb).toEqual(['[]', '[]', '["a"]']); | ||
expect(log).toEqual(['[]', '["a"]', '["b"]']); | ||
|
||
$rootScope.obj.push([]); | ||
$rootScope.obj.push({}); | ||
logb = []; | ||
log = []; | ||
$rootScope.$digest(); | ||
expect(logb).toEqual(['["b"]']); | ||
expect(log).toEqual(['["b",[],{}]']); | ||
|
||
var temp = $rootScope.obj[1]; | ||
$rootScope.obj[1] = $rootScope.obj[2]; | ||
$rootScope.obj[2] = temp; | ||
$rootScope.$digest(); | ||
expect(logb).toEqual([ '["b"]', '["b",[],{}]']); | ||
expect(log).toEqual([ '["b",[],{}]', '["b",{},[]]' ]); | ||
|
||
$rootScope.obj.shift(); | ||
|
@@ -583,6 +617,7 @@ describe('Scope', function() { | |
|
||
$rootScope.obj = {}; | ||
$rootScope.$digest(); | ||
expect(logb).toEqual(['"test"', '"test"']); | ||
expect(log).toEqual(['"test"', '{}']); | ||
}); | ||
|
||
|
@@ -605,27 +640,34 @@ describe('Scope', function() { | |
|
||
$rootScope.obj.a= 'A'; | ||
$rootScope.$digest(); | ||
expect(logb).toEqual(['{}', '{}']); | ||
expect(log).toEqual(['{}', '{"a":"A"}']); | ||
|
||
$rootScope.obj.a = 'B'; | ||
$rootScope.$digest(); | ||
expect(logb).toEqual(['{}', '{}', '{"a":"A"}']); | ||
expect(log).toEqual(['{}', '{"a":"A"}', '{"a":"B"}']); | ||
|
||
$rootScope.obj.b = []; | ||
$rootScope.obj.c = {}; | ||
log = []; | ||
logb = []; | ||
$rootScope.$digest(); | ||
expect(logb).toEqual(['{"a":"B"}']); | ||
expect(log).toEqual(['{"a":"B","b":[],"c":{}}']); | ||
|
||
var temp = $rootScope.obj.a; | ||
$rootScope.obj.a = $rootScope.obj.b; | ||
$rootScope.obj.c = temp; | ||
$rootScope.$digest(); | ||
expect(logb).toEqual([ '{"a":"B"}', '{"a":"B","b":[],"c":{}}']); | ||
expect(log).toEqual([ '{"a":"B","b":[],"c":{}}', '{"a":[],"b":[],"c":"B"}' ]); | ||
|
||
delete $rootScope.obj.a; | ||
logb = []; | ||
log = []; | ||
$rootScope.$digest(); | ||
expect(logb).toEqual([ '{"a":[],"b":[],"c":"B"}' ]); | ||
expect(log).toEqual([ '{"b":[],"c":"B"}' ]); | ||
}); | ||
}); | ||
|
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.
=
->===
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.
Igor,
I have simplified the old value checking. Let me know what you think.
On 16 March 2014 02:21, Igor Minar notifications@github.com wrote: