@@ -9,6 +9,87 @@ var Utils = require('select2/utils');
99
1010var options = new Options ( { } ) ;
1111
12+ test ( 'backspace will remove a choice' , function ( assert ) {
13+ expect ( 3 ) ;
14+
15+ var KEYS = require ( 'select2/keys' ) ;
16+
17+ var $container = $ ( '#qunit-fixture .event-container' ) ;
18+ var container = new MockContainer ( ) ;
19+
20+ var CustomSelection = Utils . Decorate ( MultipleSelection , InlineSearch ) ;
21+
22+ var $element = $ ( '#qunit-fixture .multiple' ) ;
23+ var selection = new CustomSelection ( $element , options ) ;
24+
25+ var $selection = selection . render ( ) ;
26+ selection . bind ( container , $container ) ;
27+
28+ // The unselect event should be triggered at some point
29+ selection . on ( 'unselect' , function ( ) {
30+ assert . ok ( true , 'A choice was unselected' ) ;
31+ } ) ;
32+
33+ // Add some selections and render the search
34+ selection . update ( [
35+ {
36+ id : '1' ,
37+ text : 'One'
38+ }
39+ ] ) ;
40+
41+ var $search = $selection . find ( 'input' ) ;
42+ var $choices = $selection . find ( '.select2-selection__choice' ) ;
43+
44+ assert . equal ( $search . length , 1 , 'The search was visible' ) ;
45+ assert . equal ( $choices . length , 1 , 'The choice was rendered' ) ;
46+
47+ // Trigger the backspace on the search
48+ var backspace = $ . Event ( 'keydown' , {
49+ which : KEYS . BACKSPACE
50+ } ) ;
51+ $search . trigger ( backspace ) ;
52+ } ) ;
53+
54+ test ( 'backspace will set the search text' , function ( assert ) {
55+ expect ( 3 ) ;
56+
57+ var KEYS = require ( 'select2/keys' ) ;
58+
59+ var $container = $ ( '#qunit-fixture .event-container' ) ;
60+ var container = new MockContainer ( ) ;
61+
62+ var CustomSelection = Utils . Decorate ( MultipleSelection , InlineSearch ) ;
63+
64+ var $element = $ ( '#qunit-fixture .multiple' ) ;
65+ var selection = new CustomSelection ( $element , options ) ;
66+
67+ var $selection = selection . render ( ) ;
68+ selection . bind ( container , $container ) ;
69+
70+ // Add some selections and render the search
71+ selection . update ( [
72+ {
73+ id : '1' ,
74+ text : 'One'
75+ }
76+ ] ) ;
77+
78+ var $search = $selection . find ( 'input' ) ;
79+ var $choices = $selection . find ( '.select2-selection__choice' ) ;
80+
81+ assert . equal ( $search . length , 1 , 'The search was visible' ) ;
82+ assert . equal ( $choices . length , 1 , 'The choice was rendered' ) ;
83+
84+ // Trigger the backspace on the search
85+ var backspace = $ . Event ( 'keydown' , {
86+ which : KEYS . BACKSPACE
87+ } ) ;
88+ $search . trigger ( backspace ) ;
89+
90+ assert . equal ( $search . val ( ) , 'One' , 'The search text was set' ) ;
91+ } ) ;
92+
1293test ( 'updating selection does not shift the focus' , function ( assert ) {
1394 // Check for IE 8, which triggers a false negative during testing
1495 if ( window . attachEvent && ! window . addEventListener ) {
0 commit comments