openOnKeyDown-tests.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. module('Selection containers - Open On Key Down');
  2. var KEYS = require('select2/keys');
  3. var $ = require('jquery');
  4. /**
  5. * Build a keydown event with the given key code and extra options.
  6. *
  7. * @param {Number} keyCode the keyboard code to be used for the 'which'
  8. * attribute of the keydown event.
  9. * @param {Object} eventProps extra properties to build the keydown event.
  10. *
  11. * @return {jQuery.Event} a 'keydown' type event.
  12. */
  13. function buildKeyDownEvent (keyCode, eventProps) {
  14. return $.Event('keydown', $.extend({}, { which: keyCode }, eventProps));
  15. }
  16. /**
  17. * Wrapper function providing a select2 element with a given enabled/disabled
  18. * state that will get a given keydown event triggered on it. Provide an
  19. * assertion callback function to test the results of the triggered event.
  20. *
  21. * @param {Boolean} isEnabled the enabled state of the desired select2
  22. * element.
  23. * @param {String} testName name for the test.
  24. * @param {Number} keyCode used to set the 'which' attribute of the
  25. * keydown event.
  26. * @param {Object} eventProps attributes to be used to build the keydown
  27. * event.
  28. * @param {Function} fn assertion callback to perform checks on the
  29. * result of triggering the event, receives the
  30. * 'assert' variable for the test and the select2
  31. * instance behind the built <select> element.
  32. * @return {null}
  33. */
  34. function testAbled(isEnabled, testName, keyCode, eventProps, fn) {
  35. test(testName, function (assert) {
  36. var $element = $(
  37. '<select>' +
  38. '<option>one</option>' +
  39. '<option>two</option>' +
  40. '</select>'
  41. );
  42. $('#qunit-fixture').append($element);
  43. $element.select2({ disabled: !isEnabled });
  44. var select2 = $element.data('select2');
  45. var $selection = select2.$selection;
  46. assert.notOk(select2.isOpen(), 'The instance should not be open');
  47. assert.equal(select2.isEnabled(), isEnabled);
  48. var event = buildKeyDownEvent(keyCode, eventProps);
  49. assert.ok(event.which, 'The event\'s key code (.which) should be set');
  50. $selection.trigger(event);
  51. fn(assert, select2);
  52. });
  53. }
  54. /**
  55. * Test the given keydown event on an enabled element. See #testAbled for
  56. * params.
  57. */
  58. function testEnabled (testName, keyCode, eventProps, fn) {
  59. testAbled(true, testName, keyCode, eventProps, fn);
  60. }
  61. /**
  62. * Test the given keydown event on a disabled element. See #testAbled for
  63. * params.
  64. */
  65. function testDisabled (testName, keyCode, eventProps, fn) {
  66. testAbled(false, testName, keyCode, eventProps, fn);
  67. }
  68. /**
  69. * Assertion function used by the above test* wrappers. Asserts that the given
  70. * select2 instance is open.
  71. *
  72. * @param {Assert} assert
  73. * @param {Select2} select
  74. * @return {null}
  75. */
  76. function assertOpened (assert, select2) {
  77. assert.ok(select2.isOpen(), 'The element should be open');
  78. }
  79. /**
  80. * Assertion function used by the above test* wrappers. Asserts that the given
  81. * select2 instance is not open.
  82. *
  83. * @param {Assert} assert
  84. * @param {Select2} select
  85. * @return {null}
  86. */
  87. function assertNotOpened (assert, select2) {
  88. assert.notOk(select2.isOpen(), 'The element should not be open');
  89. }
  90. /**
  91. * ENTER, SPACE, and ALT+DOWN should all open an enabled select2 element.
  92. */
  93. testEnabled(
  94. 'enabled element will open on ENTER',
  95. KEYS.ENTER, {},
  96. assertOpened
  97. );
  98. testEnabled(
  99. 'enabled element will open on SPACE',
  100. KEYS.SPACE, {},
  101. assertOpened
  102. );
  103. testEnabled(
  104. 'enabled element will open on ALT+DOWN',
  105. KEYS.DOWN, { altKey: true },
  106. assertOpened
  107. );
  108. /**
  109. * Some other keys triggered on an enabled select2 element should not open it.
  110. */
  111. testEnabled(
  112. 'enabled element will not open on UP',
  113. KEYS.UP, {},
  114. assertNotOpened
  115. );
  116. testEnabled(
  117. 'enabled element will not open on DOWN',
  118. KEYS.UP, {},
  119. assertNotOpened
  120. );
  121. testEnabled(
  122. 'enabled element will not open on LEFT',
  123. KEYS.UP, {},
  124. assertNotOpened
  125. );
  126. testEnabled(
  127. 'enabled element will not open on RIGHT',
  128. KEYS.UP, {},
  129. assertNotOpened
  130. );
  131. /*
  132. * The keys that will open an enabled select2 element should not open a disabled
  133. * one.
  134. */
  135. testDisabled(
  136. 'disabled element will not open on ENTER',
  137. KEYS.ENTER, {},
  138. assertNotOpened
  139. );
  140. testDisabled(
  141. 'disabled element will not open on SPACE',
  142. KEYS.SPACE, {},
  143. assertNotOpened
  144. );
  145. testDisabled(
  146. 'disabled element will not open on ALT+DOWN',
  147. KEYS.DOWN, { altKey: true },
  148. assertNotOpened
  149. );
  150. /**
  151. * Other keys should continue to not open a disabled select2 element.
  152. */
  153. testDisabled(
  154. 'disabled element will not open on UP',
  155. KEYS.UP, {},
  156. assertNotOpened
  157. );
  158. testDisabled(
  159. 'disabled element will not open on DOWN',
  160. KEYS.UP, {},
  161. assertNotOpened
  162. );
  163. testDisabled(
  164. 'disabled element will not open on LEFT',
  165. KEYS.UP, {},
  166. assertNotOpened
  167. );
  168. testDisabled(
  169. 'disabled element will not open on RIGHT',
  170. KEYS.UP, {},
  171. assertNotOpened
  172. );