No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search-tests.js 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. assert.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. assert.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. assert.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. });
  102. test('the focus event shifts the focus', function (assert) {
  103. // Check for IE 8, which triggers a false negative during testing
  104. if (window.attachEvent && !window.addEventListener) {
  105. // We must expect 0 assertions or the test will fail
  106. assert.expect(0);
  107. return;
  108. }
  109. var $container = $('#qunit-fixture .event-container');
  110. var container = new MockContainer();
  111. var CustomSelection = Utils.Decorate(MultipleSelection, InlineSearch);
  112. var $element = $('#qunit-fixture .multiple');
  113. var selection = new CustomSelection($element, options);
  114. var $selection = selection.render();
  115. selection.bind(container, $container);
  116. // Update the selection so the search is rendered
  117. selection.update([]);
  118. // Make it visible so the browser can place focus on the search
  119. $container.append($selection);
  120. // The search should not be automatically focused
  121. var $search = $selection.find('input');
  122. assert.notEqual(
  123. document.activeElement,
  124. $search[0],
  125. 'The search had focus originally'
  126. );
  127. assert.equal($search.length, 1, 'The search was not visible');
  128. // Focus the container
  129. container.trigger('focus');
  130. // Make sure it focuses the search
  131. assert.equal($search.length, 1, 'The search box disappeared');
  132. assert.equal(
  133. document.activeElement,
  134. $search[0],
  135. 'The search did not have focus originally'
  136. );
  137. });
  138. test('search box without text should propagate click', function (assert) {
  139. assert.expect(1);
  140. var $container = $('#qunit-fixture .event-container');
  141. var container = new MockContainer();
  142. var CustomSelection = Utils.Decorate(MultipleSelection, InlineSearch);
  143. var $element = $('#qunit-fixture .multiple');
  144. var selection = new CustomSelection($element, options);
  145. var $selection = selection.render();
  146. selection.bind(container, $container);
  147. // Update the selection so the search is rendered
  148. selection.update([]);
  149. // Make it visible so the browser can place focus on the search
  150. $container.append($selection);
  151. $selection.on('click', function () {
  152. assert.ok(true, 'The click event should not have been trapped');
  153. });
  154. var $search = $selection.find('input');
  155. $search.trigger('click');
  156. });
  157. test('search box with text should not propagate click', function (assert) {
  158. assert.expect(0);
  159. var $container = $('#qunit-fixture .event-container');
  160. var container = new MockContainer();
  161. var CustomSelection = Utils.Decorate(MultipleSelection, InlineSearch);
  162. var $element = $('#qunit-fixture .multiple');
  163. var selection = new CustomSelection($element, options);
  164. var $selection = selection.render();
  165. selection.bind(container, $container);
  166. // Update the selection so the search is rendered
  167. selection.update([]);
  168. // Make it visible so the browser can place focus on the search
  169. $container.append($selection);
  170. $selection.on('click', function () {
  171. assert.ok(false, 'The click event should have been trapped');
  172. });
  173. var $search = $selection.find('input');
  174. $search.val('test');
  175. $search.trigger('click');
  176. });
  177. test('search box with text should not close dropdown', function (assert) {
  178. assert.expect(0);
  179. var $container = $('#qunit-fixture .event-container');
  180. var container = new MockContainer();
  181. var CustomSelection = Utils.Decorate(MultipleSelection, InlineSearch);
  182. var $element = $('#qunit-fixture .multiple');
  183. var selection = new CustomSelection($element, options);
  184. var $selection = selection.render();
  185. selection.bind(container, $container);
  186. // Update the selection so the search is rendered
  187. selection.update([]);
  188. // Make it visible so the browser can place focus on the search
  189. $container.append($selection);
  190. container.on('close', function () {
  191. assert.ok(false, 'The dropdown should not have closed');
  192. });
  193. var $search = $selection.find('input');
  194. $search.val('test');
  195. $search.trigger('click');
  196. });