@@ -120,6 +120,40 @@ describe('ngOptions', function() {
120
120
return { pass : errors . length === 0 , message : message } ;
121
121
}
122
122
} ;
123
+ } ,
124
+ toBeMarkedAsSelected : function ( ) {
125
+ // Selected is special because the element property and attribute reflect each other's state.
126
+ // IE9 will wrongly report hasAttribute('selected') === true when the property is
127
+ // undefined or null, and the dev tools show that no attribute is set
128
+ return {
129
+ compare : function ( actual ) {
130
+ var errors = [ ] ;
131
+ if ( actual . selected === null || typeof actual . selected === 'undefined' || actual . selected === false ) {
132
+ errors . push ( 'Expected option property "selected" to be truthy' ) ;
133
+ }
134
+
135
+ if ( msie !== 9 && actual . hasAttribute ( 'selected' ) === false ) {
136
+ errors . push ( 'Expected option to have attribute "selected"' ) ;
137
+ }
138
+
139
+ var message = function ( ) {
140
+ return errors . join ( '\n' ) ;
141
+ } ;
142
+
143
+ var result = { } ;
144
+
145
+ result . pass = errors . length === 0 ;
146
+
147
+ if ( result . pass ) {
148
+ result . message = 'Expected option property "selected" to be falsy' +
149
+ ( msie !== 9 ? ' and option not to have attribute "selected"' : '' ) ;
150
+ } else {
151
+ result . message = message ;
152
+ }
153
+
154
+ return result ;
155
+ }
156
+ } ;
123
157
}
124
158
} ) ;
125
159
} ) ;
@@ -753,31 +787,31 @@ describe('ngOptions', function() {
753
787
} , true ) ;
754
788
755
789
var options = element . find ( 'option' ) ;
756
- expect ( options [ 0 ] . getAttribute ( 'selected' ) ) . toBe ( 'selected' ) ;
757
- expect ( options [ 1 ] . hasAttribute ( 'selected' ) ) . toBe ( false ) ;
758
- expect ( options [ 2 ] . hasAttribute ( 'selected' ) ) . toBe ( false ) ;
790
+ expect ( options [ 0 ] ) . toBeMarkedAsSelected ( ) ;
791
+ expect ( options [ 1 ] ) . not . toBeMarkedAsSelected ( ) ;
792
+ expect ( options [ 2 ] ) . not . toBeMarkedAsSelected ( ) ;
759
793
760
794
scope . selected = scope . values [ 0 ] ;
761
795
scope . $digest ( ) ;
762
796
763
- expect ( options [ 0 ] . hasAttribute ( 'selected' ) ) . toBe ( false ) ;
764
- expect ( options [ 1 ] . getAttribute ( 'selected' ) ) . toBe ( 'selected' ) ;
765
- expect ( options [ 2 ] . hasAttribute ( 'selected' ) ) . toBe ( false ) ;
797
+ expect ( options [ 0 ] ) . not . toBeMarkedAsSelected ( ) ;
798
+ expect ( options [ 1 ] ) . toBeMarkedAsSelected ( ) ;
799
+ expect ( options [ 2 ] ) . not . toBeMarkedAsSelected ( ) ;
766
800
767
801
scope . selected = scope . values [ 1 ] ;
768
802
scope . $digest ( ) ;
769
803
770
- expect ( options [ 0 ] . hasAttribute ( 'selected' ) ) . toBe ( false ) ;
771
- expect ( options [ 1 ] . hasAttribute ( 'selected' ) ) . toBe ( false ) ;
772
- expect ( options [ 2 ] . getAttribute ( 'selected' ) ) . toBe ( 'selected' ) ;
804
+ expect ( options [ 0 ] ) . not . toBeMarkedAsSelected ( ) ;
805
+ expect ( options [ 1 ] ) . not . toBeMarkedAsSelected ( ) ;
806
+ expect ( options [ 2 ] ) . toBeMarkedAsSelected ( ) ;
773
807
774
808
scope . selected = 'no match' ;
775
809
scope . $digest ( ) ;
776
810
777
811
expect ( options [ 0 ] . selected ) . toBe ( true ) ;
778
- expect ( options [ 0 ] . getAttribute ( 'selected' ) ) . toBe ( 'selected' ) ;
779
- expect ( options [ 1 ] . hasAttribute ( 'selected' ) ) . toBe ( false ) ;
780
- expect ( options [ 2 ] . hasAttribute ( 'selected' ) ) . toBe ( false ) ;
812
+ expect ( options [ 0 ] ) . toBeMarkedAsSelected ( ) ;
813
+ expect ( options [ 1 ] ) . not . toBeMarkedAsSelected ( ) ;
814
+ expect ( options [ 2 ] ) . not . toBeMarkedAsSelected ( ) ;
781
815
} ) ;
782
816
783
817
describe ( 'disableWhen expression' , function ( ) {
@@ -1446,10 +1480,9 @@ describe('ngOptions', function() {
1446
1480
scope . selected = { } ;
1447
1481
scope . $digest ( ) ;
1448
1482
1449
- expect ( options [ 0 ] . selected ) . toBe ( true ) ;
1450
- expect ( options [ 0 ] . getAttribute ( 'selected' ) ) . toBe ( 'selected' ) ;
1451
- expect ( options [ 2 ] . selected ) . toBe ( false ) ;
1452
- expect ( options [ 2 ] . hasAttribute ( 'selected' ) ) . toBe ( false ) ;
1483
+ expect ( options [ 0 ] ) . toBeMarkedAsSelected ( ) ;
1484
+ expect ( options [ 2 ] ) . not . toBeMarkedAsSelected ( ) ;
1485
+ expect ( options [ 2 ] ) . not . toBeMarkedAsSelected ( ) ;
1453
1486
} ) ;
1454
1487
1455
1488
} ) ;
0 commit comments