@@ -120,6 +120,40 @@ describe('ngOptions', function() {
120
120
return { pass : errors . length === 0 , message : message } ;
121
121
}
122
122
} ;
123
+ } ,
124
+ toBeSelected : 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,33 +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 ] ) . toBeSelected ( ) ;
791
+ expect ( options [ 1 ] ) . not . toBeSelected ( ) ;
792
+ expect ( options [ 2 ] ) . not . toBeSelected ( ) ;
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 ) ;
766
-
767
- return ;
797
+ expect ( options [ 0 ] ) . not . toBeSelected ( ) ;
798
+ expect ( options [ 1 ] ) . toBeSelected ( ) ;
799
+ expect ( options [ 2 ] ) . not . toBeSelected ( ) ;
768
800
769
801
scope . selected = scope . values [ 1 ] ;
770
802
scope . $digest ( ) ;
771
803
772
- expect ( options [ 0 ] . hasAttribute ( 'selected' ) ) . toBe ( false ) ;
773
- expect ( options [ 1 ] . hasAttribute ( 'selected' ) ) . toBe ( false ) ;
774
- expect ( options [ 2 ] . getAttribute ( 'selected' ) ) . toBe ( 'selected' ) ;
804
+ expect ( options [ 0 ] ) . not . toBeSelected ( ) ;
805
+ expect ( options [ 1 ] ) . not . toBeSelected ( ) ;
806
+ expect ( options [ 2 ] ) . toBeSelected ( ) ;
775
807
776
808
scope . selected = 'no match' ;
777
809
scope . $digest ( ) ;
778
810
779
811
expect ( options [ 0 ] . selected ) . toBe ( true ) ;
780
- expect ( options [ 0 ] . getAttribute ( 'selected' ) ) . toBe ( 'selected' ) ;
781
- expect ( options [ 1 ] . hasAttribute ( 'selected' ) ) . toBe ( false ) ;
782
- expect ( options [ 2 ] . hasAttribute ( 'selected' ) ) . toBe ( false ) ;
812
+ expect ( options [ 0 ] ) . toBeSelected ( ) ;
813
+ expect ( options [ 1 ] ) . not . toBeSelected ( ) ;
814
+ expect ( options [ 2 ] ) . not . toBeSelected ( ) ;
783
815
} ) ;
784
816
785
817
describe ( 'disableWhen expression' , function ( ) {
@@ -1448,10 +1480,9 @@ describe('ngOptions', function() {
1448
1480
scope . selected = { } ;
1449
1481
scope . $digest ( ) ;
1450
1482
1451
- expect ( options [ 0 ] . selected ) . toBe ( true ) ;
1452
- expect ( options [ 0 ] . getAttribute ( 'selected' ) ) . toBe ( 'selected' ) ;
1453
- expect ( options [ 2 ] . selected ) . toBe ( false ) ;
1454
- expect ( options [ 2 ] . hasAttribute ( 'selected' ) ) . toBe ( false ) ;
1483
+ expect ( options [ 0 ] ) . toBeSelected ( ) ;
1484
+ expect ( options [ 2 ] ) . not . toBeSelected ( ) ;
1485
+ expect ( options [ 2 ] ) . not . toBeSelected ( ) ;
1455
1486
} ) ;
1456
1487
1457
1488
} ) ;
0 commit comments