@@ -856,7 +856,87 @@ describe('ngOptions', function() {
856
856
expect ( options . eq ( 2 ) ) . toEqualTrackedOption ( 20 , 'twenty' ) ;
857
857
} ) ;
858
858
859
-
859
+
860
+ it ( 'should update the selected option even if only the tracked property on the selected object changes (single)' , function ( ) {
861
+ createSelect ( {
862
+ 'ng-model' : 'selected' ,
863
+ 'ng-options' : 'item.label for item in arr track by item.id'
864
+ } ) ;
865
+
866
+ scope . $apply ( function ( ) {
867
+ scope . selected = { id : 10 , label : 'ten' } ;
868
+ } ) ;
869
+
870
+ expect ( element . val ( ) ) . toEqual ( '10' ) ;
871
+
872
+ // Update the properties on the selected object, rather than replacing the whole object
873
+ scope . $apply ( function ( ) {
874
+ scope . selected . id = 20 ;
875
+ scope . selected . label = 'new twenty' ;
876
+ } ) ;
877
+
878
+ // The value of the select should change since the id property changed
879
+ expect ( element . val ( ) ) . toEqual ( '20' ) ;
880
+
881
+ // But the label of the selected option does not change
882
+ var option = element . find ( 'option' ) . eq ( 1 ) ;
883
+ expect ( option . prop ( 'selected' ) ) . toEqual ( true ) ;
884
+ expect ( option . text ( ) ) . toEqual ( 'twenty' ) ; // not 'new twenty'
885
+ } ) ;
886
+
887
+
888
+ it ( 'should update the selected options even if only the tracked properties on the objects in the ' +
889
+ 'selected collection change (multi)' , function ( ) {
890
+ createSelect ( {
891
+ 'ng-model' : 'selected' ,
892
+ 'multiple' : true ,
893
+ 'ng-options' : 'item.label for item in arr track by item.id'
894
+ } ) ;
895
+
896
+ scope . $apply ( function ( ) {
897
+ scope . selected = [ { id : 10 , label : 'ten' } ] ;
898
+ } ) ;
899
+
900
+ expect ( element . val ( ) ) . toEqual ( [ '10' ] ) ;
901
+
902
+ // Update the properties on the object in the selected array, rather than replacing the whole object
903
+ scope . $apply ( function ( ) {
904
+ scope . selected [ 0 ] . id = 20 ;
905
+ scope . selected [ 0 ] . label = 'new twenty' ;
906
+ } ) ;
907
+
908
+ // The value of the select should change since the id property changed
909
+ expect ( element . val ( ) ) . toEqual ( [ '20' ] ) ;
910
+
911
+ // But the label of the selected option does not change
912
+ var option = element . find ( 'option' ) . eq ( 1 ) ;
913
+ expect ( option . prop ( 'selected' ) ) . toEqual ( true ) ;
914
+ expect ( option . text ( ) ) . toEqual ( 'twenty' ) ; // not 'new twenty'
915
+ } ) ;
916
+
917
+
918
+ it ( 'should prevent changes to the selected object from modifying the options objects (single)' , function ( ) {
919
+
920
+ createSelect ( {
921
+ 'ng-model' : 'selected' ,
922
+ 'ng-options' : 'item.label for item in arr track by item.id'
923
+ } ) ;
924
+
925
+ element . val ( '10' ) ;
926
+ browserTrigger ( element , 'change' ) ;
927
+
928
+ expect ( scope . selected ) . toEqual ( scope . arr [ 0 ] ) ;
929
+
930
+ scope . $apply ( function ( ) {
931
+ scope . selected . id = 20 ;
932
+ } ) ;
933
+
934
+ expect ( scope . selected ) . not . toEqual ( scope . arr [ 0 ] ) ;
935
+ expect ( element . val ( ) ) . toEqual ( '20' ) ;
936
+ expect ( scope . arr ) . toEqual ( [ { id : 10 , label : 'ten' } , { id :20 , label : 'twenty' } ] ) ;
937
+ } ) ;
938
+
939
+
860
940
it ( 'should preserve value even when reference has changed (single&array)' , function ( ) {
861
941
createSelect ( {
862
942
'ng-model' : 'selected' ,
@@ -919,7 +999,7 @@ describe('ngOptions', function() {
919
999
expect ( element . val ( ) ) . toBe ( '10' ) ;
920
1000
921
1001
setSelectValue ( element , 1 ) ;
922
- expect ( scope . selected ) . toBe ( scope . obj [ '2' ] ) ;
1002
+ expect ( scope . selected ) . toEqual ( scope . obj [ '2' ] ) ;
923
1003
} ) ;
924
1004
925
1005
@@ -998,7 +1078,7 @@ describe('ngOptions', function() {
998
1078
999
1079
element . val ( '10' ) ;
1000
1080
browserTrigger ( element , 'change' ) ;
1001
- expect ( scope . selected ) . toBe ( scope . arr [ 0 ] . subItem ) ;
1081
+ expect ( scope . selected ) . toEqual ( scope . arr [ 0 ] . subItem ) ;
1002
1082
1003
1083
// Now reload the array
1004
1084
scope . $apply ( function ( ) {
@@ -1577,7 +1657,7 @@ describe('ngOptions', function() {
1577
1657
scope . values . pop ( ) ;
1578
1658
} ) ;
1579
1659
1580
- expect ( element . val ( ) ) . toEqualUnknownValue ( ) ;
1660
+ expect ( element . val ( ) ) . toEqual ( '' ) ;
1581
1661
expect ( scope . selected ) . toEqual ( null ) ;
1582
1662
1583
1663
// Check after model change
@@ -1591,7 +1671,7 @@ describe('ngOptions', function() {
1591
1671
scope . values . pop ( ) ;
1592
1672
} ) ;
1593
1673
1594
- expect ( element . val ( ) ) . toEqualUnknownValue ( ) ;
1674
+ expect ( element . val ( ) ) . toEqual ( '' ) ;
1595
1675
expect ( scope . selected ) . toEqual ( null ) ;
1596
1676
} ) ;
1597
1677
0 commit comments