@@ -37,6 +37,24 @@ describe('<select multiple selectize>', function() {
3737 $ ( element ) . mousedown ( ) . click ( ) . mouseup ( ) ;
3838 }
3939
40+ function testSelectedOptions ( values ) {
41+ values = angular . isArray ( values ) ? values : [ values ] ;
42+ var domValues = selectElement
43+ . find ( 'option[selected]' )
44+ . map ( function ( ) {
45+ return parseInt ( $ ( this ) . attr ( 'value' ) , 10 ) ;
46+ } )
47+ . toArray ( )
48+ . reduce ( function ( arr , v ) {
49+ if ( arr . indexOf ( v ) < 0 ) arr . push ( v ) ;
50+ return arr ;
51+ } , [ ] ) ;
52+ assert . strictEqual ( domValues . length , values . length ) ;
53+ assert . ok ( domValues . every ( function ( v ) {
54+ return values . indexOf ( v ) >= 0 ;
55+ } ) ) ;
56+ }
57+
4058 describe ( 'with create mode enabled' , function ( ) {
4159 // describe('with an empty ng-model', function() {});
4260 describe ( 'with a single value in ng-model' , function ( ) {
@@ -57,10 +75,7 @@ describe('<select multiple selectize>', function() {
5775 } ) ;
5876
5977 it ( 'should default to the ng-model value' , function ( ) {
60- var domOptions = selectElement . find ( 'option' ) ;
61- assert . strictEqual ( domOptions . length , 1 ) ;
62- assert . ok ( domOptions . attr ( 'selected' ) ) ;
63- assert . equal ( domOptions . attr ( 'value' ) , 0 ) ;
78+ testSelectedOptions ( 0 ) ;
6479 } ) ;
6580 } ) ;
6681
@@ -108,10 +123,7 @@ describe('<select multiple selectize>', function() {
108123
109124 describe ( 'when the model is updated' , function ( ) {
110125 beforeEach ( function ( ) {
111- var domOptions = selectElement . find ( 'option' ) ;
112- assert . strictEqual ( domOptions . length , 1 ) ;
113- assert . ok ( domOptions . attr ( 'selected' ) ) ;
114- assert . equal ( domOptions . attr ( 'value' ) , 0 ) ;
126+ testSelectedOptions ( 0 ) ;
115127 } ) ;
116128
117129 it ( 'should clear the selection when the model is empty' , function ( ) {
@@ -123,22 +135,13 @@ describe('<select multiple selectize>', function() {
123135 it ( 'should update the selection when the model contains a single item' , function ( ) {
124136 scope . selection = [ 'bar' ] ;
125137 scope . $apply ( ) ;
126-
127- var domOptions = selectElement . find ( 'option' ) ;
128- assert . strictEqual ( domOptions . length , 1 ) ;
129- assert . ok ( domOptions . attr ( 'selected' ) ) ;
130- assert . equal ( domOptions . attr ( 'value' ) , 1 ) ;
138+ testSelectedOptions ( 1 ) ;
131139 } ) ;
132140
133141 it ( 'should update the selection when the model contains two items' , function ( ) {
134142 scope . selection = [ 'bar' , 'baz' ] ;
135143 scope . $apply ( ) ;
136-
137- var domOptions = selectElement . find ( 'option' ) ;
138- assert . strictEqual ( domOptions . length , 2 ) ;
139- assert . ok ( domOptions . attr ( 'selected' ) ) ;
140- assert . ok ( selectElement . find ( 'option[value=1]' ) ) ;
141- assert . ok ( selectElement . find ( 'option[value=2]' ) ) ;
144+ testSelectedOptions ( [ 1 , 2 ] ) ;
142145 } ) ;
143146 } ) ;
144147
@@ -171,11 +174,7 @@ describe('<select multiple selectize>', function() {
171174 } ) ;
172175
173176 it ( 'should default to the ng-model value' , function ( ) {
174- var domOptions = selectElement . find ( 'option' ) ;
175- var selectedValue = scope . options [ parseInt ( domOptions . attr ( 'value' ) , 10 ) ] . value ;
176- assert . strictEqual ( domOptions . length , 1 ) ;
177- assert . ok ( domOptions . attr ( 'selected' ) ) ;
178- assert . strictEqual ( selectedValue , scope . selection [ 0 ] ) ;
177+ testSelectedOptions ( 0 ) ;
179178 } ) ;
180179 } ) ;
181180
@@ -222,11 +221,7 @@ describe('<select multiple selectize>', function() {
222221
223222 describe ( 'when the model is updated' , function ( ) {
224223 beforeEach ( function ( ) {
225- var domOptions = selectElement . find ( 'option' ) ;
226- var selectedValue = scope . options [ parseInt ( domOptions . attr ( 'value' ) , 10 ) ] . value ;
227- assert . strictEqual ( domOptions . length , 1 ) ;
228- assert . ok ( domOptions . attr ( 'selected' ) ) ;
229- assert . equal ( selectedValue , scope . selection ) ;
224+ testSelectedOptions ( 0 ) ;
230225 } ) ;
231226
232227 it ( 'should clear the selection when the model is empty' , function ( ) {
@@ -238,22 +233,13 @@ describe('<select multiple selectize>', function() {
238233 it ( 'should update the selection when the model contains a single item' , function ( ) {
239234 scope . selection = [ 'guid2' ] ;
240235 scope . $apply ( ) ;
241-
242- var domOptions = selectElement . find ( 'option' ) ;
243- assert . strictEqual ( domOptions . length , 1 ) ;
244- assert . ok ( domOptions . attr ( 'selected' ) ) ;
245- assert . equal ( domOptions . attr ( 'value' ) , 1 ) ;
236+ testSelectedOptions ( 1 ) ;
246237 } ) ;
247238
248239 it ( 'should update the selection when the model contains two items' , function ( ) {
249240 scope . selection = [ 'guid2' , 'guid3' ] ;
250241 scope . $apply ( ) ;
251-
252- var domOptions = selectElement . find ( 'option' ) ;
253- assert . strictEqual ( domOptions . length , 2 ) ;
254- assert . ok ( domOptions . attr ( 'selected' ) ) ;
255- assert . ok ( selectElement . find ( 'option[value=1]' ) ) ;
256- assert . ok ( selectElement . find ( 'option[value=2]' ) ) ;
242+ testSelectedOptions ( [ 1 , 2 ] ) ;
257243 } ) ;
258244 } ) ;
259245
0 commit comments