array-tests.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 nestedOptions = new Options({
  22. data: [
  23. {
  24. text: 'Default',
  25. children: [
  26. {
  27. text: 'Next',
  28. children: [
  29. {
  30. id: 'a',
  31. text: 'Option'
  32. }
  33. ]
  34. }
  35. ]
  36. }
  37. ]
  38. });
  39. test('current gets default for single', function (assert) {
  40. var $select = $('#qunit-fixture .single-empty');
  41. var data = new ArrayData($select, arrayOptions);
  42. data.current(function (val) {
  43. assert.equal(
  44. val.length,
  45. 1,
  46. 'There should always be a selected item for array data.'
  47. );
  48. var item = val[0];
  49. assert.equal(
  50. item.id,
  51. 'default',
  52. 'The first item should be selected'
  53. );
  54. });
  55. });
  56. test('current gets default for multiple', function (assert) {
  57. var $select = $('#qunit-fixture .multiple');
  58. var data = new ArrayData($select, arrayOptions);
  59. data.current(function (val) {
  60. assert.equal(
  61. val.length,
  62. 0,
  63. 'There should be no default selection.'
  64. );
  65. });
  66. });
  67. test('current works with existing selections', function (assert) {
  68. var $select = $('#qunit-fixture .multiple');
  69. var data = new ArrayData($select, arrayOptions);
  70. $select.val(['One']);
  71. data.current(function (val) {
  72. assert.equal(
  73. val.length,
  74. 1,
  75. 'There should only be one existing selection.'
  76. );
  77. var option = val[0];
  78. assert.equal(
  79. option.id,
  80. 'One',
  81. 'The id should be equal to the value of the option tag.'
  82. );
  83. assert.equal(
  84. option.text,
  85. 'One',
  86. 'The text should be equal to the text of the option tag.'
  87. );
  88. });
  89. });
  90. test('current works with selected data', function (assert) {
  91. var $select = $('#qunit-fixture .single-empty');
  92. var data = new ArrayData($select, arrayOptions);
  93. data.select({
  94. id: '2',
  95. text: '2'
  96. });
  97. data.current(function (val) {
  98. assert.equal(
  99. val.length,
  100. 1,
  101. 'There should only be one option selected.'
  102. );
  103. var option = val[0];
  104. assert.equal(
  105. option.id,
  106. '2',
  107. 'The id should match the original id from the array.'
  108. );
  109. assert.equal(
  110. option.text,
  111. '2',
  112. 'The text should match the original text from the array.'
  113. );
  114. });
  115. });
  116. test('select works for single', function (assert) {
  117. var $select = $('#qunit-fixture .single-empty');
  118. var data = new ArrayData($select, arrayOptions);
  119. assert.equal(
  120. $select.val(),
  121. 'default',
  122. 'There should already be a selection'
  123. );
  124. data.select({
  125. id: '1',
  126. text: 'One'
  127. });
  128. assert.equal(
  129. $select.val(),
  130. '1',
  131. 'The selected value should be the same as the selected id'
  132. );
  133. });
  134. test('multiple sets the value', function (assert) {
  135. var $select = $('#qunit-fixture .multiple');
  136. var data = new ArrayData($select, arrayOptions);
  137. assert.equal($select.val(), null);
  138. data.select({
  139. id: 'default',
  140. text: 'Default'
  141. });
  142. assert.deepEqual($select.val(), ['default']);
  143. });
  144. test('multiple adds to the old value', function (assert) {
  145. var $select = $('#qunit-fixture .multiple');
  146. var data = new ArrayData($select, arrayOptions);
  147. $select.val(['One']);
  148. assert.deepEqual($select.val(), ['One']);
  149. data.select({
  150. id: 'default',
  151. text: 'Default'
  152. });
  153. assert.deepEqual($select.val(), ['One', 'default']);
  154. });
  155. test('option tags are automatically generated', function (assert) {
  156. var $select = $('#qunit-fixture .single-empty');
  157. var data = new ArrayData($select, arrayOptions);
  158. assert.equal(
  159. $select.find('option').length,
  160. 3,
  161. 'An <option> element should be created for each object'
  162. );
  163. });
  164. test('optgroup tags can also be generated', function (assert) {
  165. var $select = $('#qunit-fixture .single-empty');
  166. var data = new ArrayData($select, nestedOptions);
  167. assert.equal(
  168. $select.find('option').length,
  169. 1,
  170. 'An <option> element should be created for the one selectable object'
  171. );
  172. assert.equal(
  173. $select.find('optgroup').length,
  174. 2,
  175. 'An <optgroup> element should be created for the two with children'
  176. );
  177. });
  178. test('optgroup tags have the right properties', function (assert) {
  179. var $select = $('#qunit-fixture .single-empty');
  180. var data = new ArrayData($select, nestedOptions);
  181. var $group = $select.children('optgroup');
  182. assert.equal(
  183. $group.prop('label'),
  184. 'Default',
  185. 'An `<optgroup>` label should match the text property'
  186. );
  187. assert.equal(
  188. $group.children().length,
  189. 1,
  190. 'The <optgroup> should have one child under it'
  191. );
  192. });