select-tests.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. var SelectData = require('select2/data/select');
  2. var $ = require('jquery');
  3. var Options = require('select2/options');
  4. var options = new Options({});
  5. module('Data adapters - Select - current');
  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.deepEqual(
  11. val,
  12. [{
  13. id: 'default',
  14. text: 'Default',
  15. disabled: false
  16. }],
  17. 'The first option should be selected by default (by the browser).'
  18. );
  19. });
  20. });
  21. test('current gets default for multiple', function (assert) {
  22. var $select = $('#qunit-fixture .multiple');
  23. var data = new SelectData($select, options);
  24. data.current(function (val) {
  25. assert.deepEqual(
  26. val,
  27. [],
  28. 'Multiple selects have no default selection.'
  29. );
  30. });
  31. });
  32. test('current gets options with explicit value', function (assert) {
  33. var $select = $('#qunit-fixture .single');
  34. var data = new SelectData($select, options);
  35. $select.val('1');
  36. data.current(function (val) {
  37. assert.deepEqual(
  38. val,
  39. [{
  40. id: '1',
  41. text: 'One',
  42. disabled: false
  43. }],
  44. 'The text and id should match the value and text for the option tag.'
  45. );
  46. });
  47. });
  48. test('current gets options with implicit value', function (assert) {
  49. var $select = $('#qunit-fixture .single');
  50. var data = new SelectData($select, options);
  51. $select.val('2');
  52. data.current(function (val) {
  53. assert.deepEqual(
  54. val,
  55. [{
  56. id: '2',
  57. text: '2',
  58. disabled: false
  59. }],
  60. 'The text and id should match the text within the option tag.'
  61. );
  62. });
  63. });
  64. test('select works for single', function (assert) {
  65. var $select = $('#qunit-fixture .single');
  66. var data = new SelectData($select, options);
  67. assert.equal($select.val(), 'default');
  68. data.select({
  69. id: '1',
  70. text: 'One'
  71. });
  72. assert.equal($select.val(), '1');
  73. });
  74. test('multiple sets the value', function (assert) {
  75. var $select = $('#qunit-fixture .multiple');
  76. var data = new SelectData($select, options);
  77. assert.equal($select.val(), null);
  78. data.select({
  79. id: 'default',
  80. text: 'Default'
  81. });
  82. assert.deepEqual($select.val(), ['default']);
  83. });
  84. test('multiple adds to the old value', function (assert) {
  85. var $select = $('#qunit-fixture .multiple');
  86. var data = new SelectData($select, options);
  87. $select.val(['2']);
  88. assert.deepEqual($select.val(), ['2']);
  89. data.select({
  90. id: 'default',
  91. text: 'Default'
  92. });
  93. assert.deepEqual($select.val(), ['default', '2']);
  94. });
  95. module('Data adapter - Select - query');
  96. test('all options are returned with no term', function (assert) {
  97. var $select = $('#qunit-fixture .single');
  98. var data = new SelectData($select, options);
  99. data.query({}, function (data) {
  100. assert.equal(
  101. data.length,
  102. 3,
  103. 'The number of items returned should be equal to the number of options'
  104. );
  105. });
  106. });
  107. test('the matcher checks the text', function (assert) {
  108. var $select = $('#qunit-fixture .single');
  109. var data = new SelectData($select, options);
  110. data.query({
  111. term: 'Default'
  112. }, function (data) {
  113. assert.equal(
  114. data.length,
  115. 1,
  116. 'Only the "Default" option should be found'
  117. );
  118. });
  119. });
  120. test('the matcher ignores case', function (assert) {
  121. var $select = $('#qunit-fixture .single');
  122. var data = new SelectData($select, options);
  123. data.query({
  124. term: 'one'
  125. }, function (data) {
  126. assert.equal(
  127. data.length,
  128. 1,
  129. 'The "One" option should still be found'
  130. );
  131. });
  132. });
  133. test('no options may be returned with no matches', function (assert) {
  134. var $select = $('#qunit-fixture .single');
  135. var data = new SelectData($select, options);
  136. data.query({
  137. term: 'qwerty'
  138. }, function (data) {
  139. assert.equal(
  140. data.length,
  141. 0,
  142. 'Only matching items should be returned'
  143. );
  144. });
  145. });
  146. test('optgroup tags are marked with children', function (assert) {
  147. var $select = $('#qunit-fixture .groups');
  148. var data = new SelectData($select, options);
  149. data.query({}, function (data) {
  150. assert.ok(
  151. 'children' in data[0],
  152. 'The optgroup element should have children when queried'
  153. );
  154. });
  155. });
  156. test('empty optgroups are still shown when queried', function (assert) {
  157. var $select = $('#qunit-fixture .groups');
  158. var data = new SelectData($select, options);
  159. data.query({}, function (data) {
  160. assert.deepEqual(
  161. data[1],
  162. {
  163. text: 'Empty',
  164. children: []
  165. },
  166. 'The empty optgroup element should still be returned when queried'
  167. );
  168. });
  169. });