@@ -94,7 +94,8 @@ describe('ui-select tests', function() {
9494 }
9595
9696 function createUiSelect ( attrs ) {
97- var attrsHtml = '' ;
97+ var attrsHtml = '' ,
98+ matchAttrsHtml = '' ;
9899 if ( attrs !== undefined ) {
99100 if ( attrs . disabled !== undefined ) { attrsHtml += ' ng-disabled="' + attrs . disabled + '"' ; }
100101 if ( attrs . required !== undefined ) { attrsHtml += ' ng-required="' + attrs . required + '"' ; }
@@ -104,11 +105,12 @@ describe('ui-select tests', function() {
104105 if ( attrs . taggingTokens !== undefined ) { attrsHtml += ' tagging-tokens="' + attrs . taggingTokens + '"' ; }
105106 if ( attrs . title !== undefined ) { attrsHtml += ' title="' + attrs . title + '"' ; }
106107 if ( attrs . appendToBody != undefined ) { attrsHtml += ' append-to-body="' + attrs . appendToBody + '"' ; }
108+ if ( attrs . allowClear != undefined ) { matchAttrsHtml += ' allow-clear="' + attrs . allowClear + '"' ; }
107109 }
108110
109111 return compileTemplate (
110112 '<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> \
112114 <ui-select-choices repeat="person in people | filter: $select.search"> \
113115 <div ng-bind-html="person.name | highlight: $select.search"></div> \
114116 <div ng-bind-html="person.email | highlight: $select.search"></div> \
@@ -296,6 +298,43 @@ describe('ui-select tests', function() {
296298 expect ( $select . open ) . toEqual ( false ) ;
297299 } ) ;
298300
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+
299338 it ( 'should pass tabindex to focusser' , function ( ) {
300339 var el = createUiSelect ( { tabindex : 5 } ) ;
301340
0 commit comments