@@ -458,7 +458,7 @@ describe('select', function() {
458
458
459
459
460
460
describe ( 'ngOptions' , function ( ) {
461
- function createSelect ( attrs , blank , unknown ) {
461
+ function createSelect ( attrs , blank , unknown , disabled ) {
462
462
var html = '<select' ;
463
463
forEach ( attrs , function ( value , key ) {
464
464
if ( isBoolean ( value ) ) {
@@ -470,6 +470,7 @@ describe('select', function() {
470
470
html += '>' +
471
471
( blank ? ( isString ( blank ) ? blank : '<option value="">blank</option>' ) : '' ) +
472
472
( unknown ? ( isString ( unknown ) ? unknown : '<option value="?">unknown</option>' ) : '' ) +
473
+ ( disabled ? ( isString ( disabled ) ? blank : '<option value="" disabled="true">blank</option>' ) : '' ) +
473
474
'</select>' ;
474
475
475
476
compile ( html ) ;
@@ -744,14 +745,48 @@ describe('select', function() {
744
745
745
746
var options = element . find ( 'option' ) ;
746
747
var optionToSelect = options . eq ( 1 ) ;
747
-
748
748
expect ( optionToSelect . text ( ) ) . toBe ( 'B' ) ;
749
749
750
750
optionToSelect . prop ( 'selected' , true ) ;
751
751
scope . $digest ( ) ;
752
+ expect ( optionToSelect . prop ( 'selected' ) ) . not . toBe ( true ) ;
753
+ expect ( scope . selected ) . toBe ( scope . values [ 0 ] ) ;
754
+ } ) ;
752
755
756
+ it ( 'should allow the initial selection of a disabled element' ,
757
+ function ( ) {
758
+ //Create a select element with a disabled option
759
+ createSelect ( {
760
+ 'ng-model' :'selected' ,
761
+ 'ng-options' :'value.name for value in values' ,
762
+ 'ng-required' : 'required'
763
+ } , false , false , true ) ;
764
+
765
+ //Add options 'A' and 'B'
766
+ //Set the select to be HTML5 'required'
767
+ scope . $apply ( function ( ) {
768
+ scope . values = [ { name : 'A' } , { name : 'B' } ] ;
769
+ scope . required = true ;
770
+ } ) ;
771
+
772
+ //Find the disabled option and select it
773
+ var options = element . find ( 'option' ) ;
774
+ var optionToSelect = options . eq ( 0 ) ;
775
+ optionToSelect . prop ( 'selected' , true ) ;
776
+
777
+ //Expect that the disabled item can be selected
753
778
expect ( optionToSelect . prop ( 'selected' ) ) . toBe ( true ) ;
754
- expect ( scope . selected ) . toBe ( scope . values [ 0 ] ) ;
779
+ //Expect that the browser won't automatically pick a non-disabled item
780
+ expect ( options . eq ( 1 ) . prop ( 'selected' ) ) . not . toBe ( true ) ;
781
+ //Expect that with the disabled option selected, validity will be false
782
+ expect ( element [ 0 ] . validity . valid ) . toBe ( false ) ;
783
+
784
+ //Select a non-disabled option
785
+ options . eq ( 1 ) . prop ( 'selected' , true ) ;
786
+ //Expect it to be selected
787
+ expect ( options . eq ( 1 ) . prop ( 'selected' ) ) . toBe ( true ) ;
788
+ //Expect the validity to be true
789
+ expect ( element [ 0 ] . validity . valid ) . toBe ( true ) ;
755
790
} ) ;
756
791
757
792
describe ( 'binding' , function ( ) {
0 commit comments