@@ -364,179 +364,204 @@ describe('Scope', function() {
364
364
} ) ) ;
365
365
366
366
describe ( '$watchCollection' , function ( ) {
367
- var log , $rootScope , deregister ;
367
+ describe ( '(newCollection tests)' , function ( ) {
368
+ var log , $rootScope , deregister ;
368
369
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 ] ) ;
370
+ beforeEach ( inject ( function ( _$rootScope_ ) {
371
+ log = [ ] ;
372
+ $rootScope = _$rootScope_ ;
373
+ deregister = $rootScope . $watchCollection ( 'obj' , function logger ( newCollection ) {
374
+ log . push ( toJson ( newCollection ) ) ;
375
+ } ) ;
376
+ } ) ) ;
381
377
382
- $rootScope . $digest ( ) ;
383
- expect ( log ) . toEqual ( [ undefined ] ) ;
384
- } ) ) ;
385
378
379
+ it ( 'should not trigger if nothing change' , inject ( function ( $rootScope ) {
380
+ $rootScope . $digest ( ) ;
381
+ expect ( log ) . toEqual ( [ undefined ] ) ;
386
382
387
- it ( 'should allow deregistration' , inject ( function ( $rootScope ) {
388
- $rootScope . obj = [ ] ;
389
- $rootScope . $digest ( ) ;
383
+ $rootScope . $digest ( ) ;
384
+ expect ( log ) . toEqual ( [ undefined ] ) ;
385
+ } ) ) ;
390
386
391
- expect ( log ) . toEqual ( [ '[]' ] ) ;
392
387
393
- $rootScope . obj . push ( 'a' ) ;
394
- deregister ( ) ;
388
+ it ( 'should allow deregistration' , inject ( function ( $rootScope ) {
389
+ $rootScope . obj = [ ] ;
390
+ $rootScope . $digest ( ) ;
395
391
396
- $rootScope . $digest ( ) ;
397
- expect ( log ) . toEqual ( [ '[]' ] ) ;
398
- } ) ) ;
392
+ expect ( log ) . toEqual ( [ '[]' ] ) ;
399
393
394
+ $rootScope . obj . push ( 'a' ) ;
395
+ deregister ( ) ;
400
396
401
- describe ( 'array' , function ( ) {
402
- it ( 'should trigger when property changes into array' , function ( ) {
403
- $rootScope . obj = 'test' ;
404
397
$rootScope . $digest ( ) ;
405
- expect ( log ) . toEqual ( [ '"test"' ] ) ;
398
+ expect ( log ) . toEqual ( [ '[]' ] ) ;
399
+ } ) ) ;
406
400
407
- $rootScope . obj = [ ] ;
408
- $rootScope . $digest ( ) ;
409
- expect ( log ) . toEqual ( [ '"test"' , '[]' ] ) ;
410
401
411
- $rootScope . obj = { } ;
412
- $rootScope . $digest ( ) ;
413
- expect ( log ) . toEqual ( [ '"test"' , '[]' , '{}' ] ) ;
402
+ describe ( 'array' , function ( ) {
403
+ it ( 'should trigger when property changes into array' , function ( ) {
404
+ $rootScope . obj = 'test' ;
405
+ $rootScope . $digest ( ) ;
406
+ expect ( log ) . toEqual ( [ '"test"' ] ) ;
414
407
415
- $rootScope . obj = [ ] ;
416
- $rootScope . $digest ( ) ;
417
- expect ( log ) . toEqual ( [ '"test"' , '[]' , '{} ', '[]' ] ) ;
408
+ $rootScope . obj = [ ] ;
409
+ $rootScope . $digest ( ) ;
410
+ expect ( log ) . toEqual ( [ '"test"' , '[]' ] ) ;
418
411
419
- $rootScope . obj = undefined ;
420
- $rootScope . $digest ( ) ;
421
- expect ( log ) . toEqual ( [ '"test"' , '[]' , '{}' , '[]' , undefined ] ) ;
422
- } ) ;
412
+ $rootScope . obj = { } ;
413
+ $rootScope . $digest ( ) ;
414
+ expect ( log ) . toEqual ( [ '"test"' , '[]' , '{}' ] ) ;
423
415
416
+ $rootScope . obj = [ ] ;
417
+ $rootScope . $digest ( ) ;
418
+ expect ( log ) . toEqual ( [ '"test"' , '[]' , '{}' , '[]' ] ) ;
424
419
425
- it ( 'should not trigger change when object in collection changes' , function ( ) {
426
- $rootScope . obj = [ { } ] ;
427
- $rootScope . $digest ( ) ;
428
- expect ( log ) . toEqual ( [ '[{}]' ] ) ;
420
+ $rootScope . obj = undefined ;
421
+ $rootScope . $digest ( ) ;
422
+ expect ( log ) . toEqual ( [ '"test"' , '[]' , '{}' , '[]' , undefined ] ) ;
423
+ } ) ;
429
424
430
- $rootScope . obj [ 0 ] . name = 'foo' ;
431
- $rootScope . $digest ( ) ;
432
- expect ( log ) . toEqual ( [ '[{}]' ] ) ;
433
- } ) ;
434
425
426
+ it ( 'should not trigger change when object in collection changes' , function ( ) {
427
+ $rootScope . obj = [ { } ] ;
428
+ $rootScope . $digest ( ) ;
429
+ expect ( log ) . toEqual ( [ '[{}]' ] ) ;
435
430
436
- it ( 'should watch array properties' , function ( ) {
437
- $rootScope . obj = [ ] ;
438
- $rootScope . $digest ( ) ;
439
- expect ( log ) . toEqual ( [ '[]' ] ) ;
431
+ $rootScope . obj [ 0 ] . name = 'foo' ;
432
+ $rootScope . $digest ( ) ;
433
+ expect ( log ) . toEqual ( [ '[{}]' ] ) ;
434
+ } ) ;
440
435
441
- $rootScope . obj . push ( 'a' ) ;
442
- $rootScope . $digest ( ) ;
443
- expect ( log ) . toEqual ( [ '[]' , '["a"]' ] ) ;
444
436
445
- $rootScope . obj [ 0 ] = 'b' ;
446
- $rootScope . $digest ( ) ;
447
- expect ( log ) . toEqual ( [ '[]' , '["a"]' , '["b"]' ] ) ;
437
+ it ( 'should watch array properties' , function ( ) {
438
+ $rootScope . obj = [ ] ;
439
+ $rootScope . $digest ( ) ;
440
+ expect ( log ) . toEqual ( [ '[]' ] ) ;
448
441
449
- $rootScope . obj . push ( [ ] ) ;
450
- $rootScope . obj . push ( { } ) ;
451
- log = [ ] ;
452
- $rootScope . $digest ( ) ;
453
- expect ( log ) . toEqual ( [ '["b",[],{}]' ] ) ;
442
+ $rootScope . obj . push ( 'a' ) ;
443
+ $rootScope . $digest ( ) ;
444
+ expect ( log ) . toEqual ( [ '[]' , '["a"]' ] ) ;
454
445
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",{},[]]' ] ) ;
446
+ $rootScope . obj [ 0 ] = 'b' ;
447
+ $rootScope . $digest ( ) ;
448
+ expect ( log ) . toEqual ( [ '[]' , '["a"]' , '["b"]' ] ) ;
460
449
461
- $rootScope . obj . shift ( )
462
- log = [ ] ;
463
- $rootScope . $digest ( ) ;
464
- expect ( log ) . toEqual ( [ '[{},[]]' ] ) ;
465
- } ) ;
450
+ $rootScope . obj . push ( [ ] ) ;
451
+ $rootScope . obj . push ( { } ) ;
452
+ log = [ ] ;
453
+ $rootScope . $digest ( ) ;
454
+ expect ( log ) . toEqual ( [ '["b",[],{}]' ] ) ;
466
455
467
- it ( 'should watch array-like objects like arrays' , function ( ) {
468
- var arrayLikelog = [ ] ;
469
- $rootScope . $watchCollection ( 'arrayLikeObject' , function logger ( obj ) {
470
- forEach ( obj , function ( element ) {
471
- arrayLikelog . push ( element . name ) ;
472
- } )
456
+ var temp = $rootScope . obj [ 1 ] ;
457
+ $rootScope . obj [ 1 ] = $rootScope . obj [ 2 ] ;
458
+ $rootScope . obj [ 2 ] = temp ;
459
+ $rootScope . $digest ( ) ;
460
+ expect ( log ) . toEqual ( [ '["b",[],{}]' , '["b",{},[]]' ] ) ;
461
+
462
+ $rootScope . obj . shift ( )
463
+ log = [ ] ;
464
+ $rootScope . $digest ( ) ;
465
+ expect ( log ) . toEqual ( [ '[{},[]]' ] ) ;
473
466
} ) ;
474
- document . body . innerHTML = "<p>" +
475
- "<a name='x'>a</a>" +
476
- "<a name='y'>b</a>" +
477
- "</p>" ;
478
467
479
- $rootScope . arrayLikeObject = document . getElementsByTagName ( 'a' )
480
- $rootScope . $digest ( ) ;
481
- expect ( arrayLikelog ) . toEqual ( [ 'x' , 'y' ] ) ;
468
+ it ( 'should watch array-like objects like arrays' , function ( ) {
469
+ var arrayLikelog = [ ] ;
470
+ $rootScope . $watchCollection ( 'arrayLikeObject' , function logger ( obj ) {
471
+ forEach ( obj , function ( element ) {
472
+ arrayLikelog . push ( element . name ) ;
473
+ } )
474
+ } ) ;
475
+ document . body . innerHTML = "<p>" +
476
+ "<a name='x'>a</a>" +
477
+ "<a name='y'>b</a>" +
478
+ "</p>" ;
479
+
480
+ $rootScope . arrayLikeObject = document . getElementsByTagName ( 'a' )
481
+ $rootScope . $digest ( ) ;
482
+ expect ( arrayLikelog ) . toEqual ( [ 'x' , 'y' ] ) ;
483
+ } ) ;
482
484
} ) ;
483
- } ) ;
484
485
485
486
486
- describe ( 'object' , function ( ) {
487
- it ( 'should trigger when property changes into object' , function ( ) {
488
- $rootScope . obj = 'test' ;
489
- $rootScope . $digest ( ) ;
490
- expect ( log ) . toEqual ( [ '"test"' ] ) ;
487
+ describe ( 'object' , function ( ) {
488
+ it ( 'should trigger when property changes into object' , function ( ) {
489
+ $rootScope . obj = 'test' ;
490
+ $rootScope . $digest ( ) ;
491
+ expect ( log ) . toEqual ( [ '"test"' ] ) ;
491
492
492
- $rootScope . obj = { } ;
493
- $rootScope . $digest ( ) ;
494
- expect ( log ) . toEqual ( [ '"test"' , '{}' ] ) ;
495
- } ) ;
493
+ $rootScope . obj = { } ;
494
+ $rootScope . $digest ( ) ;
495
+ expect ( log ) . toEqual ( [ '"test"' , '{}' ] ) ;
496
+ } ) ;
496
497
497
498
498
- it ( 'should not trigger change when object in collection changes' , function ( ) {
499
- $rootScope . obj = { name : { } } ;
500
- $rootScope . $digest ( ) ;
501
- expect ( log ) . toEqual ( [ '{"name":{}}' ] ) ;
499
+ it ( 'should not trigger change when object in collection changes' , function ( ) {
500
+ $rootScope . obj = { name : { } } ;
501
+ $rootScope . $digest ( ) ;
502
+ expect ( log ) . toEqual ( [ '{"name":{}}' ] ) ;
502
503
503
- $rootScope . obj . name . bar = 'foo' ;
504
- $rootScope . $digest ( ) ;
505
- expect ( log ) . toEqual ( [ '{"name":{}}' ] ) ;
506
- } ) ;
504
+ $rootScope . obj . name . bar = 'foo' ;
505
+ $rootScope . $digest ( ) ;
506
+ expect ( log ) . toEqual ( [ '{"name":{}}' ] ) ;
507
+ } ) ;
507
508
508
509
509
- it ( 'should watch object properties' , function ( ) {
510
- $rootScope . obj = { } ;
511
- $rootScope . $digest ( ) ;
512
- expect ( log ) . toEqual ( [ '{}' ] ) ;
510
+ it ( 'should watch object properties' , function ( ) {
511
+ $rootScope . obj = { } ;
512
+ $rootScope . $digest ( ) ;
513
+ expect ( log ) . toEqual ( [ '{}' ] ) ;
514
+
515
+ $rootScope . obj . a = 'A' ;
516
+ $rootScope . $digest ( ) ;
517
+ expect ( log ) . toEqual ( [ '{}' , '{"a":"A"}' ] ) ;
518
+
519
+ $rootScope . obj . a = 'B' ;
520
+ $rootScope . $digest ( ) ;
521
+ expect ( log ) . toEqual ( [ '{}' , '{"a":"A"}' , '{"a":"B"}' ] ) ;
522
+
523
+ $rootScope . obj . b = [ ] ;
524
+ $rootScope . obj . c = { } ;
525
+ log = [ ] ;
526
+ $rootScope . $digest ( ) ;
527
+ expect ( log ) . toEqual ( [ '{"a":"B","b":[],"c":{}}' ] ) ;
528
+
529
+ var temp = $rootScope . obj . a ;
530
+ $rootScope . obj . a = $rootScope . obj . b ;
531
+ $rootScope . obj . c = temp ;
532
+ $rootScope . $digest ( ) ;
533
+ expect ( log ) . toEqual ( [ '{"a":"B","b":[],"c":{}}' , '{"a":[],"b":[],"c":"B"}' ] ) ;
534
+
535
+ delete $rootScope . obj . a ;
536
+ log = [ ] ;
537
+ $rootScope . $digest ( ) ;
538
+ expect ( log ) . toEqual ( [ '{"b":[],"c":"B"}' ] ) ;
539
+ } )
540
+ } ) ;
541
+ } ) ;
513
542
514
- $rootScope . obj . a = 'A' ;
515
- $rootScope . $digest ( ) ;
516
- expect ( log ) . toEqual ( [ '{}' , '{"a":"A"}' ] ) ;
543
+ describe ( 'newCollection and oldCollection' , function ( ) {
544
+ it ( 'should have different contents' , inject ( function ( $rootScope ) {
545
+ var apples = { 'item' : 'apples' } ;
546
+ var blackberries = { 'item' : 'blackberries' } ;
517
547
518
- $rootScope . obj . a = 'B' ;
519
- $rootScope . $digest ( ) ;
520
- expect ( log ) . toEqual ( [ '{}' , '{"a":"A"}' , '{"a":"B"}' ] ) ;
548
+ $rootScope . obj = [ apples ] ;
521
549
522
- $rootScope . obj . b = [ ] ;
523
- $rootScope . obj . c = { } ;
524
- log = [ ] ;
525
- $rootScope . $digest ( ) ;
526
- expect ( log ) . toEqual ( [ '{"a":"B","b":[],"c":{}}' ] ) ;
550
+ $rootScope . $watchCollection ( 'obj' , function ( newCollection , oldCollection ) {
551
+ if ( oldCollection ) {
552
+ expect ( oldCollection . length ) . toEqual ( 1 ) ;
553
+ expect ( newCollection . length ) . toEqual ( 2 ) ;
527
554
528
- var temp = $rootScope . obj . a ;
529
- $rootScope . obj . a = $rootScope . obj . b ;
530
- $rootScope . obj . c = temp ;
531
- $rootScope . $digest ( ) ;
532
- expect ( log ) . toEqual ( [ '{"a":"B","b":[],"c":{}}' , '{"a":[],"b":[],"c":"B"}' ] ) ;
555
+ expect ( oldCollection [ 0 ] ) . toEqual ( apples ) ;
556
+ expect ( newCollection [ 0 ] ) . toEqual ( apples ) ;
557
+ expect ( newCollection [ 1 ] ) . toEqual ( blackberries ) ;
558
+ }
559
+ } ) ;
533
560
534
- delete $rootScope . obj . a ;
535
- log = [ ] ;
536
- $rootScope . $digest ( ) ;
537
- expect ( log ) . toEqual ( [ '{"b":[],"c":"B"}' ] ) ;
538
- } )
539
- } ) ;
561
+ $rootScope . obj . push ( blackberries ) ;
562
+ $rootScope . digest ( ) ;
563
+ } ) )
564
+ } )
540
565
} ) ;
541
566
} ) ;
542
567
0 commit comments