@@ -617,6 +617,51 @@ describe('angular', function() {
617
617
forEach ( obj , function ( value , key ) { log . push ( key + ':' + value ) ; } ) ;
618
618
expect ( log ) . toEqual ( [ 'length:2' , 'foo:bar' ] ) ;
619
619
} ) ;
620
+
621
+ function testForEachSpec ( expectedSize , collection ) {
622
+ var that = { } ;
623
+
624
+ forEach ( collection , function ( value , key , collectionArg ) {
625
+ expect ( collectionArg ) . toBe ( collection ) ;
626
+ expect ( collectionArg [ key ] ) . toBe ( value ) ;
627
+
628
+ expect ( this ) . toBe ( that ) ;
629
+
630
+ expectedSize -- ;
631
+ } , that ) ;
632
+
633
+ expect ( expectedSize ) . toBe ( 0 ) ;
634
+ }
635
+ it ( 'should follow the ES spec when called with array' , function ( ) {
636
+ testForEachSpec ( 2 , [ 1 , 2 ] ) ;
637
+ } ) ;
638
+ it ( 'should follow the ES spec when called with arguments' , function ( ) {
639
+ testForEachSpec ( 2 , ( function ( ) { return arguments ; } ( 1 , 2 ) ) ) ;
640
+ } ) ;
641
+ it ( 'should follow the ES spec when called with string' , function ( ) {
642
+ testForEachSpec ( 2 , '12' ) ;
643
+ } ) ;
644
+ it ( 'should follow the ES spec when called with jQuery/jqLite' , function ( ) {
645
+ testForEachSpec ( 2 , jqLite ( "<span>a</span><span>b</span>" ) ) ;
646
+ } ) ;
647
+ it ( 'should follow the ES spec when called with childNodes NodeList' , function ( ) {
648
+ testForEachSpec ( 2 , jqLite ( "<p><span>a</span><span>b</span></p>" ) [ 0 ] . childNodes ) ;
649
+ } ) ;
650
+ it ( 'should follow the ES spec when called with getElementsByTagName HTMLCollection' , function ( ) {
651
+ testForEachSpec ( 2 , jqLite ( "<p><span>a</span><span>b</span></p>" ) [ 0 ] . getElementsByTagName ( "*" ) ) ;
652
+ } ) ;
653
+ it ( 'should follow the ES spec when called with querySelectorAll HTMLCollection' , function ( ) {
654
+ testForEachSpec ( 2 , jqLite ( "<p><span>a</span><span>b</span></p>" ) [ 0 ] . querySelectorAll ( "*" ) ) ;
655
+ } ) ;
656
+ it ( 'should follow the ES spec when called with JSON' , function ( ) {
657
+ testForEachSpec ( 2 , { a : 1 , b : 2 } ) ;
658
+ } ) ;
659
+ it ( 'should follow the ES spec when called with function' , function ( ) {
660
+ function f ( ) { }
661
+ f . a = 1 ;
662
+ f . b = 2 ;
663
+ testForEachSpec ( 2 , f ) ;
664
+ } ) ;
620
665
} ) ;
621
666
622
667
0 commit comments