search-tests.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. // Check for IE 8, which triggers a false negative during testing
  10. if (window.attachEvent && !window.addEventListener) {
  11. // We must expect 0 assertions or the test will fail
  12. expect(0);
  13. return;
  14. }
  15. var $container = $('#qunit-fixture .event-container');
  16. var container = new MockContainer();
  17. var CustomSelection = Utils.Decorate(MultipleSelection, InlineSearch);
  18. var $element = $('#qunit-fixture .multiple');
  19. var selection = new CustomSelection($element, options);
  20. var $selection = selection.render();
  21. selection.bind(container, $container);
  22. // Update the selection so the search is rendered
  23. selection.update([]);
  24. // Make it visible so the browser can place focus on the search
  25. $container.append($selection);
  26. var $search = $selection.find('input');
  27. $search.trigger('focus');
  28. assert.equal($search.length, 1, 'The search was not visible');
  29. assert.equal(
  30. document.activeElement,
  31. $search[0],
  32. 'The search did not have focus originally'
  33. );
  34. // Trigger an update, this should redraw the search box
  35. selection.update([]);
  36. assert.equal($search.length, 1, 'The search box disappeared');
  37. assert.equal(
  38. document.activeElement,
  39. $search[0],
  40. 'The search did not have focus after the selection was updated'
  41. );
  42. });