@@ -722,7 +722,29 @@ 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
+ } ) ;
727
+
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 ] ) ;
726
748
} ) ;
727
749
728
750
@@ -735,14 +757,14 @@ describe('select', function() {
735
757
scope . $apply ( function ( ) {
736
758
scope . selected = scope . arr [ 0 ] ;
737
759
} ) ;
738
- expect ( element . val ( ) ) . toBe ( '0 ' ) ;
760
+ expect ( element . val ( ) ) . toBe ( '10 ' ) ;
739
761
740
762
scope . $apply ( function ( ) {
741
763
scope . arr [ 0 ] = { id : 10 , label : 'new ten' } ;
742
764
} ) ;
743
- expect ( element . val ( ) ) . toBe ( '0 ' ) ;
765
+ expect ( element . val ( ) ) . toBe ( '10 ' ) ;
744
766
745
- element . children ( ) [ 1 ] . selected = 1 ;
767
+ element . children ( ) [ 1 ] . selected = 'selected' ;
746
768
browserTrigger ( element , 'change' ) ;
747
769
expect ( scope . selected ) . toEqual ( scope . arr [ 1 ] ) ;
748
770
} ) ;
@@ -758,12 +780,12 @@ describe('select', function() {
758
780
scope . $apply ( function ( ) {
759
781
scope . selected = scope . arr ;
760
782
} ) ;
761
- expect ( element . val ( ) ) . toEqual ( [ '0 ' , '1 ' ] ) ;
783
+ expect ( element . val ( ) ) . toEqual ( [ '10 ' , '20 ' ] ) ;
762
784
763
785
scope . $apply ( function ( ) {
764
786
scope . arr [ 0 ] = { id : 10 , label : 'new ten' } ;
765
787
} ) ;
766
- expect ( element . val ( ) ) . toEqual ( [ '0 ' , '1 ' ] ) ;
788
+ expect ( element . val ( ) ) . toEqual ( [ '10 ' , '20 ' ] ) ;
767
789
768
790
element . children ( ) [ 0 ] . selected = false ;
769
791
browserTrigger ( element , 'change' ) ;
@@ -778,18 +800,18 @@ describe('select', function() {
778
800
} ) ;
779
801
780
802
scope . $apply ( function ( ) {
781
- scope . selected = scope . obj [ '10 ' ] ;
803
+ scope . selected = scope . obj [ '1 ' ] ;
782
804
} ) ;
783
805
expect ( element . val ( ) ) . toBe ( '10' ) ;
784
806
785
807
scope . $apply ( function ( ) {
786
- scope . obj [ '10 ' ] = { score : 10 , label : 'ten' } ;
808
+ scope . obj [ '1 ' ] = { score : 10 , label : 'ten' } ;
787
809
} ) ;
788
810
expect ( element . val ( ) ) . toBe ( '10' ) ;
789
811
790
812
element . val ( '20' ) ;
791
813
browserTrigger ( element , 'change' ) ;
792
- expect ( scope . selected ) . toBe ( scope . obj [ 20 ] ) ;
814
+ expect ( scope . selected ) . toBe ( scope . obj [ '2' ] ) ;
793
815
} ) ;
794
816
795
817
@@ -801,18 +823,18 @@ describe('select', function() {
801
823
} ) ;
802
824
803
825
scope . $apply ( function ( ) {
804
- scope . selected = [ scope . obj [ '10 ' ] ] ;
826
+ scope . selected = [ scope . obj [ '1 ' ] ] ;
805
827
} ) ;
806
828
expect ( element . val ( ) ) . toEqual ( [ '10' ] ) ;
807
829
808
830
scope . $apply ( function ( ) {
809
- scope . obj [ '10 ' ] = { score : 10 , label : 'ten' } ;
831
+ scope . obj [ '1 ' ] = { score : 10 , label : 'ten' } ;
810
832
} ) ;
811
833
expect ( element . val ( ) ) . toEqual ( [ '10' ] ) ;
812
834
813
835
element . children ( ) [ 1 ] . selected = 'selected' ;
814
836
browserTrigger ( element , 'change' ) ;
815
- expect ( scope . selected ) . toEqual ( [ scope . obj [ 10 ] , scope . obj [ 20 ] ] ) ;
837
+ expect ( scope . selected ) . toEqual ( [ scope . obj [ '1' ] , scope . obj [ '2' ] ] ) ;
816
838
} ) ;
817
839
} ) ;
818
840
@@ -824,7 +846,7 @@ describe('select', function() {
824
846
describe ( 'selectAs+trackBy expression' , function ( ) {
825
847
beforeEach ( function ( ) {
826
848
scope . arr = [ { subItem : { label : 'ten' , id : 10 } } , { subItem : { label : 'twenty' , id : 20 } } ] ;
827
- scope . obj = { '10 ' : { subItem : { id : 10 , label : 'ten' } } , '20 ' : { subItem : { id : 20 , label : 'twenty' } } } ;
849
+ scope . obj = { '1 ' : { subItem : { id : 10 , label : 'ten' } } , '2 ' : { subItem : { id : 20 , label : 'twenty' } } } ;
828
850
} ) ;
829
851
830
852
@@ -840,16 +862,16 @@ describe('select', function() {
840
862
scope . $apply ( function ( ) {
841
863
scope . selected = scope . arr [ 0 ] . subItem ;
842
864
} ) ;
843
- expect ( element . val ( ) ) . toEqual ( '0 ' ) ;
865
+ expect ( element . val ( ) ) . toEqual ( '10 ' ) ;
844
866
845
867
scope . $apply ( function ( ) {
846
868
scope . selected = scope . arr [ 1 ] . subItem ;
847
869
} ) ;
848
- expect ( element . val ( ) ) . toEqual ( '1 ' ) ;
870
+ expect ( element . val ( ) ) . toEqual ( '20 ' ) ;
849
871
850
872
// Now test view -> model
851
873
852
- element . val ( '0 ' ) ;
874
+ element . val ( '10 ' ) ;
853
875
browserTrigger ( element , 'change' ) ;
854
876
expect ( scope . selected ) . toBe ( scope . arr [ 0 ] . subItem ) ;
855
877
@@ -861,7 +883,7 @@ describe('select', function() {
861
883
subItem : { label : 'new twenty' , id : 20 }
862
884
} ] ;
863
885
} ) ;
864
- expect ( element . val ( ) ) . toBe ( '0 ' ) ;
886
+ expect ( element . val ( ) ) . toBe ( '10 ' ) ;
865
887
expect ( scope . selected . id ) . toBe ( 10 ) ;
866
888
} ) ;
867
889
@@ -879,12 +901,12 @@ describe('select', function() {
879
901
scope . $apply ( function ( ) {
880
902
scope . selected = [ scope . arr [ 0 ] . subItem ] ;
881
903
} ) ;
882
- expect ( element . val ( ) ) . toEqual ( [ '0 ' ] ) ;
904
+ expect ( element . val ( ) ) . toEqual ( [ '10 ' ] ) ;
883
905
884
906
scope . $apply ( function ( ) {
885
907
scope . selected = [ scope . arr [ 1 ] . subItem ] ;
886
908
} ) ;
887
- expect ( element . val ( ) ) . toEqual ( [ '1 ' ] ) ;
909
+ expect ( element . val ( ) ) . toEqual ( [ '20 ' ] ) ;
888
910
889
911
// Now test view -> model
890
912
@@ -901,7 +923,7 @@ describe('select', function() {
901
923
subItem : { label : 'new twenty' , id : 20 }
902
924
} ] ;
903
925
} ) ;
904
- expect ( element . val ( ) ) . toEqual ( [ '0 ' ] ) ;
926
+ expect ( element . val ( ) ) . toEqual ( [ '10 ' ] ) ;
905
927
expect ( scope . selected [ 0 ] . id ) . toEqual ( 10 ) ;
906
928
expect ( scope . selected . length ) . toBe ( 1 ) ;
907
929
} ) ;
@@ -918,13 +940,12 @@ describe('select', function() {
918
940
// First test model -> view
919
941
920
942
scope . $apply ( function ( ) {
921
- scope . selected = [ scope . obj [ '10 ' ] . subItem ] ;
943
+ scope . selected = [ scope . obj [ '1 ' ] . subItem ] ;
922
944
} ) ;
923
945
expect ( element . val ( ) ) . toEqual ( [ '10' ] ) ;
924
946
925
-
926
947
scope . $apply ( function ( ) {
927
- scope . selected = [ scope . obj [ '10 ' ] . subItem ] ;
948
+ scope . selected = [ scope . obj [ '1 ' ] . subItem ] ;
928
949
} ) ;
929
950
expect ( element . val ( ) ) . toEqual ( [ '10' ] ) ;
930
951
@@ -933,15 +954,15 @@ describe('select', function() {
933
954
element . find ( 'option' ) [ 0 ] . selected = true ;
934
955
element . find ( 'option' ) [ 1 ] . selected = false ;
935
956
browserTrigger ( element , 'change' ) ;
936
- expect ( scope . selected ) . toEqual ( [ scope . obj [ '10 ' ] . subItem ] ) ;
957
+ expect ( scope . selected ) . toEqual ( [ scope . obj [ '1 ' ] . subItem ] ) ;
937
958
938
959
// Now reload the object
939
960
scope . $apply ( function ( ) {
940
961
scope . obj = {
941
- '10 ' : {
962
+ '1 ' : {
942
963
subItem : { label : 'new ten' , id : 10 }
943
964
} ,
944
- '20 ' : {
965
+ '2 ' : {
945
966
subItem : { label : 'new twenty' , id : 20 }
946
967
}
947
968
} ;
@@ -962,29 +983,29 @@ describe('select', function() {
962
983
// First test model -> view
963
984
964
985
scope . $apply ( function ( ) {
965
- scope . selected = scope . obj [ '10 ' ] . subItem ;
986
+ scope . selected = scope . obj [ '1 ' ] . subItem ;
966
987
} ) ;
967
988
expect ( element . val ( ) ) . toEqual ( '10' ) ;
968
989
969
990
970
991
scope . $apply ( function ( ) {
971
- scope . selected = scope . obj [ '10 ' ] . subItem ;
992
+ scope . selected = scope . obj [ '1 ' ] . subItem ;
972
993
} ) ;
973
994
expect ( element . val ( ) ) . toEqual ( '10' ) ;
974
995
975
996
// Now test view -> model
976
997
977
998
element . find ( 'option' ) [ 0 ] . selected = true ;
978
999
browserTrigger ( element , 'change' ) ;
979
- expect ( scope . selected ) . toEqual ( scope . obj [ '10 ' ] . subItem ) ;
1000
+ expect ( scope . selected ) . toEqual ( scope . obj [ '1 ' ] . subItem ) ;
980
1001
981
1002
// Now reload the object
982
1003
scope . $apply ( function ( ) {
983
1004
scope . obj = {
984
- '10 ' : {
1005
+ '1 ' : {
985
1006
subItem : { label : 'new ten' , id : 10 }
986
1007
} ,
987
- '20 ' : {
1008
+ '2 ' : {
988
1009
subItem : { label : 'new twenty' , id : 20 }
989
1010
}
990
1011
} ;
@@ -1338,20 +1359,20 @@ describe('select', function() {
1338
1359
scope . selected = scope . values [ 1 ] ;
1339
1360
} ) ;
1340
1361
1341
- expect ( element . val ( ) ) . toEqual ( '1 ' ) ;
1362
+ expect ( element . val ( ) ) . toEqual ( '2 ' ) ;
1342
1363
1343
1364
var first = jqLite ( element . find ( 'option' ) [ 0 ] ) ;
1344
1365
expect ( first . text ( ) ) . toEqual ( 'first' ) ;
1345
- expect ( first . attr ( 'value' ) ) . toEqual ( '0 ' ) ;
1366
+ expect ( first . attr ( 'value' ) ) . toEqual ( '1 ' ) ;
1346
1367
var forth = jqLite ( element . find ( 'option' ) [ 3 ] ) ;
1347
1368
expect ( forth . text ( ) ) . toEqual ( 'forth' ) ;
1348
- expect ( forth . attr ( 'value' ) ) . toEqual ( '3 ' ) ;
1369
+ expect ( forth . attr ( 'value' ) ) . toEqual ( '4 ' ) ;
1349
1370
1350
1371
scope . $apply ( function ( ) {
1351
1372
scope . selected = scope . values [ 3 ] ;
1352
1373
} ) ;
1353
1374
1354
- expect ( element . val ( ) ) . toEqual ( '3 ' ) ;
1375
+ expect ( element . val ( ) ) . toEqual ( '4 ' ) ;
1355
1376
} ) ;
1356
1377
1357
1378
0 commit comments