@@ -362,6 +362,165 @@ describe('Scope', function() {
362
362
$rootScope . $digest ( ) ;
363
363
expect ( log ) . toEqual ( [ ] ) ;
364
364
} ) ) ;
365
+
366
+ describe ( '$watchCollection' , function ( ) {
367
+ var log , $rootScope , deregister ;
368
+
369
+ beforeEach ( inject ( function ( _$rootScope_ ) {
370
+ log = [ ] ;
371
+ $rootScope = _$rootScope_ ;
372
+ deregister = $rootScope . $watchCollection ( 'obj' , function logger ( obj ) {
373
+ log . push ( toJson ( obj ) ) ;
374
+ } ) ;
375
+ } ) ) ;
376
+
377
+
378
+ it ( 'should not trigger if nothing change' , inject ( function ( $rootScope ) {
379
+ $rootScope . $digest ( ) ;
380
+ expect ( log ) . toEqual ( [ undefined ] ) ;
381
+
382
+ $rootScope . $digest ( ) ;
383
+ expect ( log ) . toEqual ( [ undefined ] ) ;
384
+ } ) ) ;
385
+
386
+
387
+ it ( 'should allow deregistration' , inject ( function ( $rootScope ) {
388
+ $rootScope . obj = [ ] ;
389
+ $rootScope . $digest ( ) ;
390
+
391
+ expect ( log ) . toEqual ( [ '[]' ] ) ;
392
+
393
+ $rootScope . obj . push ( 'a' ) ;
394
+ deregister ( ) ;
395
+
396
+ $rootScope . $digest ( ) ;
397
+ expect ( log ) . toEqual ( [ '[]' ] ) ;
398
+ } ) ) ;
399
+
400
+
401
+ describe ( 'array' , function ( ) {
402
+ it ( 'should trigger when property changes into array' , function ( ) {
403
+ $rootScope . obj = 'test' ;
404
+ $rootScope . $digest ( ) ;
405
+ expect ( log ) . toEqual ( [ '"test"' ] ) ;
406
+
407
+ $rootScope . obj = [ ] ;
408
+ $rootScope . $digest ( ) ;
409
+ expect ( log ) . toEqual ( [ '"test"' , '[]' ] ) ;
410
+
411
+ $rootScope . obj = { } ;
412
+ $rootScope . $digest ( ) ;
413
+ expect ( log ) . toEqual ( [ '"test"' , '[]' , '{}' ] ) ;
414
+
415
+ $rootScope . obj = [ ] ;
416
+ $rootScope . $digest ( ) ;
417
+ expect ( log ) . toEqual ( [ '"test"' , '[]' , '{}' , '[]' ] ) ;
418
+
419
+ $rootScope . obj = undefined ;
420
+ $rootScope . $digest ( ) ;
421
+ expect ( log ) . toEqual ( [ '"test"' , '[]' , '{}' , '[]' , undefined ] ) ;
422
+ } ) ;
423
+
424
+
425
+ it ( 'should not trigger change when object in collection changes' , function ( ) {
426
+ $rootScope . obj = [ { } ] ;
427
+ $rootScope . $digest ( ) ;
428
+ expect ( log ) . toEqual ( [ '[{}]' ] ) ;
429
+
430
+ $rootScope . obj [ 0 ] . name = 'foo' ;
431
+ $rootScope . $digest ( ) ;
432
+ expect ( log ) . toEqual ( [ '[{}]' ] ) ;
433
+ } ) ;
434
+
435
+
436
+ it ( 'should watch array properties' , function ( ) {
437
+ $rootScope . obj = [ ] ;
438
+ $rootScope . $digest ( ) ;
439
+ expect ( log ) . toEqual ( [ '[]' ] ) ;
440
+
441
+ $rootScope . obj . push ( 'a' ) ;
442
+ $rootScope . $digest ( ) ;
443
+ expect ( log ) . toEqual ( [ '[]' , '["a"]' ] ) ;
444
+
445
+ $rootScope . obj [ 0 ] = 'b' ;
446
+ $rootScope . $digest ( ) ;
447
+ expect ( log ) . toEqual ( [ '[]' , '["a"]' , '["b"]' ] ) ;
448
+
449
+ $rootScope . obj . push ( [ ] ) ;
450
+ $rootScope . obj . push ( { } ) ;
451
+ log = [ ] ;
452
+ $rootScope . $digest ( ) ;
453
+ expect ( log ) . toEqual ( [ '["b",[],{}]' ] ) ;
454
+
455
+ var temp = $rootScope . obj [ 1 ] ;
456
+ $rootScope . obj [ 1 ] = $rootScope . obj [ 2 ] ;
457
+ $rootScope . obj [ 2 ] = temp ;
458
+ $rootScope . $digest ( ) ;
459
+ expect ( log ) . toEqual ( [ '["b",[],{}]' , '["b",{},[]]' ] ) ;
460
+
461
+ $rootScope . obj . shift ( )
462
+ log = [ ] ;
463
+ $rootScope . $digest ( ) ;
464
+ expect ( log ) . toEqual ( [ '[{},[]]' ] ) ;
465
+ } ) ;
466
+ } ) ;
467
+
468
+
469
+ describe ( 'object' , function ( ) {
470
+ it ( 'should trigger when property changes into object' , function ( ) {
471
+ $rootScope . obj = 'test' ;
472
+ $rootScope . $digest ( ) ;
473
+ expect ( log ) . toEqual ( [ '"test"' ] ) ;
474
+
475
+ $rootScope . obj = { } ;
476
+ $rootScope . $digest ( ) ;
477
+ expect ( log ) . toEqual ( [ '"test"' , '{}' ] ) ;
478
+ } ) ;
479
+
480
+
481
+ it ( 'should not trigger change when object in collection changes' , function ( ) {
482
+ $rootScope . obj = { name : { } } ;
483
+ $rootScope . $digest ( ) ;
484
+ expect ( log ) . toEqual ( [ '{"name":{}}' ] ) ;
485
+
486
+ $rootScope . obj . name . bar = 'foo' ;
487
+ $rootScope . $digest ( ) ;
488
+ expect ( log ) . toEqual ( [ '{"name":{}}' ] ) ;
489
+ } ) ;
490
+
491
+
492
+ it ( 'should watch object properties' , function ( ) {
493
+ $rootScope . obj = { } ;
494
+ $rootScope . $digest ( ) ;
495
+ expect ( log ) . toEqual ( [ '{}' ] ) ;
496
+
497
+ $rootScope . obj . a = 'A' ;
498
+ $rootScope . $digest ( ) ;
499
+ expect ( log ) . toEqual ( [ '{}' , '{"a":"A"}' ] ) ;
500
+
501
+ $rootScope . obj . a = 'B' ;
502
+ $rootScope . $digest ( ) ;
503
+ expect ( log ) . toEqual ( [ '{}' , '{"a":"A"}' , '{"a":"B"}' ] ) ;
504
+
505
+ $rootScope . obj . b = [ ] ;
506
+ $rootScope . obj . c = { } ;
507
+ log = [ ] ;
508
+ $rootScope . $digest ( ) ;
509
+ expect ( log ) . toEqual ( [ '{"a":"B","b":[],"c":{}}' ] ) ;
510
+
511
+ var temp = $rootScope . obj . a ;
512
+ $rootScope . obj . a = $rootScope . obj . b ;
513
+ $rootScope . obj . c = temp ;
514
+ $rootScope . $digest ( ) ;
515
+ expect ( log ) . toEqual ( [ '{"a":"B","b":[],"c":{}}' , '{"a":[],"b":[],"c":"B"}' ] ) ;
516
+
517
+ delete $rootScope . obj . a ;
518
+ log = [ ] ;
519
+ $rootScope . $digest ( ) ;
520
+ expect ( log ) . toEqual ( [ '{"b":[],"c":"B"}' ] ) ;
521
+ } )
522
+ } ) ;
523
+ } ) ;
365
524
} ) ;
366
525
367
526
0 commit comments