search-tests.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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('backspace will remove a choice', function (assert) {
  9. expect(3);
  10. var KEYS = require('select2/keys');
  11. var $container = $('#qunit-fixture .event-container');
  12. var container = new MockContainer();
  13. var CustomSelection = Utils.Decorate(MultipleSelection, InlineSearch);
  14. var $element = $('#qunit-fixture .multiple');
  15. var selection = new CustomSelection($element, options);
  16. var $selection = selection.render();
  17. selection.bind(container, $container);
  18. // The unselect event should be triggered at some point
  19. selection.on('unselect', function () {
  20. assert.ok(true, 'A choice was unselected');
  21. });
  22. // Add some selections and render the search
  23. selection.update([
  24. {
  25. id: '1',
  26. text: 'One'
  27. }
  28. ]);
  29. var $search = $selection.find('input');
  30. var $choices = $selection.find('.select2-selection__choice');
  31. assert.equal($search.length, 1, 'The search was visible');
  32. assert.equal($choices.length, 1, 'The choice was rendered');
  33. // Trigger the backspace on the search
  34. var backspace = $.Event('keydown', {
  35. which: KEYS.BACKSPACE
  36. });
  37. $search.trigger(backspace);
  38. });
  39. test('backspace will set the search text', function (assert) {
  40. expect(3);
  41. var KEYS = require('select2/keys');
  42. var $container = $('#qunit-fixture .event-container');
  43. var container = new MockContainer();
  44. var CustomSelection = Utils.Decorate(MultipleSelection, InlineSearch);
  45. var $element = $('#qunit-fixture .multiple');
  46. var selection = new CustomSelection($element, options);
  47. var $selection = selection.render();
  48. selection.bind(container, $container);
  49. // Add some selections and render the search
  50. selection.update([
  51. {
  52. id: '1',
  53. text: 'One'
  54. }
  55. ]);
  56. var $search = $selection.find('input');
  57. var $choices = $selection.find('.select2-selection__choice');
  58. assert.equal($search.length, 1, 'The search was visible');
  59. assert.equal($choices.length, 1, 'The choice was rendered');
  60. // Trigger the backspace on the search
  61. var backspace = $.Event('keydown', {
  62. which: KEYS.BACKSPACE
  63. });
  64. $search.trigger(backspace);
  65. assert.equal($search.val(), 'One', 'The search text was set');
  66. });
  67. test('updating selection does not shift the focus', function (assert) {
  68. // Check for IE 8, which triggers a false negative during testing
  69. if (window.attachEvent && !window.addEventListener) {
  70. // We must expect 0 assertions or the test will fail
  71. expect(0);
  72. return;
  73. }
  74. var $container = $('#qunit-fixture .event-container');
  75. var container = new MockContainer();
  76. var CustomSelection = Utils.Decorate(MultipleSelection, InlineSearch);
  77. var $element = $('#qunit-fixture .multiple');
  78. var selection = new CustomSelection($element, options);
  79. var $selection = selection.render();
  80. selection.bind(container, $container);
  81. // Update the selection so the search is rendered
  82. selection.update([]);
  83. // Make it visible so the browser can place focus on the search
  84. $container.append($selection);
  85. var $search = $selection.find('input');
  86. $search.trigger('focus');
  87. assert.equal($search.length, 1, 'The search was not visible');
  88. assert.equal(
  89. document.activeElement,
  90. $search[0],
  91. 'The search did not have focus originally'
  92. );
  93. // Trigger an update, this should redraw the search box
  94. selection.update([]);
  95. assert.equal($search.length, 1, 'The search box disappeared');
  96. assert.equal(
  97. document.activeElement,
  98. $search[0],
  99. 'The search did not have focus after the selection was updated'
  100. );
  101. });