allowClear-tests.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. module('Selection containers - Placeholders - Allow clear');
  2. var Placeholder = require('select2/selection/placeholder');
  3. var AllowClear = require('select2/selection/allowClear');
  4. var SingleSelection = require('select2/selection/single');
  5. var $ = require('jquery');
  6. var Options = require('select2/options');
  7. var Utils = require('select2/utils');
  8. var AllowClearPlaceholder = Utils.Decorate(
  9. Utils.Decorate(SingleSelection, Placeholder),
  10. AllowClear
  11. );
  12. var allowClearOptions = new Options({
  13. placeholder: {
  14. id: 'placeholder',
  15. text: 'This is the placeholder'
  16. },
  17. allowClear: true
  18. });
  19. test('clear is not displayed for single placeholder', function (assert) {
  20. var selection = new AllowClearPlaceholder(
  21. $('#qunit-fixture .single-with-placeholder'),
  22. allowClearOptions
  23. );
  24. var $selection = selection.render();
  25. selection.update([{
  26. id: 'placeholder'
  27. }]);
  28. assert.equal(
  29. $selection.find('.select2-selection__clear').length,
  30. 0,
  31. 'The clear icon should not be displayed'
  32. );
  33. });
  34. test('clear is not displayed for multiple placeholder', function (assert) {
  35. var selection = new AllowClearPlaceholder(
  36. $('#qunit-fixture .multiple'),
  37. allowClearOptions
  38. );
  39. var $selection = selection.render();
  40. selection.update([]);
  41. assert.equal(
  42. $selection.find('.select2-selection__clear').length,
  43. 0,
  44. 'The clear icon should not be displayed'
  45. );
  46. });
  47. test('clear is displayed for placeholder', function (assert) {
  48. var selection = new AllowClearPlaceholder(
  49. $('#qunit-fixture .single-with-placeholder'),
  50. allowClearOptions
  51. );
  52. var $selection = selection.render();
  53. selection.update([{
  54. id: 'one',
  55. test: 'one'
  56. }]);
  57. assert.equal(
  58. $selection.find('.select2-selection__clear').length,
  59. 1,
  60. 'The clear icon should be displayed'
  61. );
  62. });
  63. test('clicking clear will set the placeholder value', function (assert) {
  64. var $element = $('#qunit-fixture .single-with-placeholder');
  65. var selection = new AllowClearPlaceholder(
  66. $element,
  67. allowClearOptions
  68. );
  69. var container = new MockContainer();
  70. var $selection = selection.render();
  71. selection.bind(container, $('<div></div>'));
  72. $element.val('One');
  73. selection.update([{
  74. id: 'One',
  75. text: 'One'
  76. }]);
  77. var $remove = $selection.find('.select2-selection__clear');
  78. $remove.trigger('mousedown');
  79. assert.equal(
  80. $element.val(),
  81. 'placeholder',
  82. 'The value should have been reset to the placeholder'
  83. );
  84. });
  85. test('clicking clear will trigger the unselect event', function (assert) {
  86. assert.expect(4);
  87. var $element = $('#qunit-fixture .single-with-placeholder');
  88. var selection = new AllowClearPlaceholder(
  89. $element,
  90. allowClearOptions
  91. );
  92. var container = new MockContainer();
  93. var $selection = selection.render();
  94. selection.bind(container, $('<div></div>'));
  95. $element.val('One');
  96. selection.update([{
  97. id: 'One',
  98. text: 'One'
  99. }]);
  100. selection.on('unselect', function (ev) {
  101. assert.ok(
  102. 'data' in ev && ev.data,
  103. 'The event should have been triggered with the data property'
  104. );
  105. assert.ok(
  106. $.isPlainObject(ev.data),
  107. 'The data should be an object'
  108. );
  109. assert.equal(
  110. ev.data.id,
  111. 'One',
  112. 'The data should be the unselected object'
  113. );
  114. assert.equal(
  115. $element.val(),
  116. 'placeholder',
  117. 'The previous value should be unselected'
  118. );
  119. });
  120. var $remove = $selection.find('.select2-selection__clear');
  121. $remove.trigger('mousedown');
  122. });
  123. test('preventing the unselect event cancels the clearing', function (assert) {
  124. var $element = $('#qunit-fixture .single-with-placeholder');
  125. var selection = new AllowClearPlaceholder(
  126. $element,
  127. allowClearOptions
  128. );
  129. var container = new MockContainer();
  130. var $selection = selection.render();
  131. selection.bind(container, $('<div></div>'));
  132. $element.val('One');
  133. selection.update([{
  134. id: 'One',
  135. text: 'One'
  136. }]);
  137. selection.on('unselect', function (ev) {
  138. ev.prevented = true;
  139. });
  140. var $remove = $selection.find('.select2-selection__clear');
  141. $remove.trigger('mousedown');
  142. assert.equal(
  143. $element.val(),
  144. 'One',
  145. 'The placeholder should not have been set'
  146. );
  147. });
  148. test('clicking clear will trigger the clear event', function (assert) {
  149. assert.expect(5);
  150. var $element = $('#qunit-fixture .single-with-placeholder');
  151. var selection = new AllowClearPlaceholder(
  152. $element,
  153. allowClearOptions
  154. );
  155. var container = new MockContainer();
  156. var $selection = selection.render();
  157. selection.bind(container, $('<div></div>'));
  158. $element.val('One');
  159. selection.update([{
  160. id: 'One',
  161. text: 'One'
  162. }]);
  163. selection.on('clear', function (ev) {
  164. assert.ok(
  165. 'data' in ev && ev.data,
  166. 'The event should have been triggered with the data property'
  167. );
  168. assert.ok(
  169. $.isArray(ev.data),
  170. 'The data should be an array'
  171. );
  172. assert.equal(
  173. ev.data.length,
  174. 1,
  175. 'The data should contain one item for each value'
  176. );
  177. assert.equal(
  178. ev.data[0].id,
  179. 'One',
  180. 'The data should contain unselected objects'
  181. );
  182. assert.equal(
  183. $element.val(),
  184. 'placeholder',
  185. 'The previous value should be unselected'
  186. );
  187. });
  188. var $remove = $selection.find('.select2-selection__clear');
  189. $remove.trigger('mousedown');
  190. });
  191. test('preventing the clear event cancels the clearing', function (assert) {
  192. var $element = $('#qunit-fixture .single-with-placeholder');
  193. var selection = new AllowClearPlaceholder(
  194. $element,
  195. allowClearOptions
  196. );
  197. var container = new MockContainer();
  198. var $selection = selection.render();
  199. selection.bind(container, $('<div></div>'));
  200. $element.val('One');
  201. selection.update([{
  202. id: 'One',
  203. text: 'One'
  204. }]);
  205. selection.on('clear', function (ev) {
  206. ev.prevented = true;
  207. });
  208. var $remove = $selection.find('.select2-selection__clear');
  209. $remove.trigger('mousedown');
  210. assert.equal(
  211. $element.val(),
  212. 'One',
  213. 'The placeholder should not have been set'
  214. );
  215. });
  216. test('clear does not work when disabled', function (assert) {
  217. var $element = $('#qunit-fixture .single-with-placeholder');
  218. var selection = new AllowClearPlaceholder(
  219. $element,
  220. allowClearOptions
  221. );
  222. var container = new MockContainer();
  223. var $selection = selection.render();
  224. selection.bind(container, $('<div></div>'));
  225. selection.update([{
  226. id: 'One',
  227. text: 'One'
  228. }]);
  229. $element.val('One');
  230. selection.options.set('disabled', true);
  231. var $remove = $selection.find('.select2-selection__clear');
  232. $remove.trigger('mousedown');
  233. assert.equal(
  234. $element.val(),
  235. 'One',
  236. 'The placeholder should not have been set'
  237. );
  238. });