@@ -267,45 +267,160 @@ describe('ui-select tests', function() {
267
267
} ) ;
268
268
269
269
describe ( 'disabled options' , function ( ) {
270
- function createUiSelect ( ) {
270
+ function createUiSelect ( attrs ) {
271
+ var attrsDisabled = '' ;
272
+ if ( attrs !== undefined ) {
273
+ if ( attrs . disabled !== undefined ) {
274
+ attrsDisabled = ' ui-disable-choice="' + attrs . disabled + '"' ;
275
+ } else {
276
+ attrsDisabled = '' ;
277
+ }
278
+ }
279
+
271
280
return compileTemplate (
272
281
'<ui-select ng-model="selection.selected"> \
273
282
<ui-select-match placeholder="Pick one...">{{$select.selected.name}}</ui-select-match> \
274
- <ui-select-choices repeat="person in people | filter: $select.search"> \
283
+ <ui-select-choices repeat="person in people | filter: $select.search"' + attrsDisabled + ' > \
275
284
<div ng-bind-html="person.name | highlight: $select.search"></div> \
276
285
<div ng-bind-html="person.email | highlight: $select.search"></div> \
277
286
</ui-select-choices> \
278
287
</ui-select>'
279
288
) ;
280
289
}
281
290
282
- beforeEach ( function ( ) {
283
- scope . _people = angular . copy ( scope . people ) ;
284
- scope . people = scope . people . map ( function ( person ) {
285
- if ( person . name == 'Wladimir' ) {
286
- person . disabled = true ;
291
+ function disablePerson ( opts ) {
292
+ opts = opts || { } ;
293
+
294
+ var key = opts . key || 'people' ,
295
+ disableAttr = opts . disableAttr || 'disabled' ,
296
+ disableBool = opts . disableBool === undefined ? true : opts . disableBool ,
297
+ matchAttr = opts . match || 'name' ,
298
+ matchVal = opts . matchVal || 'Wladimir' ;
299
+
300
+ scope [ '_' + key ] = angular . copy ( scope [ key ] ) ;
301
+ scope [ key ] . map ( function ( model ) {
302
+ if ( model [ matchAttr ] == matchVal ) {
303
+ model [ disableAttr ] = disableBool ;
287
304
}
288
- return person ;
305
+ return model ;
306
+ } ) ;
307
+ }
308
+
309
+ function resetScope ( opts ) {
310
+ opts = opts || { } ;
311
+ var key = opts . key || 'people' ;
312
+ scope [ key ] = angular . copy ( scope [ '_' + key ] ) ;
313
+ }
314
+
315
+ describe ( 'without disabling expression' , function ( ) {
316
+ beforeEach ( function ( ) {
317
+ disablePerson ( ) ;
318
+ this . el = createUiSelect ( ) ;
289
319
} ) ;
290
320
291
- this . el = createUiSelect ( ) ;
321
+ it ( 'should not allow disabled options to be selected' , function ( ) {
322
+ clickItem ( this . el , 'Wladimir' ) ;
323
+
324
+ expect ( getMatchLabel ( this . el ) ) . toEqual ( 'Wladimir' ) ;
325
+ } ) ;
326
+
327
+ it ( 'should set a disabled class on the option' , function ( ) {
328
+ var option = $ ( this . el ) . find ( '.ui-select-choices-row div:contains("Wladimir")' ) ;
329
+ var container = option . closest ( '.ui-select-choices-row' ) ;
330
+
331
+ expect ( container . hasClass ( 'disabled' ) ) . toBeFalsy ( ) ;
332
+ } ) ;
292
333
} ) ;
293
334
294
- afterEach ( function ( ) {
295
- scope . people = angular . copy ( scope . _people ) ;
335
+ describe ( 'disable on truthy property' , function ( ) {
336
+ beforeEach ( function ( ) {
337
+ disablePerson ( {
338
+ disableAttr : 'inactive' ,
339
+ disableBool : true ,
340
+ } ) ;
341
+ this . el = createUiSelect ( {
342
+ disabled : 'person.inactive'
343
+ } ) ;
344
+ } ) ;
345
+
346
+ it ( 'should allow the user to define the selected option' , function ( ) {
347
+ expect ( $ ( this . el ) . find ( '.ui-select-choices' ) . attr ( 'ui-disable-choice' ) ) . toBe ( 'person.inactive' ) ;
348
+ } ) ;
349
+
350
+ it ( 'should not allow disabled options to be selected' , function ( ) {
351
+ clickItem ( this . el , 'Wladimir' ) ;
352
+
353
+ expect ( getMatchLabel ( this . el ) ) . not . toEqual ( 'Wladimir' ) ;
354
+ } ) ;
355
+
356
+ it ( 'should set a disabled class on the option' , function ( ) {
357
+ var option = $ ( this . el ) . find ( '.ui-select-choices-row div:contains("Wladimir")' ) ;
358
+ var container = option . closest ( '.ui-select-choices-row' ) ;
359
+
360
+ expect ( container . hasClass ( 'disabled' ) ) . toBeTruthy ( ) ;
361
+ } ) ;
296
362
} ) ;
297
363
298
- it ( 'should not allow disabled options to be selected' , function ( ) {
299
- clickItem ( this . el , 'Wladimir' ) ;
364
+ describe ( 'disable on inverse property check' , function ( ) {
365
+ beforeEach ( function ( ) {
366
+ disablePerson ( {
367
+ disableAttr : 'active' ,
368
+ disableBool : false ,
369
+ } ) ;
370
+ this . el = createUiSelect ( {
371
+ disabled : '!person.active'
372
+ } ) ;
373
+ } ) ;
374
+
375
+ it ( 'should allow the user to define the selected option' , function ( ) {
376
+ expect ( $ ( this . el ) . find ( '.ui-select-choices' ) . attr ( 'ui-disable-choice' ) ) . toBe ( '!person.active' ) ;
377
+ } ) ;
378
+
379
+ it ( 'should not allow disabled options to be selected' , function ( ) {
380
+ clickItem ( this . el , 'Wladimir' ) ;
381
+
382
+ expect ( getMatchLabel ( this . el ) ) . not . toEqual ( 'Wladimir' ) ;
383
+ } ) ;
384
+
385
+ it ( 'should set a disabled class on the option' , function ( ) {
386
+ var option = $ ( this . el ) . find ( '.ui-select-choices-row div:contains("Wladimir")' ) ;
387
+ var container = option . closest ( '.ui-select-choices-row' ) ;
300
388
301
- expect ( getMatchLabel ( this . el ) ) . not . toEqual ( 'Wladimir' ) ;
389
+ expect ( container . hasClass ( 'disabled' ) ) . toBeTruthy ( ) ;
390
+ } ) ;
302
391
} ) ;
303
392
304
- it ( 'should set a disabled class on the option' , function ( ) {
305
- var option = $ ( this . el ) . find ( '.ui-select-choices-row div:contains("Wladimir")' ) ;
306
- var container = option . closest ( '.ui-select-choices-row' ) ;
393
+ describe ( 'disable on expression' , function ( ) {
394
+ beforeEach ( function ( ) {
395
+ disablePerson ( {
396
+ disableAttr : 'status' ,
397
+ disableBool : 'inactive'
398
+ } ) ;
399
+ this . el = createUiSelect ( {
400
+ disabled : "person.status == 'inactive'"
401
+ } ) ;
402
+ } ) ;
403
+
404
+ it ( 'should allow the user to define the selected option' , function ( ) {
405
+ expect ( $ ( this . el ) . find ( '.ui-select-choices' ) . attr ( 'ui-disable-choice' ) ) . toBe ( "person.status == 'inactive'" ) ;
406
+ } ) ;
407
+
408
+ it ( 'should not allow disabled options to be selected' , function ( ) {
409
+ clickItem ( this . el , 'Wladimir' ) ;
410
+
411
+ expect ( getMatchLabel ( this . el ) ) . not . toEqual ( 'Wladimir' ) ;
412
+ } ) ;
307
413
308
- expect ( container . hasClass ( 'disabled' ) ) . toBeTruthy ( ) ;
414
+ it ( 'should set a disabled class on the option' , function ( ) {
415
+ var option = $ ( this . el ) . find ( '.ui-select-choices-row div:contains("Wladimir")' ) ;
416
+ var container = option . closest ( '.ui-select-choices-row' ) ;
417
+
418
+ expect ( container . hasClass ( 'disabled' ) ) . toBeTruthy ( ) ;
419
+ } ) ;
420
+ } ) ;
421
+
422
+ afterEach ( function ( ) {
423
+ resetScope ( ) ;
309
424
} ) ;
310
425
} ) ;
311
426
0 commit comments