@@ -4,6 +4,10 @@ describe('ngMessages', function() {
4
4
beforeEach ( inject . strictDi ( ) ) ;
5
5
beforeEach ( module ( 'ngMessages' ) ) ;
6
6
7
+ function messageChildren ( element ) {
8
+ return ( element . length ? element [ 0 ] : element ) . querySelectorAll ( '[ng-message], [ng-message-exp]' ) ;
9
+ }
10
+
7
11
function they ( msg , vals , spec , focus ) {
8
12
forEach ( vals , function ( val , key ) {
9
13
var m = msg . replace ( '$prop' , key ) ;
@@ -346,29 +350,29 @@ describe('ngMessages', function() {
346
350
] ;
347
351
} ) ;
348
352
349
- expect ( messageChildren ( ) . length ) . toBe ( 0 ) ;
353
+ expect ( messageChildren ( element ) . length ) . toBe ( 0 ) ;
350
354
expect ( trim ( element . text ( ) ) ) . toEqual ( "" ) ;
351
355
352
356
$rootScope . $apply ( function ( ) {
353
357
$rootScope . col = { hair : true } ;
354
358
} ) ;
355
359
356
- expect ( messageChildren ( ) . length ) . toBe ( 1 ) ;
360
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
357
361
expect ( trim ( element . text ( ) ) ) . toEqual ( "Your hair is too long" ) ;
358
362
359
363
$rootScope . $apply ( function ( ) {
360
364
$rootScope . col = { age : true , hair : true } ;
361
365
} ) ;
362
366
363
- expect ( messageChildren ( ) . length ) . toBe ( 1 ) ;
367
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
364
368
expect ( trim ( element . text ( ) ) ) . toEqual ( "Your age is incorrect" ) ;
365
369
366
370
$rootScope . $apply ( function ( ) {
367
371
// remove the age!
368
372
$rootScope . items . shift ( ) ;
369
373
} ) ;
370
374
371
- expect ( messageChildren ( ) . length ) . toBe ( 1 ) ;
375
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
372
376
expect ( trim ( element . text ( ) ) ) . toEqual ( "Your hair is too long" ) ;
373
377
374
378
$rootScope . $apply ( function ( ) {
@@ -377,12 +381,8 @@ describe('ngMessages', function() {
377
381
$rootScope . col . primary = true ;
378
382
} ) ;
379
383
380
- expect ( messageChildren ( ) . length ) . toBe ( 1 ) ;
384
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
381
385
expect ( trim ( element . text ( ) ) ) . toEqual ( "Enter something" ) ;
382
-
383
- function messageChildren ( ) {
384
- return element [ 0 ] . querySelectorAll ( '[ng-message], [ng-message-exp]' ) ;
385
- }
386
386
} ) ) ;
387
387
388
388
@@ -437,7 +437,7 @@ describe('ngMessages', function() {
437
437
} ;
438
438
} ) ;
439
439
440
- expect ( element . children ( ) . length ) . toBe ( 1 ) ;
440
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
441
441
expect ( trim ( element . text ( ) ) ) . toEqual ( "A" ) ;
442
442
443
443
$rootScope . $apply ( function ( ) {
@@ -446,11 +446,108 @@ describe('ngMessages', function() {
446
446
} ;
447
447
} ) ;
448
448
449
- expect ( element . children ( ) . length ) . toBe ( 1 ) ;
449
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
450
450
expect ( trim ( element . text ( ) ) ) . toEqual ( "C" ) ;
451
451
} ) ;
452
452
} ) ;
453
453
454
+ they ( 'should work with a dynamic collection model which is managed by ngRepeat' ,
455
+ { '<div ng-messages-include="...">' : '<div ng-messages="item">' +
456
+ '<div ng-messages-include="abc.html"></div>' +
457
+ '</div>' ,
458
+ '<ng-messages-include src="...">' : '<ng-messages for="item">' +
459
+ '<ng-messages-include src="abc.html"></ng-messages-include>' +
460
+ '</ng-messages>' } ,
461
+ function ( html ) {
462
+ inject ( function ( $compile , $rootScope , $templateCache ) {
463
+ $templateCache . put ( 'abc.html' , '<div ng-message="a">A</div>' +
464
+ '<div ng-message="b">B</div>' +
465
+ '<div ng-message="c">C</div>' ) ;
466
+
467
+ html = '<div><div ng-repeat="item in items">' + html + '</div></div>' ;
468
+ $rootScope . items = [ { } , { } , { } ] ;
469
+
470
+ element = $compile ( html ) ( $rootScope ) ;
471
+ $rootScope . $apply ( function ( ) {
472
+ $rootScope . items [ 0 ] . a = true ;
473
+ $rootScope . items [ 1 ] . b = true ;
474
+ $rootScope . items [ 2 ] . c = true ;
475
+ } ) ;
476
+
477
+ var elements = element [ 0 ] . querySelectorAll ( '[ng-repeat]' ) ;
478
+
479
+ expect ( messageChildren ( element ) . length ) . toBe ( 3 ) ;
480
+ expect ( messageChildren ( elements [ 0 ] ) . length ) . toBe ( 1 ) ;
481
+ expect ( messageChildren ( elements [ 1 ] ) . length ) . toBe ( 1 ) ;
482
+ expect ( messageChildren ( elements [ 2 ] ) . length ) . toBe ( 1 ) ;
483
+
484
+ expect ( element . text ( ) . trim ( ) ) . toBe ( 'ABC' ) ;
485
+
486
+ $rootScope . $apply ( function ( ) {
487
+ $rootScope . items [ 0 ] . a = false ;
488
+ $rootScope . items [ 0 ] . c = true ;
489
+
490
+ $rootScope . items [ 1 ] . b = false ;
491
+
492
+ $rootScope . items [ 2 ] . c = false ;
493
+ $rootScope . items [ 2 ] . a = true ;
494
+ } ) ;
495
+
496
+ expect ( element . text ( ) . trim ( ) ) . toBe ( 'CA' ) ;
497
+
498
+ $rootScope . $apply ( function ( ) {
499
+ $rootScope . items [ 1 ] . b = true ;
500
+ $rootScope . items . reverse ( ) ;
501
+ } ) ;
502
+
503
+ expect ( element . text ( ) . trim ( ) ) . toBe ( 'ABC' ) ;
504
+ } ) ;
505
+ } ) ;
506
+
507
+ they ( 'should retain the messages include container within the children of ngMessages and make sure the order is the same' ,
508
+ { '<div ng-messages-include="...">' : '<div ng-messages="data" ng-messages-multiple="true">' +
509
+ '<div ng-message="0">0</div>' +
510
+ '<div ng-messages-include="abc.html"></div>' +
511
+ '<div ng-message="X">X</div>' +
512
+ '</div>' ,
513
+ '<ng-messages-include src="...">' : '<ng-messages for="data" multiple="true">' +
514
+ '<div ng-message="0">0</div>' +
515
+ '<ng-messages-include src="abc.html"></ng-messages-include>' +
516
+ '<div ng-message="X">X</div>' +
517
+ '</ng-messages>' } ,
518
+ function ( html ) {
519
+ inject ( function ( $compile , $rootScope , $templateCache ) {
520
+ $templateCache . put ( 'abc.html' , '<div ng-message="a">A</div>' +
521
+ '<div ng-message="b">B</div>' +
522
+ '<div ng-message="c">C</div>' ) ;
523
+
524
+ element = $compile ( html ) ( $rootScope ) ;
525
+
526
+ var includeContainer = element [ 0 ] . querySelector ( 'ng-messages-include, [ng-messages-include]' ) ;
527
+ expect ( includeContainer ) . toBeTruthy ( ) ;
528
+ expect ( includeContainer . parentNode ) . toBe ( element [ 0 ] ) ;
529
+
530
+ $rootScope . $apply ( function ( ) {
531
+ $rootScope . data = {
532
+ '0' : true ,
533
+ 'a' : 1 ,
534
+ 'b' : 2 ,
535
+ 'c' : 3 ,
536
+ 'X' : true
537
+ } ;
538
+ } ) ;
539
+
540
+ expect ( messageChildren ( includeContainer ) . length ) . toBe ( 3 ) ;
541
+ expect ( messageChildren ( element ) . length ) . toBe ( 5 ) ;
542
+
543
+ var children = element . children ( ) ;
544
+ var firstElement = children [ 0 ] ;
545
+ var lastElement = children [ children . length - 1 ] ;
546
+ expect ( firstElement ) . toBe ( includeContainer . previousElementSibling ) ;
547
+ expect ( lastElement ) . toBe ( includeContainer . nextElementSibling ) ;
548
+ } ) ;
549
+ } ) ;
550
+
454
551
it ( 'should cache the template after download' ,
455
552
inject ( function ( $rootScope , $compile , $templateCache , $httpBackend ) {
456
553
@@ -484,13 +581,13 @@ describe('ngMessages', function() {
484
581
485
582
$rootScope . $digest ( ) ;
486
583
487
- expect ( element . children ( ) . length ) . toBe ( 1 ) ;
584
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
488
585
expect ( trim ( element . text ( ) ) ) . toEqual ( "Your value is that of failure" ) ;
489
586
490
587
$httpBackend . flush ( ) ;
491
588
$rootScope . $digest ( ) ;
492
589
493
- expect ( element . children ( ) . length ) . toBe ( 1 ) ;
590
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
494
591
expect ( trim ( element . text ( ) ) ) . toEqual ( "You did not enter a value" ) ;
495
592
} ) ) ;
496
593
@@ -515,7 +612,7 @@ describe('ngMessages', function() {
515
612
} ;
516
613
} ) ;
517
614
518
- expect ( element . children ( ) . length ) . toBe ( 1 ) ;
615
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
519
616
expect ( trim ( element . text ( ) ) ) . toEqual ( "AAA" ) ;
520
617
521
618
$rootScope . $apply ( function ( ) {
@@ -525,7 +622,7 @@ describe('ngMessages', function() {
525
622
} ;
526
623
} ) ;
527
624
528
- expect ( element . children ( ) . length ) . toBe ( 1 ) ;
625
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
529
626
expect ( trim ( element . text ( ) ) ) . toEqual ( "B" ) ;
530
627
531
628
$rootScope . $apply ( function ( ) {
@@ -534,7 +631,7 @@ describe('ngMessages', function() {
534
631
} ;
535
632
} ) ;
536
633
537
- expect ( element . children ( ) . length ) . toBe ( 1 ) ;
634
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
538
635
expect ( trim ( element . text ( ) ) ) . toEqual ( "C" ) ;
539
636
} ) ) ;
540
637
@@ -584,14 +681,14 @@ describe('ngMessages', function() {
584
681
} ;
585
682
} ) ;
586
683
587
- expect ( element . children ( ) . length ) . toBe ( 2 ) ;
684
+ expect ( messageChildren ( element ) . length ) . toBe ( 2 ) ;
588
685
expect ( s ( element . text ( ) ) ) . toEqual ( "XZ" ) ;
589
686
590
687
$rootScope . $apply ( function ( ) {
591
688
$rootScope . data . y = { } ;
592
689
} ) ;
593
690
594
- expect ( element . children ( ) . length ) . toBe ( 3 ) ;
691
+ expect ( messageChildren ( element ) . length ) . toBe ( 3 ) ;
595
692
expect ( s ( element . text ( ) ) ) . toEqual ( "XYZ" ) ;
596
693
} ) ) ;
597
694
@@ -616,14 +713,14 @@ describe('ngMessages', function() {
616
713
} ;
617
714
} ) ;
618
715
619
- expect ( element . children ( ) . length ) . toBe ( 2 ) ;
716
+ expect ( messageChildren ( element ) . length ) . toBe ( 2 ) ;
620
717
expect ( s ( element . text ( ) ) ) . toEqual ( "ZZZX" ) ;
621
718
622
719
$rootScope . $apply ( function ( ) {
623
720
$rootScope . data . y = { } ;
624
721
} ) ;
625
722
626
- expect ( element . children ( ) . length ) . toBe ( 3 ) ;
723
+ expect ( messageChildren ( element ) . length ) . toBe ( 3 ) ;
627
724
expect ( s ( element . text ( ) ) ) . toEqual ( "YYYZZZX" ) ;
628
725
} ) ) ;
629
726
} ) ;
0 commit comments