@@ -94,7 +94,8 @@ describe('ui-select tests', function() {
94
94
}
95
95
96
96
function createUiSelect ( attrs ) {
97
- var attrsHtml = '' ;
97
+ var attrsHtml = '' ,
98
+ matchAttrsHtml = '' ;
98
99
if ( attrs !== undefined ) {
99
100
if ( attrs . disabled !== undefined ) { attrsHtml += ' ng-disabled="' + attrs . disabled + '"' ; }
100
101
if ( attrs . required !== undefined ) { attrsHtml += ' ng-required="' + attrs . required + '"' ; }
@@ -104,11 +105,12 @@ describe('ui-select tests', function() {
104
105
if ( attrs . taggingTokens !== undefined ) { attrsHtml += ' tagging-tokens="' + attrs . taggingTokens + '"' ; }
105
106
if ( attrs . title !== undefined ) { attrsHtml += ' title="' + attrs . title + '"' ; }
106
107
if ( attrs . appendToBody != undefined ) { attrsHtml += ' append-to-body="' + attrs . appendToBody + '"' ; }
108
+ if ( attrs . allowClear != undefined ) { matchAttrsHtml += ' allow-clear="' + attrs . allowClear + '"' ; }
107
109
}
108
110
109
111
return compileTemplate (
110
112
'<ui-select ng-model="selection.selected"' + attrsHtml + '> \
111
- <ui-select-match placeholder="Pick one...">{{$select.selected.name}}</ui-select-match> \
113
+ <ui-select-match placeholder="Pick one..."' + matchAttrsHtml + ' >{{$select.selected.name}}</ui-select-match> \
112
114
<ui-select-choices repeat="person in people | filter: $select.search"> \
113
115
<div ng-bind-html="person.name | highlight: $select.search"></div> \
114
116
<div ng-bind-html="person.email | highlight: $select.search"></div> \
@@ -296,6 +298,43 @@ describe('ui-select tests', function() {
296
298
expect ( $select . open ) . toEqual ( false ) ;
297
299
} ) ;
298
300
301
+ it ( 'should clear selection' , function ( ) {
302
+ scope . selection . selected = scope . people [ 0 ] ;
303
+
304
+ var el = createUiSelect ( { theme : 'select2' , allowClear : 'true' } ) ;
305
+ var $select = el . scope ( ) . $select ;
306
+
307
+ // allowClear should be true.
308
+ expect ( $select . allowClear ) . toEqual ( true ) ;
309
+
310
+ // Trigger clear.
311
+ el . find ( '.select2-search-choice-close' ) . click ( ) ;
312
+ expect ( scope . selection . selected ) . toEqual ( undefined ) ;
313
+
314
+ // If there is no selection it the X icon should be gone.
315
+ expect ( el . find ( '.select2-search-choice-close' ) . length ) . toEqual ( 0 ) ;
316
+
317
+ } ) ;
318
+
319
+ it ( 'should toggle allow-clear directive' , function ( ) {
320
+ scope . selection . selected = scope . people [ 0 ] ;
321
+ scope . isClearAllowed = false ;
322
+
323
+ var el = createUiSelect ( { theme : 'select2' , allowClear : '{{isClearAllowed}}' } ) ;
324
+ var $select = el . scope ( ) . $select ;
325
+
326
+ expect ( $select . allowClear ) . toEqual ( false ) ;
327
+ expect ( el . find ( '.select2-search-choice-close' ) . length ) . toEqual ( 0 ) ;
328
+
329
+ // Turn clear on
330
+ scope . isClearAllowed = true ;
331
+ scope . $digest ( ) ;
332
+
333
+ expect ( $select . allowClear ) . toEqual ( true ) ;
334
+ expect ( el . find ( '.select2-search-choice-close' ) . length ) . toEqual ( 1 ) ;
335
+ } ) ;
336
+
337
+
299
338
it ( 'should pass tabindex to focusser' , function ( ) {
300
339
var el = createUiSelect ( { tabindex : 5 } ) ;
301
340
0 commit comments