Skip to content

Commit 1b640f9

Browse files
jbedardpkozlowski-opensource
authored andcommitted
test($interpolate): adding tests for watching $interpolate functions
Closes angular#10119
1 parent 32806ca commit 1b640f9

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

test/ng/interpolateSpec.js

+68
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,74 @@ describe('$interpolate', function() {
5858
expect($interpolate("Hello, world!{{bloop}}")()).toBe("Hello, world!");
5959
}));
6060

61+
describe('watching', function() {
62+
it('should be watchable with any input types', inject(function($interpolate, $rootScope) {
63+
var lastVal;
64+
$rootScope.$watch($interpolate('{{i}}'), function(val) {
65+
lastVal = val;
66+
});
67+
$rootScope.$apply();
68+
expect(lastVal).toBe('');
69+
70+
$rootScope.i = null;
71+
$rootScope.$apply();
72+
expect(lastVal).toBe('');
73+
74+
$rootScope.i = '';
75+
$rootScope.$apply();
76+
expect(lastVal).toBe('');
77+
78+
$rootScope.i = 0;
79+
$rootScope.$apply();
80+
expect(lastVal).toBe('0');
81+
82+
$rootScope.i = [0];
83+
$rootScope.$apply();
84+
expect(lastVal).toBe('[0]');
85+
86+
$rootScope.i = {a: 1, b: 2};
87+
$rootScope.$apply();
88+
expect(lastVal).toBe('{"a":1,"b":2}');
89+
}));
90+
91+
it('should be watchable with literal values', inject(function($interpolate, $rootScope) {
92+
var lastVal;
93+
$rootScope.$watch($interpolate('{{1}}{{"2"}}{{true}}{{[false]}}{{ {a: 2} }}'), function(val) {
94+
lastVal = val;
95+
});
96+
$rootScope.$apply();
97+
expect(lastVal).toBe('12true[false]{"a":2}');
98+
99+
expect($rootScope.$countWatchers()).toBe(0);
100+
}));
101+
102+
it('should respect one-time bindings for each individual expression', inject(function($interpolate, $rootScope) {
103+
var calls = [];
104+
$rootScope.$watch($interpolate('{{::a | limitTo:1}} {{::s}} {{::i | number}}'), function(val) {
105+
calls.push(val);
106+
});
107+
108+
$rootScope.$apply();
109+
expect(calls.length).toBe(1);
110+
111+
$rootScope.a = [1];
112+
$rootScope.$apply();
113+
expect(calls.length).toBe(2);
114+
expect(calls[1]).toBe('[1] ');
115+
116+
$rootScope.a = [0];
117+
$rootScope.$apply();
118+
expect(calls.length).toBe(2);
119+
120+
$rootScope.i = $rootScope.a = 123;
121+
$rootScope.s = 'str!';
122+
$rootScope.$apply();
123+
expect(calls.length).toBe(3);
124+
expect(calls[2]).toBe('[1] str! 123');
125+
126+
expect($rootScope.$countWatchers()).toBe(0);
127+
}));
128+
});
61129

62130
describe('interpolation escaping', function() {
63131
var obj;

0 commit comments

Comments
 (0)