@@ -722,10 +722,45 @@ describe('select', function() {
722
722
describe ( 'trackBy expression' , function ( ) {
723
723
beforeEach ( function ( ) {
724
724
scope . arr = [ { id : 10 , label : 'ten' } , { id :20 , label : 'twenty' } ] ;
725
- scope . obj = { '10 ' : { score : 10 , label : 'ten' } , '20 ' : { score : 20 , label : 'twenty' } } ;
725
+ scope . obj = { '1 ' : { score : 10 , label : 'ten' } , '2 ' : { score : 20 , label : 'twenty' } } ;
726
726
} ) ;
727
727
728
728
729
+ it ( 'should set the result of track by expression to element value' , function ( ) {
730
+ createSelect ( {
731
+ 'ng-model' : 'selected' ,
732
+ 'ng-options' : 'item.label for item in arr track by item.id'
733
+ } ) ;
734
+
735
+ scope . $apply ( function ( ) {
736
+ scope . selected = scope . arr [ 0 ] ;
737
+ } ) ;
738
+ expect ( element . val ( ) ) . toBe ( '10' ) ;
739
+
740
+ scope . $apply ( function ( ) {
741
+ scope . arr [ 0 ] = { id : 10 , label : 'new ten' } ;
742
+ } ) ;
743
+ expect ( element . val ( ) ) . toBe ( '10' ) ;
744
+
745
+ element . children ( ) [ 1 ] . selected = 'selected' ;
746
+ browserTrigger ( element , 'change' ) ;
747
+ expect ( scope . selected ) . toEqual ( scope . arr [ 1 ] ) ;
748
+ } ) ;
749
+
750
+
751
+ it ( 'should use the tracked expression as option value' , function ( ) {
752
+ createSelect ( {
753
+ 'ng-model' : 'selected' ,
754
+ 'ng-options' : 'item.label for item in arr track by item.id'
755
+ } ) ;
756
+
757
+ var options = element . find ( 'option' ) ;
758
+ expect ( options . length ) . toEqual ( 3 ) ;
759
+ expect ( sortedHtml ( options [ 0 ] ) ) . toEqual ( '<option value="?"></option>' ) ;
760
+ expect ( sortedHtml ( options [ 1 ] ) ) . toEqual ( '<option value="10">ten</option>' ) ;
761
+ expect ( sortedHtml ( options [ 2 ] ) ) . toEqual ( '<option value="20">twenty</option>' ) ;
762
+ } ) ;
763
+
729
764
it ( 'should preserve value even when reference has changed (single&array)' , function ( ) {
730
765
createSelect ( {
731
766
'ng-model' : 'selected' ,
@@ -735,12 +770,12 @@ describe('select', function() {
735
770
scope . $apply ( function ( ) {
736
771
scope . selected = scope . arr [ 0 ] ;
737
772
} ) ;
738
- expect ( element . val ( ) ) . toBe ( '0 ' ) ;
773
+ expect ( element . val ( ) ) . toBe ( '10 ' ) ;
739
774
740
775
scope . $apply ( function ( ) {
741
776
scope . arr [ 0 ] = { id : 10 , label : 'new ten' } ;
742
777
} ) ;
743
- expect ( element . val ( ) ) . toBe ( '0 ' ) ;
778
+ expect ( element . val ( ) ) . toBe ( '10 ' ) ;
744
779
745
780
element . children ( ) [ 1 ] . selected = 1 ;
746
781
browserTrigger ( element , 'change' ) ;
@@ -758,12 +793,12 @@ describe('select', function() {
758
793
scope . $apply ( function ( ) {
759
794
scope . selected = scope . arr ;
760
795
} ) ;
761
- expect ( element . val ( ) ) . toEqual ( [ '0 ' , '1 ' ] ) ;
796
+ expect ( element . val ( ) ) . toEqual ( [ '10 ' , '20 ' ] ) ;
762
797
763
798
scope . $apply ( function ( ) {
764
799
scope . arr [ 0 ] = { id : 10 , label : 'new ten' } ;
765
800
} ) ;
766
- expect ( element . val ( ) ) . toEqual ( [ '0 ' , '1 ' ] ) ;
801
+ expect ( element . val ( ) ) . toEqual ( [ '10 ' , '20 ' ] ) ;
767
802
768
803
element . children ( ) [ 0 ] . selected = false ;
769
804
browserTrigger ( element , 'change' ) ;
@@ -778,18 +813,18 @@ describe('select', function() {
778
813
} ) ;
779
814
780
815
scope . $apply ( function ( ) {
781
- scope . selected = scope . obj [ '10 ' ] ;
816
+ scope . selected = scope . obj [ '1 ' ] ;
782
817
} ) ;
783
818
expect ( element . val ( ) ) . toBe ( '10' ) ;
784
819
785
820
scope . $apply ( function ( ) {
786
- scope . obj [ '10 ' ] = { score : 10 , label : 'ten' } ;
821
+ scope . obj [ '1 ' ] = { score : 10 , label : 'ten' } ;
787
822
} ) ;
788
823
expect ( element . val ( ) ) . toBe ( '10' ) ;
789
824
790
825
element . val ( '20' ) ;
791
826
browserTrigger ( element , 'change' ) ;
792
- expect ( scope . selected ) . toBe ( scope . obj [ 20 ] ) ;
827
+ expect ( scope . selected ) . toBe ( scope . obj [ '2' ] ) ;
793
828
} ) ;
794
829
795
830
@@ -801,18 +836,18 @@ describe('select', function() {
801
836
} ) ;
802
837
803
838
scope . $apply ( function ( ) {
804
- scope . selected = [ scope . obj [ '10 ' ] ] ;
839
+ scope . selected = [ scope . obj [ '1 ' ] ] ;
805
840
} ) ;
806
841
expect ( element . val ( ) ) . toEqual ( [ '10' ] ) ;
807
842
808
843
scope . $apply ( function ( ) {
809
- scope . obj [ '10 ' ] = { score : 10 , label : 'ten' } ;
844
+ scope . obj [ '1 ' ] = { score : 10 , label : 'ten' } ;
810
845
} ) ;
811
846
expect ( element . val ( ) ) . toEqual ( [ '10' ] ) ;
812
847
813
848
element . children ( ) [ 1 ] . selected = 'selected' ;
814
849
browserTrigger ( element , 'change' ) ;
815
- expect ( scope . selected ) . toEqual ( [ scope . obj [ 10 ] , scope . obj [ 20 ] ] ) ;
850
+ expect ( scope . selected ) . toEqual ( [ scope . obj [ '1' ] , scope . obj [ '2' ] ] ) ;
816
851
} ) ;
817
852
} ) ;
818
853
@@ -840,16 +875,16 @@ describe('select', function() {
840
875
scope . $apply ( function ( ) {
841
876
scope . selected = scope . arr [ 0 ] . subItem ;
842
877
} ) ;
843
- expect ( element . val ( ) ) . toEqual ( '0 ' ) ;
878
+ expect ( element . val ( ) ) . toEqual ( '10 ' ) ;
844
879
845
880
scope . $apply ( function ( ) {
846
881
scope . selected = scope . arr [ 1 ] . subItem ;
847
882
} ) ;
848
- expect ( element . val ( ) ) . toEqual ( '1 ' ) ;
883
+ expect ( element . val ( ) ) . toEqual ( '20 ' ) ;
849
884
850
885
// Now test view -> model
851
886
852
- element . val ( '0 ' ) ;
887
+ element . val ( '10 ' ) ;
853
888
browserTrigger ( element , 'change' ) ;
854
889
expect ( scope . selected ) . toBe ( scope . arr [ 0 ] . subItem ) ;
855
890
@@ -861,7 +896,7 @@ describe('select', function() {
861
896
subItem : { label : 'new twenty' , id : 20 }
862
897
} ] ;
863
898
} ) ;
864
- expect ( element . val ( ) ) . toBe ( '0 ' ) ;
899
+ expect ( element . val ( ) ) . toBe ( '10 ' ) ;
865
900
expect ( scope . selected . id ) . toBe ( 10 ) ;
866
901
} ) ;
867
902
@@ -879,12 +914,12 @@ describe('select', function() {
879
914
scope . $apply ( function ( ) {
880
915
scope . selected = [ scope . arr [ 0 ] . subItem ] ;
881
916
} ) ;
882
- expect ( element . val ( ) ) . toEqual ( [ '0 ' ] ) ;
917
+ expect ( element . val ( ) ) . toEqual ( [ '10 ' ] ) ;
883
918
884
919
scope . $apply ( function ( ) {
885
920
scope . selected = [ scope . arr [ 1 ] . subItem ] ;
886
921
} ) ;
887
- expect ( element . val ( ) ) . toEqual ( [ '1 ' ] ) ;
922
+ expect ( element . val ( ) ) . toEqual ( [ '20 ' ] ) ;
888
923
889
924
// Now test view -> model
890
925
@@ -901,7 +936,7 @@ describe('select', function() {
901
936
subItem : { label : 'new twenty' , id : 20 }
902
937
} ] ;
903
938
} ) ;
904
- expect ( element . val ( ) ) . toEqual ( [ '0 ' ] ) ;
939
+ expect ( element . val ( ) ) . toEqual ( [ '10 ' ] ) ;
905
940
expect ( scope . selected [ 0 ] . id ) . toEqual ( 10 ) ;
906
941
expect ( scope . selected . length ) . toBe ( 1 ) ;
907
942
} ) ;
@@ -1338,20 +1373,20 @@ describe('select', function() {
1338
1373
scope . selected = scope . values [ 1 ] ;
1339
1374
} ) ;
1340
1375
1341
- expect ( element . val ( ) ) . toEqual ( '1 ' ) ;
1376
+ expect ( element . val ( ) ) . toEqual ( '2 ' ) ;
1342
1377
1343
1378
var first = jqLite ( element . find ( 'option' ) [ 0 ] ) ;
1344
1379
expect ( first . text ( ) ) . toEqual ( 'first' ) ;
1345
- expect ( first . attr ( 'value' ) ) . toEqual ( '0 ' ) ;
1380
+ expect ( first . attr ( 'value' ) ) . toEqual ( '1 ' ) ;
1346
1381
var forth = jqLite ( element . find ( 'option' ) [ 3 ] ) ;
1347
1382
expect ( forth . text ( ) ) . toEqual ( 'forth' ) ;
1348
- expect ( forth . attr ( 'value' ) ) . toEqual ( '3 ' ) ;
1383
+ expect ( forth . attr ( 'value' ) ) . toEqual ( '4 ' ) ;
1349
1384
1350
1385
scope . $apply ( function ( ) {
1351
1386
scope . selected = scope . values [ 3 ] ;
1352
1387
} ) ;
1353
1388
1354
- expect ( element . val ( ) ) . toEqual ( '3 ' ) ;
1389
+ expect ( element . val ( ) ) . toEqual ( '4 ' ) ;
1355
1390
} ) ;
1356
1391
1357
1392
0 commit comments