search-tests.js 1.4 KB

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