select-tests.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 options = new Options({});
  6. test('current gets default for single', function (assert) {
  7. var $select = $('#qunit-fixture .single');
  8. var data = new SelectData($select, options);
  9. data.current(function (val) {
  10. assert.equal(
  11. val.length,
  12. 1,
  13. 'There should only be one selected option'
  14. );
  15. var option = val[0];
  16. assert.equal(
  17. option.id,
  18. 'default',
  19. 'The value of the option tag should be the id'
  20. );
  21. assert.equal(
  22. option.text,
  23. 'Default',
  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, options);
  31. data.current(function (val) {
  32. assert.equal(
  33. val.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 data = new SelectData($select, options);
  42. $select.val('1');
  43. data.current(function (val) {
  44. assert.equal(
  45. val.length,
  46. 1,
  47. 'There should be one selected option'
  48. );
  49. var option = val[0];
  50. assert.equal(
  51. option.id,
  52. '1',
  53. 'The option value should be the selected id'
  54. );
  55. assert.equal(
  56. option.text,
  57. 'One',
  58. 'The text should match the text for the option tag'
  59. );
  60. });
  61. });
  62. test('current gets options with implicit value', function (assert) {
  63. var $select = $('#qunit-fixture .single');
  64. var data = new SelectData($select, options);
  65. $select.val('2');
  66. data.current(function (val) {
  67. assert.equal(
  68. val.length,
  69. 1,
  70. 'There should only be one selected value'
  71. );
  72. var option = val[0];
  73. assert.equal(
  74. option.id,
  75. '2',
  76. 'The id should be the same as the option text'
  77. );
  78. assert.equal(
  79. option.text,
  80. '2',
  81. 'The text should be the same as the option text'
  82. );
  83. });
  84. });
  85. test('select works for single', function (assert) {
  86. var $select = $('#qunit-fixture .single');
  87. var data = new SelectData($select, options);
  88. assert.equal($select.val(), 'default');
  89. data.select({
  90. id: '1',
  91. text: 'One'
  92. });
  93. assert.equal($select.val(), '1');
  94. });
  95. test('multiple sets the value', function (assert) {
  96. var $select = $('#qunit-fixture .multiple');
  97. var data = new SelectData($select, options);
  98. assert.equal($select.val(), null);
  99. data.select({
  100. id: 'default',
  101. text: 'Default'
  102. });
  103. assert.deepEqual($select.val(), ['default']);
  104. });
  105. test('multiple adds to the old value', function (assert) {
  106. var $select = $('#qunit-fixture .multiple');
  107. var data = new SelectData($select, options);
  108. $select.val(['2']);
  109. assert.deepEqual($select.val(), ['2']);
  110. data.select({
  111. id: 'default',
  112. text: 'Default'
  113. });
  114. assert.deepEqual($select.val(), ['default', '2']);
  115. });
  116. test('duplicates - single - same id on select triggers change',
  117. function (assert) {
  118. var $select = $('#qunit-fixture .duplicates');
  119. var data = new SelectData($select, data);
  120. var second = $('#qunit-fixture .duplicates option')[2];
  121. var changeTriggered = false;
  122. assert.equal($select.val(), 'one');
  123. $select.on('change', function () {
  124. changeTriggered = true;
  125. });
  126. data.select({
  127. id: 'one',
  128. text: 'Uno',
  129. element: second
  130. });
  131. assert.equal(
  132. $select.val(),
  133. 'one',
  134. 'The value never changed'
  135. );
  136. assert.ok(
  137. changeTriggered,
  138. 'The change event should be triggered'
  139. );
  140. assert.ok(
  141. second.selected,
  142. 'The second duplicate is selected, not the first'
  143. );
  144. });
  145. test('duplicates - single - different id on select triggers change',
  146. function (assert) {
  147. var $select = $('#qunit-fixture .duplicates');
  148. var data = new SelectData($select, data);
  149. var second = $('#qunit-fixture .duplicates option')[2];
  150. var changeTriggered = false;
  151. $select.val('two');
  152. $select.on('change', function () {
  153. changeTriggered = true;
  154. });
  155. data.select({
  156. id: 'one',
  157. text: 'Uno',
  158. element: second
  159. });
  160. assert.equal(
  161. $select.val(),
  162. 'one',
  163. 'The value changed to the duplicate id'
  164. );
  165. assert.ok(
  166. changeTriggered,
  167. 'The change event should be triggered'
  168. );
  169. assert.ok(
  170. second.selected,
  171. 'The second duplicate is selected, not the first'
  172. );
  173. });
  174. test('duplicates - multiple - same id on select triggers change',
  175. function (assert) {
  176. var $select = $('#qunit-fixture .duplicates-multi');
  177. var data = new SelectData($select, data);
  178. var second = $('#qunit-fixture .duplicates-multi option')[2];
  179. var changeTriggered = false;
  180. $select.val(['one']);
  181. $select.on('change', function () {
  182. changeTriggered = true;
  183. });
  184. data.select({
  185. id: 'one',
  186. text: 'Uno',
  187. element: second
  188. });
  189. assert.deepEqual(
  190. $select.val(),
  191. ['one', 'one'],
  192. 'The value now has duplicates'
  193. );
  194. assert.ok(
  195. changeTriggered,
  196. 'The change event should be triggered'
  197. );
  198. assert.ok(
  199. second.selected,
  200. 'The second duplicate is selected, not the first'
  201. );
  202. });
  203. test('duplicates - multiple - different id on select triggers change',
  204. function (assert) {
  205. var $select = $('#qunit-fixture .duplicates-multi');
  206. var data = new SelectData($select, data);
  207. var second = $('#qunit-fixture .duplicates-multi option')[2];
  208. var changeTriggered = false;
  209. $select.val(['two']);
  210. $select.on('change', function () {
  211. changeTriggered = true;
  212. });
  213. data.select({
  214. id: 'one',
  215. text: 'Uno',
  216. element: second
  217. });
  218. assert.deepEqual(
  219. $select.val(),
  220. ['two', 'one'],
  221. 'The value has the new id'
  222. );
  223. assert.ok(
  224. changeTriggered,
  225. 'The change event should be triggered'
  226. );
  227. assert.ok(
  228. second.selected,
  229. 'The second duplicate is selected, not the first'
  230. );
  231. });
  232. module('Data adapter - Select - query');
  233. test('all options are returned with no term', function (assert) {
  234. var $select = $('#qunit-fixture .single');
  235. var data = new SelectData($select, options);
  236. data.query({}, function (data) {
  237. assert.equal(
  238. data.length,
  239. 3,
  240. 'The number of items returned should be equal to the number of options'
  241. );
  242. });
  243. });
  244. test('the matcher checks the text', function (assert) {
  245. var $select = $('#qunit-fixture .single');
  246. var data = new SelectData($select, options);
  247. data.query({
  248. term: 'Default'
  249. }, function (data) {
  250. assert.equal(
  251. data.length,
  252. 1,
  253. 'Only the "Default" option should be found'
  254. );
  255. });
  256. });
  257. test('the matcher ignores case', function (assert) {
  258. var $select = $('#qunit-fixture .single');
  259. var data = new SelectData($select, options);
  260. data.query({
  261. term: 'one'
  262. }, function (data) {
  263. assert.equal(
  264. data.length,
  265. 1,
  266. 'The "One" option should still be found'
  267. );
  268. });
  269. });
  270. test('no options may be returned with no matches', function (assert) {
  271. var $select = $('#qunit-fixture .single');
  272. var data = new SelectData($select, options);
  273. data.query({
  274. term: 'qwerty'
  275. }, function (data) {
  276. assert.equal(
  277. data.length,
  278. 0,
  279. 'Only matching items should be returned'
  280. );
  281. });
  282. });
  283. test('optgroup tags are marked with children', function (assert) {
  284. var $select = $('#qunit-fixture .groups');
  285. var data = new SelectData($select, options);
  286. data.query({}, function (data) {
  287. assert.ok(
  288. 'children' in data[0],
  289. 'The optgroup element should have children when queried'
  290. );
  291. });
  292. });
  293. test('empty optgroups are still shown when queried', function (assert) {
  294. var $select = $('#qunit-fixture .groups');
  295. var data = new SelectData($select, options);
  296. data.query({}, function (data) {
  297. assert.equal(
  298. data.length,
  299. 2,
  300. 'The empty optgroup element should still be returned when queried'
  301. );
  302. var item = data[1];
  303. assert.equal(
  304. item.text,
  305. 'Empty',
  306. 'The text of the empty optgroup should match the label'
  307. );
  308. assert.equal(
  309. item.children.length,
  310. 0,
  311. 'There should be no children in the empty opgroup'
  312. );
  313. });
  314. });
  315. test('multiple options with the same value are returned', function (assert) {
  316. var $select = $('#qunit-fixture .duplicates');
  317. var data = new SelectData($select, options);
  318. data.query({}, function (data) {
  319. assert.equal(
  320. data.length,
  321. 3,
  322. 'The duplicate option should still be returned when queried'
  323. );
  324. var first = data[0];
  325. var duplicate = data[2];
  326. assert.equal(
  327. first.id,
  328. duplicate.id,
  329. 'The duplicates should have the same id'
  330. );
  331. assert.notEqual(
  332. first.text,
  333. duplicate.text,
  334. 'The duplicates do not have the same text'
  335. );
  336. });
  337. });