설명 없음

bootstrap-multiselect.js 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. describe('Bootstrap Multiselect "Core".', function() {
  2. beforeEach(function() {
  3. var $select = $('<select id="multiselect" multiple="multiple"></select>');
  4. for (var i = 1; i < 100; i++) {
  5. var $option = $('<option value="' + i + '">' + i + '</option>');
  6. if (i < 10) {
  7. $option.prop('selected', true);
  8. }
  9. $select.append($option);
  10. }
  11. $('body').append($select);
  12. $select.multiselect({
  13. buttonContainer: '<div id="multiselect-container"></div>'
  14. });
  15. });
  16. it('Should add the container after the select.', function() {
  17. expect($('#multiselect-container').length).toBe(1);
  18. });
  19. it('Should add the multiselect button.', function() {
  20. expect($('#multiselect-container .multiselect').length).toBe(1);
  21. });
  22. it('Should add the dropdown menu.', function() {
  23. expect($('#multiselect-container .dropdown-menu').length).toBe(1);
  24. });
  25. it('Should add an li element with checkbox and label for each option.', function() {
  26. expect($('#multiselect-container li').length).toBe(99);
  27. expect($('#multiselect-container label').length).toBe(99);
  28. expect($('#multiselect-container input[type="checkbox"]').length).toBe(99);
  29. });
  30. it('Should preserve selected options.', function() {
  31. expect($('#multiselect-container input[type="checkbox"]:checked').length).toBe(9);
  32. expect($('#multiselect option:selected').length).toBe(9);
  33. });
  34. it('Should be able to select options by value.', function() {
  35. $('#multiselect').multiselect('select', '10');
  36. expect($('#multiselect option[value="10"]').prop('selected')).toBe(true);
  37. expect($('#multiselect-container input[value="10"]').prop('checked')).toBe(true);
  38. });
  39. it('Select method should handle "null" and "undefined" correctly.', function() {
  40. expect($('#multiselect option:selected').length).toBe(9);
  41. $('#multiselect').multiselect('select', null);
  42. expect($('#multiselect option:selected').length).toBe(9);
  43. $('#multiselect').multiselect('select', undefined);
  44. expect($('#multiselect option:selected').length).toBe(9);
  45. });
  46. it('Should be able to deselect options by value.', function() {
  47. $('#multiselect').multiselect('select', '10');
  48. $('#multiselect').multiselect('deselect', '10');
  49. expect($('#multiselect option[value="10"]').prop('selected')).toBe(false);
  50. expect($('#multiselect-container input[value="10"]').prop('checked')).toBe(false);
  51. });
  52. it('Deselect method should handle "null" and "undefined" correctly.', function() {
  53. expect($('#multiselect option:selected').length).toBe(9);
  54. $('#multiselect').multiselect('deselect', null);
  55. expect($('#multiselect option:selected').length).toBe(9);
  56. $('#multiselect').multiselect('deselect', undefined);
  57. expect($('#multiselect option:selected').length).toBe(9);
  58. });
  59. it('Should be able to select options using an array of values.', function() {
  60. $('#multiselect').multiselect('select', ['10', '11']);
  61. expect($('#multiselect option[value="10"]').prop('selected')).toBe(true);
  62. expect($('#multiselect-container input[value="10"]').prop('checked')).toBe(true);
  63. expect($('#multiselect option[value="11"]').prop('selected')).toBe(true);
  64. expect($('#multiselect-container input[value="11"]').prop('checked')).toBe(true);
  65. });
  66. it('Should be able to deselect options using an array of values.', function() {
  67. $('#multiselect').multiselect('select', ['10', '11']);
  68. $('#multiselect').multiselect('deselect', ['10', '11']);
  69. expect($('#multiselect option[value="10"]').prop('selected')).toBe(false);
  70. expect($('#multiselect-container input[value="10"]').prop('checked')).toBe(false);
  71. expect($('#multiselect option[value="11"]').prop('selected')).toBe(false);
  72. expect($('#multiselect-container input[value="11"]').prop('checked')).toBe(false);
  73. });
  74. it('Should be able to disable the multiselect', function() {
  75. $('#multiselect').multiselect('disable');
  76. expect($('#multiselect').prop('disabled')).toBe(true);
  77. });
  78. it('Should be able to enable the multiselect', function() {
  79. $('#multiselect').multiselect('disable');
  80. $('#multiselect').multiselect('enable');
  81. expect($('#multiselect').prop('disabled')).toBe(false);
  82. });
  83. it('Should be able to select all options.', function() {
  84. $('#multiselect').multiselect('selectAll');
  85. for (var i = 1; i < 100; i++) {
  86. expect($('#multiselect option[value="' + i.toString() + '"]').prop('selected')).toBe(true);
  87. expect($('#multiselect-container input[value="' + i.toString() + '"]').prop('checked')).toBe(true);
  88. }
  89. });
  90. it('Should be able to deselect all options.', function() {
  91. $('#multiselect').multiselect('selectAll');
  92. for (var i = 1; i < 100; i++) {
  93. expect($('#multiselect option[value="' + i.toString() + '"]').prop('selected')).toBe(true);
  94. expect($('#multiselect-container input[value="' + i.toString() + '"]').prop('checked')).toBe(true);
  95. }
  96. $('#multiselect').multiselect('deselectAll');
  97. for (var i = 1; i < 100; i++) {
  98. expect($('#multiselect option[value="' + i.toString() + '"]').prop('selected')).toBe(false);
  99. expect($('#multiselect-container input[value="' + i.toString() + '"]').prop('checked')).toBe(false);
  100. }
  101. });
  102. it('Should update the checkboxes according to the selected options after using refresh.', function() {
  103. for (var i = 10; i < 20; i++) {
  104. $('#multiselect option[value="' + i + '"]').prop('selected', true);
  105. }
  106. expect($('#multiselect option:selected').length).toBe(19);
  107. expect($('#multiselect-container input[type="checkbox"]:checked').length).toBe(9);
  108. $('#multiselect').multiselect('refresh');
  109. expect($('#multiselect-container input[type="checkbox"]:checked').length).toBe(19);
  110. for (var i = 10; i < 20; i++) {
  111. expect($('#multiselect option[value="' + i + '"]').prop('selected')).toBe(true);
  112. }
  113. });
  114. it('Should remove container, button and ul after destroy.', function() {
  115. $('#multiselect').multiselect('destroy');
  116. // Destroy should remove container, button and ul.
  117. expect($('#multiselect-container.multiselect-container').length).toBe(0);
  118. expect($('#multiselect-container .multiselect').length).toBe(0);
  119. expect($('#multiselect-container .dropdown-menu').length).toBe(0);
  120. });
  121. it('Should select an option when checkbox is changed (change event).', function() {
  122. $('#multiselect-container li input[value="10"]').prop('checked', true);
  123. $('#multiselect-container li input[value="10"]').trigger('change');
  124. expect($('#multiselect-container input[value="10"]').prop('checked')).toBe(true);
  125. expect($('#multiselect option[value="10"]').prop('selected')).toBe(true);
  126. });
  127. it('Should deselect an option when checkbox is changed (change event).', function() {
  128. $('#multiselect-container li input[value="10"]').prop('checked', true);
  129. $('#multiselect-container li input[value="10"]').trigger('change');
  130. // Already checked above.
  131. $('#multiselect-container li input[value="10"]').prop('checked', false);
  132. $('#multiselect-container li input[value="10"]').trigger('change');
  133. expect($('#multiselect-container input[value="10"]').prop('checked')).toBe(false);
  134. expect($('#multiselect option[value="10"]').prop('selected')).toBe(false);
  135. });
  136. it('Should select an option when checkbox is clicked.', function() {
  137. $('#multiselect-container li input[value="10"]').click();
  138. expect($('#multiselect-container input[value="10"]').prop('checked')).toBe(true);
  139. expect($('#multiselect option[value="10"]').prop('selected')).toBe(true);
  140. });
  141. it('Should deselect an option when checkbox is clicked.', function() {
  142. $('#multiselect-container li input[value="10"]').click();
  143. $('#multiselect-container li input[value="10"]').click();
  144. expect($('#multiselect-container input[value="10"]').prop('checked')).toBe(false);
  145. expect($('#multiselect option[value="10"]').prop('selected')).toBe(false);
  146. });
  147. afterEach(function() {
  148. $('#multiselect').multiselect('destroy');
  149. $('#multiselect').remove();
  150. });
  151. });
  152. describe('Bootstrap Multiselect "Single Selection"', function() {
  153. beforeEach(function() {
  154. var $select = $('<select id="multiselect"></select>');
  155. for (var i = 1; i < 100; i++) {
  156. $select.append('<option value="' + i + '">Option ' + i + '</option>');
  157. }
  158. $('body').append($select);
  159. $select.multiselect({
  160. buttonContainer: '<div id="multiselect-container"></div>'
  161. });
  162. });
  163. it('Should create radio buttons instead of checkboxes.', function() {
  164. expect($('#multiselect-container input[type="radio"]').length).toBe(99);
  165. expect($('#multiselect-container input[type="checkbox"]').length).toBe(0);
  166. // Browser selects one option per default.
  167. expect($('#multiselect option:selected').length).toBe(1);
  168. });
  169. it('Only one option at a time can be selected.', function() {
  170. expect($('#multiselect option:selected').length).toBe(1);
  171. var i = 0;
  172. $('#multiselect-container input').each(function() {
  173. if (i === 0) {
  174. expect($(this).prop('checked')).toBe(true);
  175. i++;
  176. }
  177. else {
  178. expect($(this).prop('checked')).toBe(false);
  179. $(this).click();
  180. expect($('#multiselect option:selected').length).toBe(1);
  181. expect($(this).prop('checked')).toBe(true);
  182. i++;
  183. }
  184. });
  185. });
  186. afterEach(function() {
  187. $('#multiselect').multiselect('destroy');
  188. $('#multiselect').remove();
  189. });
  190. });
  191. describe('Bootstrap Multiselect "Optgroups"', function() {
  192. beforeEach(function() {
  193. var $select = $('<select id="multiselect" multiple="multiple"></select>');
  194. for (var i = 1; i < 11; i++) {
  195. var $optgroup = $('<optgroup label="Group ' + i + '"></optgroup>');
  196. for (var j = 1; j < 11; j++) {
  197. $optgroup.append('<option value="' + i + '-' + j + '">Option ' + i + '.' + j + '</option>');
  198. }
  199. $select.append($optgroup);
  200. }
  201. $('body').append($select);
  202. $select.multiselect({
  203. buttonContainer: '<div id="multiselect-container"></div>',
  204. enableClickableOptGroups: true
  205. });
  206. });
  207. it('Should correctly create labels for optgroups.', function() {
  208. expect($('#multiselect-container li.multiselect-group').length).toBe(10);
  209. expect($('#multiselect-container li.multiselect-group-clickable').length).toBe(10);
  210. $('#multiselect-container label.multiselect-group').each(function() {
  211. expect($('input', $(this)).length).toBe(10);
  212. });
  213. });
  214. it('Groups should be clickable.', function() {
  215. expect($('#multiselect option:selected').length).toBe(0);
  216. var i = 0;
  217. $('#multiselect-container li.multiselect-group').each(function() {
  218. $('label', $(this)).click();
  219. expect($('option:selected', $('#multiselect optgroup')[i]).length).toBe(10);
  220. expect($('#multiselect option:selected').length).toBe(10);
  221. $('label', $(this)).click();
  222. i++;
  223. });
  224. });
  225. afterEach(function() {
  226. $('#multiselect').multiselect('destroy');
  227. $('#multiselect').remove();
  228. });
  229. });
  230. describe('Bootstrap Multiselect "Dataprovider"', function() {
  231. beforeEach(function() {
  232. var $select = $('<select id="multiselect" multiple="multiple"></select>');
  233. $('body').append($select);
  234. $select.multiselect({
  235. buttonContainer: '<div id="multiselect-container"></div>'
  236. });
  237. });
  238. var options = [
  239. {label: 'Option 1', value: '1', selected: true, title: 'Option 1 Title'},
  240. {label: 'Option 2', value: '2', title: 'Option 2 Title'},
  241. {label: 'Option 3', value: '3', selected: true, title: 'Option 3 Title'},
  242. {label: 'Option 4', value: '4', title: 'Option 4 Title'},
  243. {label: 'Option 5', value: '5', title: 'Option 5 Title'},
  244. {label: 'Option 6', value: '6', title: 'Option 6 Title'}
  245. ];
  246. it("Should be able to add options.", function() {
  247. $('#multiselect').multiselect('dataprovider', options);
  248. expect($('#multiselect option').length).toBe(6);
  249. expect($('#multiselect-container input').length).toBe(6);
  250. expect($('#multiselect option[value="1"]').length).toBe(1);
  251. expect($('#multiselect option[value="1"]').prop('selected')).toBe(true);
  252. expect($('#multiselect-container input[value="1"]').prop('checked')).toBe(true);
  253. expect($('#multiselect option[value="2"]').length).toBe(1);
  254. expect($('#multiselect option[value="2"]').prop('selected')).toBe(false);
  255. expect($('#multiselect-container input[value="2"]').prop('checked')).toBe(false);
  256. expect($('#multiselect option[value="3"]').length).toBe(1);
  257. expect($('#multiselect option[value="3"]').prop('selected')).toBe(true);
  258. expect($('#multiselect-container input[value="3"]').prop('checked')).toBe(true);
  259. expect($('#multiselect option[value="4"]').length).toBe(1);
  260. expect($('#multiselect option[value="4"]').prop('selected')).toBe(false);
  261. expect($('#multiselect-container input[value="4"]').prop('checked')).toBe(false);
  262. expect($('#multiselect option[value="5"]').length).toBe(1);
  263. expect($('#multiselect option[value="5"]').prop('selected')).toBe(false);
  264. expect($('#multiselect-container input[value="5"]').prop('checked')).toBe(false);
  265. expect($('#multiselect option[value="6"]').length).toBe(1);
  266. expect($('#multiselect option[value="6"]').prop('selected')).toBe(false);
  267. expect($('#multiselect-container input[value="6"]').prop('checked')).toBe(false);
  268. });
  269. it("Should be able to define title.", function() {
  270. $('#multiselect').multiselect('dataprovider', options);
  271. expect($('#multiselect option[value="1"]').attr('title')).toBe('Option 1 Title');
  272. expect($('#multiselect option[value="2"]').attr('title')).toBe('Option 2 Title');
  273. expect($('#multiselect option[value="3"]').attr('title')).toBe('Option 3 Title');
  274. expect($('#multiselect option[value="4"]').attr('title')).toBe('Option 4 Title');
  275. expect($('#multiselect option[value="5"]').attr('title')).toBe('Option 5 Title');
  276. expect($('#multiselect option[value="6"]').attr('title')).toBe('Option 6 Title');
  277. expect($('#multiselect-container input[value="1"]').closest('label').attr('title')).toBe('Option 1 Title');
  278. expect($('#multiselect-container input[value="2"]').closest('label').attr('title')).toBe('Option 2 Title');
  279. expect($('#multiselect-container input[value="3"]').closest('label').attr('title')).toBe('Option 3 Title');
  280. expect($('#multiselect-container input[value="4"]').closest('label').attr('title')).toBe('Option 4 Title');
  281. expect($('#multiselect-container input[value="5"]').closest('label').attr('title')).toBe('Option 5 Title');
  282. expect($('#multiselect-container input[value="6"]').closest('label').attr('title')).toBe('Option 6 Title');
  283. });
  284. var optgroups = [
  285. {
  286. label: 'Group 1', children: [
  287. {label: 'Option 1.1', value: '1-1'},
  288. {label: 'Option 1.2', value: '1-2'},
  289. {label: 'Option 1.3', value: '1-3'}
  290. ]
  291. },
  292. {
  293. label: 'Group 2', children: [
  294. {label: 'Option 2.1', value: '1'},
  295. {label: 'Option 2.2', value: '2'},
  296. {label: 'Option 2.3', value: '3'}
  297. ]
  298. }
  299. ];
  300. it('Should be able to handle optgroups.', function() {
  301. $('#multiselect').multiselect('dataprovider', optgroups);
  302. expect($('#multiselect optgroup').length).toBe(2);
  303. expect($('#multiselect option').length).toBe(6);
  304. expect($('#multiselect-container input').length).toBe(6);
  305. expect($('#multiselect optgroup[label="Group 1"] option').length).toBe(3);
  306. expect($('#multiselect optgroup[label="Group 2"] option').length).toBe(3);
  307. });
  308. afterEach(function() {
  309. $('#multiselect').multiselect('destroy');
  310. $('#multiselect').remove();
  311. });
  312. });
  313. describe('Bootstrap Multiselect "Select All".', function() {
  314. var onSelectAllTriggered = false;
  315. var onDeselectAllTriggered = false;
  316. beforeEach(function() {
  317. var $select = $('<select id="multiselect" multiple="multiple"></select>');
  318. for (var i = 1; i < 100; i++) {
  319. $select.append('<option value="' + i + '">1</option>');
  320. }
  321. $('body').append($select);
  322. $select.multiselect({
  323. buttonContainer: '<div id="multiselect-container"></div>',
  324. includeSelectAllOption: true,
  325. selectAllValue: 'multiselect-all',
  326. onSelectAll: function() {
  327. onSelectAllTriggered = true;
  328. },
  329. onDeselectAll: function() {
  330. onDeselectAllTriggered = true;
  331. }
  332. });
  333. });
  334. it('Should not add an additional option to the select.', function() {
  335. expect($('#multiselect option[value="multiselect-all"]').length).toBe(0);
  336. expect($('#multiselect-container input[value="multiselect-all"]').length).toBe(1);
  337. expect($('#multiselect option').length).toBe(99);
  338. expect($('#multiselect-container input').length).toBe(100);
  339. });
  340. it('Should update the select all when all options are selected by the "select" method.', function() {
  341. expect($('#multiselect option:selected').length).toBe(0);
  342. expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
  343. for (var i = 1; i < 100; i++) {
  344. $('#multiselect').multiselect('select', i.toString());
  345. expect($('#multiselect option[value="' + i.toString() + '"]').prop('selected')).toBe(true);
  346. }
  347. expect($('#multiselect option:selected').length).toBe(99);
  348. expect($('#multiselect-container input').length).toBe(100);
  349. expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(true);
  350. });
  351. it('Should update the select all when all options are deselected by the "deselect" method (first all options are selected as before).', function() {
  352. expect($('#multiselect option:selected').length).toBe(0);
  353. expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
  354. for (var i = 1; i < 100; i++) {
  355. $('#multiselect').multiselect('select', i.toString());
  356. }
  357. expect($('#multiselect option:selected').length).toBe(99);
  358. expect($('#multiselect-container input:checked').length).toBe(100);
  359. expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(true);
  360. for (var i = 1; i < 100; i++) {
  361. $('#multiselect').multiselect('deselect', i.toString());
  362. }
  363. expect($('#multiselect option:selected').length).toBe(0);
  364. expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
  365. });
  366. it('Should update the select all option when all options are selected by the change event.', function() {
  367. expect($('#multiselect option:selected').length).toBe(0);
  368. expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
  369. // Change all checkboxes except the select all one.
  370. $('#multiselect-container input[value!="multiselect-all"]').prop('checked', true);
  371. $('#multiselect-container input[value!="multiselect-all"]').trigger('change');
  372. expect($('#multiselect option:selected[value!="multiselect-all"]').length).toBe(99);
  373. // 100 options seleted including the select all.
  374. expect($('#multiselect option:selected').length).toBe(99);
  375. expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(true);
  376. });
  377. it('Should update the select all option when all options are deselected by the change event.', function() {
  378. expect($('#multiselect option:selected').length).toBe(0);
  379. expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
  380. $('#multiselect-container input[value!="multiselect-all"]').prop('checked', true);
  381. $('#multiselect-container input[value!="multiselect-all"]').trigger('change');
  382. expect($('#multiselect option:selected[value!="multiselect-all"]').length).toBe(99);
  383. expect($('#multiselect option:selected').length).toBe(99);
  384. expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(true);
  385. // Change all checkboxes except the select all one.
  386. $('#multiselect-container input[value!="multiselect-all"]').prop('checked', false);
  387. $('#multiselect-container input[value!="multiselect-all"]').trigger('change');
  388. expect($('#multiselect option:selected').length).toBe(0);
  389. expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
  390. });
  391. it('Should update the select all option when all options are selected by the click event.', function() {
  392. expect($('#multiselect option:selected').length).toBe(0);
  393. expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
  394. $('#multiselect-container input[value!="multiselect-all"]').click();
  395. expect($('#multiselect option:selected').length).toBe(99);
  396. expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(true);
  397. });
  398. it('Should update the select all option when all options are deselected by the click event.', function() {
  399. expect($('#multiselect option:selected').length).toBe(0);
  400. expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
  401. $('#multiselect-container input[value!="multiselect-all"]').click();
  402. expect($('#multiselect option:selected').length).toBe(99);
  403. expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(true);
  404. $('#multiselect-container input[value!="multiselect-all"]').click();
  405. expect($('#multiselect option:selected').length).toBe(0);
  406. expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
  407. });
  408. it('Should trigger onSelectAll based on the change event.', function() {
  409. expect($('#multiselect option:selected').length).toBe(0);
  410. expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
  411. // Change all checkboxes except the select all one.
  412. $('#multiselect-container input[value!="multiselect-all"]').prop('checked', true);
  413. $('#multiselect-container input[value!="multiselect-all"]').trigger('change');
  414. expect($('#multiselect option:selected[value!="multiselect-all"]').length).toBe(99);
  415. // 100 options seleted including the select all.
  416. expect($('#multiselect option:selected').length).toBe(99);
  417. expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(true);
  418. expect(onSelectAllTriggered).toBe(true);
  419. });
  420. it('Should trigger onSelectAll based on the click event.', function() {
  421. expect($('#multiselect option:selected').length).toBe(0);
  422. expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(false);
  423. $('#multiselect-container input[value!="multiselect-all"]').click();
  424. expect($('#multiselect option:selected').length).toBe(99);
  425. expect($('#multiselect-container input[value="multiselect-all"]').prop('checked')).toBe(true);
  426. expect(onSelectAllTriggered).toBe(true);
  427. });
  428. it('Should trigger onSelectAll on function call.', function() {
  429. $('#multiselect').multiselect('selectAll', true, true);
  430. expect(onSelectAllTriggered).toBe(true);
  431. });
  432. afterEach(function() {
  433. $('#multiselect').multiselect('destroy');
  434. $('#multiselect').remove();
  435. });
  436. });
  437. describe('Bootstrap Multiselect Specific Issues', function() {
  438. it('#393', function() {
  439. var $select = $('<select id="multiselect" multiple="multiple"></select>');
  440. for (var i = 1; i < 100; i++) {
  441. $select.append('<option value="' + i + '">1</option>');
  442. }
  443. $('body').append($select);
  444. $select.multiselect({
  445. buttonContainer: '<div id="multiselect-container"></div>',
  446. includeSelectAllOption: true,
  447. selectAllValue: 0
  448. });
  449. expect($('#multiselect-container input[value="0"]').length).toBe(1);
  450. expect($('#multiselect-container input[value="0"]').prop('checked')).toBe(false);
  451. $('#multiselect').multiselect('selectAll');
  452. expect($('#multiselect option:selected').length).toBe(99);
  453. expect($('#multiselect-container input[value="0"]').prop('checked')).toBe(true);
  454. $('#multiselect').multiselect('deselectAll');
  455. expect($('#multiselect option:selected').length).toBe(0);
  456. expect($('#multiselect-container input[value="0"]').prop('checked')).toBe(false);
  457. $('#multiselect-container input[value="0"]').click();
  458. expect($('#multiselect option:selected').length).toBe(99);
  459. expect($('#multiselect-container input[value="0"]').prop('checked')).toBe(true);
  460. $('#multiselect-container input[value="0"]').click();
  461. expect($('#multiselect option:selected').length).toBe(0);
  462. expect($('#multiselect-container input[value="0"]').prop('checked')).toBe(false);
  463. $('#multiselect').multiselect('destroy');
  464. $('#multiselect').remove();
  465. });
  466. it('#405', function() {
  467. var selection = document.getSelection();
  468. var range = document.createRange();
  469. var $selection = $('<span>Some text to select</span>');
  470. var $select = $('<select id="multiselect" multiple="multiple"></select>');
  471. for (var i = 1; i < 5; i++) {
  472. $select.append('<option value="' + i + '">select option</option>');
  473. }
  474. $('body').append($selection).append($select);
  475. $select.multiselect({
  476. buttonContainer: '<div id="multiselect-container"></div>',
  477. });
  478. range.selectNodeContents($selection.get(0));
  479. selection.removeAllRanges();
  480. selection.addRange(range);
  481. if (document.getSelection().type === 'Range') {
  482. $('#multiselect-container').find('a:first label').trigger('click');
  483. expect($('#multiselect-container').find('input:first').prop('checked')).toBe(true);
  484. }
  485. $('#multiselect').multiselect('destroy');
  486. $('#multiselect').remove();
  487. $selection.remove();
  488. });
  489. });