array-tests.js 6.1 KB

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