select-tests.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. module('Data adapter - Select - query');
  117. test('all options are returned with no term', function (assert) {
  118. var $select = $('#qunit-fixture .single');
  119. var data = new SelectData($select, options);
  120. data.query({}, function (data) {
  121. assert.equal(
  122. data.length,
  123. 3,
  124. 'The number of items returned should be equal to the number of options'
  125. );
  126. });
  127. });
  128. test('the matcher checks the text', function (assert) {
  129. var $select = $('#qunit-fixture .single');
  130. var data = new SelectData($select, options);
  131. data.query({
  132. term: 'Default'
  133. }, function (data) {
  134. assert.equal(
  135. data.length,
  136. 1,
  137. 'Only the "Default" option should be found'
  138. );
  139. });
  140. });
  141. test('the matcher ignores case', function (assert) {
  142. var $select = $('#qunit-fixture .single');
  143. var data = new SelectData($select, options);
  144. data.query({
  145. term: 'one'
  146. }, function (data) {
  147. assert.equal(
  148. data.length,
  149. 1,
  150. 'The "One" option should still be found'
  151. );
  152. });
  153. });
  154. test('no options may be returned with no matches', function (assert) {
  155. var $select = $('#qunit-fixture .single');
  156. var data = new SelectData($select, options);
  157. data.query({
  158. term: 'qwerty'
  159. }, function (data) {
  160. assert.equal(
  161. data.length,
  162. 0,
  163. 'Only matching items should be returned'
  164. );
  165. });
  166. });
  167. test('optgroup tags are marked with children', function (assert) {
  168. var $select = $('#qunit-fixture .groups');
  169. var data = new SelectData($select, options);
  170. data.query({}, function (data) {
  171. assert.ok(
  172. 'children' in data[0],
  173. 'The optgroup element should have children when queried'
  174. );
  175. });
  176. });
  177. test('empty optgroups are still shown when queried', function (assert) {
  178. var $select = $('#qunit-fixture .groups');
  179. var data = new SelectData($select, options);
  180. data.query({}, function (data) {
  181. assert.equal(
  182. data.length,
  183. 2,
  184. 'The empty optgroup element should still be returned when queried'
  185. );
  186. var item = data[1];
  187. assert.equal(
  188. item.text,
  189. 'Empty',
  190. 'The text of the empty optgroup should match the label'
  191. );
  192. assert.equal(
  193. item.children.length,
  194. 0,
  195. 'There should be no children in the empty opgroup'
  196. );
  197. });
  198. });