Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

allowClear-tests.js 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 MultipleSelection = require('select2/selection/multiple');
  6. var $ = require('jquery');
  7. var Options = require('select2/options');
  8. var Utils = require('select2/utils');
  9. var AllowClearPlaceholder = Utils.Decorate(
  10. Utils.Decorate(SingleSelection, Placeholder),
  11. AllowClear
  12. );
  13. var allowClearOptions = new Options({
  14. placeholder: {
  15. id: 'placeholder',
  16. text: 'This is the placeholder'
  17. },
  18. allowClear: true
  19. });
  20. test('clear is not displayed for single placeholder', function (assert) {
  21. var selection = new AllowClearPlaceholder(
  22. $('#qunit-fixture .single-with-placeholder'),
  23. allowClearOptions
  24. );
  25. var $selection = selection.render();
  26. selection.update([{
  27. id: 'placeholder'
  28. }]);
  29. assert.equal(
  30. $selection.find('.select2-selection__clear').length,
  31. 0,
  32. 'The clear icon should not be displayed'
  33. );
  34. });
  35. test('clear is not displayed for multiple placeholder', function (assert) {
  36. var selection = new AllowClearPlaceholder(
  37. $('#qunit-fixture .multiple'),
  38. allowClearOptions
  39. );
  40. var $selection = selection.render();
  41. selection.update([]);
  42. assert.equal(
  43. $selection.find('.select2-selection__clear').length,
  44. 0,
  45. 'The clear icon should not be displayed'
  46. );
  47. });
  48. test('clear is displayed for placeholder', function (assert) {
  49. var selection = new AllowClearPlaceholder(
  50. $('#qunit-fixture .single-with-placeholder'),
  51. allowClearOptions
  52. );
  53. var $selection = selection.render();
  54. selection.update([{
  55. id: 'one',
  56. test: 'one'
  57. }]);
  58. assert.equal(
  59. $selection.find('.select2-selection__clear').length,
  60. 1,
  61. 'The clear icon should be displayed'
  62. );
  63. });
  64. test('clear icon should have title displayed', function (assert) {
  65. var selection = new AllowClearPlaceholder(
  66. $('#qunit-fixture .single-with-placeholder'),
  67. allowClearOptions
  68. );
  69. var $selection = selection.render();
  70. selection.update([{
  71. id: 'one',
  72. test: 'one'
  73. }]);
  74. assert.equal(
  75. $selection.find('.select2-selection__clear').attr('title'),
  76. 'Remove all items',
  77. 'The clear icon should have title displayed'
  78. );
  79. });
  80. test('clicking clear will set the placeholder value', function (assert) {
  81. var $element = $('#qunit-fixture .single-with-placeholder');
  82. var selection = new AllowClearPlaceholder(
  83. $element,
  84. allowClearOptions
  85. );
  86. var container = new MockContainer();
  87. var $selection = selection.render();
  88. selection.bind(container, $('<div></div>'));
  89. $element.val('One');
  90. selection.update([{
  91. id: 'One',
  92. text: 'One'
  93. }]);
  94. var $remove = $selection.find('.select2-selection__clear');
  95. $remove.trigger('mousedown');
  96. assert.equal(
  97. $element.val(),
  98. 'placeholder',
  99. 'The value should have been reset to the placeholder'
  100. );
  101. });
  102. test('clicking clear will trigger the unselect event', function (assert) {
  103. assert.expect(4);
  104. var $element = $('#qunit-fixture .single-with-placeholder');
  105. var selection = new AllowClearPlaceholder(
  106. $element,
  107. allowClearOptions
  108. );
  109. var container = new MockContainer();
  110. var $selection = selection.render();
  111. selection.bind(container, $('<div></div>'));
  112. $element.val('One');
  113. selection.update([{
  114. id: 'One',
  115. text: 'One'
  116. }]);
  117. selection.on('unselect', function (ev) {
  118. assert.ok(
  119. 'data' in ev && ev.data,
  120. 'The event should have been triggered with the data property'
  121. );
  122. assert.ok(
  123. $.isPlainObject(ev.data),
  124. 'The data should be an object'
  125. );
  126. assert.equal(
  127. ev.data.id,
  128. 'One',
  129. 'The data should be the unselected object'
  130. );
  131. assert.equal(
  132. $element.val(),
  133. 'placeholder',
  134. 'The previous value should be unselected'
  135. );
  136. });
  137. var $remove = $selection.find('.select2-selection__clear');
  138. $remove.trigger('mousedown');
  139. });
  140. test('preventing the unselect event cancels the clearing', function (assert) {
  141. var $element = $('#qunit-fixture .single-with-placeholder');
  142. var selection = new AllowClearPlaceholder(
  143. $element,
  144. allowClearOptions
  145. );
  146. var container = new MockContainer();
  147. var $selection = selection.render();
  148. selection.bind(container, $('<div></div>'));
  149. $element.val('One');
  150. selection.update([{
  151. id: 'One',
  152. text: 'One'
  153. }]);
  154. selection.on('unselect', function (ev) {
  155. ev.prevented = true;
  156. });
  157. var $remove = $selection.find('.select2-selection__clear');
  158. $remove.trigger('mousedown');
  159. assert.equal(
  160. $element.val(),
  161. 'One',
  162. 'The placeholder should not have been set'
  163. );
  164. });
  165. test('clicking clear will trigger the clear event', function (assert) {
  166. assert.expect(5);
  167. var $element = $('#qunit-fixture .single-with-placeholder');
  168. var selection = new AllowClearPlaceholder(
  169. $element,
  170. allowClearOptions
  171. );
  172. var container = new MockContainer();
  173. var $selection = selection.render();
  174. selection.bind(container, $('<div></div>'));
  175. $element.val('One');
  176. selection.update([{
  177. id: 'One',
  178. text: 'One'
  179. }]);
  180. selection.on('clear', function (ev) {
  181. assert.ok(
  182. 'data' in ev && ev.data,
  183. 'The event should have been triggered with the data property'
  184. );
  185. assert.ok(
  186. $.isArray(ev.data),
  187. 'The data should be an array'
  188. );
  189. assert.equal(
  190. ev.data.length,
  191. 1,
  192. 'The data should contain one item for each value'
  193. );
  194. assert.equal(
  195. ev.data[0].id,
  196. 'One',
  197. 'The data should contain unselected objects'
  198. );
  199. assert.equal(
  200. $element.val(),
  201. 'placeholder',
  202. 'The previous value should be unselected'
  203. );
  204. });
  205. var $remove = $selection.find('.select2-selection__clear');
  206. $remove.trigger('mousedown');
  207. });
  208. test('preventing the clear event cancels the clearing', function (assert) {
  209. var $element = $('#qunit-fixture .single-with-placeholder');
  210. var selection = new AllowClearPlaceholder(
  211. $element,
  212. allowClearOptions
  213. );
  214. var container = new MockContainer();
  215. var $selection = selection.render();
  216. selection.bind(container, $('<div></div>'));
  217. $element.val('One');
  218. selection.update([{
  219. id: 'One',
  220. text: 'One'
  221. }]);
  222. selection.on('clear', function (ev) {
  223. ev.prevented = true;
  224. });
  225. var $remove = $selection.find('.select2-selection__clear');
  226. $remove.trigger('mousedown');
  227. assert.equal(
  228. $element.val(),
  229. 'One',
  230. 'The placeholder should not have been set'
  231. );
  232. });
  233. test('clear does not work when disabled', function (assert) {
  234. var $element = $('#qunit-fixture .single-with-placeholder');
  235. var selection = new AllowClearPlaceholder(
  236. $element,
  237. allowClearOptions
  238. );
  239. var container = new MockContainer();
  240. var $selection = selection.render();
  241. selection.bind(container, $('<div></div>'));
  242. selection.update([{
  243. id: 'One',
  244. text: 'One'
  245. }]);
  246. $element.val('One');
  247. selection.options.set('disabled', true);
  248. var $remove = $selection.find('.select2-selection__clear');
  249. $remove.trigger('mousedown');
  250. assert.equal(
  251. $element.val(),
  252. 'One',
  253. 'The placeholder should not have been set'
  254. );
  255. });
  256. test('clear button doesnt visually break selected options', function (assert) {
  257. var $element = $('<select></select>');
  258. var Selection = Utils.Decorate(
  259. Utils.Decorate(MultipleSelection, Placeholder),
  260. AllowClear
  261. );
  262. var selection = new Selection(
  263. $element,
  264. allowClearOptions
  265. );
  266. var container = new MockContainer();
  267. var $container = $(
  268. '<span class="select2-container select2-container--default"></span>'
  269. );
  270. $('#qunit-fixture').append($container);
  271. var $selection = selection.render();
  272. $container.append($selection);
  273. $container.css('width', '100px');
  274. selection.bind(container, $container);
  275. selection.update([{
  276. id: '1',
  277. text: '1'
  278. }]);
  279. var singleHeight = $container.height();
  280. selection.update([
  281. {
  282. id: '10',
  283. text: '10'
  284. },
  285. {
  286. id: '20',
  287. text: '20'
  288. }
  289. ]);
  290. var doubleHeight = $container.height();
  291. selection.update([
  292. {
  293. id: '1',
  294. text: '1'
  295. },
  296. {
  297. id: '2',
  298. text: '2'
  299. }
  300. ]);
  301. assert.notEqual(
  302. singleHeight,
  303. doubleHeight,
  304. 'The height of the two different rows should be different'
  305. );
  306. assert.equal(
  307. $container.height(),
  308. doubleHeight,
  309. 'There should be two full lines of selections'
  310. );
  311. });