@@ -22,15 +22,17 @@ describe('ngOptions', function() {
22
22
this . addMatchers ( {
23
23
toEqualSelectValue : function ( value , multiple ) {
24
24
var errors = [ ] ;
25
+ var actual = this . actual . val ( ) ;
25
26
26
27
if ( multiple ) {
27
28
value = value . map ( function ( val ) { return hashKey ( val ) ; } ) ;
29
+ actual = actual || [ ] ;
28
30
} else {
29
31
value = hashKey ( value ) ;
30
32
}
31
33
32
- if ( ! equals ( this . actual . val ( ) , value ) ) {
33
- errors . push ( 'Expected select value "' + this . actual . val ( ) + '" to equal "' + value + '"' ) ;
34
+ if ( ! equals ( actual , value ) ) {
35
+ errors . push ( 'Expected select value "' + actual + '" to equal "' + value + '"' ) ;
34
36
}
35
37
this . message = function ( ) {
36
38
return errors . join ( '\n' ) ;
@@ -1279,6 +1281,52 @@ describe('ngOptions', function() {
1279
1281
var option = element . find ( 'option' ) . eq ( 0 ) ;
1280
1282
expect ( option ) . toEqualUnknownOption ( ) ;
1281
1283
} ) ;
1284
+
1285
+
1286
+ it ( 'should update the model if the selected option is removed' , function ( ) {
1287
+ scope . values = [ { value : 0 , label : 'zero' } , { value : 1 , label : 'one' } ] ;
1288
+ scope . selected = 1 ;
1289
+ createSelect ( {
1290
+ 'ng-model' : 'selected' ,
1291
+ 'ng-options' : 'option.value as option.label for option in values'
1292
+ } ) ;
1293
+ expect ( element ) . toEqualSelectValue ( 1 ) ;
1294
+
1295
+ scope . $apply ( function ( ) {
1296
+ scope . values . pop ( ) ;
1297
+ } ) ;
1298
+
1299
+ expect ( element . val ( ) ) . toEqualUnknownValue ( ) ;
1300
+ expect ( scope . selected ) . toEqual ( null ) ;
1301
+ } ) ;
1302
+
1303
+
1304
+ it ( 'should update the model if all the selected (multiple) options are removed' , function ( ) {
1305
+ scope . values = [ { value : 0 , label : 'zero' } , { value : 1 , label : 'one' } , { value : 2 , label : 'two' } ] ;
1306
+ scope . selected = [ 1 , 2 ] ;
1307
+ createSelect ( {
1308
+ 'ng-model' : 'selected' ,
1309
+ 'multiple' : true ,
1310
+ 'ng-options' : 'option.value as option.label for option in values'
1311
+ } ) ;
1312
+
1313
+ expect ( element ) . toEqualSelectValue ( [ 1 , 2 ] , true ) ;
1314
+
1315
+ scope . $apply ( function ( ) {
1316
+ scope . values . pop ( ) ;
1317
+ } ) ;
1318
+
1319
+ expect ( element ) . toEqualSelectValue ( [ 1 ] , true ) ;
1320
+ expect ( scope . selected ) . toEqual ( [ 1 ] ) ;
1321
+
1322
+ scope . $apply ( function ( ) {
1323
+ scope . values . pop ( ) ;
1324
+ } ) ;
1325
+
1326
+ expect ( element ) . toEqualSelectValue ( [ ] , true ) ;
1327
+ expect ( scope . selected ) . toEqual ( [ ] ) ;
1328
+ } ) ;
1329
+
1282
1330
} ) ;
1283
1331
1284
1332
0 commit comments