@@ -10,12 +10,34 @@ describe('select', function() {
10
10
scope . $apply ( ) ;
11
11
}
12
12
13
+
13
14
beforeEach ( inject ( function ( $injector , $rootScope ) {
14
15
scope = $rootScope ;
15
16
$compile = $injector . get ( '$compile' ) ;
16
17
formElement = element = null ;
17
18
} ) ) ;
18
19
20
+
21
+ beforeEach ( function ( ) {
22
+ this . addMatchers ( {
23
+ toEqualSelect : function ( expected ) {
24
+ var actualValues = [ ] ,
25
+ expectedValues = [ ] . slice . call ( arguments ) ;
26
+
27
+ forEach ( this . actual . find ( 'option' ) , function ( option ) {
28
+ actualValues . push ( option . selected ? [ option . value ] : option . value ) ;
29
+ } ) ;
30
+
31
+ this . message = function ( ) {
32
+ return 'Expected ' + toJson ( actualValues ) + ' to equal ' + toJson ( expectedValues ) + '.' ;
33
+ } ;
34
+
35
+ return equals ( expectedValues , actualValues ) ;
36
+ }
37
+ } ) ;
38
+ } ) ;
39
+
40
+
19
41
afterEach ( function ( ) {
20
42
dealoc ( formElement ) ;
21
43
} ) ;
@@ -102,15 +124,13 @@ describe('select', function() {
102
124
scope . selection = [ 'A' ] ;
103
125
} ) ;
104
126
105
- expect ( element . find ( 'option' ) [ 0 ] . selected ) . toEqual ( true ) ;
106
- expect ( element . find ( 'option' ) [ 1 ] . selected ) . toEqual ( false ) ;
127
+ expect ( element ) . toEqualSelect ( [ 'A' ] , 'B' ) ;
107
128
108
129
scope . $apply ( function ( ) {
109
130
scope . selection . push ( 'B' ) ;
110
131
} ) ;
111
132
112
- expect ( element . find ( 'option' ) [ 0 ] . selected ) . toEqual ( true ) ;
113
- expect ( element . find ( 'option' ) [ 1 ] . selected ) . toEqual ( true ) ;
133
+ expect ( element ) . toEqualSelect ( [ 'A' ] , [ 'B' ] ) ;
114
134
} ) ;
115
135
116
136
@@ -817,47 +837,27 @@ describe('select', function() {
817
837
818
838
819
839
describe ( 'OPTION value' , function ( ) {
820
- beforeEach ( function ( ) {
821
- this . addMatchers ( {
822
- toHaveValue : function ( expected ) {
823
- this . message = function ( ) {
824
- return 'Expected "' + this . actual . html ( ) + '" to have value="' + expected + '".' ;
825
- } ;
826
-
827
- var value ;
828
- htmlParser ( this . actual . html ( ) , {
829
- start :function ( tag , attrs ) {
830
- value = attrs . value ;
831
- } ,
832
- end :noop ,
833
- chars :noop
834
- } ) ;
835
- return trim ( value ) == trim ( expected ) ;
836
- }
837
- } ) ;
838
- } ) ;
839
-
840
840
841
- it ( 'should populate value attribute on OPTION' , inject ( function ( $rootScope , $compile ) {
842
- element = $ compile( '<select ng-model="x"><option>abc</option></select>' ) ( $rootScope )
843
- expect ( element ) . toHaveValue ( 'abc' ) ;
844
- } ) ) ;
841
+ it ( 'should populate value attribute on OPTION' , function ( ) {
842
+ compile ( '<select ng-model="x"><option selected >abc</option></select>' ) ;
843
+ expect ( element ) . toEqualSelect ( 'abc' ) ;
844
+ } ) ;
845
845
846
- it ( 'should ignore value if already exists' , inject ( function ( $rootScope , $compile ) {
847
- element = $ compile( '<select ng-model="x"><option value="abc">xyz</option></select>' ) ( $rootScope )
848
- expect ( element ) . toHaveValue ( 'abc' ) ;
849
- } ) ) ;
846
+ it ( 'should ignore value if already exists' , function ( ) {
847
+ compile ( '<select ng-model="x"><option value="abc">xyz</option></select>' ) ;
848
+ expect ( element ) . toEqualSelect ( 'abc' ) ;
849
+ } ) ;
850
850
851
- it ( 'should set value even if newlines present' , inject ( function ( $rootScope , $compile ) {
852
- element = $ compile( '<select ng-model="x"><option attr="\ntext\n" \n>\nabc\n</option></select>' ) ( $rootScope )
853
- expect ( element ) . toHaveValue ( '\nabc\n' ) ;
854
- } ) ) ;
851
+ it ( 'should set value even if newlines present' , function ( ) {
852
+ compile ( '<select ng-model="x"><option attr="\ntext\n" \n>\nabc\n</option></select>' ) ;
853
+ expect ( element ) . toEqualSelect ( '\nabc\n' ) ;
854
+ } ) ;
855
855
856
- it ( 'should set value even if self closing HTML' , inject ( function ( $rootScope , $compile ) {
856
+ it ( 'should set value even if self closing HTML' , function ( ) {
857
857
// IE removes the \n from option, which makes this test pointless
858
858
if ( msie ) return ;
859
- element = $ compile( '<select ng-model="x"><option>\n</option></select>' ) ( $rootScope )
860
- expect ( element ) . toHaveValue ( '\n' ) ;
861
- } ) ) ;
859
+ compile ( '<select ng-model="x"><option>\n</option></select>' ) ;
860
+ expect ( element ) . toEqualSelect ( '\n' ) ;
861
+ } ) ;
862
862
} ) ;
863
863
} ) ;
0 commit comments