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

Commit aebf6b0

Browse files
committed
test($rootScope): add tests clarifying $watchGroup oldValues behavior
Closes #16024 BREAKING CHANGE (caused by c2b8fab) Previously the entries in `newValues` and `oldValues` reflected the differences in each *individual* espression, and not the difference of the values between each call of the listener. Now the entries in `oldValues` will equal the `newValues` of the previous call of the listener. This means comparing the entries in `newValues` and `oldValues` can be used to determine which individual expressions changed. For example `$scope.$watchGroup(['a', 'b'], fn)` would previously: | Action | newValue | oldValue | |----------|------------|------------| | (init) | [undefined, undefined] | [undefined, undefined] | | `a=1` | [1, undefined] | [undefined, undefined] | | `a=2` | [2, undefined] | [1, undefined] | | `b=3` | [2, 3] | [1, undefined] | Now the `oldValue` will always equal the previous `newValue`: | Action | newValue | oldValue | |----------|------------|------------| | (init) | [undefined, undefined] | [undefined, undefined] | | `a=1` | [1, undefined] | [undefined, undefined] | | `a=2` | [2, undefined] | [1, undefined] | | `b=3` | [2, 3] | [2, undefined] | Note the last call now shows `a === 2` in the `oldValues` array. This also makes the `oldValue` of one-time watchers more clear. Previously the `oldValue` of a one-time watcher would remain `undefined` forever. For example `$scope.$watchGroup(['a', '::b'], fn)` would previously: | Action | newValue | oldValue | |----------|------------|------------| | (init) | [undefined, undefined] | [undefined, undefined] | | `a=1` | [1, undefined] | [undefined, undefined] | | `b=2` | [1, 2] | [undefined, undefined] | | `a=b=3` | [3, 2] | [1, undefined] | Where now the `oldValue` will will lways equal the previous `newValue`: | Action | newValue | oldValue | |----------|------------|------------| | (init) | [undefined, undefined] | [undefined, undefined] | | `a=1` | [1, undefined] | [undefined, undefined] | | `b=2` | [1, 2] | [1, undefined] | | `a=b=3` | [3, 2] | [1, 2] |
1 parent a8bef95 commit aebf6b0

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

test/ng/rootScopeSpec.js

+109
Original file line numberDiff line numberDiff line change
@@ -1462,6 +1462,49 @@ describe('Scope', function() {
14621462
}));
14631463

14641464

1465+
it('should pass same group instance on first call (no expressions)', function() {
1466+
var newValues;
1467+
var oldValues;
1468+
scope.$watchGroup([], function(n, o) {
1469+
newValues = n;
1470+
oldValues = o;
1471+
});
1472+
1473+
scope.$apply();
1474+
expect(newValues).toBe(oldValues);
1475+
});
1476+
1477+
1478+
it('should pass same group instance on first call (single expression)', function() {
1479+
var newValues;
1480+
var oldValues;
1481+
scope.$watchGroup(['a'], function(n, o) {
1482+
newValues = n;
1483+
oldValues = o;
1484+
});
1485+
1486+
scope.$apply();
1487+
expect(newValues).toBe(oldValues);
1488+
1489+
scope.$apply('a = 1');
1490+
expect(newValues).not.toBe(oldValues);
1491+
});
1492+
1493+
it('should pass same group instance on first call (multiple expressions)', function() {
1494+
var newValues;
1495+
var oldValues;
1496+
scope.$watchGroup(['a', 'b'], function(n, o) {
1497+
newValues = n;
1498+
oldValues = o;
1499+
});
1500+
1501+
scope.$apply();
1502+
expect(newValues).toBe(oldValues);
1503+
1504+
scope.$apply('a = 1');
1505+
expect(newValues).not.toBe(oldValues);
1506+
});
1507+
14651508
it('should detect a change to any one expression in the group', function() {
14661509
scope.$watchGroup(['a', 'b'], function(values, oldValues, s) {
14671510
expect(s).toBe(scope);
@@ -1542,6 +1585,72 @@ describe('Scope', function() {
15421585
expect(log).toEqual('');
15431586
});
15441587

1588+
it('should have each individual old value equal to new values of previous watcher invocation', function() {
1589+
var newValues;
1590+
var oldValues;
1591+
scope.$watchGroup(['a', 'b'], function(n, o) {
1592+
newValues = n.slice();
1593+
oldValues = o.slice();
1594+
});
1595+
1596+
scope.$apply(); //skip the initial invocation
1597+
1598+
scope.$apply('a = 1');
1599+
expect(newValues).toEqual([1, undefined]);
1600+
expect(oldValues).toEqual([undefined, undefined]);
1601+
1602+
scope.$apply('a = 2');
1603+
expect(newValues).toEqual([2, undefined]);
1604+
expect(oldValues).toEqual([1, undefined]);
1605+
1606+
scope.$apply('b = 3');
1607+
expect(newValues).toEqual([2, 3]);
1608+
expect(oldValues).toEqual([2, undefined]);
1609+
1610+
scope.$apply('a = b = 4');
1611+
expect(newValues).toEqual([4, 4]);
1612+
expect(oldValues).toEqual([2, 3]);
1613+
1614+
scope.$apply('a = 5');
1615+
expect(newValues).toEqual([5, 4]);
1616+
expect(oldValues).toEqual([4, 4]);
1617+
1618+
scope.$apply('b = 6');
1619+
expect(newValues).toEqual([5, 6]);
1620+
expect(oldValues).toEqual([5, 4]);
1621+
});
1622+
1623+
1624+
it('should have each individual old value equal to new values of previous watcher invocation, with modifications from other watchers', function() {
1625+
scope.$watch('a', function() { scope.b++; });
1626+
scope.$watch('b', function() { scope.c++; });
1627+
1628+
var newValues;
1629+
var oldValues;
1630+
scope.$watchGroup(['a', 'b', 'c'], function(n, o) {
1631+
newValues = n.slice();
1632+
oldValues = o.slice();
1633+
});
1634+
1635+
scope.$apply(); //skip the initial invocation
1636+
1637+
scope.$apply('a = b = c = 1');
1638+
expect(newValues).toEqual([1, 2, 2]);
1639+
expect(oldValues).toEqual([undefined, NaN, NaN]);
1640+
1641+
scope.$apply('a = 3');
1642+
expect(newValues).toEqual([3, 3, 3]);
1643+
expect(oldValues).toEqual([1, 2, 2]);
1644+
1645+
scope.$apply('b = 5');
1646+
expect(newValues).toEqual([3, 5, 4]);
1647+
expect(oldValues).toEqual([3, 3, 3]);
1648+
1649+
scope.$apply('c = 7');
1650+
expect(newValues).toEqual([3, 5, 7]);
1651+
expect(oldValues).toEqual([3, 5, 4]);
1652+
});
1653+
15451654
it('should remove all watchers once one-time/constant bindings are stable', function() {
15461655
//empty
15471656
scope.$watchGroup([], noop);

0 commit comments

Comments
 (0)