Skip to content

Commit 88503d2

Browse files
committed
Add test for 698fe7b
This adds the test that ensures that the search focus is still focused, even after the selection is updated (for whatever reason). Note that we are not triggering the `change` event here, and are instead just re-calling `update` on the selection adapter. This is because we do not bind the `change` event in tests, so the selection is never re-rendered and the tests will pass. The `update` method is triggered during the `change` cycle anyway, so this has the same effect while supporting cases where the selection is re-rendered without the selected values changing.
1 parent 698fe7b commit 88503d2

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

tests/selection/search-tests.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module('Selection containers - Inline search');
2+
3+
var MultipleSelection = require('select2/selection/multiple');
4+
var InlineSearch = require('select2/selection/search');
5+
6+
var $ = require('jquery');
7+
var Options = require('select2/options');
8+
var Utils = require('select2/utils');
9+
10+
var options = new Options({});
11+
12+
test('updating selection does not shift the focus', function (assert) {
13+
var $container = $('#qunit-fixture .event-container');
14+
var container = new MockContainer();
15+
16+
var CustomSelection = Utils.Decorate(MultipleSelection, InlineSearch);
17+
18+
var $element = $('#qunit-fixture .multiple');
19+
var selection = new CustomSelection($element, options);
20+
21+
var $selection = selection.render();
22+
selection.bind(container, $container);
23+
24+
// Update the selection so the search is rendered
25+
selection.update([]);
26+
27+
// Make it visible so the browser can place focus on the search
28+
$container.append($selection);
29+
30+
var $search = $selection.find('input');
31+
$search.trigger('focus');
32+
33+
assert.equal($search.length, 1, 'The search was not visible');
34+
35+
assert.equal(
36+
document.activeElement,
37+
$search[0],
38+
'The search did not have focus originally'
39+
);
40+
41+
// Trigger an update, this should redraw the search box
42+
selection.update([]);
43+
44+
assert.equal($search.length, 1, 'The search box disappeared');
45+
46+
assert.equal(
47+
document.activeElement,
48+
$search[0],
49+
'The search did not have focus after the selection was updated'
50+
);
51+
});

tests/unit.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
<script src="selection/containerCss-tests.js" type="text/javascript"></script>
8080
<script src="selection/multiple-tests.js" type="text/javascript"></script>
8181
<script src="selection/placeholder-tests.js" type="text/javascript"></script>
82+
<script src="selection/search-tests.js" type="text/javascript"></script>
8283
<script src="selection/single-tests.js" type="text/javascript"></script>
8384
<script src="selection/stopPropagation-tests.js" type="text/javascript"></script>
8485

0 commit comments

Comments
 (0)