@@ -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 ) ;
@@ -236,7 +240,7 @@ describe('ngMessages', function() {
236
240
$rootScope . col = { } ;
237
241
} ) ;
238
242
239
- expect ( element . children ( ) . length ) . toBe ( 0 ) ;
243
+ expect ( messageChildren ( element ) . length ) . toBe ( 0 ) ;
240
244
expect ( trim ( element . text ( ) ) ) . toEqual ( '' ) ;
241
245
242
246
$rootScope . $apply ( function ( ) {
@@ -246,7 +250,7 @@ describe('ngMessages', function() {
246
250
} ;
247
251
} ) ;
248
252
249
- expect ( element . children ( ) . length ) . toBe ( 1 ) ;
253
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
250
254
expect ( trim ( element . text ( ) ) ) . toEqual ( 'This message is blue' ) ;
251
255
252
256
$rootScope . $apply ( function ( ) {
@@ -255,13 +259,13 @@ describe('ngMessages', function() {
255
259
} ;
256
260
} ) ;
257
261
258
- expect ( element . children ( ) . length ) . toBe ( 1 ) ;
262
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
259
263
expect ( trim ( element . text ( ) ) ) . toEqual ( 'This message is red' ) ;
260
264
261
265
$rootScope . $apply ( function ( ) {
262
266
$rootScope . col = null ;
263
267
} ) ;
264
- expect ( element . children ( ) . length ) . toBe ( 0 ) ;
268
+ expect ( messageChildren ( element ) . length ) . toBe ( 0 ) ;
265
269
expect ( trim ( element . text ( ) ) ) . toEqual ( '' ) ;
266
270
267
271
@@ -272,7 +276,7 @@ describe('ngMessages', function() {
272
276
} ;
273
277
} ) ;
274
278
275
- expect ( element . children ( ) . length ) . toBe ( 0 ) ;
279
+ expect ( messageChildren ( element ) . length ) . toBe ( 0 ) ;
276
280
expect ( trim ( element . text ( ) ) ) . toEqual ( '' ) ;
277
281
} ) ;
278
282
} ) ;
@@ -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
@@ -415,6 +415,88 @@ describe('ngMessages', function() {
415
415
} ) ;
416
416
417
417
describe ( 'when including templates' , function ( ) {
418
+ they ( 'should work with a dynamic collection model which is managed by ngRepeat' ,
419
+ { '<div ng-messages-include="...">' : '<div ng-messages="item">' +
420
+ '<div ng-messages-include="abc.html"></div>' +
421
+ '</div>' ,
422
+ '<ng-messages-include src="...">' : '<ng-messages for="item">' +
423
+ '<ng-messages-include src="abc.html"></ng-messages-include>' +
424
+ '</ng-messages>' } ,
425
+ function ( html ) {
426
+ inject ( function ( $compile , $rootScope , $templateCache ) {
427
+ $templateCache . put ( 'abc.html' , '<div ng-message="a">A</div>' +
428
+ '<div ng-message="b">B</div>' +
429
+ '<div ng-message="c">C</div>' ) ;
430
+
431
+ html = '<div><div ng-repeat="item in items">' + html + '</div></div>' ;
432
+ $rootScope . items = [ { } , { } , { } ] ;
433
+
434
+ element = $compile ( html ) ( $rootScope ) ;
435
+ $rootScope . $apply ( function ( ) {
436
+ $rootScope . items [ 0 ] . a = true ;
437
+ $rootScope . items [ 1 ] . b = true ;
438
+ $rootScope . items [ 2 ] . c = true ;
439
+ } ) ;
440
+
441
+ var elements = element [ 0 ] . querySelectorAll ( '[ng-repeat]' ) ;
442
+
443
+ // all three collections should have atleast one error showing up
444
+ expect ( messageChildren ( element ) . length ) . toBe ( 3 ) ;
445
+ expect ( messageChildren ( elements [ 0 ] ) . length ) . toBe ( 1 ) ;
446
+ expect ( messageChildren ( elements [ 1 ] ) . length ) . toBe ( 1 ) ;
447
+ expect ( messageChildren ( elements [ 2 ] ) . length ) . toBe ( 1 ) ;
448
+
449
+ // this is the standard order of the displayed error messages
450
+ expect ( element . text ( ) . trim ( ) ) . toBe ( 'ABC' ) ;
451
+
452
+ $rootScope . $apply ( function ( ) {
453
+ $rootScope . items [ 0 ] . a = false ;
454
+ $rootScope . items [ 0 ] . c = true ;
455
+
456
+ $rootScope . items [ 1 ] . b = false ;
457
+
458
+ $rootScope . items [ 2 ] . c = false ;
459
+ $rootScope . items [ 2 ] . a = true ;
460
+ } ) ;
461
+
462
+ // with the 2nd item gone and the values changed
463
+ // we should see both 1 and 3 changed
464
+ expect ( element . text ( ) . trim ( ) ) . toBe ( 'CA' ) ;
465
+
466
+ $rootScope . $apply ( function ( ) {
467
+ // add the value for the 2nd item back
468
+ $rootScope . items [ 1 ] . b = true ;
469
+ $rootScope . items . reverse ( ) ;
470
+ } ) ;
471
+
472
+ // when reversed we get back to our original value
473
+ expect ( element . text ( ) . trim ( ) ) . toBe ( 'ABC' ) ;
474
+ } ) ;
475
+ } ) ;
476
+
477
+ they ( 'should remove the $prop element and place a comment anchor node where it used to be' ,
478
+ { '<div ng-messages-include="...">' : '<div ng-messages="data">' +
479
+ '<div ng-messages-include="abc.html"></div>' +
480
+ '</div>' ,
481
+ '<ng-messages-include src="...">' : '<ng-messages for="data">' +
482
+ '<ng-messages-include src="abc.html"></ng-messages-include>' +
483
+ '</ng-messages>' } ,
484
+ function ( html ) {
485
+ inject ( function ( $compile , $rootScope , $templateCache ) {
486
+ $templateCache . put ( 'abc.html' , '<div></div>' ) ;
487
+
488
+ element = $compile ( html ) ( $rootScope ) ;
489
+ $rootScope . $digest ( ) ;
490
+
491
+ var includeElement = element [ 0 ] . querySelector ( '[ng-messages-include], ng-messages-include' ) ;
492
+ expect ( includeElement ) . toBeFalsy ( ) ;
493
+
494
+ var comment = element [ 0 ] . childNodes [ 0 ] ;
495
+ expect ( comment . nodeType ) . toBe ( 8 ) ;
496
+ expect ( comment . nodeValue ) . toBe ( ' ngMessagesInclude: abc.html ' ) ;
497
+ } ) ;
498
+ } ) ;
499
+
418
500
they ( 'should load a remote template using $prop' ,
419
501
{ '<div ng-messages-include="...">' : '<div ng-messages="data">' +
420
502
'<div ng-messages-include="abc.html"></div>' +
@@ -437,7 +519,7 @@ describe('ngMessages', function() {
437
519
} ;
438
520
} ) ;
439
521
440
- expect ( element . children ( ) . length ) . toBe ( 1 ) ;
522
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
441
523
expect ( trim ( element . text ( ) ) ) . toEqual ( "A" ) ;
442
524
443
525
$rootScope . $apply ( function ( ) {
@@ -446,15 +528,15 @@ describe('ngMessages', function() {
446
528
} ;
447
529
} ) ;
448
530
449
- expect ( element . children ( ) . length ) . toBe ( 1 ) ;
531
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
450
532
expect ( trim ( element . text ( ) ) ) . toEqual ( "C" ) ;
451
533
} ) ;
452
534
} ) ;
453
535
454
536
it ( 'should cache the template after download' ,
455
537
inject ( function ( $rootScope , $compile , $templateCache , $httpBackend ) {
456
538
457
- $httpBackend . expect ( 'GET' , 'tpl' ) . respond ( 201 , 'abc' ) ;
539
+ $httpBackend . expect ( 'GET' , 'tpl' ) . respond ( 201 , '<div> abc</div> ' ) ;
458
540
459
541
expect ( $templateCache . get ( 'tpl' ) ) . toBeUndefined ( ) ;
460
542
@@ -484,13 +566,13 @@ describe('ngMessages', function() {
484
566
485
567
$rootScope . $digest ( ) ;
486
568
487
- expect ( element . children ( ) . length ) . toBe ( 1 ) ;
569
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
488
570
expect ( trim ( element . text ( ) ) ) . toEqual ( "Your value is that of failure" ) ;
489
571
490
572
$httpBackend . flush ( ) ;
491
573
$rootScope . $digest ( ) ;
492
574
493
- expect ( element . children ( ) . length ) . toBe ( 1 ) ;
575
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
494
576
expect ( trim ( element . text ( ) ) ) . toEqual ( "You did not enter a value" ) ;
495
577
} ) ) ;
496
578
@@ -515,7 +597,7 @@ describe('ngMessages', function() {
515
597
} ;
516
598
} ) ;
517
599
518
- expect ( element . children ( ) . length ) . toBe ( 1 ) ;
600
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
519
601
expect ( trim ( element . text ( ) ) ) . toEqual ( "AAA" ) ;
520
602
521
603
$rootScope . $apply ( function ( ) {
@@ -525,7 +607,7 @@ describe('ngMessages', function() {
525
607
} ;
526
608
} ) ;
527
609
528
- expect ( element . children ( ) . length ) . toBe ( 1 ) ;
610
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
529
611
expect ( trim ( element . text ( ) ) ) . toEqual ( "B" ) ;
530
612
531
613
$rootScope . $apply ( function ( ) {
@@ -534,7 +616,7 @@ describe('ngMessages', function() {
534
616
} ;
535
617
} ) ;
536
618
537
- expect ( element . children ( ) . length ) . toBe ( 1 ) ;
619
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
538
620
expect ( trim ( element . text ( ) ) ) . toEqual ( "C" ) ;
539
621
} ) ) ;
540
622
@@ -560,7 +642,7 @@ describe('ngMessages', function() {
560
642
} ;
561
643
} ) ;
562
644
563
- expect ( element . children ( ) . length ) . toBe ( 2 ) ;
645
+ expect ( messageChildren ( element ) . length ) . toBe ( 2 ) ;
564
646
expect ( s ( element . text ( ) ) ) . toContain ( "13" ) ;
565
647
} ) ;
566
648
} ) ;
@@ -584,14 +666,14 @@ describe('ngMessages', function() {
584
666
} ;
585
667
} ) ;
586
668
587
- expect ( element . children ( ) . length ) . toBe ( 2 ) ;
669
+ expect ( messageChildren ( element ) . length ) . toBe ( 2 ) ;
588
670
expect ( s ( element . text ( ) ) ) . toEqual ( "XZ" ) ;
589
671
590
672
$rootScope . $apply ( function ( ) {
591
673
$rootScope . data . y = { } ;
592
674
} ) ;
593
675
594
- expect ( element . children ( ) . length ) . toBe ( 3 ) ;
676
+ expect ( messageChildren ( element ) . length ) . toBe ( 3 ) ;
595
677
expect ( s ( element . text ( ) ) ) . toEqual ( "XYZ" ) ;
596
678
} ) ) ;
597
679
@@ -616,14 +698,14 @@ describe('ngMessages', function() {
616
698
} ;
617
699
} ) ;
618
700
619
- expect ( element . children ( ) . length ) . toBe ( 2 ) ;
701
+ expect ( messageChildren ( element ) . length ) . toBe ( 2 ) ;
620
702
expect ( s ( element . text ( ) ) ) . toEqual ( "ZZZX" ) ;
621
703
622
704
$rootScope . $apply ( function ( ) {
623
705
$rootScope . data . y = { } ;
624
706
} ) ;
625
707
626
- expect ( element . children ( ) . length ) . toBe ( 3 ) ;
708
+ expect ( messageChildren ( element ) . length ) . toBe ( 3 ) ;
627
709
expect ( s ( element . text ( ) ) ) . toEqual ( "YYYZZZX" ) ;
628
710
} ) ) ;
629
711
} ) ;
0 commit comments