select-tests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. module('Data adapters - Select - current');
  2. var SelectData = require('select2/data/select');
  3. var $ = require('jquery');
  4. var Options = require('select2/options');
  5. var selectOptions = new Options({});
  6. test('current gets default for single', function (assert) {
  7. var $select = $('#qunit-fixture .single');
  8. var data = new SelectData($select, selectOptions);
  9. data.current(function (data) {
  10. assert.equal(
  11. data.length,
  12. 1,
  13. 'There should only be one selected option'
  14. );
  15. var option = data[0];
  16. assert.equal(
  17. option.id,
  18. 'One',
  19. 'The value of the option tag should be the id'
  20. );
  21. assert.equal(
  22. option.text,
  23. 'One',
  24. 'The text within the option tag should be the text'
  25. );
  26. });
  27. });
  28. test('current gets default for multiple', function (assert) {
  29. var $select = $('#qunit-fixture .multiple');
  30. var data = new SelectData($select, selectOptions);
  31. data.current(function (data) {
  32. assert.equal(
  33. data.length,
  34. 0,
  35. 'Multiple selects have no default selection.'
  36. );
  37. });
  38. });
  39. test('current gets options with explicit value', function (assert) {
  40. var $select = $('#qunit-fixture .single');
  41. var $option = $('<option value="1">One</option>');
  42. $select.append($option);
  43. var data = new SelectData($select, selectOptions);
  44. $select.val('1');
  45. data.current(function (data) {
  46. assert.equal(
  47. data.length,
  48. 1,
  49. 'There should be one selected option'
  50. );
  51. var option = data[0];
  52. assert.equal(
  53. option.id,
  54. '1',
  55. 'The option value should be the selected id'
  56. );
  57. assert.equal(
  58. option.text,
  59. 'One',
  60. 'The text should match the text for the option tag'
  61. );
  62. });
  63. });
  64. test('current gets options with implicit value', function (assert) {
  65. var $select = $('#qunit-fixture .single');
  66. var data = new SelectData($select, selectOptions);
  67. $select.val('One');
  68. data.current(function (val) {
  69. assert.equal(
  70. val.length,
  71. 1,
  72. 'There should only be one selected value'
  73. );
  74. var option = val[0];
  75. assert.equal(
  76. option.id,
  77. 'One',
  78. 'The id should be the same as the option text'
  79. );
  80. assert.equal(
  81. option.text,
  82. 'One',
  83. 'The text should be the same as the option text'
  84. );
  85. });
  86. });
  87. test('select works for single', function (assert) {
  88. var $select = $('#qunit-fixture .single-with-placeholder');
  89. var data = new SelectData($select, selectOptions);
  90. assert.equal($select.val(), 'placeholder');
  91. data.select({
  92. id: 'One',
  93. text: 'One'
  94. });
  95. assert.equal($select.val(), 'One');
  96. });
  97. test('multiple sets the value', function (assert) {
  98. var $select = $('#qunit-fixture .multiple');
  99. var data = new SelectData($select, selectOptions);
  100. assert.ok(
  101. $select.val() == null || $select.val().length == 0,
  102. 'nothing should be selected'
  103. );
  104. data.select({
  105. id: 'Two',
  106. text: 'Two'
  107. });
  108. assert.deepEqual($select.val(), ['Two']);
  109. });
  110. test('multiple adds to the old value', function (assert) {
  111. var $select = $('#qunit-fixture .multiple');
  112. var data = new SelectData($select, selectOptions);
  113. $select.val(['Two']);
  114. assert.deepEqual($select.val(), ['Two']);
  115. data.select({
  116. id: 'One',
  117. text: 'One'
  118. });
  119. assert.deepEqual($select.val(), ['One', 'Two']);
  120. });
  121. test('duplicates - single - same id on select triggers change',
  122. function (assert) {
  123. var $select = $('#qunit-fixture .duplicates');
  124. var data = new SelectData($select, data);
  125. var second = $('#qunit-fixture .duplicates option')[2];
  126. var changeTriggered = false, inputTriggered = false;
  127. assert.equal($select.val(), 'one');
  128. $select.on('change', function () {
  129. changeTriggered = inputTriggered;
  130. }).on('input', function() {
  131. inputTriggered = true;
  132. });
  133. data.select({
  134. id: 'one',
  135. text: 'Uno',
  136. element: second
  137. });
  138. assert.equal(
  139. $select.val(),
  140. 'one',
  141. 'The value never changed'
  142. );
  143. assert.ok(
  144. inputTriggered,
  145. 'The input event should be triggered'
  146. );
  147. assert.ok(
  148. changeTriggered,
  149. 'The change event should be triggered after the input event'
  150. );
  151. assert.ok(
  152. second.selected,
  153. 'The second duplicate is selected, not the first'
  154. );
  155. });
  156. test('duplicates - single - different id on select triggers change',
  157. function (assert) {
  158. var $select = $('#qunit-fixture .duplicates');
  159. var data = new SelectData($select, data);
  160. var second = $('#qunit-fixture .duplicates option')[2];
  161. var changeTriggered = false, inputTriggered = false;
  162. $select.val('two');
  163. $select.on('change', function () {
  164. changeTriggered = inputTriggered;
  165. }).on('input', function() {
  166. inputTriggered = true;
  167. });
  168. data.select({
  169. id: 'one',
  170. text: 'Uno',
  171. element: second
  172. });
  173. assert.equal(
  174. $select.val(),
  175. 'one',
  176. 'The value changed to the duplicate id'
  177. );
  178. assert.ok(
  179. inputTriggered,
  180. 'The input event should be triggered'
  181. );
  182. assert.ok(
  183. changeTriggered,
  184. 'The change event should be triggered after the input event'
  185. );
  186. assert.ok(
  187. second.selected,
  188. 'The second duplicate is selected, not the first'
  189. );
  190. });
  191. test('duplicates - multiple - same id on select triggers change',
  192. function (assert) {
  193. var $select = $('#qunit-fixture .duplicates-multi');
  194. var data = new SelectData($select, data);
  195. var second = $('#qunit-fixture .duplicates-multi option')[2];
  196. var changeTriggered = false, inputTriggered = false;
  197. $select.val(['one']);
  198. $select.on('change', function () {
  199. changeTriggered = inputTriggered;
  200. }).on('input', function() {
  201. inputTriggered = true;
  202. });
  203. data.select({
  204. id: 'one',
  205. text: 'Uno',
  206. element: second
  207. });
  208. assert.deepEqual(
  209. $select.val(),
  210. ['one', 'one'],
  211. 'The value now has duplicates'
  212. );
  213. assert.ok(
  214. inputTriggered,
  215. 'The input event should be triggered'
  216. );
  217. assert.ok(
  218. changeTriggered,
  219. 'The change event should be triggered after the input event'
  220. );
  221. assert.ok(
  222. second.selected,
  223. 'The second duplicate is selected, not the first'
  224. );
  225. });
  226. test('duplicates - multiple - different id on select triggers change',
  227. function (assert) {
  228. var $select = $('#qunit-fixture .duplicates-multi');
  229. var data = new SelectData($select, data);
  230. var second = $('#qunit-fixture .duplicates-multi option')[2];
  231. var changeTriggered = false, inputTriggered = false;
  232. $select.val(['two']);
  233. $select.on('change', function () {
  234. changeTriggered = inputTriggered;
  235. }).on('input', function() {
  236. inputTriggered = true;
  237. });
  238. data.select({
  239. id: 'one',
  240. text: 'Uno',
  241. element: second
  242. });
  243. assert.deepEqual(
  244. $select.val(),
  245. ['two', 'one'],
  246. 'The value has the new id'
  247. );
  248. assert.ok(
  249. inputTriggered,
  250. 'The input event should be triggered'
  251. );
  252. assert.ok(
  253. changeTriggered,
  254. 'The change event should be triggered after the input event'
  255. );
  256. assert.ok(
  257. second.selected,
  258. 'The second duplicate is selected, not the first'
  259. );
  260. });
  261. module('Data adapter - Select - query');
  262. test('all options are returned with no term', function (assert) {
  263. var $select = $('#qunit-fixture .single');
  264. var data = new SelectData($select, selectOptions);
  265. data.query({}, function (data) {
  266. assert.equal(
  267. data.results.length,
  268. 1,
  269. 'The number of items returned should be equal to the number of options'
  270. );
  271. });
  272. });
  273. test('the matcher checks the text', function (assert) {
  274. var $select = $('#qunit-fixture .single');
  275. var data = new SelectData($select, selectOptions);
  276. data.query({
  277. term: 'One'
  278. }, function (data) {
  279. assert.equal(
  280. data.results.length,
  281. 1,
  282. 'Only the "One" option should be found'
  283. );
  284. });
  285. });
  286. test('the matcher ignores case', function (assert) {
  287. var $select = $('#qunit-fixture .single');
  288. var data = new SelectData($select, selectOptions);
  289. data.query({
  290. term: 'one'
  291. }, function (data) {
  292. assert.equal(
  293. data.results.length,
  294. 1,
  295. 'The "One" option should still be found'
  296. );
  297. });
  298. });
  299. test('no options may be returned with no matches', function (assert) {
  300. var $select = $('#qunit-fixture .single');
  301. var data = new SelectData($select, selectOptions);
  302. data.query({
  303. term: 'qwerty'
  304. }, function (data) {
  305. assert.equal(
  306. data.results.length,
  307. 0,
  308. 'Only matching items should be returned'
  309. );
  310. });
  311. });
  312. test('optgroup tags are marked with children', function (assert) {
  313. var $select = $('#qunit-fixture .groups');
  314. var data = new SelectData($select, selectOptions);
  315. data.query({}, function (data) {
  316. assert.ok(
  317. 'children' in data.results[0],
  318. 'The optgroup element should have children when queried'
  319. );
  320. });
  321. });
  322. test('empty optgroups are still shown when queried', function (assert) {
  323. var $select = $('#qunit-fixture .groups');
  324. var data = new SelectData($select, selectOptions);
  325. data.query({}, function (data) {
  326. assert.equal(
  327. data.results.length,
  328. 2,
  329. 'The empty optgroup element should still be returned when queried'
  330. );
  331. var item = data.results[1];
  332. assert.equal(
  333. item.text,
  334. 'Empty',
  335. 'The text of the empty optgroup should match the label'
  336. );
  337. assert.equal(
  338. item.children.length,
  339. 0,
  340. 'There should be no children in the empty opgroup'
  341. );
  342. });
  343. });
  344. test('multiple options with the same value are returned', function (assert) {
  345. var $select = $('#qunit-fixture .duplicates');
  346. var data = new SelectData($select, selectOptions);
  347. data.query({}, function (data) {
  348. assert.equal(
  349. data.results.length,
  350. 3,
  351. 'The duplicate option should still be returned when queried'
  352. );
  353. var first = data.results[0];
  354. var duplicate = data.results[2];
  355. assert.equal(
  356. first.id,
  357. duplicate.id,
  358. 'The duplicates should have the same id'
  359. );
  360. assert.notEqual(
  361. first.text,
  362. duplicate.text,
  363. 'The duplicates do not have the same text'
  364. );
  365. });
  366. });
  367. test('data objects use the text of the option', function (assert) {
  368. var $select = $('#qunit-fixture .duplicates');
  369. var data = new SelectData($select, selectOptions);
  370. var $option = $('<option>&amp;</option>');
  371. var item = data.item($option);
  372. assert.equal(item.id, '&');
  373. assert.equal(item.text, '&');
  374. });
  375. test('select option construction accepts id=0 (zero) value', function (assert) {
  376. var $select = $('#qunit-fixture .single');
  377. var selectOptions = [{ id: 0, text: 'Zero Value'}];
  378. var data = new SelectData($select, selectOptions);
  379. var optionElem = data.option(selectOptions[0]);
  380. // If was "Zero Value"", then it ignored id property
  381. assert.equal(
  382. optionElem[0].value,
  383. '0',
  384. 'Built option value should be "0" (zero as a string).'
  385. );
  386. });
  387. test('select option construction accepts id="" (empty string) value',
  388. function (assert) {
  389. var $select = $('#qunit-fixture .single');
  390. var selectOptions = [{ id: '', text: 'Empty String'}];
  391. var data = new SelectData($select, selectOptions);
  392. var optionElem = data.option(selectOptions[0]);
  393. assert.equal(
  394. optionElem[0].value,
  395. '',
  396. 'Built option value should be an empty string.'
  397. );
  398. });
  399. test('user-defined types are normalized properly', function (assert) {
  400. var $select = $('#qunit-fixture .user-defined'),
  401. UserDefinedType = function (id, text) {
  402. var self = this;
  403. self.id = id;
  404. self.text = text;
  405. return self;
  406. };
  407. var testData = [
  408. 'Test',
  409. {
  410. id: 4,
  411. text: 'item'
  412. },
  413. new UserDefinedType(1, 'aaaaaa')
  414. ];
  415. var data = new SelectData($select, selectOptions);
  416. var normalizedItem = data._normalizeItem(testData[0]);
  417. var normalizedItem2 = data._normalizeItem(testData[1]);
  418. var normalizedItem3 = data._normalizeItem(testData[2]);
  419. assert.equal(
  420. testData[0],
  421. normalizedItem.id,
  422. 'id property should be equal to text after normalize'
  423. );
  424. assert.equal(
  425. testData[0],
  426. normalizedItem.text,
  427. 'text property should be equal after normalize'
  428. );
  429. assert.equal(
  430. testData[1].id,
  431. normalizedItem2.id,
  432. 'id property should be equal after normalize'
  433. );
  434. assert.equal(
  435. testData[1].text,
  436. normalizedItem2.text,
  437. 'text property should be equal after normalize'
  438. );
  439. assert.equal(
  440. testData[2].id,
  441. normalizedItem3.id,
  442. 'id property should be equal after normalize'
  443. );
  444. assert.equal(
  445. testData[2].text,
  446. normalizedItem3.text,
  447. 'text property should be equal after normalize'
  448. );
  449. });