array-tests.js 6.2 KB

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