array-tests.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. module('Data adapters - Array');
  2. var ArrayData = require('select2/data/array');
  3. var $ = require('jquery');
  4. var Options = require('select2/options');
  5. var arrayOptions = new Options({
  6. data: [
  7. {
  8. id: 'default',
  9. text: 'Default'
  10. },
  11. {
  12. id: '1',
  13. text: 'One'
  14. },
  15. {
  16. id: '2',
  17. text: '2'
  18. }
  19. ]
  20. });
  21. var extraOptions = new Options ({
  22. data: [
  23. {
  24. id: 'default',
  25. text: 'Default',
  26. extra: true
  27. },
  28. {
  29. id: 'One',
  30. text: 'One',
  31. extra: true
  32. }
  33. ]
  34. });
  35. var nestedOptions = new Options({
  36. data: [
  37. {
  38. text: 'Default',
  39. children: [
  40. {
  41. text: 'Next',
  42. children: [
  43. {
  44. id: 'a',
  45. text: 'Option'
  46. }
  47. ]
  48. }
  49. ]
  50. }
  51. ]
  52. });
  53. test('current gets default for single', function (assert) {
  54. var $select = $('#qunit-fixture .single-empty');
  55. var data = new ArrayData($select, arrayOptions);
  56. data.current(function (val) {
  57. assert.equal(
  58. val.length,
  59. 1,
  60. 'There should always be a selected item for array data.'
  61. );
  62. var item = val[0];
  63. assert.equal(
  64. item.id,
  65. 'default',
  66. 'The first item should be selected'
  67. );
  68. });
  69. });
  70. test('current gets default for multiple', function (assert) {
  71. var $select = $('#qunit-fixture .multiple');
  72. var data = new ArrayData($select, arrayOptions);
  73. data.current(function (val) {
  74. assert.equal(
  75. val.length,
  76. 0,
  77. 'There should be no default selection.'
  78. );
  79. });
  80. });
  81. test('current works with existing selections', function (assert) {
  82. var $select = $('#qunit-fixture .multiple');
  83. var data = new ArrayData($select, arrayOptions);
  84. $select.val(['One']);
  85. data.current(function (val) {
  86. assert.equal(
  87. val.length,
  88. 1,
  89. 'There should only be one existing selection.'
  90. );
  91. var option = val[0];
  92. assert.equal(
  93. option.id,
  94. 'One',
  95. 'The id should be equal to the value of the option tag.'
  96. );
  97. assert.equal(
  98. option.text,
  99. 'One',
  100. 'The text should be equal to the text of the option tag.'
  101. );
  102. });
  103. });
  104. test('current works with selected data', function (assert) {
  105. var $select = $('#qunit-fixture .single-empty');
  106. var data = new ArrayData($select, arrayOptions);
  107. data.select({
  108. id: '2',
  109. text: '2'
  110. });
  111. data.current(function (val) {
  112. assert.equal(
  113. val.length,
  114. 1,
  115. 'There should only be one option selected.'
  116. );
  117. var option = val[0];
  118. assert.equal(
  119. option.id,
  120. '2',
  121. 'The id should match the original id from the array.'
  122. );
  123. assert.equal(
  124. option.text,
  125. '2',
  126. 'The text should match the original text from the array.'
  127. );
  128. });
  129. });
  130. test('select works for single', function (assert) {
  131. var $select = $('#qunit-fixture .single-empty');
  132. var data = new ArrayData($select, arrayOptions);
  133. assert.equal(
  134. $select.val(),
  135. 'default',
  136. 'There should already be a selection'
  137. );
  138. data.select({
  139. id: '1',
  140. text: 'One'
  141. });
  142. assert.equal(
  143. $select.val(),
  144. '1',
  145. 'The selected value should be the same as the selected id'
  146. );
  147. });
  148. test('multiple sets the value', function (assert) {
  149. var $select = $('#qunit-fixture .multiple');
  150. var data = new ArrayData($select, arrayOptions);
  151. assert.equal($select.val(), null);
  152. data.select({
  153. id: 'default',
  154. text: 'Default'
  155. });
  156. assert.deepEqual($select.val(), ['default']);
  157. });
  158. test('multiple adds to the old value', function (assert) {
  159. var $select = $('#qunit-fixture .multiple');
  160. var data = new ArrayData($select, arrayOptions);
  161. $select.val(['One']);
  162. assert.deepEqual($select.val(), ['One']);
  163. data.select({
  164. id: 'default',
  165. text: 'Default'
  166. });
  167. assert.deepEqual($select.val(), ['One', 'default']);
  168. });
  169. test('option tags are automatically generated', function (assert) {
  170. var $select = $('#qunit-fixture .single-empty');
  171. var data = new ArrayData($select, arrayOptions);
  172. assert.equal(
  173. $select.find('option').length,
  174. 3,
  175. 'An <option> element should be created for each object'
  176. );
  177. });
  178. test('option tags can receive new data', function(assert) {
  179. var $select = $('#qunit-fixture .single');
  180. var data = new ArrayData($select, extraOptions);
  181. assert.equal(
  182. $select.find('option').length,
  183. 2,
  184. 'Only one more <option> element should be created'
  185. );
  186. data.select({
  187. id: 'default'
  188. });
  189. assert.ok(
  190. $select.find(':selected').data('data').extra,
  191. '<option> default should have new data'
  192. );
  193. data.select({
  194. id: 'One'
  195. });
  196. assert.ok(
  197. $select.find(':selected').data('data').extra,
  198. '<option> One should have new data'
  199. );
  200. });
  201. test('optgroup tags can also be generated', function (assert) {
  202. var $select = $('#qunit-fixture .single-empty');
  203. var data = new ArrayData($select, nestedOptions);
  204. assert.equal(
  205. $select.find('option').length,
  206. 1,
  207. 'An <option> element should be created for the one selectable object'
  208. );
  209. assert.equal(
  210. $select.find('optgroup').length,
  211. 2,
  212. 'An <optgroup> element should be created for the two with children'
  213. );
  214. });
  215. test('optgroup tags have the right properties', function (assert) {
  216. var $select = $('#qunit-fixture .single-empty');
  217. var data = new ArrayData($select, nestedOptions);
  218. var $group = $select.children('optgroup');
  219. assert.equal(
  220. $group.prop('label'),
  221. 'Default',
  222. 'An `<optgroup>` label should match the text property'
  223. );
  224. assert.equal(
  225. $group.children().length,
  226. 1,
  227. 'The <optgroup> should have one child under it'
  228. );
  229. });
  230. test('existing selections are respected on initialization', function (assert) {
  231. var $select = $(
  232. '<select>' +
  233. '<option>First</option>' +
  234. '<option selected>Second</option>' +
  235. '</select>'
  236. );
  237. var options = new Options({
  238. data: [
  239. {
  240. id: 'Second',
  241. text: 'Second'
  242. },
  243. {
  244. id: 'Third',
  245. text: 'Third'
  246. }
  247. ]
  248. });
  249. assert.equal($select.val(), 'Second');
  250. var data = new ArrayData($select, options);
  251. assert.equal($select.val(), 'Second');
  252. });