@@ -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,96 @@ 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 ng-message="a">A</div>' +
487
+ '<div ng-message="b">B</div>' +
488
+ '<div ng-message="c">C</div>' ) ;
489
+
490
+ element = $compile ( html ) ( $rootScope ) ;
491
+ $rootScope . $apply ( function ( ) {
492
+ $rootScope . data = {
493
+ 'a' : 1 ,
494
+ 'b' : 2 ,
495
+ 'c' : 3
496
+ } ;
497
+ } ) ;
498
+
499
+ var includeElement = element [ 0 ] . querySelector ( '[ng-messages-include], ng-messages-include' ) ;
500
+ expect ( includeElement ) . toBeFalsy ( ) ;
501
+
502
+ var comment = element [ 0 ] . childNodes [ 0 ] ;
503
+ expect ( comment . nodeType ) . toBe ( 8 ) ;
504
+ expect ( comment . nodeValue ) . toBe ( ' ngMessagesInclude: abc.html ' ) ;
505
+ } ) ;
506
+ } ) ;
507
+
418
508
they ( 'should load a remote template using $prop' ,
419
509
{ '<div ng-messages-include="...">' : '<div ng-messages="data">' +
420
510
'<div ng-messages-include="abc.html"></div>' +
@@ -437,7 +527,7 @@ describe('ngMessages', function() {
437
527
} ;
438
528
} ) ;
439
529
440
- expect ( element . children ( ) . length ) . toBe ( 1 ) ;
530
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
441
531
expect ( trim ( element . text ( ) ) ) . toEqual ( "A" ) ;
442
532
443
533
$rootScope . $apply ( function ( ) {
@@ -446,15 +536,15 @@ describe('ngMessages', function() {
446
536
} ;
447
537
} ) ;
448
538
449
- expect ( element . children ( ) . length ) . toBe ( 1 ) ;
539
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
450
540
expect ( trim ( element . text ( ) ) ) . toEqual ( "C" ) ;
451
541
} ) ;
452
542
} ) ;
453
543
454
544
it ( 'should cache the template after download' ,
455
545
inject ( function ( $rootScope , $compile , $templateCache , $httpBackend ) {
456
546
457
- $httpBackend . expect ( 'GET' , 'tpl' ) . respond ( 201 , 'abc' ) ;
547
+ $httpBackend . expect ( 'GET' , 'tpl' ) . respond ( 201 , '<div> abc</div> ' ) ;
458
548
459
549
expect ( $templateCache . get ( 'tpl' ) ) . toBeUndefined ( ) ;
460
550
@@ -484,13 +574,13 @@ describe('ngMessages', function() {
484
574
485
575
$rootScope . $digest ( ) ;
486
576
487
- expect ( element . children ( ) . length ) . toBe ( 1 ) ;
577
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
488
578
expect ( trim ( element . text ( ) ) ) . toEqual ( "Your value is that of failure" ) ;
489
579
490
580
$httpBackend . flush ( ) ;
491
581
$rootScope . $digest ( ) ;
492
582
493
- expect ( element . children ( ) . length ) . toBe ( 1 ) ;
583
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
494
584
expect ( trim ( element . text ( ) ) ) . toEqual ( "You did not enter a value" ) ;
495
585
} ) ) ;
496
586
@@ -515,7 +605,7 @@ describe('ngMessages', function() {
515
605
} ;
516
606
} ) ;
517
607
518
- expect ( element . children ( ) . length ) . toBe ( 1 ) ;
608
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
519
609
expect ( trim ( element . text ( ) ) ) . toEqual ( "AAA" ) ;
520
610
521
611
$rootScope . $apply ( function ( ) {
@@ -525,7 +615,7 @@ describe('ngMessages', function() {
525
615
} ;
526
616
} ) ;
527
617
528
- expect ( element . children ( ) . length ) . toBe ( 1 ) ;
618
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
529
619
expect ( trim ( element . text ( ) ) ) . toEqual ( "B" ) ;
530
620
531
621
$rootScope . $apply ( function ( ) {
@@ -534,7 +624,7 @@ describe('ngMessages', function() {
534
624
} ;
535
625
} ) ;
536
626
537
- expect ( element . children ( ) . length ) . toBe ( 1 ) ;
627
+ expect ( messageChildren ( element ) . length ) . toBe ( 1 ) ;
538
628
expect ( trim ( element . text ( ) ) ) . toEqual ( "C" ) ;
539
629
} ) ) ;
540
630
@@ -560,7 +650,7 @@ describe('ngMessages', function() {
560
650
} ;
561
651
} ) ;
562
652
563
- expect ( element . children ( ) . length ) . toBe ( 2 ) ;
653
+ expect ( messageChildren ( element ) . length ) . toBe ( 2 ) ;
564
654
expect ( s ( element . text ( ) ) ) . toContain ( "13" ) ;
565
655
} ) ;
566
656
} ) ;
@@ -584,14 +674,14 @@ describe('ngMessages', function() {
584
674
} ;
585
675
} ) ;
586
676
587
- expect ( element . children ( ) . length ) . toBe ( 2 ) ;
677
+ expect ( messageChildren ( element ) . length ) . toBe ( 2 ) ;
588
678
expect ( s ( element . text ( ) ) ) . toEqual ( "XZ" ) ;
589
679
590
680
$rootScope . $apply ( function ( ) {
591
681
$rootScope . data . y = { } ;
592
682
} ) ;
593
683
594
- expect ( element . children ( ) . length ) . toBe ( 3 ) ;
684
+ expect ( messageChildren ( element ) . length ) . toBe ( 3 ) ;
595
685
expect ( s ( element . text ( ) ) ) . toEqual ( "XYZ" ) ;
596
686
} ) ) ;
597
687
@@ -616,14 +706,14 @@ describe('ngMessages', function() {
616
706
} ;
617
707
} ) ;
618
708
619
- expect ( element . children ( ) . length ) . toBe ( 2 ) ;
709
+ expect ( messageChildren ( element ) . length ) . toBe ( 2 ) ;
620
710
expect ( s ( element . text ( ) ) ) . toEqual ( "ZZZX" ) ;
621
711
622
712
$rootScope . $apply ( function ( ) {
623
713
$rootScope . data . y = { } ;
624
714
} ) ;
625
715
626
- expect ( element . children ( ) . length ) . toBe ( 3 ) ;
716
+ expect ( messageChildren ( element ) . length ) . toBe ( 3 ) ;
627
717
expect ( s ( element . text ( ) ) ) . toEqual ( "YYYZZZX" ) ;
628
718
} ) ) ;
629
719
} ) ;
0 commit comments