1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
|
'use strict';
describe('Scope', function(){
var root, mockHandler;
beforeEach(function(){
root = createScope(angular.service, {
$updateView: function(){
root.$flush();
},
'$exceptionHandler': $exceptionHandlerMockFactory()
});
mockHandler = root.$service('$exceptionHandler');
});
describe('$root', function(){
it('should point to itself', function(){
expect(root.$root).toEqual(root);
expect(root.hasOwnProperty('$root')).toBeTruthy();
});
it('should not have $root on children, but should inherit', function(){
var child = root.$new();
expect(child.$root).toEqual(root);
expect(child.hasOwnProperty('$root')).toBeFalsy();
});
});
describe('$parent', function(){
it('should point to itself in root', function(){
expect(root.$root).toEqual(root);
});
it('should point to parent', function(){
var child = root.$new();
expect(root.$parent).toEqual(null);
expect(child.$parent).toEqual(root);
expect(child.$new().$parent).toEqual(child);
});
});
describe('$id', function(){
it('should have a unique id', function(){
expect(root.$id < root.$new().$id).toBeTruthy();
});
});
describe('this', function(){
it('should have a \'this\'', function(){
expect(root['this']).toEqual(root);
});
});
describe('$new()', function(){
it('should create a child scope', function(){
var child = root.$new();
root.a = 123;
expect(child.a).toEqual(123);
});
it('should instantiate controller and bind functions', function(){
function Cntl($browser, name){
this.$browser = $browser;
this.callCount = 0;
this.name = name;
}
Cntl.$inject = ['$browser'];
Cntl.prototype = {
myFn: function(){
expect(this).toEqual(cntl);
this.callCount++;
}
};
var cntl = root.$new(Cntl, ['misko']);
expect(root.$browser).toBeUndefined();
expect(root.myFn).toBeUndefined();
expect(cntl.$browser).toBeDefined();
expect(cntl.name).toEqual('misko');
cntl.myFn();
cntl.$new().myFn();
expect(cntl.callCount).toEqual(2);
});
});
describe('$service', function(){
it('should have it on root', function(){
expect(root.hasOwnProperty('$service')).toBeTruthy();
});
});
describe('$watch/$digest', function(){
it('should watch and fire on simple property change', function(){
var spy = jasmine.createSpy();
root.$watch('name', spy);
expect(spy).not.wasCalled();
root.$digest();
expect(spy).not.wasCalled();
root.name = 'misko';
root.$digest();
expect(spy).wasCalledWith(root, 'misko', undefined);
});
it('should watch and fire on expression change', function(){
var spy = jasmine.createSpy();
root.$watch('name.first', spy);
root.name = {};
expect(spy).not.wasCalled();
root.$digest();
expect(spy).not.wasCalled();
root.name.first = 'misko';
root.$digest();
expect(spy).wasCalled();
});
it('should delegate exceptions', function(){
root.$watch('a', function(){throw new Error('abc');});
root.a = 1;
root.$digest();
expect(mockHandler.errors[0].message).toEqual('abc');
$logMock.error.logs.length = 0;
});
it('should fire watches in order of addition', function(){
// this is not an external guarantee, just our own sanity
var log = '';
root.$watch('a', function(){ log += 'a'; });
root.$watch('b', function(){ log += 'b'; });
root.$watch('c', function(){ log += 'c'; });
root.a = root.b = root.c = 1;
root.$digest();
expect(log).toEqual('abc');
});
it('should delegate $digest to children in addition order', function(){
// this is not an external guarantee, just our own sanity
var log = '';
var childA = root.$new();
var childB = root.$new();
var childC = root.$new();
childA.$watch('a', function(){ log += 'a'; });
childB.$watch('b', function(){ log += 'b'; });
childC.$watch('c', function(){ log += 'c'; });
childA.a = childB.b = childC.c = 1;
root.$digest();
expect(log).toEqual('abc');
});
it('should repeat watch cycle while model changes are identified', function(){
var log = '';
root.$watch('c', function(self, v){self.d = v; log+='c'; });
root.$watch('b', function(self, v){self.c = v; log+='b'; });
root.$watch('a', function(self, v){self.b = v; log+='a'; });
root.a = 1;
expect(root.$digest()).toEqual(3);
expect(root.b).toEqual(1);
expect(root.c).toEqual(1);
expect(root.d).toEqual(1);
expect(log).toEqual('abc');
});
it('should prevent infinite recursion', function(){
root.$watch('a', function(self, v){self.b++;});
root.$watch('b', function(self, v){self.a++;});
root.a = root.b = 0;
expect(function(){
root.$digest();
}).toThrow('100 $digest() iterations reached. Aborting!');
});
it('should not fire upon $watch registration on initial $digest', function(){
var log = '';
root.a = 1;
root.$watch('a', function(){ log += 'a'; });
root.$watch('b', function(){ log += 'b'; });
expect(log).toEqual('');
expect(root.$digest()).toEqual(0);
expect(log).toEqual('');
});
it('should return the listener to force a initial watch', function(){
var log = '';
root.a = 1;
root.$watch('a', function(scope, o1, o2){ log += scope.a + ':' + (o1 == o2 == 1) ; })();
expect(log).toEqual('1:true');
expect(root.$digest()).toEqual(0);
expect(log).toEqual('1:true');
});
it('should watch objects', function(){
var log = '';
root.a = [];
root.b = {};
root.$watch('a', function(){ log +='.';});
root.$watch('b', function(){ log +='!';});
root.$digest();
expect(log).toEqual('');
root.a.push({});
root.b.name = '';
root.$digest();
expect(log).toEqual('.!');
});
it('should prevent recursion', function(){
var callCount = 0;
root.$watch('name', function(){
expect(function(){
root.$digest();
}).toThrow('$digest already in progress');
expect(function(){
root.$flush();
}).toThrow('$digest already in progress');
callCount++;
});
root.name = 'a';
root.$digest();
expect(callCount).toEqual(1);
});
});
describe('$observe/$flush', function(){
it('should register simple property observer and fire on change', function(){
var spy = jasmine.createSpy();
root.$observe('name', spy);
expect(spy).not.wasCalled();
root.$flush();
expect(spy).wasCalled();
expect(spy.mostRecentCall.args[0]).toEqual(root);
expect(spy.mostRecentCall.args[1]).toEqual(undefined);
expect(spy.mostRecentCall.args[2].toString()).toEqual(NaN.toString());
root.name = 'misko';
root.$flush();
expect(spy).wasCalledWith(root, 'misko', undefined);
});
it('should register expression observers and fire them on change', function(){
var spy = jasmine.createSpy();
root.$observe('name.first', spy);
root.name = {};
expect(spy).not.wasCalled();
root.$flush();
expect(spy).wasCalled();
root.name.first = 'misko';
root.$flush();
expect(spy).wasCalled();
});
it('should delegate exceptions', function(){
root.$observe('a', function(){throw new Error('abc');});
root.a = 1;
root.$flush();
expect(mockHandler.errors[0].message).toEqual('abc');
$logMock.error.logs.shift();
});
it('should fire observers in order of addition', function(){
// this is not an external guarantee, just our own sanity
var log = '';
root.$observe('a', function(){ log += 'a'; });
root.$observe('b', function(){ log += 'b'; });
root.$observe('c', function(){ log += 'c'; });
root.a = root.b = root.c = 1;
root.$flush();
expect(log).toEqual('abc');
});
it('should delegate $flush to children in addition order', function(){
// this is not an external guarantee, just our own sanity
var log = '';
var childA = root.$new();
var childB = root.$new();
var childC = root.$new();
childA.$observe('a', function(){ log += 'a'; });
childB.$observe('b', function(){ log += 'b'; });
childC.$observe('c', function(){ log += 'c'; });
childA.a = childB.b = childC.c = 1;
root.$flush();
expect(log).toEqual('abc');
});
it('should fire observers once at beggining and on change', function(){
var log = '';
root.$observe('c', function(self, v){self.d = v; log += 'c';});
root.$observe('b', function(self, v){self.c = v; log += 'b';});
root.$observe('a', function(self, v){self.b = v; log += 'a';});
root.a = 1;
root.$flush();
expect(root.b).toEqual(1);
expect(log).toEqual('cba');
root.$flush();
expect(root.c).toEqual(1);
expect(log).toEqual('cbab');
root.$flush();
expect(root.d).toEqual(1);
expect(log).toEqual('cbabc');
});
it('should fire on initial observe', function(){
var log = '';
root.a = 1;
root.$observe('a', function(){ log += 'a'; });
root.$observe('b', function(){ log += 'b'; });
expect(log).toEqual('');
root.$flush();
expect(log).toEqual('ab');
});
it('should observe objects', function(){
var log = '';
root.a = [];
root.b = {};
root.$observe('a', function(){ log +='.';});
root.$observe('a', function(){ log +='!';});
root.$flush();
expect(log).toEqual('.!');
root.$flush();
expect(log).toEqual('.!');
root.a.push({});
root.b.name = '';
root.$digest();
expect(log).toEqual('.!');
});
it('should prevent recursion', function(){
var callCount = 0;
root.$observe('name', function(){
expect(function(){
root.$digest();
}).toThrow('$flush already in progress');
expect(function(){
root.$flush();
}).toThrow('$flush already in progress');
callCount++;
});
root.name = 'a';
root.$flush();
expect(callCount).toEqual(1);
});
});
describe('$destroy', function(){
var first, middle, last, log;
beforeEach(function(){
log = '';
first = root.$new();
middle = root.$new();
last = root.$new();
first.$watch(function(){ log += '1';});
middle.$watch(function(){ log += '2';});
last.$watch(function(){ log += '3';});
log = '';
});
it('should ignore remove on root', function(){
root.$destroy();
root.$digest();
expect(log).toEqual('123');
});
it('should remove first', function(){
first.$destroy();
root.$digest();
expect(log).toEqual('23');
});
it('should remove middle', function(){
middle.$destroy();
root.$digest();
expect(log).toEqual('13');
});
it('should remove last', function(){
last.$destroy();
root.$digest();
expect(log).toEqual('12');
});
});
describe('$eval', function(){
it('should eval an expression', function(){
expect(root.$eval('a=1')).toEqual(1);
expect(root.a).toEqual(1);
root.$eval(function(self){self.b=2;});
expect(root.b).toEqual(2);
});
});
describe('$apply', function(){
it('should apply expression with full lifecycle', function(){
var log = '';
var child = root.$new();
root.$watch('a', function(scope, a){ log += '1'; });
root.$observe('a', function(scope, a){ log += '2'; });
child.$apply('$parent.a=0');
expect(log).toEqual('12');
});
it('should catch exceptions', function(){
var log = '';
var child = root.$new();
root.$watch('a', function(scope, a){ log += '1'; });
root.$observe('a', function(scope, a){ log += '2'; });
root.a = 0;
child.$apply(function(){ throw new Error('MyError'); });
expect(log).toEqual('12');
expect(mockHandler.errors[0].message).toEqual('MyError');
$logMock.error.logs.shift();
});
describe('exceptions', function(){
var $exceptionHandler, $updateView, log;
beforeEach(function(){
log = '';
$exceptionHandler = jasmine.createSpy('$exceptionHandler');
$updateView = jasmine.createSpy('$updateView');
root.$service = function(name) {
return {$updateView:$updateView, $exceptionHandler:$exceptionHandler}[name];
};
root.$watch(function(){ log += '$digest;'; });
log = '';
});
it('should execute and return value and update', function(){
root.name = 'abc';
expect(root.$apply(function(scope){
return scope.name;
})).toEqual('abc');
expect(log).toEqual('$digest;');
expect($exceptionHandler).not.wasCalled();
expect($updateView).wasCalled();
});
it('should catch exception and update', function(){
var error = new Error('MyError');
root.$apply(function(){ throw error; });
expect(log).toEqual('$digest;');
expect($exceptionHandler).wasCalledWith(error);
expect($updateView).wasCalled();
});
});
});
});
|